Class: Observed::Plugins::Logstash

Inherits:
Observer
  • Object
show all
Defined in:
lib/observed/logstash.rb

Instance Method Summary collapse

Instance Method Details

#build_bodyObject



32
33
34
35
36
37
# File 'lib/observed/logstash.rb', line 32

def build_body
  {
    :query => query,
    :filter => build_timestamp_filter
  }
end

#build_clientObject



17
18
19
# File 'lib/observed/logstash.rb', line 17

def build_client
  Elasticsearch::Client.new host: host
end

#build_data(hits) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/observed/logstash.rb', line 39

def build_data(hits)
  data = { hits: hits, min_hits: min_hits, max_hits: max_hits }
  if hits < min_hits
    data[:status] = :error
    data[:message] = "Not enough hits. Only #{hits} in the last #{timespan_in_seconds} seconds, required at least #{min_hits}" 
  elsif hits > max_hits
    data[:status] = :error
    data[:message] = "Too many hits. Got #{hits} in the last #{timespan_in_seconds} seconds, required at most #{max_hits}" 
  else
    data[:status] = :success
    data[:message] = "#{hits} hits in the last #{timespan_in_seconds} seconds" 
  end

  data
end

#build_timestamp_filterObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/observed/logstash.rb', line 21

def build_timestamp_filter
  {
    :range => {
      :@timestamp => {
        :from => (system.now.to_f * 1000).to_i - (1000 * timespan_in_seconds),
        :to => (system.now.to_f * 1000).to_i
      }
    }
  }
end

#loggerObject



73
74
75
# File 'lib/observed/logstash.rb', line 73

def logger
  @logger ||= Logger.new(STDOUT)
end

#observeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/observed/logstash.rb', line 55

def observe
  logger.debug "Host: #{host}, index name format: #{index_name_format}, query: [#{query}], timespan: #{timespan_in_seconds}s, max hits: #{max_hits}, min hits: #{min_hits}"

  index = system.now.strftime(index_name_format)
  body = build_body

  logger.debug "Index: #{index}, Body: #{body}"

  client = build_client
  response = client.search :index => index,
                           :body => body
  hits = response['hits']['total'].to_i
  logger.debug("Hits: #{hits}")

  data = build_data(hits)
  system.report("#{self.tag}.#{data[:status]}", data)
end