|
@@ -36,17 +36,20 @@ class nagios_service(dict):
|
36
|
36
|
SID_ERROR = 16
|
37
|
37
|
#
|
38
|
38
|
SNAME_OK = "OKAY"
|
|
39
|
+ SNAME_WARNING_LAST = "WARNING_LAST"
|
|
40
|
+ SNAME_ERROR_LAST = "ERROR_LAST"
|
39
|
41
|
SNAME_FLAPPING = "FLAPPING"
|
40
|
42
|
SNAME_WARNING = "WARNING"
|
41
|
43
|
SNAME_ERROR = "ERROR"
|
42
|
44
|
SNAME_UNKNOWN = "UNKNOWN"
|
43
|
45
|
TIMEFORMAT = "%d.%m.%Y %H:%M"
|
44
|
46
|
|
45
|
|
- def __init__(self, service_dict) -> None:
|
|
47
|
+ def __init__(self, service_dict, last) -> None:
|
46
|
48
|
super().__init__(service_dict)
|
|
49
|
+ self.__last__ = last
|
47
|
50
|
|
48
|
51
|
def is_problem(self):
|
49
|
|
- return self["status"] != self.SID_OK or self.flapping()
|
|
52
|
+ return self.status() != self.SNAME_OK
|
50
|
53
|
|
51
|
54
|
def host_name(self):
|
52
|
55
|
return self['host_name']
|
|
@@ -57,8 +60,14 @@ class nagios_service(dict):
|
57
|
60
|
def status(self):
|
58
|
61
|
status = self["status"]
|
59
|
62
|
default = self.SNAME_UNKNOWN
|
60
|
|
- if status == self.SID_OK and self.flapping():
|
61
|
|
- return self.SNAME_FLAPPING
|
|
63
|
+ tm_interrest = time.localtime(time.time() - 60 * 60 * self.__last__)
|
|
64
|
+ if status == self.SID_OK:
|
|
65
|
+ if self.flapping():
|
|
66
|
+ return self.SNAME_FLAPPING
|
|
67
|
+ elif self.__last_critical__() > tm_interrest:
|
|
68
|
+ return self.SNAME_ERROR_LAST
|
|
69
|
+ elif self.__last_warning__() > tm_interrest:
|
|
70
|
+ return self.SNAME_WARNING_LAST
|
62
|
71
|
return {
|
63
|
72
|
self.SID_OK: self.SNAME_OK,
|
64
|
73
|
self.SID_WARNING: self.SNAME_WARNING,
|
|
@@ -68,6 +77,21 @@ class nagios_service(dict):
|
68
|
77
|
def last_okay(self):
|
69
|
78
|
return time.strftime(self.TIMEFORMAT, time.localtime(self["last_time_ok"] / 1000))
|
70
|
79
|
|
|
80
|
+ def __last_warning__(self):
|
|
81
|
+ return time.localtime(self["last_time_warning"] / 1000)
|
|
82
|
+
|
|
83
|
+ def last_warning(self):
|
|
84
|
+ return time.strftime(self.TIMEFORMAT, self.__last_warning__())
|
|
85
|
+
|
|
86
|
+ def __last_critical__(self):
|
|
87
|
+ return time.localtime(self["last_time_critical"] / 1000)
|
|
88
|
+
|
|
89
|
+ def last_critical(self):
|
|
90
|
+ return time.strftime(self.TIMEFORMAT, self.__last_critical__())
|
|
91
|
+
|
|
92
|
+ def last_unknown(self):
|
|
93
|
+ return time.strftime(self.TIMEFORMAT, time.localtime(self["last_time_unknown"] / 1000))
|
|
94
|
+
|
71
|
95
|
def info(self):
|
72
|
96
|
return self.get("plugin_output") or self.get("long_plugin_output")
|
73
|
97
|
|
|
@@ -77,29 +101,29 @@ class nagios_service(dict):
|
77
|
101
|
def __color__(self):
|
78
|
102
|
return {
|
79
|
103
|
self.SNAME_OK: colors.OKGREEN,
|
|
104
|
+ self.SNAME_WARNING_LAST: colors.OKBLUE,
|
|
105
|
+ self.SNAME_ERROR_LAST: colors.OKBLUE,
|
80
|
106
|
self.SNAME_FLAPPING: colors.OKBLUE,
|
81
|
107
|
self.SNAME_WARNING: colors.WARNING,
|
82
|
108
|
self.SNAME_ERROR: colors.FAIL,
|
83
|
109
|
self.SNAME_UNKNOWN: colors.OKCYAN
|
84
|
110
|
}.get(self.status())
|
85
|
111
|
|
86
|
|
- return colors.OKBLUE
|
87
|
|
-
|
88
|
112
|
def __head__(self, color=False):
|
89
|
113
|
rv = headline(color)
|
90
|
|
- rv += "+-----------------------------------------------------------------------------------------\n"
|
91
|
|
- rv += "| Host | Service Name | State | Last time Okay |\n"
|
92
|
|
- rv += "+---------------------------+-----------------+----------+------------------+-------------\n"
|
|
114
|
+ rv += "+---------------------------------------------------------------------------------------------\n"
|
|
115
|
+ rv += "| Host | Service Name | State | Last time Okay |\n"
|
|
116
|
+ rv += "+---------------------------+-----------------+--------------+------------------+-------------\n"
|
93
|
117
|
return rv
|
94
|
118
|
|
95
|
119
|
def __foot__(self):
|
96
|
|
- return "+---------------------------+-----------------+----------+------------------+-------------\n"
|
|
120
|
+ return "+---------------------------+-----------------+--------------+------------------+-------------\n"
|
97
|
121
|
|
98
|
122
|
def __str__(self, color=False):
|
99
|
123
|
cols = []
|
100
|
124
|
cols.append((self.host_name, 25, False))
|
101
|
125
|
cols.append((self.name, 15, color))
|
102
|
|
- cols.append((self.status, 8, color))
|
|
126
|
+ cols.append((self.status, 12, color))
|
103
|
127
|
cols.append((self.last_okay, 16, color))
|
104
|
128
|
cols.append((self.info, 0, False))
|
105
|
129
|
#
|
|
@@ -136,13 +160,13 @@ class __nagios_status__(dict):
|
136
|
160
|
for host in self:
|
137
|
161
|
for service in self[host]:
|
138
|
162
|
if service.is_problem():
|
139
|
|
- rv.__add_service__(host, service)
|
|
163
|
+ rv.__add_service__(host, service, service.__last__)
|
140
|
164
|
return rv
|
141
|
165
|
|
142
|
|
- def __add_service__(self, host, service):
|
|
166
|
+ def __add_service__(self, host, service, last):
|
143
|
167
|
if host not in self:
|
144
|
168
|
self[host] = []
|
145
|
|
- self[host].append(nagios_service(service))
|
|
169
|
+ self[host].append(nagios_service(service, last))
|
146
|
170
|
|
147
|
171
|
def __str__(self, color=False) -> str:
|
148
|
172
|
rv = ""
|
|
@@ -164,7 +188,7 @@ class nagios_status(__nagios_status__):
|
164
|
188
|
KEY_DATA = 'data'
|
165
|
189
|
KEY_SERVICELIST = 'servicelist'
|
166
|
190
|
|
167
|
|
- def __init__(self, host, secure) -> None:
|
|
191
|
+ def __init__(self, host, secure, last) -> None:
|
168
|
192
|
super().__init__()
|
169
|
193
|
#
|
170
|
194
|
if secure:
|
|
@@ -177,7 +201,7 @@ class nagios_status(__nagios_status__):
|
177
|
201
|
hosts = data.get(self.KEY_DATA, {}).get(self.KEY_SERVICELIST, {})
|
178
|
202
|
for host in hosts:
|
179
|
203
|
for service in hosts.get(host, {}):
|
180
|
|
- self.__add_service__(host, hosts.get(host).get(service))
|
|
204
|
+ self.__add_service__(host, hosts.get(host).get(service), last)
|
181
|
205
|
|
182
|
206
|
|
183
|
207
|
if __name__ == "__main__":
|
|
@@ -185,12 +209,13 @@ if __name__ == "__main__":
|
185
|
209
|
parser.add_argument("hostname", help="The hostname and port of the nagios server (e.g. nagios:8080)")
|
186
|
210
|
parser.add_argument("-a", "--all", action="store_true", default=False,
|
187
|
211
|
help="print the status of all nagios monitorings. Default is all problems.")
|
|
212
|
+ parser.add_argument("-last", "--last", type=int, default=0, help="Report problems of the last l hours.")
|
188
|
213
|
parser.add_argument("-s", "--secure", action="store_true", default=False, help="Enables secure connection (https)")
|
189
|
214
|
parser.add_argument("-m", "--monochrome", action="store_true", default=False, help="No colored output")
|
190
|
215
|
args = parser.parse_args()
|
191
|
216
|
#
|
192
|
217
|
try:
|
193
|
|
- s = nagios_status(args.hostname, args.secure)
|
|
218
|
+ s = nagios_status(args.hostname, args.secure, args.last)
|
194
|
219
|
except requests.ConnectionError:
|
195
|
220
|
print(headline(not args.monochrome) + "Can not connect to given host.")
|
196
|
221
|
else:
|