Browse Source

nagios status: added possibilty to report recent problems of the last -l hours

master
Dirk Alders 3 months ago
parent
commit
930f4d13e1
1 changed files with 42 additions and 17 deletions
  1. 42
    17
      status.py

+ 42
- 17
status.py View File

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

Loading…
Cancel
Save