Class: LogStash::Inputs::Nagioscheck

Inherits:
Exec
  • Object
show all
Defined in:
lib/logstash/inputs/nagioscheck.rb

Overview

Generate a repeating message.

This plugin is intented only as an example.

Instance Method Summary collapse

Instance Method Details

#execute(queue) ⇒ Object

Override the execute routine from the exec input



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
# File 'lib/logstash/inputs/nagioscheck.rb', line 22

def execute(queue)
  start = Time.now
  output = exit_status = nil
  begin
    @logger.debug? && @logger.debug("Running exec", :command => @command)
    output, exit_status = run_command()
  rescue StandardError => e
    @logger.error("Error while running command",
      :command => @command, :e => e, :backtrace => e.backtrace)
  rescue Exception => e
    @logger.error("Exception while running command",
      :command => @command, :e => e, :backtrace => e.backtrace)
  end
  duration = Time.now - start
  unless exit_status.nil? #exit status will be nil if the command never completed running
    @logger.debug? && @logger.debug("Command completed", :command => @command, :duration => duration)
    @codec.decode(output) do |event|
      decorate(event)

      cmd_message, cmd_perf = event.get("message").split('|')

      event.set("check_uuid", SecureRandom.uuid)

      unless cmd_perf.nil?
        cmd_perf.strip.split_by_spaces_except_single_quoted.each { |metric| 
  
            results = parse_performance(metric)
  
            if results.nil?
              
              @logger.warn("Error parsing nagios performance data (malformed)", :raw => event.get("message"))
              event.tag(@failure_tag)
  
            else 
  
              perf_event = event.clone
              perf_event.remove("message")

              perf_event.set("type", "nagiosmetric")
              perf_event.set("name", @name)
              perf_event.set("label", results[1])
              perf_event.set("uom", results[3])
              perf_event.set("value", results[2].to_f)
              perf_event.set("warning", results[4].to_f)
              perf_event.set("critical", results[5].to_f)
              perf_event.set("min", results[6].to_f)
              perf_event.set("max", results[7].to_f)
              
              queue << perf_event
  
            end
  
        }
      end

      event.set("host", @hostname)
      event.set("command", @command)
      event.set("type", "nagioscheck")
      event.set("message", cmd_message.nil? ? "" : cmd_message.strip)
      event.set("name", @name)
      event.set("took_ms", duration * 1000)
      event.set("status_code", exit_status)
      event.set("status", nice_status(exit_status))
      
      queue << event

    end
  end
  duration
end