class LogStash::Filters::HandsetDetection

Public Instance Methods

add_convenience_fields(specs) click to toggle source
# File lib/logstash/filters/handsetdetection.rb, line 139
def add_convenience_fields(specs)
  unless specs.key? 'general_device_name'
    if specs.key? 'general_aliases' and specs['general_aliases'].is_a? Array and specs['general_aliases'].count > 0
      specs['general_device_name'] = specs['general_aliases'][0].strip
    else
      specs['general_device_name'] = "#{specs.fetch('general_vendor', '')} #{specs.fetch('general_model', '')}".strip
    end
  end
  unless specs.key? 'general_platform_name'
    specs['general_platform_name'] = "#{specs.fetch('general_platform', '')} #{specs.fetch('general_platform_version', '')}".strip
  end
  unless specs.key? 'general_browser_name'
    specs['general_browser_name']  = "#{specs.fetch('general_browser', '')} #{specs.fetch('general_browser_version', '')}".strip
  end
  unless specs.key? 'general_app_name'
    specs['general_app_name']      = "#{specs.fetch('general_app', '')} #{specs.fetch('general_app_version', '')}".strip
  end
  specs
end
filter(event) click to toggle source
# 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'
      r['hd_specs'] = add_convenience_fields r['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
register() click to toggle source
# 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