| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from collections import OrderedDict |
| 22 |
from Nagstamon.servers.Generic import GenericServer |
| 23 |
from Nagstamon.config import conf |
| 24 |
import sys |
| 25 |
import json |
| 26 |
import datetime |
| 27 |
import copy |
| 28 |
import urllib.parse |
| 29 |
|
| 30 |
from Nagstamon.helpers import human_readable_duration_from_timestamp |
| 31 |
from Nagstamon.helpers import webbrowser_open |
| 32 |
from Nagstamon.objects import (GenericHost, GenericService, Result) |
| 33 |
|
| 34 |
|
| 35 |
class ThrukServer(GenericServer): |
| 36 |
""" |
| 37 |
Thruk is derived from generic (Nagios) server |
| 38 |
""" |
| 39 |
TYPE = 'Thruk' |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
STATUS_MAPPING = { "ack.gif" : "acknowledged", \ |
| 45 |
"passiveonly.gif" : "passiveonly", \ |
| 46 |
"disabled.gif" : "passiveonly", \ |
| 47 |
"ndisabled.gif" : "notifications_disabled", \ |
| 48 |
"downtime.gif" : "scheduled_downtime", \ |
| 49 |
"flapping.gif" : "flapping"} |
| 50 |
|
| 51 |
|
| 52 |
MENU_ACTIONS = ["Monitor", "Recheck", "Acknowledge", "Submit check result", "Downtime"] |
| 53 |
|
| 54 |
|
| 55 |
SUBMIT_CHECK_RESULT_ARGS = ["check_output", "performance_data"] |
| 56 |
|
| 57 |
|
| 58 |
BROWSER_URLS = { "monitor": "$MONITOR$", \ |
| 59 |
"hosts": "$MONITOR-CGI$/status.cgi?hostgroup=all&style=hostdetail&hoststatustypes=12&page=1&entries=all", \ |
| 60 |
"services": "$MONITOR-CGI$/status.cgi?dfl_s0_value_sel=5&dfl_s0_servicestatustypes=29&dfl_s0_op=%3D&style=detail&dfl_s0_type=host&dfl_s0_serviceprops=0&dfl_s0_servicestatustype=4&dfl_s0_servicestatustype=8&dfl_s0_servicestatustype=16&dfl_s0_servicestatustype=1&hidetop=&dfl_s0_hoststatustypes=15&dfl_s0_val_pre=&hidesearch=2&dfl_s0_value=all&dfl_s0_hostprops=0&nav=&page=1&entries=all", \ |
| 61 |
"history": "$MONITOR-CGI$/history.cgi?host=all&page=1&entries=all"} |
| 62 |
|
| 63 |
STATES_MAPPING = {"hosts" : {0 : "OK", 1 : "DOWN", 2 : "UNREACHABLE"}, \ |
| 64 |
"services" : {0 : "OK", 1 : "WARNING", 2 : "CRITICAL", 3 : "UNKNOWN"}} |
| 65 |
|
| 66 |
|
| 67 |
def __init__(self, **kwds): |
| 68 |
GenericServer.__init__(self, **kwds) |
| 69 |
|
| 70 |
|
| 71 |
def init_http(self): |
| 72 |
""" |
| 73 |
partly not constantly working Basic Authorization requires extra Autorization headers, |
| 74 |
different between various server types |
| 75 |
""" |
| 76 |
if self.session is None: |
| 77 |
GenericServer.init_http(self) |
| 78 |
|
| 79 |
|
| 80 |
try: |
| 81 |
|
| 82 |
if self.session is None or self.session.cookies.get('thruk_auth') is None: |
| 83 |
self.login() |
| 84 |
except: |
| 85 |
self.error(sys.exc_info()) |
| 86 |
|
| 87 |
|
| 88 |
def init_config(self): |
| 89 |
""" |
| 90 |
set URLs for CGI - they are static and there is no need to set them with every cycle |
| 91 |
""" |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
self.cgiurl_services = self.monitor_cgi_url + "/status.cgi?host=all&servicestatustypes=28&view_mode=json&"\ |
| 96 |
"entries=all&columns=host_name,description,state,last_check,"\ |
| 97 |
"last_state_change,plugin_output,current_attempt,"\ |
| 98 |
"max_check_attempts,active_checks_enabled,is_flapping,"\ |
| 99 |
"notifications_enabled,acknowledged,state_type,"\ |
| 100 |
"scheduled_downtime_depth,host_display_name,display_name" |
| 101 |
|
| 102 |
self.cgiurl_hosts = self.monitor_cgi_url + "/status.cgi?hostgroup=all&style=hostdetail&"\ |
| 103 |
"dfl_s0_hoststatustypes=12&dfl_s1_hostprops=1&dfl_s2_hostprops=4&dfl_s3_hostprops=524288&&dfl_s4_hostprops=4096&dfl_s5_hostprop=16&"\ |
| 104 |
"view_mode=json&entries=all&"\ |
| 105 |
"columns=name,state,last_check,last_state_change,"\ |
| 106 |
"plugin_output,current_attempt,max_check_attempts,"\ |
| 107 |
"active_checks_enabled,notifications_enabled,is_flapping,"\ |
| 108 |
"acknowledged,scheduled_downtime_depth,state_type,host_display_name,display_name" |
| 109 |
|
| 110 |
def login(self): |
| 111 |
""" |
| 112 |
use pure session instead of fetch_url to get Thruk session |
| 113 |
""" |
| 114 |
if self.session is None: |
| 115 |
self.refresh_authentication = False |
| 116 |
GenericServer.init_http(self) |
| 117 |
|
| 118 |
if self.use_autologin is True: |
| 119 |
req = self.session.post(self.monitor_cgi_url + '/user.cgi?', data={}, headers={'X-Thruk-Auth-Key':self.autologin_key.strip()}) |
| 120 |
if conf.debug_mode: |
| 121 |
self.debug(server=self.get_name(), debug='Auto Login status: ' + req.url + ' http code : ' + str(req.status_code)) |
| 122 |
if req.status_code != 200: |
| 123 |
self.refresh_authentication = True |
| 124 |
return Result(result=None, error="Login failed") |
| 125 |
else: |
| 126 |
|
| 127 |
self.session.cookies.set('thruk_test', '***') |
| 128 |
req = self.session.post(self.monitor_cgi_url + '/login.cgi?', |
| 129 |
data={'login': self.get_username(), |
| 130 |
'password': self.get_password(), |
| 131 |
'submit': 'Login'}) |
| 132 |
if conf.debug_mode: |
| 133 |
self.debug(server=self.get_name(), debug='Login status: ' + req.url + ' http code : ' + str(req.status_code)) |
| 134 |
if req.status_code != 200: |
| 135 |
self.refresh_authentication = True |
| 136 |
return Result(result=None, error="Login failed") |
| 137 |
|
| 138 |
if self.disabled_backends is not None: |
| 139 |
self.session.cookies.set('thruk_backends', '&'.join((f"{v}=2" for v in self.disabled_backends.split(',')))) |
| 140 |
|
| 141 |
def open_monitor(self, host, service=''): |
| 142 |
''' |
| 143 |
open monitor from tablewidget context menu |
| 144 |
''' |
| 145 |
|
| 146 |
if service == '': |
| 147 |
url = self.monitor_cgi_url + '/extinfo.cgi?type=1&' + urllib.parse.urlencode( { 'host': host }) |
| 148 |
else: |
| 149 |
url = self.monitor_cgi_url + '/extinfo.cgi?type=2&' + urllib.parse.urlencode( { 'host': host, 'service': self.hosts[host].services[ service ].real_name }) |
| 150 |
|
| 151 |
if conf.debug_mode: |
| 152 |
self.debug(server=self.get_name(), host=host, service=service, |
| 153 |
debug='Open host/service monitor web page {0}'.format(url)) |
| 154 |
webbrowser_open(url) |
| 155 |
|
| 156 |
def _set_acknowledge(self, host, service, author, comment, sticky, notify, persistent, all_services=None): |
| 157 |
''' |
| 158 |
send acknowledge to monitor server - might be different on every monitor type |
| 159 |
''' |
| 160 |
|
| 161 |
url = self.monitor_cgi_url + '/cmd.cgi' |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
cgi_data = OrderedDict() |
| 175 |
if service == '': |
| 176 |
cgi_data['cmd_typ'] = '33' |
| 177 |
else: |
| 178 |
cgi_data['cmd_typ'] = '34' |
| 179 |
cgi_data['cmd_mod'] = '2' |
| 180 |
cgi_data['host'] = host |
| 181 |
if service != '': |
| 182 |
cgi_data['service'] = self.hosts[host].services[ service ].real_name |
| 183 |
cgi_data['com_author'] = author |
| 184 |
cgi_data['com_data'] = comment |
| 185 |
cgi_data['btnSubmit'] = 'Commit' |
| 186 |
if notify is True: |
| 187 |
cgi_data['send_notification'] = 'on' |
| 188 |
if persistent is True: |
| 189 |
cgi_data['persistent'] = 'on' |
| 190 |
if sticky is True: |
| 191 |
cgi_data['sticky_ack'] = 'on' |
| 192 |
|
| 193 |
self.fetch_url(url, giveback='raw', cgi_data=cgi_data) |
| 194 |
|
| 195 |
|
| 196 |
if all_services: |
| 197 |
for s in all_services: |
| 198 |
cgi_data['cmd_typ'] = '34' |
| 199 |
cgi_data['service'] = self.hosts[host].services[ s ].real_name |
| 200 |
self.fetch_url(url, giveback='raw', cgi_data=cgi_data) |
| 201 |
|
| 202 |
def _set_recheck(self, host, service): |
| 203 |
self.session.headers.update({'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}) |
| 204 |
|
| 205 |
if service != '': |
| 206 |
if self.hosts[host].services[ service ].is_passive_only(): |
| 207 |
|
| 208 |
return |
| 209 |
try: |
| 210 |
|
| 211 |
result = self.fetch_url( |
| 212 |
self.monitor_cgi_url + '/cmd.cgi?' + urllib.parse.urlencode({'cmd_typ': '96', 'host': host})) |
| 213 |
self.start_time = dict(result.result.find(attrs={'name': 'start_time'}).attrs)['value'] |
| 214 |
|
| 215 |
if service == '': |
| 216 |
|
| 217 |
cmd_typ = '96' |
| 218 |
service_name = '' |
| 219 |
else: |
| 220 |
|
| 221 |
cmd_typ = '7' |
| 222 |
service_name = self.hosts[host].services[ service ].real_name |
| 223 |
|
| 224 |
cgi_data = urllib.parse.urlencode([('cmd_typ', cmd_typ), |
| 225 |
('cmd_mod', '2'), |
| 226 |
('host', host), |
| 227 |
('service', service_name), |
| 228 |
('start_time', self.start_time), |
| 229 |
('force_check', 'on'), |
| 230 |
('btnSubmit', 'Commit')]) |
| 231 |
|
| 232 |
self.fetch_url(self.monitor_cgi_url + '/cmd.cgi', giveback='raw', cgi_data=cgi_data) |
| 233 |
except: |
| 234 |
traceback.print_exc(file=sys.stdout) |
| 235 |
|
| 236 |
def _set_downtime(self, host, service, author, comment, fixed, start_time, end_time, hours, minutes): |
| 237 |
''' |
| 238 |
finally send downtime command to monitor server |
| 239 |
''' |
| 240 |
url = self.monitor_cgi_url + '/cmd.cgi' |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
cgi_data = OrderedDict() |
| 245 |
if service == '': |
| 246 |
cgi_data['cmd_typ'] = '55' |
| 247 |
else: |
| 248 |
cgi_data['cmd_typ'] = '56' |
| 249 |
cgi_data['cmd_mod'] = '2' |
| 250 |
cgi_data['trigger'] = '0' |
| 251 |
cgi_data['host'] = host |
| 252 |
if service != '': |
| 253 |
cgi_data['service'] = self.hosts[host].services[ service ].real_name |
| 254 |
cgi_data['com_author'] = author |
| 255 |
cgi_data['com_data'] = comment |
| 256 |
cgi_data['fixed'] = fixed |
| 257 |
cgi_data['start_time'] = start_time |
| 258 |
cgi_data['end_time'] = end_time |
| 259 |
cgi_data['hours'] = hours |
| 260 |
cgi_data['minutes'] = minutes |
| 261 |
cgi_data['btnSubmit'] = 'Commit' |
| 262 |
|
| 263 |
|
| 264 |
self.fetch_url(url, giveback='raw', cgi_data=cgi_data) |
| 265 |
|
| 266 |
def _get_status(self): |
| 267 |
""" |
| 268 |
Get status from Thruk Server |
| 269 |
""" |
| 270 |
|
| 271 |
self.new_hosts = dict() |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
try: |
| 277 |
|
| 278 |
result = self.fetch_url(self.cgiurl_hosts, giveback='raw') |
| 279 |
jsonraw, error, status_code = copy.deepcopy(result.result),\ |
| 280 |
copy.deepcopy(result.error),\ |
| 281 |
result.status_code |
| 282 |
|
| 283 |
|
| 284 |
errors_occured = self.check_for_error(jsonraw, error, status_code) |
| 285 |
|
| 286 |
if errors_occured is not None: |
| 287 |
return errors_occured |
| 288 |
|
| 289 |
|
| 290 |
if jsonraw.startswith("<"): |
| 291 |
self.refresh_authentication = True |
| 292 |
return Result(result=None, error="Login failed") |
| 293 |
|
| 294 |
|
| 295 |
elif not jsonraw == "[]": |
| 296 |
hosts = json.loads(jsonraw) |
| 297 |
|
| 298 |
for h in hosts: |
| 299 |
if h["name"] not in self.new_hosts: |
| 300 |
self.new_hosts[h["name"]] = GenericHost() |
| 301 |
self.new_hosts[h["name"]].name = h["name"] |
| 302 |
self.new_hosts[h["name"]].server = self.name |
| 303 |
self.new_hosts[h["name"]].status = self.STATES_MAPPING["hosts"][h["state"]] |
| 304 |
self.new_hosts[h["name"]].last_check = datetime.datetime.fromtimestamp(int(h["last_check"])).isoformat(" ") |
| 305 |
self.new_hosts[h["name"]].duration = human_readable_duration_from_timestamp(h["last_state_change"]) |
| 306 |
self.new_hosts[h["name"]].attempt = "%s/%s" % (h["current_attempt"], h["max_check_attempts"]) |
| 307 |
self.new_hosts[h["name"]].status_information = h["plugin_output"].replace("\n", " ").strip() |
| 308 |
self.new_hosts[h["name"]].passiveonly = not(bool(int(h["active_checks_enabled"]))) |
| 309 |
self.new_hosts[h["name"]].notifications_disabled = not(bool(int(h["notifications_enabled"]))) |
| 310 |
self.new_hosts[h["name"]].flapping = bool(int(h["is_flapping"])) |
| 311 |
self.new_hosts[h["name"]].acknowledged = bool(int(h["acknowledged"])) |
| 312 |
self.new_hosts[h["name"]].scheduled_downtime = bool(int(h["scheduled_downtime_depth"])) |
| 313 |
self.new_hosts[h["name"]].status_type = {0: "soft", 1: "hard"}[h["state_type"]] |
| 314 |
del h |
| 315 |
except: |
| 316 |
import traceback |
| 317 |
traceback.print_exc(file=sys.stdout) |
| 318 |
|
| 319 |
self.isChecking = False |
| 320 |
result, error = self.error(sys.exc_info()) |
| 321 |
return Result(result=result, error=error) |
| 322 |
|
| 323 |
|
| 324 |
try: |
| 325 |
|
| 326 |
result = self.fetch_url(self.cgiurl_services, giveback="raw") |
| 327 |
jsonraw, error, status_code = copy.deepcopy(result.result),\ |
| 328 |
copy.deepcopy(result.error),\ |
| 329 |
result.status_code |
| 330 |
|
| 331 |
|
| 332 |
errors_occured = self.check_for_error(jsonraw, error, status_code) |
| 333 |
|
| 334 |
if errors_occured is not None: |
| 335 |
return errors_occured |
| 336 |
|
| 337 |
|
| 338 |
if jsonraw.startswith("<"): |
| 339 |
self.refresh_authentication = True |
| 340 |
return Result(result=None, error="Login failed") |
| 341 |
|
| 342 |
|
| 343 |
elif not jsonraw == "[]": |
| 344 |
services = json.loads(jsonraw) |
| 345 |
|
| 346 |
for s in services: |
| 347 |
|
| 348 |
if s["host_name"] not in self.new_hosts: |
| 349 |
self.new_hosts[s["host_name"]] = GenericHost() |
| 350 |
self.new_hosts[s["host_name"]].name = s["host_name"] |
| 351 |
self.new_hosts[s["host_name"]].server = self.name |
| 352 |
self.new_hosts[s["host_name"]].status = "UP" |
| 353 |
|
| 354 |
if self.use_display_name_service: |
| 355 |
entry = s["display_name"] |
| 356 |
else: |
| 357 |
entry = s["description"] |
| 358 |
|
| 359 |
|
| 360 |
if entry not in self.new_hosts[s["host_name"]].services: |
| 361 |
self.new_hosts[s["host_name"]].services[ entry ] = GenericService() |
| 362 |
self.new_hosts[s["host_name"]].services[ entry ].host = s["host_name"] |
| 363 |
|
| 364 |
self.new_hosts[s["host_name"]].services[ entry ].name = entry |
| 365 |
self.new_hosts[s["host_name"]].services[ entry ].real_name = s["description"] |
| 366 |
|
| 367 |
self.new_hosts[s["host_name"]].services[ entry ].server = self.name |
| 368 |
self.new_hosts[s["host_name"]].services[ entry ].status = self.STATES_MAPPING["services"][s["state"]] |
| 369 |
self.new_hosts[s["host_name"]].services[ entry ].last_check = datetime.datetime.fromtimestamp(int(s["last_check"])).isoformat(" ") |
| 370 |
self.new_hosts[s["host_name"]].services[ entry ].duration = human_readable_duration_from_timestamp(s["last_state_change"]) |
| 371 |
self.new_hosts[s["host_name"]].services[ entry ].attempt = "%s/%s" % (s["current_attempt"], s["max_check_attempts"]) |
| 372 |
self.new_hosts[s["host_name"]].services[ entry ].status_information = s["plugin_output"].replace("\n", " ").strip() |
| 373 |
self.new_hosts[s["host_name"]].services[ entry ].passiveonly = not(bool(int(s["active_checks_enabled"]))) |
| 374 |
self.new_hosts[s["host_name"]].services[ entry ].notifications_disabled = not(bool(int(s["notifications_enabled"]))) |
| 375 |
self.new_hosts[s["host_name"]].services[ entry ].flapping = not(bool(int(s["notifications_enabled"]))) |
| 376 |
self.new_hosts[s["host_name"]].services[ entry ] .acknowledged = bool(int(s["acknowledged"])) |
| 377 |
self.new_hosts[s["host_name"]].services[ entry ].scheduled_downtime = bool(int(s["scheduled_downtime_depth"])) |
| 378 |
self.new_hosts[s["host_name"]].services[ entry ].status_type = {0: "soft", 1: "hard"}[s["state_type"]] |
| 379 |
del s |
| 380 |
except: |
| 381 |
import traceback |
| 382 |
traceback.print_exc(file=sys.stdout) |
| 383 |
|
| 384 |
self.isChecking = False |
| 385 |
result, error = self.error(sys.exc_info()) |
| 386 |
return Result(result=result, error=error) |
| 387 |
|
| 388 |
|
| 389 |
return Result() |