-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_nagios
89 lines (72 loc) · 2.95 KB
/
parse_nagios
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#!/usr/bin/env python
import re
import time
import datetime
import sys
from optparse import OptionParser
def return_red(input):
return "\033[1;31m" + input + "\033[m"
def return_light_orange(input):
return "\033[0;33m" + input + "\033[m"
def return_light_green(input):
return "\033[1;32m" + input + "\033[m"
def return_purple(input):
return "\033[1;35m" + input + "\033[m"
def return_yellow(input):
return "\033[1;33m" + input + "\033[m"
def print_separator():
print return_purple("-------------------------------")
def read_from_a_file(filename, type_r):
f = open(filename, "r")
if type_r == "read":
data = f.read()
else:
data = f.readlines()
f.close()
return data
def get_generic_item_from_match(strip_string, match_string, m):
return [j.replace(strip_string, "") for i in m for j in i.split("\n") if match_string in j]
def main(data, disacked_display):
print_separator()
m = [m.group(1) for m in re.finditer(r"[^{]*\{([^}]+)\}", data) if "last_check" in m.group(1)]
#perf_data = [j.replace("performance_data=","") for i in m for j in re.findall("performance_data=?[^;]*",i) ]
host_names = get_generic_item_from_match("\thost_name=", "\thost_name=", m)
last_check = get_generic_item_from_match("\tlast_check=", "\tlast_check=", m)
current_state = get_generic_item_from_match("\tcurrent_state=", "\tcurrent_state=", m)
plugin_output = get_generic_item_from_match("\tplugin_output=", "\tplugin_output=", m)
acked = get_generic_item_from_match("\tproblem_has_been_acknowledged=", "\tproblem_has_been_acknowledged=", m)
output_g = []
output_w = []
output_c_u_down = []
assert len (host_names) == len(last_check), "the number of hosts in the nagios status file should equal the number of last_check in the file."
for (host, plugin_out, last_c, current_state, acked) in zip(host_names, plugin_output, last_check, current_state, acked):
temp = str(host) + " " + str(plugin_out) + " "+ str(datetime.datetime.fromtimestamp(float(last_c) ) )
current_state = int(current_state)
acked = int(acked)
if acked and disacked_display:
pass
elif current_state == 0: #ok
output_g.append(return_light_green(temp))
elif current_state == 1: #warning
output_w.append(return_yellow(temp))
elif current_state == 2: #critical
output_c_u_down.append(return_red(temp))
elif current_state == 3: #down/not reachable - unknown
output_c_u_down.append(return_red(temp))
else:
print return_red("THIS SHOULD NOT HAPPEN!")
sys.exit(1)
output_g.sort()
output_w.sort()
output_c_u_down.sort()
output = output_g + output_w + output_c_u_down
for i in output:
print i
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-d", "--disacked", action="store_true", dest="disacked_display", help = "do not output acknowledged problems")
(options, args) = parser.parse_args()
if options.disacked_display is None:
options.disacked_display = False
data = read_from_a_file("/var/cache/nagios3/status.dat", "read")
main(data, options.disacked_display)