Class: LogStash::Filters::HandsetDetection

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/handsetdetection.rb

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/logstash/filters/handsetdetection.rb', line 99

def filter(event)
  data = {}
  @match.each do |src, dest|
    unless event.get(src).nil?
      data[dest] = event.get src
    end
  end 
  event.set 'handset_detection', {}
  unless data.empty?
    hd = @@pool.pop
    hd = HD4.new @hd_config if hd.nil?
    hd.device_detect data
    r = hd.get_reply
    @@pool << hd
    if r.key? 'status'
      event.set '[handset_detection][status]', r['status']
    end
    if r.key? 'message'
      event.set '[handset_detection][message]', r['message']
    end
    if r.key? 'hd_specs'
      if @filter.empty?
        event.set '[handset_detection][specs]', r['hd_specs']
      else
        event.set '[handset_detection][specs]', {} 
        @filter.each do |f|
          if r['hd_specs'].key? f
            event.set "[handset_detection][specs][#{f}]", r['hd_specs'][f]
          end
        end 
      end
    end
    else
      event.set '[handset_detection][status]', 299 
      event.set '[handset_detection][message]', 'Error : Missing Data' 
  end
  filter_matched event
end

#registerObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/logstash/filters/handsetdetection.rb', line 56

def register
  @hd_config = {}
  @hd_config['username'] = @username
  @hd_config['secret'] = @password
  @hd_config['site_id'] = @site_id
  @hd_config['filesdir'] = Dir.tmpdir 
  @hd_config['cache'] = @cache ? {'memory' => {'thread_safe' => true}} : {'none' => {}}
  @hd_config['debug'] = false
  @hd_config['api_server'] = @apiserver
  @hd_config['cache_requests'] = @cache_requests
  @hd_config['geoip'] = true
  @hd_config['timeout'] = 30
  @hd_config['use_proxy'] = @use_proxy
  @hd_config['proxy_server'] = @proxy_server
  @hd_config['proxy_port'] = @proxy_port
  @hd_config['proxy_user'] = @proxy_user
  @hd_config['proxy_pass'] = @proxy_pass
  @hd_config['retries'] = 3 
  @hd_config['log_unknown'] = @log_unknown 
  @hd_config['local_archive_source'] = @local_archive_source 

  @@pool = ThreadSafe::Array.new 
  if @detection_type == 'ultimate' or @detection_type == 'community'
    @hd_config['use_local'] = true
    hd = HD4.new @hd_config
    path = (@detection_type == 'ultimate') ? hd.device_get_zip_path :  hd.community_get_zip_path
    if !File.exist?(path) or (Time.now - File.mtime(path)) / (24 * 3600) > @db_refresh_days
      hd.set_timeout 500
      result = (@detection_type == 'ultimate') ? hd.device_fetch_archive : hd.community_fetch_archive
      raise LogStash::Error, "Error downloading the Handset Detection database. (Original cause: \"#{hd.get_reply['message']}\") Please try again." unless result
      hd.set_timeout 30
    else
      # Do not download the ZIP file.
    end
    @@pool << hd
  elsif @detection_type == 'cloud'
    @hd_config['use_local'] = false
  else
    raise LogStash::ConfigurationError, 'Detection_type should be one of: cloud, ultimate, community.'
  end
end