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
|
# File 'lib/logstash/outputs/nagios.rb', line 25
def receive(event)
if !File.exists?(@cmdfile)
@logger.warn(["Skipping nagios output; command file is missing",
{"cmdfile" => @cmdfile, "missed_event" => event}])
return
end
host = event.fields["nagios_host"]
if !host
@logger.warn(["Skipping nagios output; nagios_host field is missing",
{"missed_event" => event}])
return
end
service = event.fields["nagios_service"]
if !service
@logger.warn(["Skipping nagios output; nagios_service field is missing",
{"missed_event" => event}])
return
end
annotation = event.fields["nagios_annotation"]
level = NAGIOS_CRITICAL
if event.fields["nagios_level"] and event.fields["nagios_level"][0].downcase == "warn"
level = NAGIOS_WARN
end
cmd = "[#{Time.now.to_i}] PROCESS_SERVICE_CHECK_RESULT;#{host[0]};#{service[0]};#{level};"
if annotation
cmd += "#{annotation[0]}: "
end
cmd += "#{event.source}: "
cmd += event.message.gsub("\n", "\\n")
@logger.debug({"cmdfile" => @cmdfile, "nagios_command" => cmd})
begin
File.open(@cmdfile, "a") do |f|
f.puts cmd
end
rescue
@logger.warn(["Skipping nagios output; error writing to command file",
{"error" => $!, "cmdfile" => @cmdfile,
"missed_event" => event}])
end
end
|