| 1 |
# encoding: utf-8 |
| 2 |
|
| 3 |
# Nagstamon - Nagios status monitor for your desktop |
| 4 |
# Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al. |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 19 |
|
| 20 |
import urllib.request |
| 21 |
import urllib.parse |
| 22 |
import urllib.error |
| 23 |
import socket |
| 24 |
import sys |
| 25 |
import re |
| 26 |
import copy |
| 27 |
|
| 28 |
from datetime import datetime, timedelta |
| 29 |
|
| 30 |
from Nagstamon.objects import * |
| 31 |
from Nagstamon.servers.Generic import GenericServer |
| 32 |
from Nagstamon.config import conf |
| 33 |
from Nagstamon.helpers import webbrowser_open |
| 34 |
|
| 35 |
|
| 36 |
class CentreonServer(GenericServer): |
| 37 |
|
| 38 |
def __init__(self, **kwds): |
| 39 |
|
| 40 |
GenericServer.__init__(self, **kwds) |
| 41 |
|
| 42 |
self.TYPE = 'Centreon' |
| 43 |
|
| 44 |
# centreon generic web interface uses a sid which is needed to ask for news |
| 45 |
self.SID = None |
| 46 |
|
| 47 |
# HARD/SOFT state mapping |
| 48 |
self.HARD_SOFT = {'(H)': 'hard', '(S)': 'soft'} |
| 49 |
|
| 50 |
# apparently necessesary because of non-english states as in https://github.com/HenriWahl/Nagstamon/issues/91 (Centeron 2.5) |
| 51 |
self.TRANSLATIONS = {'INDISPONIBLE': 'DOWN', |
| 52 |
'INJOIGNABLE': 'UNREACHABLE', |
| 53 |
'CRITIQUE': 'CRITICAL', |
| 54 |
'INCONNU': 'UNKNOWN', |
| 55 |
'ALERTE': 'WARNING'} |
| 56 |
|
| 57 |
# Entries for monitor default actions in context menu |
| 58 |
self.MENU_ACTIONS = ['Monitor', 'Recheck', 'Acknowledge', 'Downtime'] |
| 59 |
|
| 60 |
# Centreon works better or at all with html.parser for BeautifulSoup |
| 61 |
self.PARSER = 'html.parser' |
| 62 |
|
| 63 |
# Needed to detect each Centreon's version |
| 64 |
self.centreon_version = None |
| 65 |
# Token that centreon use to protect the system |
| 66 |
self.centreon_token = None |
| 67 |
# To only detect broker once |
| 68 |
self.first_login = True |
| 69 |
# limit number of services retrived |
| 70 |
self.limit_services_number = 9999 |
| 71 |
# default value, applies to version 2.2 and others |
| 72 |
self.XML_PATH = 'xml' |
| 73 |
|
| 74 |
def init_config(self): |
| 75 |
''' |
| 76 |
dummy init_config, called at thread start, not really needed here, just omit extra properties |
| 77 |
''' |
| 78 |
# set URLs here already |
| 79 |
self.init_http() |
| 80 |
if not self.tls_error and self.centreon_version is not None: |
| 81 |
self._define_url() |
| 82 |
|
| 83 |
def init_http(self): |
| 84 |
""" |
| 85 |
initialize HTTP connection |
| 86 |
""" |
| 87 |
if self.session is None: |
| 88 |
GenericServer.init_http(self) |
| 89 |
|
| 90 |
if self.centreon_version is None: |
| 91 |
result_versioncheck = self.fetch_url(self.monitor_cgi_url + '/index.php', giveback='raw') |
| 92 |
raw_versioncheck, error_versioncheck = result_versioncheck.result, result_versioncheck.error |
| 93 |
if error_versioncheck == '': |
| 94 |
if re.search(r'2\.2\.[0-9]', raw_versioncheck): |
| 95 |
self.centreon_version = 2.2 |
| 96 |
if conf.debug_mode is True: |
| 97 |
self.debug(server=self.get_name(), debug='Centreon version detected : 2.2') |
| 98 |
# URLs for browser shortlinks/buttons on popup window |
| 99 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?p=1', |
| 100 |
'hosts': '$MONITOR$/main.php?p=20103&o=hpb', |
| 101 |
'services': '$MONITOR$/main.php?p=20202&o=svcpb', |
| 102 |
'history': '$MONITOR$/main.php?p=203'} |
| 103 |
elif re.search(r'2\.[3-6]\.[0-5]', raw_versioncheck): |
| 104 |
self.centreon_version = 2.3456 |
| 105 |
if conf.debug_mode is True: |
| 106 |
self.debug(server=self.get_name(), debug='Centreon version detected : 2.3 <=> 2.6.5') |
| 107 |
# URLs for browser shortlinks/buttons on popup window |
| 108 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?p=1', |
| 109 |
'hosts': '$MONITOR$/main.php?p=20103&o=hpb', |
| 110 |
'services': '$MONITOR$/main.php?p=20202&o=svcpb', |
| 111 |
'history': '$MONITOR$/main.php?p=203'} |
| 112 |
elif re.search(r'2\.6\.[6-9]', raw_versioncheck): |
| 113 |
self.centreon_version = 2.66 |
| 114 |
if conf.debug_mode is True: |
| 115 |
self.debug(server=self.get_name(), debug='Centreon version detected : 2.6.6') |
| 116 |
# URLs for browser shortlinks/buttons on popup window |
| 117 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?p=1', |
| 118 |
'hosts': '$MONITOR$/main.php?p=20103&o=hpb', |
| 119 |
'services': '$MONITOR$/main.php?p=20202&o=svcpb', |
| 120 |
'history': '$MONITOR$/main.php?p=203'} |
| 121 |
elif re.search(r'2\.7\.[0-9]', raw_versioncheck): |
| 122 |
# Centreon 2.7 only support C. Broker |
| 123 |
self.centreon_version = 2.7 |
| 124 |
if conf.debug_mode is True: |
| 125 |
self.debug(server=self.get_name(), debug='Centreon version detected : 2.7') |
| 126 |
# URLs for browser shortlinks/buttons on popup window |
| 127 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?', |
| 128 |
'hosts': '$MONITOR$/main.php?p=20202&o=hpb', |
| 129 |
'services': '$MONITOR$/main.php?p=20201&o=svcpb', |
| 130 |
'history': '$MONITOR$/main.php?p=203'} |
| 131 |
elif re.search(r'2\.8\.[0-9]', raw_versioncheck): |
| 132 |
# Centreon 2.8 only support C. Broker |
| 133 |
self.centreon_version = 2.8 |
| 134 |
if conf.debug_mode is True: |
| 135 |
self.debug(server=self.get_name(), debug='Centreon version detected : 2.8') |
| 136 |
# URLs for browser shortlinks/buttons on popup window |
| 137 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?', |
| 138 |
'hosts': '$MONITOR$/main.php?p=20202', |
| 139 |
'services': '$MONITOR$/main.php?p=20201', |
| 140 |
'history': '$MONITOR$/main.php?p=203'} |
| 141 |
elif re.search(r'18\.10\.[0-9]', raw_versioncheck): |
| 142 |
self.centreon_version = 18.10 |
| 143 |
if conf.debug_mode is True: |
| 144 |
self.debug(server=self.get_name(), debug='Centreon version detected : 18.10') |
| 145 |
# URLs for browser shortlinks/buttons on popup window |
| 146 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?', |
| 147 |
'hosts': '$MONITOR$/main.php?p=20202', |
| 148 |
'services': '$MONITOR$/main.php?p=20201', |
| 149 |
'history': '$MONITOR$/main.php?p=203'} |
| 150 |
elif re.search(r'19\.(04|10)\.[0-9]', raw_versioncheck) or re.search(r'20\.(04|10)\.[0-9]', raw_versioncheck): |
| 151 |
self.centreon_version = 19.04 |
| 152 |
if conf.debug_mode is True: |
| 153 |
self.debug(server=self.get_name(), debug='Centreon version detected : 19.04 <=> 20.10') |
| 154 |
# URLs for browser shortlinks/buttons on popup window |
| 155 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?', |
| 156 |
'hosts': '$MONITOR$/main.php?p=20202', |
| 157 |
'services': '$MONITOR$/main.php?p=20201', |
| 158 |
'history': '$MONITOR$/main.php?p=203'} |
| 159 |
else: |
| 160 |
# unsupported version or unable do determine |
| 161 |
self.centreon_version = 19.04 |
| 162 |
if conf.debug_mode is True: |
| 163 |
self.debug(server=self.get_name(), debug='Centreon version unknown : supposed to be >= 19.04') |
| 164 |
# URLs for browser shortlinks/buttons on popup window |
| 165 |
self.BROWSER_URLS = {'monitor': '$MONITOR$/main.php?', |
| 166 |
'hosts': '$MONITOR$/main.php?p=20202&o=hpb', |
| 167 |
'services': '$MONITOR$/main.php?p=20201&o=svcpb', |
| 168 |
'history': '$MONITOR$/main.php?p=203'} |
| 169 |
else: |
| 170 |
if conf.debug_mode is True: |
| 171 |
self.debug(server=self.get_name(), debug='Error getting the home page : ' + error_versioncheck) |
| 172 |
|
| 173 |
if self.first_login: |
| 174 |
self.SID = self._get_sid().result |
| 175 |
self.first_login = False |
| 176 |
|
| 177 |
del result_versioncheck, raw_versioncheck, error_versioncheck |
| 178 |
|
| 179 |
def reset_http(self): |
| 180 |
''' |
| 181 |
Centreon needs deletion of SID |
| 182 |
''' |
| 183 |
self.SID = None |
| 184 |
self.SID = self._get_sid().result |
| 185 |
|
| 186 |
def open_monitor(self, host, service=''): |
| 187 |
if self.use_autologin is True: |
| 188 |
auth = '&autologin=1&useralias=' + self.username + '&token=' + self.autologin_key |
| 189 |
else: |
| 190 |
auth = '' |
| 191 |
|
| 192 |
# Meta |
| 193 |
if host == '_Module_Meta': |
| 194 |
# Centreon < 2.7 |
| 195 |
if self.centreon_version < 2.7: |
| 196 |
webbrowser_open(self.urls_centreon['index'] + '?' + urllib.parse.urlencode({'p': 20206, 'o': 'meta'}) + auth ) |
| 197 |
# Centreon 2.7 |
| 198 |
elif self.centreon_version == 2.7: |
| 199 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':20206, 'o':'meta'}) + auth ) |
| 200 |
# Centreon 2.8 |
| 201 |
elif self.centreon_version == 2.8: |
| 202 |
m = re.search(r'^.+ \((?P<rsd>.+)\)$', service) |
| 203 |
if m: |
| 204 |
service = m.group('rsd') |
| 205 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':20201,'o':'svcd','host_name':'_Module_Meta','service_description':service}) + auth ) |
| 206 |
# Starting from Centreon 18.10 |
| 207 |
else: |
| 208 |
m = re.search(r'^.+ \((?P<rsd>.+)\)$', service) |
| 209 |
if m: |
| 210 |
service = m.group('rsd') |
| 211 |
webbrowser_open(self.urls_centreon['main_with_frames'] + '?' + urllib.parse.urlencode({'p':20201,'o':'svcd','host_name':'_Module_Meta','service_description':service}) + auth ) |
| 212 |
|
| 213 |
# must be a host if service is empty |
| 214 |
elif service == '': |
| 215 |
if self.centreon_version < 2.7: |
| 216 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':201,'o':'hd', 'host_name':host}) + auth ) |
| 217 |
elif self.centreon_version in [2.7, 2.8]: |
| 218 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':20202,'o':'hd', 'host_name':host}) + auth ) |
| 219 |
else: |
| 220 |
webbrowser_open(self.urls_centreon['main_with_frames'] + '?' + urllib.parse.urlencode({'p':20202,'o':'hd', 'host_name':host}) + auth ) |
| 221 |
|
| 222 |
# so it's a service |
| 223 |
else: |
| 224 |
if self.centreon_version < 2.7: |
| 225 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':202, 'o':'svcd', 'host_name':host, 'service_description':service}) + auth ) |
| 226 |
elif self.centreon_version in [2.7, 2.8]: |
| 227 |
webbrowser_open(self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p':20201,'o':'svcd', 'host_name':host, 'service_description':service}) + auth ) |
| 228 |
else: |
| 229 |
webbrowser_open(self.urls_centreon['main_with_frames'] + '?' + urllib.parse.urlencode({'p':20201,'o':'svcd', 'host_name':host, 'service_description':service}) + auth ) |
| 230 |
|
| 231 |
|
| 232 |
def _get_sid(self): |
| 233 |
''' |
| 234 |
gets a shiny new SID for XML HTTP requests to Centreon cutting it out via .partition() from raw HTML |
| 235 |
additionally get php session cookie |
| 236 |
''' |
| 237 |
try: |
| 238 |
# Aulogin with key, BROWSER_URLS needs the key |
| 239 |
if self.use_autologin: |
| 240 |
auth = '&autologin=1&useralias=' + self.username + '&token=' + self.autologin_key |
| 241 |
self.BROWSER_URLS= { 'monitor': self.BROWSER_URLS['monitor'] + auth,\ |
| 242 |
'hosts': self.BROWSER_URLS['hosts'] + auth,\ |
| 243 |
'services': self.BROWSER_URLS['services'] + auth,\ |
| 244 |
'history': self.BROWSER_URLS['history'] + auth} |
| 245 |
raw = self.fetch_url(self.monitor_cgi_url + '/index.php?p=101&autologin=1&useralias=' + self.username + '&token=' + self.autologin_key, giveback='raw') |
| 246 |
if conf.debug_mode: |
| 247 |
self.debug(server=self.get_name(), debug='Autologin : ' + self.username + ' : ' + self.autologin_key) |
| 248 |
# Gathering of the token who will be used to interact with Centreon (start with 2.66) |
| 249 |
if self.centreon_version >= 2.66 and self.centreon_version < 19.04: |
| 250 |
page = self.fetch_url(self.monitor_cgi_url + '/main.get.php') |
| 251 |
self.centreon_token = page.result.find('input', {'name': "centreon_token"})['value'] |
| 252 |
|
| 253 |
# Password auth |
| 254 |
else: |
| 255 |
login = self.fetch_url(self.monitor_cgi_url + '/index.php') |
| 256 |
if login.error == '' and login.status_code == 200: |
| 257 |
# Centreon >= 2.6.6 implement a token |
| 258 |
if self.centreon_version >= 2.66 and self.centreon_version <= 19.04: |
| 259 |
form = login.result.find('form') |
| 260 |
form_inputs = {} |
| 261 |
# Need to catch the centreon_token for login to work |
| 262 |
for form_input in ('centreon_token', 'submitLogin'): |
| 263 |
form_inputs[form_input] = form.find('input', {'name': form_input})['value'] |
| 264 |
self.centreon_token = form_inputs['centreon_token'] |
| 265 |
form_inputs['useralias'] = self.username |
| 266 |
form_inputs['password'] = self.password |
| 267 |
# fire up login button with all needed data |
| 268 |
raw = self.fetch_url(self.monitor_cgi_url + '/index.php', cgi_data=form_inputs) |
| 269 |
else: |
| 270 |
login_data = {"useralias" : self.username, "password" : self.password, "submit" : "Login"} |
| 271 |
raw = self.fetch_url(self.monitor_cgi_url + "/index.php", cgi_data=login_data, giveback="raw") |
| 272 |
if conf.debug_mode: |
| 273 |
self.debug(server=self.get_name(), debug='Password login : ' + self.username + ' : ' + self.password) |
| 274 |
|
| 275 |
sid = self.session.cookies.get('PHPSESSID', '') |
| 276 |
if conf.debug_mode: |
| 277 |
self.debug(server=self.get_name(), debug='SID : ' + sid) |
| 278 |
if self.centreon_version >= 2.66 and self.centreon_version < 19.04: |
| 279 |
self.debug(server=self.get_name(), debug='Centreon Token : ' + self.centreon_token) |
| 280 |
# those broker urls would not be changing too often so this check migth be done here |
| 281 |
if self.first_login: |
| 282 |
self._get_xml_path(sid) |
| 283 |
self.first_login = False |
| 284 |
return Result(result=sid) |
| 285 |
|
| 286 |
except: |
| 287 |
import traceback |
| 288 |
traceback.print_exc(file=sys.stdout) |
| 289 |
result, error = self.error(sys.exc_info()) |
| 290 |
return Result(result=result, error=error) |
| 291 |
|
| 292 |
|
| 293 |
def get_start_end(self, host): |
| 294 |
''' |
| 295 |
get start and end time for downtime from Centreon server |
| 296 |
''' |
| 297 |
try: |
| 298 |
# It's not possible since 18.10 to get date from the webinterface |
| 299 |
# because it's set in javascript |
| 300 |
if self.centreon_version < 18.10: |
| 301 |
cgi_data = {'o':'ah',\ |
| 302 |
'host_name':host} |
| 303 |
if self.centreon_version < 2.7: |
| 304 |
cgi_data['p'] = '20106' |
| 305 |
elif self.centreon_version == 2.7: |
| 306 |
cgi_data['p'] = '210' |
| 307 |
elif self.centreon_version == 2.8: |
| 308 |
cgi_data['o'] = 'a' |
| 309 |
cgi_data['p'] = '210' |
| 310 |
result = self.fetch_url(self.urls_centreon['main'], cgi_data = cgi_data, giveback='obj') |
| 311 |
|
| 312 |
html, error = result.result, result.error |
| 313 |
if error == '': |
| 314 |
start_date = html.find(attrs={'name':'start'}).attrs['value'] |
| 315 |
start_hour = html.find(attrs={'name':'start_time'}).attrs['value'] |
| 316 |
start_time = start_date + ' ' + start_hour |
| 317 |
|
| 318 |
end_date = html.find(attrs={'name':'end'}).attrs['value'] |
| 319 |
end_hour = html.find(attrs={'name':'end_time'}).attrs['value'] |
| 320 |
end_time = end_date + ' ' + end_hour |
| 321 |
return start_time, end_time |
| 322 |
|
| 323 |
else: |
| 324 |
start_time = datetime.now().strftime("%m/%d/%Y %H:%M") |
| 325 |
end_time = datetime.now() + timedelta(hours=2) |
| 326 |
end_time = end_time.strftime("%m/%d/%Y %H:%M") |
| 327 |
return start_time, end_time |
| 328 |
|
| 329 |
except: |
| 330 |
self.error(sys.exc_info()) |
| 331 |
return 'n/a', 'n/a' |
| 332 |
|
| 333 |
|
| 334 |
def get_host(self, host): |
| 335 |
''' |
| 336 |
Centreonified way to get host ip - attribute 'a' in down hosts xml is of no use for up |
| 337 |
hosts so we need to get ip anyway from web page |
| 338 |
''' |
| 339 |
# the fastest method is taking hostname as used in monitor |
| 340 |
if conf.connect_by_host == True or host == '': |
| 341 |
return Result(result=host) |
| 342 |
|
| 343 |
# do a web interface search limited to only one result - the hostname |
| 344 |
cgi_data = {'sid': self.SID, |
| 345 |
'search': host, |
| 346 |
'num': 0, |
| 347 |
'limit': 1, |
| 348 |
'sort_type':'hostname', |
| 349 |
'order': 'ASC', |
| 350 |
'date_time_format_status': 'd/m/Y H:i:s', |
| 351 |
'o': 'h', |
| 352 |
'p': 20102, |
| 353 |
'time': 0} |
| 354 |
|
| 355 |
centreon_hosts = self.urls_centreon['xml_hosts'] + '?' + urllib.parse.urlencode(cgi_data) |
| 356 |
|
| 357 |
result = self.fetch_url(centreon_hosts, giveback='xml') |
| 358 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 359 |
|
| 360 |
# initialize ip string |
| 361 |
ip = '' |
| 362 |
|
| 363 |
if len(xmlobj) != 0: |
| 364 |
ip = str(xmlobj.l.a.text) |
| 365 |
# when connection by DNS is not configured do it by IP |
| 366 |
try: |
| 367 |
if conf.connect_by_dns: |
| 368 |
# try to get DNS name for ip (reverse DNS), if not available use ip |
| 369 |
try: |
| 370 |
address = socket.gethostbyaddr(ip)[0] |
| 371 |
except: |
| 372 |
if conf.debug_mode: |
| 373 |
self.debug(server=self.get_name(), debug='Unable to do a reverse DNS lookup on IP: ' + ip) |
| 374 |
address = ip |
| 375 |
else: |
| 376 |
address = ip |
| 377 |
except: |
| 378 |
result, error = self.error(sys.exc_info()) |
| 379 |
return Result(result=result, error=error) |
| 380 |
|
| 381 |
else: |
| 382 |
result, error = self.error(sys.exc_info()) |
| 383 |
return Result(error=error) |
| 384 |
|
| 385 |
del xmlobj |
| 386 |
|
| 387 |
# print IP in debug mode |
| 388 |
if conf.debug_mode: |
| 389 |
self.debug(server=self.get_name(), debug='IP of %s:' % (host) + ' ' + address) |
| 390 |
|
| 391 |
# give back host or ip |
| 392 |
return Result(result=address) |
| 393 |
|
| 394 |
|
| 395 |
def _get_xml_path(self, sid): |
| 396 |
''' |
| 397 |
Find out where this instance of Centreon is publishing the status XMLs |
| 398 |
Centreon 2.6 + ndo/c.broker - /include/monitoring/status/Hosts/xml/{ndo,broker}/hostXML.php according to configuration |
| 399 |
Centreon 2.7 + c.broker - /include/monitoring/status/Hosts/xml/hostXML.php |
| 400 |
Centreon 2.8 + c.broker - /include/monitoring/status/Hosts/xml/hostXML.php |
| 401 |
regexping HTML for Javascript |
| 402 |
''' |
| 403 |
if self.centreon_version <= 2.66: |
| 404 |
# 2.6 support NDO and C. Broker, we must check which one is used |
| 405 |
cgi_data = {'p':201, 'sid':sid} |
| 406 |
result = self.fetch_url(self.monitor_cgi_url + '/main.php', cgi_data=cgi_data, giveback='raw') |
| 407 |
raw, error = result.result, result.error |
| 408 |
if error == '': |
| 409 |
if re.search(r'var _addrXML.*xml\/ndo\/host', raw): |
| 410 |
self.XML_PATH = 'xml/ndo' |
| 411 |
if conf.debug_mode: |
| 412 |
self.debug(server=self.get_name(), debug='Detected broker : NDO') |
| 413 |
elif re.search(r'var _addrXML.*xml\/broker\/host', raw): |
| 414 |
self.XML_PATH = 'xml/broker' |
| 415 |
if conf.debug_mode: |
| 416 |
self.debug(server=self.get_name(), debug='Detected broker : C. Broker') |
| 417 |
else: |
| 418 |
if conf.debug_mode: |
| 419 |
self.debug(server=self.get_name(), debug='Could not detect the broker for Centeron 2.[3-6]. Using Centreon Broker') |
| 420 |
self.XML_PATH = 'xml/broker' |
| 421 |
del raw |
| 422 |
else: |
| 423 |
if conf.debug_mode: |
| 424 |
self.debug(server=self.get_name(), debug='Unable to fetch the main page to detect the broker : ' + error) |
| 425 |
del result, error |
| 426 |
else: |
| 427 |
if conf.debug_mode: |
| 428 |
self.debug(server=self.get_name(), debug='Only Centreon Broker is supported in Centeon >= 2.7 -> XML_PATH=' + self.XML_PATH) |
| 429 |
|
| 430 |
|
| 431 |
def _define_url(self): |
| 432 |
urls_centreon_2_2 = { |
| 433 |
'main': self.monitor_cgi_url + '/main.php', |
| 434 |
'index': self.monitor_cgi_url + '/index.php', |
| 435 |
'xml_services': self.monitor_cgi_url + '/include/monitoring/status/Services/' + self.XML_PATH + '/serviceXML.php', |
| 436 |
'xml_hosts': self.monitor_cgi_url + '/include/monitoring/status/Hosts/' + self.XML_PATH + '/hostXML.php', |
| 437 |
'xml_meta': self.monitor_cgi_url + '/include/monitoring/status/Meta/' + self.XML_PATH + '/metaServiceXML.php', |
| 438 |
'xml_hostSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/hostSendCommand.php', |
| 439 |
'xml_serviceSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/serviceSendCommand.php', |
| 440 |
'external_cmd_cmdPopup': self.monitor_cgi_url + '/include/monitoring/external_cmd/cmdPopup.php', |
| 441 |
# no idea if this really exist in centreon < 2.7 |
| 442 |
'autologoutXMLresponse': self.monitor_cgi_url + '/include/common/javascript/autologoutXMLresponse.php' |
| 443 |
} |
| 444 |
|
| 445 |
# inconsistant url in Centreon 2.7 |
| 446 |
urls_centreon_2_7 = { |
| 447 |
'main': self.monitor_cgi_url + '/main.php', |
| 448 |
'index': self.monitor_cgi_url + '/index.php', |
| 449 |
'xml_services': self.monitor_cgi_url + '/include/monitoring/status/Services/' + self.XML_PATH + '/serviceXML.php', |
| 450 |
'xml_hosts': self.monitor_cgi_url + '/include/monitoring/status/Hosts/' + self.XML_PATH + '/broker/hostXML.php', |
| 451 |
'xml_meta': self.monitor_cgi_url + '/include/monitoring/status/Meta/' + self.XML_PATH + '/broker/metaServiceXML.php', |
| 452 |
'xml_hostSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/hostSendCommand.php', |
| 453 |
'xml_serviceSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/serviceSendCommand.php', |
| 454 |
'external_cmd_cmdPopup': self.monitor_cgi_url + '/include/monitoring/external_cmd/cmdPopup.php', |
| 455 |
'autologoutXMLresponse': self.monitor_cgi_url + '/include/common/javascript/autologoutXMLresponse.php' |
| 456 |
} |
| 457 |
|
| 458 |
urls_centreon_2_8 = { |
| 459 |
'main': self.monitor_cgi_url + '/main.php', |
| 460 |
'index': self.monitor_cgi_url + '/index.php', |
| 461 |
'xml_services': self.monitor_cgi_url + '/include/monitoring/status/Services/' + self.XML_PATH + '/serviceXML.php', |
| 462 |
'xml_hosts': self.monitor_cgi_url + '/include/monitoring/status/Hosts/' + self.XML_PATH + '/hostXML.php', |
| 463 |
'xml_hostSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/hostSendCommand.php', |
| 464 |
'xml_serviceSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/serviceSendCommand.php', |
| 465 |
'external_cmd_cmdPopup': self.monitor_cgi_url + '/include/monitoring/external_cmd/cmdPopup.php', |
| 466 |
'autologoutXMLresponse': self.monitor_cgi_url + '/include/core/autologout/autologoutXMLresponse.php' |
| 467 |
} |
| 468 |
|
| 469 |
urls_centreon_18_10 = { |
| 470 |
'main': self.monitor_cgi_url + '/main.get.php', |
| 471 |
# needed to get the frames around the page when opening the monitoring on a host/service |
| 472 |
'main_with_frames': self.monitor_cgi_url + '/main.php', |
| 473 |
'index': self.monitor_cgi_url + '/index.php', |
| 474 |
'xml_services': self.monitor_cgi_url + '/include/monitoring/status/Services/' + self.XML_PATH + '/serviceXML.php', |
| 475 |
'xml_hosts': self.monitor_cgi_url + '/include/monitoring/status/Hosts/' + self.XML_PATH + '/hostXML.php', |
| 476 |
'xml_hostSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/hostSendCommand.php', |
| 477 |
'xml_serviceSendCommand': self.monitor_cgi_url + '/include/monitoring/objectDetails/xml/serviceSendCommand.php', |
| 478 |
'external_cmd_cmdPopup': self.monitor_cgi_url + '/include/monitoring/external_cmd/cmdPopup.php', |
| 479 |
'keepAlive': self.monitor_cgi_url + '/api/internal.php?object=centreon_keepalive&action=keepAlive' |
| 480 |
} |
| 481 |
|
| 482 |
if self.centreon_version < 2.7: |
| 483 |
self.urls_centreon = urls_centreon_2_2 |
| 484 |
elif self.centreon_version == 2.7: |
| 485 |
self.urls_centreon = urls_centreon_2_7 |
| 486 |
elif self.centreon_version == 2.8: |
| 487 |
self.urls_centreon = urls_centreon_2_8 |
| 488 |
# 18.10 and beyond |
| 489 |
elif self.centreon_version >= 18.10: |
| 490 |
self.urls_centreon = urls_centreon_18_10 |
| 491 |
if conf.debug_mode: |
| 492 |
self.debug(server=self.get_name(), debug='URLs defined for Centreon %s' % (self.centreon_version)) |
| 493 |
|
| 494 |
|
| 495 |
def _get_host_id(self, host): |
| 496 |
''' |
| 497 |
get host_id via parsing raw html |
| 498 |
''' |
| 499 |
if self.centreon_version < 2.7: |
| 500 |
cgi_data = {'p': 20102, 'o': 'hd', 'host_name': host, 'sid': self.SID} |
| 501 |
else: |
| 502 |
cgi_data = {'p': 20202, 'o': 'hd', 'host_name': host, 'sid': self.SID} |
| 503 |
|
| 504 |
url = self.urls_centreon['main'] + '?' + urllib.parse.urlencode(cgi_data) |
| 505 |
|
| 506 |
result = self.fetch_url(url, giveback='raw') |
| 507 |
raw, error = result.result, result.error |
| 508 |
|
| 509 |
if error == '': |
| 510 |
host_id = raw.partition("var host_id = '")[2].partition("'")[0] |
| 511 |
del raw |
| 512 |
else: |
| 513 |
if conf.debug_mode: |
| 514 |
self.debug(server=self.get_name(), debug='Host ID could not be retrieved.') |
| 515 |
|
| 516 |
# some cleanup |
| 517 |
del result, error |
| 518 |
|
| 519 |
# only if host_id is an usable integer return it |
| 520 |
try: |
| 521 |
if int(host_id): |
| 522 |
if conf.debug_mode: |
| 523 |
self.debug(server=self.get_name(), host=host, debug='Host ID is ' + host_id) |
| 524 |
return host_id |
| 525 |
else: |
| 526 |
return '' |
| 527 |
except: |
| 528 |
return '' |
| 529 |
|
| 530 |
|
| 531 |
def _get_host_and_service_id(self, host, service): |
| 532 |
''' |
| 533 |
parse a ton of html to get a host and a service id... |
| 534 |
''' |
| 535 |
cgi_data = {'p':'20201',\ |
| 536 |
'host_name':host,\ |
| 537 |
'service_description':service,\ |
| 538 |
'o':'svcd'} |
| 539 |
|
| 540 |
# This request must be done in a GET, so just encode the parameters and fetch |
| 541 |
result = self.fetch_url(self.urls_centreon['main'] + '?' + urllib.parse.urlencode(cgi_data), giveback="raw") |
| 542 |
raw, error = result.result, result.error |
| 543 |
|
| 544 |
if error == '': |
| 545 |
host_id = raw.partition("var host_id = '")[2].partition("'")[0] |
| 546 |
svc_id = raw.partition("var svc_id = '")[2].partition("'")[0] |
| 547 |
del raw |
| 548 |
if conf.debug_mode: |
| 549 |
self.debug(server=self.get_name(), host=host, service=service, debug='- Get host/svc ID : ' + host_id + '/' + svc_id) |
| 550 |
else: |
| 551 |
if conf.debug_mode: |
| 552 |
self.debug(server=self.get_name(), host=host, service=service, debug='- IDs could not be retrieved.') |
| 553 |
|
| 554 |
# some cleanup |
| 555 |
del result, error |
| 556 |
|
| 557 |
# only if host_id is an usable integer return it |
| 558 |
try: |
| 559 |
if int(host_id) and int(svc_id): |
| 560 |
if conf.debug_mode: |
| 561 |
self.debug(server=self.get_name(), host=host, service=service, debug='- Host & Service ID are valid (int)') |
| 562 |
return host_id,svc_id |
| 563 |
else: |
| 564 |
return '','' |
| 565 |
except: |
| 566 |
return '','' |
| 567 |
|
| 568 |
|
| 569 |
def _get_status(self): |
| 570 |
''' |
| 571 |
Get status from Centreon Server |
| 572 |
''' |
| 573 |
# Be sure that the session is still active |
| 574 |
result = self._check_session() |
| 575 |
if result is not None: |
| 576 |
if result.result == 'ERROR': |
| 577 |
if 'urls_centreon' in result.error: |
| 578 |
result.error = 'Connection error' |
| 579 |
return result |
| 580 |
|
| 581 |
# services (unknown, warning or critical?) |
| 582 |
if self.centreon_version < 2.7: |
| 583 |
nagcgiurl_services = self.urls_centreon['xml_services'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'svcpb', 'sort_type':'status', 'sid':self.SID}) |
| 584 |
else: |
| 585 |
nagcgiurl_services = self.urls_centreon['xml_services'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'svcpb', 'p':20201, 'nc':0, 'criticality':0, 'statusService':'svcpb', 'sSetOrderInMemory':1, 'sid':self.SID}) |
| 586 |
|
| 587 |
# hosts (up or down or unreachable) |
| 588 |
# define hosts xml URL, because of inconsistant url |
| 589 |
if self.centreon_version < 2.7: |
| 590 |
nagcgiurl_hosts = self.urls_centreon['xml_hosts'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'hpb', 'sort_type':'status', 'sid':self.SID}) |
| 591 |
elif self.centreon_version >= 2.7 and self.centreon_version < 19.04: |
| 592 |
nagcgiurl_hosts = self.urls_centreon['xml_hosts'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'hpb', 'p':20202, 'criticality':0, 'statusHost':'hpb', 'sSetOrderInMemory':1, 'sid':self.SID}) |
| 593 |
else: |
| 594 |
nagcgiurl_hosts = self.urls_centreon['xml_hosts'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'hpb', 'p':20202, 'criticality':0, 'statusHost':'hpb', 'sSetOrderInMemory':1}) |
| 595 |
|
| 596 |
# hosts - mostly the down ones |
| 597 |
# unfortunately the hosts status page has a different structure so |
| 598 |
# hosts must be analyzed separately |
| 599 |
try: |
| 600 |
result = self.fetch_url(nagcgiurl_hosts, giveback='xml') |
| 601 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 602 |
|
| 603 |
# check if any error occured |
| 604 |
errors_occured = self.check_for_error(xmlobj, error, status_code) |
| 605 |
|
| 606 |
# if there are errors return them |
| 607 |
if errors_occured is not None: |
| 608 |
return errors_occured |
| 609 |
|
| 610 |
# Check if the result is not empty |
| 611 |
if len(xmlobj) == 0: |
| 612 |
if conf.debug_mode: |
| 613 |
self.debug(server=self.get_name(), debug='Empty host XML result') |
| 614 |
return Result(result=None, error="Empty host XML result") |
| 615 |
|
| 616 |
# in case there are no children session ID is expired |
| 617 |
if xmlobj.text.lower() == 'bad session id': |
| 618 |
del xmlobj |
| 619 |
if conf.debug_mode: |
| 620 |
self.debug(server=self.get_name(), debug='Bad session ID, retrieving new one...') |
| 621 |
|
| 622 |
# try again... |
| 623 |
self.SID = self._get_sid().result |
| 624 |
result = self.fetch_url(nagcgiurl_hosts, giveback='xml') |
| 625 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 626 |
errors_occured = self.check_for_error(xmlobj, error, status_code) |
| 627 |
# if there are errors return them |
| 628 |
if errors_occured is not None: |
| 629 |
return errors_occured |
| 630 |
|
| 631 |
# a second time a bad session id should raise an error |
| 632 |
if xmlobj.text.lower() == 'bad session id': |
| 633 |
if conf.debug_mode: |
| 634 |
self.debug(server=self.get_name(), debug='Even after renewing session ID, unable to get the XML') |
| 635 |
return Result(result='ERROR', |
| 636 |
error='Bad session ID', |
| 637 |
status_code=status_code) |
| 638 |
|
| 639 |
for l in xmlobj.findAll('l'): |
| 640 |
try: |
| 641 |
# host objects contain service objects |
| 642 |
if not l.hn.text in self.new_hosts: |
| 643 |
self.new_hosts[str(l.hn.text)] = GenericHost() |
| 644 |
self.new_hosts[str(l.hn.text)].name = str(l.hn.text) |
| 645 |
self.new_hosts[str(l.hn.text)].server = self.name |
| 646 |
self.new_hosts[str(l.hn.text)].status = str(l.cs.text) |
| 647 |
# disgusting workaround for https://github.com/HenriWahl/Nagstamon/issues/91 |
| 648 |
if self.new_hosts[str(l.hn.text)].status in self.TRANSLATIONS: |
| 649 |
self.new_hosts[str(l.hn.text)].status = self.TRANSLATIONS[self.new_hosts[str(l.hn.text)].status] |
| 650 |
self.new_hosts[str(l.hn.text)].attempt, self.new_hosts[str(l.hn.text)].status_type = str(l.tr.text).split(' ') |
| 651 |
self.new_hosts[str(l.hn.text)].status_type = self.HARD_SOFT[self.new_hosts[str(l.hn.text)].status_type] |
| 652 |
self.new_hosts[str(l.hn.text)].last_check = str(l.lc.text) |
| 653 |
self.new_hosts[str(l.hn.text)].duration = str(l.lsc.text) |
| 654 |
self.new_hosts[str(l.hn.text)].status_information = str(l.ou.text).replace('\n', ' ').strip() |
| 655 |
if l.find('cih') != None: |
| 656 |
self.new_hosts[str(l.hn.text)].criticality = str(l.cih.text) |
| 657 |
else: |
| 658 |
self.new_hosts[str(l.hn.text)].criticality = '' |
| 659 |
self.new_hosts[str(l.hn.text)].acknowledged = bool(int(str(l.ha.text))) |
| 660 |
self.new_hosts[str(l.hn.text)].scheduled_downtime = bool(int(str(l.hdtm.text))) |
| 661 |
if l.find('is') != None: |
| 662 |
self.new_hosts[str(l.hn.text)].flapping = bool(int(str(l.find('is').text))) |
| 663 |
else: |
| 664 |
self.new_hosts[str(l.hn.text)].flapping = False |
| 665 |
self.new_hosts[str(l.hn.text)].notifications_disabled = not bool(int(str(l.ne.text))) |
| 666 |
self.new_hosts[str(l.hn.text)].passiveonly = not bool(int(str(l.ace.text))) |
| 667 |
except: |
| 668 |
import traceback |
| 669 |
traceback.print_exc(file=sys.stdout) |
| 670 |
# set checking flag back to False |
| 671 |
self.isChecking = False |
| 672 |
result, error = self.error(sys.exc_info()) |
| 673 |
return Result(result=result, error=error) |
| 674 |
|
| 675 |
del xmlobj |
| 676 |
|
| 677 |
except: |
| 678 |
import traceback |
| 679 |
traceback.print_exc(file=sys.stdout) |
| 680 |
# set checking flag back to False |
| 681 |
self.isChecking = False |
| 682 |
result, error = self.error(sys.exc_info()) |
| 683 |
return Result(result=result, error=error) |
| 684 |
|
| 685 |
# services |
| 686 |
try: |
| 687 |
result = self.fetch_url(nagcgiurl_services, giveback='xml') |
| 688 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 689 |
|
| 690 |
# check if any error occured |
| 691 |
errors_occured = self.check_for_error(xmlobj, error, status_code) |
| 692 |
# if there are errors return them |
| 693 |
if errors_occured is not None: |
| 694 |
return errors_occured |
| 695 |
|
| 696 |
# Check if the result is not empty |
| 697 |
if len(xmlobj) == 0: |
| 698 |
if conf.debug_mode: |
| 699 |
self.debug(server=self.get_name(), debug='Empty service XML result') |
| 700 |
return Result(result=None, error="Empty service XML result") |
| 701 |
|
| 702 |
# in case there are no children session id is invalid |
| 703 |
if xmlobj.text.lower() == 'bad session id': |
| 704 |
# debug |
| 705 |
if conf.debug_mode: |
| 706 |
self.debug(server=self.get_name(), debug='Bad session ID, retrieving new one...') |
| 707 |
# try again... |
| 708 |
self.SID = self._get_sid().result |
| 709 |
result = self.fetch_url(nagcgiurl_services, giveback='xml') |
| 710 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 711 |
errors_occured = self.check_for_error(xmlobj, error, status_code) |
| 712 |
# if there are errors return them |
| 713 |
if errors_occured is not None: |
| 714 |
return errors_occured |
| 715 |
|
| 716 |
# a second time a bad session id should raise an error |
| 717 |
if xmlobj.text.lower() == 'bad session id': |
| 718 |
return Result(result='ERROR', |
| 719 |
error='Bad session ID', |
| 720 |
status_code=status_code) |
| 721 |
|
| 722 |
# In Centreon 2.8, Meta are merged with regular services |
| 723 |
if self.centreon_version < 2.8: |
| 724 |
# define meta-services xml URL |
| 725 |
if self.centreon_version == 2.7: |
| 726 |
nagcgiurl_meta_services = self.urls_centreon['xml_meta'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'meta', 'sort_type':'status', 'sid':self.SID}) |
| 727 |
else: |
| 728 |
nagcgiurl_meta_services = self.urls_centreon['xml_meta'] + '?' + urllib.parse.urlencode({'num':0, 'limit':self.limit_services_number, 'o':'meta', 'sort_type':'status', 'sid':self.SID}) |
| 729 |
|
| 730 |
# retrive meta-services xml STATUS |
| 731 |
result_meta = self.fetch_url(nagcgiurl_meta_services, giveback='xml') |
| 732 |
xmlobj_meta, error_meta, status_code_meta = result_meta.result, result_meta.error, result_meta.status_code |
| 733 |
|
| 734 |
# check if any error occured |
| 735 |
errors_occured = self.check_for_error(xmlobj_meta, error_meta, status_code_meta) |
| 736 |
|
| 737 |
# if there are errors return them |
| 738 |
if errors_occured is not None: |
| 739 |
return errors_occured |
| 740 |
|
| 741 |
# a second time a bad session id should raise an error |
| 742 |
if xmlobj_meta.text.lower() == 'bad session id': |
| 743 |
if conf.debug_mode: |
| 744 |
self.debug(server=self.get_name(), debug='Even after renewing session ID, unable to get the XML') |
| 745 |
|
| 746 |
return Result(result='ERROR', |
| 747 |
error='Bad session ID', |
| 748 |
status_code=status_code_meta) |
| 749 |
|
| 750 |
# INSERT META-services xml at the end of the services xml |
| 751 |
try: |
| 752 |
xmlobj.append(xmlobj_meta.reponse) |
| 753 |
except: |
| 754 |
import traceback |
| 755 |
traceback.print_exc(file=sys.stdout) |
| 756 |
# set checking flag back to False |
| 757 |
self.isChecking = False |
| 758 |
result, error = self.error(sys.exc_info()) |
| 759 |
return Result(result=result, error=error) |
| 760 |
# do some cleanup |
| 761 |
del xmlobj_meta |
| 762 |
|
| 763 |
for l in xmlobj.findAll('l'): |
| 764 |
try: |
| 765 |
# host objects contain service objects |
| 766 |
###if not self.new_hosts.has_key(str(l.hn.text)): |
| 767 |
if not l.hn.text in self.new_hosts: |
| 768 |
self.new_hosts[str(l.hn.text)] = GenericHost() |
| 769 |
self.new_hosts[str(l.hn.text)].name = str(l.hn.text) |
| 770 |
self.new_hosts[str(l.hn.text)].status = 'UP' |
| 771 |
# if a service does not exist create its object |
| 772 |
if not l.sd.text in self.new_hosts[str(l.hn.text)].services: |
| 773 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)] = GenericService() |
| 774 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].host = str(l.hn.text) |
| 775 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].name = str(l.sd.text) |
| 776 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].server = self.name |
| 777 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status = str(l.cs.text) |
| 778 |
|
| 779 |
if self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].host == '_Module_Meta': |
| 780 |
# ajusting service name for Meta services |
| 781 |
if self.centreon_version < 2.8: |
| 782 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].name = '{} ({})'.format(str(l.sd.text), l.rsd.text) |
| 783 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].attempt = str(l.ca.text) |
| 784 |
else: |
| 785 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].name = '{} ({})'.format(str(l.sdn.text), l.sdl.text) |
| 786 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].attempt, \ |
| 787 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_type = str(l.ca.text).split(' ') |
| 788 |
else: |
| 789 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].attempt, \ |
| 790 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_type = str(l.ca.text).split(' ') |
| 791 |
|
| 792 |
# disgusting workaround for https://github.com/HenriWahl/Nagstamon/issues/91 |
| 793 |
# Still needed in Centreon 2.8 at least : https://github.com/HenriWahl/Nagstamon/issues/344 |
| 794 |
# Need enhancement, we can do service state matching with this field <sc>service_unknown</sc> |
| 795 |
#if self.centreon_version < 2.66: |
| 796 |
if self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status in self.TRANSLATIONS: |
| 797 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status = self.TRANSLATIONS[\ |
| 798 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status] |
| 799 |
|
| 800 |
if not (self.centreon_version < 2.8 and self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].host == '_Module_Meta'): |
| 801 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_type =\ |
| 802 |
self.HARD_SOFT[self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_type] |
| 803 |
|
| 804 |
if conf.debug_mode: |
| 805 |
self.debug(server=self.get_name(), debug='Parsing service XML (Host/Service/Status_type) ' + self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].host + '/' + self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].name + '/' + self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_type) |
| 806 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].last_check = str(l.lc.text) |
| 807 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].duration = str(l.d.text) |
| 808 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].status_information = str(l.po.text).replace('\n', ' ').strip() |
| 809 |
|
| 810 |
if l.find('cih') != None: |
| 811 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].criticality = str(l.cih.text) |
| 812 |
else: |
| 813 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].criticality = '' |
| 814 |
|
| 815 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].acknowledged = bool(int(str(l.pa.text))) |
| 816 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].notifications_disabled = not bool(int(str(l.ne.text))) |
| 817 |
|
| 818 |
# for features not available in centreon < 2.8 and meta services |
| 819 |
if not (self.centreon_version < 2.8 and self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].host == '_Module_Meta'): |
| 820 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].scheduled_downtime = bool(int(str(l.dtm.text))) |
| 821 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].flapping = bool(int(str(l.find('is').text))) |
| 822 |
self.new_hosts[str(l.hn.text)].services[str(l.sd.text)].passiveonly = not bool(int(str(l.ac.text))) |
| 823 |
|
| 824 |
except: |
| 825 |
import traceback |
| 826 |
traceback.print_exc(file=sys.stdout) |
| 827 |
# set checking flag back to False |
| 828 |
self.isChecking = False |
| 829 |
result, error = self.error(sys.exc_info()) |
| 830 |
return Result(result=result, error=error) |
| 831 |
|
| 832 |
# do some cleanup |
| 833 |
del xmlobj |
| 834 |
|
| 835 |
except: |
| 836 |
import traceback |
| 837 |
traceback.print_exc(file=sys.stdout) |
| 838 |
# set checking flag back to False |
| 839 |
self.isChecking = False |
| 840 |
result, error = self.error(sys.exc_info()) |
| 841 |
return Result(result=result, error=error) |
| 842 |
|
| 843 |
# return True if all worked well |
| 844 |
return Result() |
| 845 |
|
| 846 |
|
| 847 |
def _set_acknowledge(self, host, service, author, comment, sticky, notify, persistent, all_services=None): |
| 848 |
# decision about host or service - they have different URLs |
| 849 |
try: |
| 850 |
if service == '': |
| 851 |
# host |
| 852 |
cgi_data = {'cmd': '14', |
| 853 |
'host_name': host, |
| 854 |
'author': author, |
| 855 |
'comment': comment, |
| 856 |
'submit': 'Add', |
| 857 |
'notify': int(notify), |
| 858 |
'persistent': int(persistent), |
| 859 |
'sticky': int(sticky), |
| 860 |
'ackhostservice': '0', |
| 861 |
'en': '1'} |
| 862 |
if self.centreon_version < 2.7: |
| 863 |
cgi_data['p'] = '20105' |
| 864 |
cgi_data['o'] = 'hpb' |
| 865 |
else: |
| 866 |
cgi_data['p'] = '20202' |
| 867 |
cgi_data['o'] = 'hpb' |
| 868 |
cgi_data['centreon_token'] = self.centreon_token |
| 869 |
|
| 870 |
# Post |
| 871 |
raw = self.fetch_url(self.urls_centreon['main'], cgi_data=cgi_data, giveback='raw') |
| 872 |
del raw |
| 873 |
|
| 874 |
# if host is acknowledged and all services should be to or if a service is acknowledged |
| 875 |
# (and all other on this host too) |
| 876 |
if service != '' or all_services: |
| 877 |
# service(s) @ host |
| 878 |
# if all_services is empty only one service has to be checked - the one clicked |
| 879 |
# otherwise if there all services should be acknowledged |
| 880 |
if not all_services: |
| 881 |
all_services = [service] |
| 882 |
|
| 883 |
# acknowledge all services on a host |
| 884 |
for s in all_services: |
| 885 |
cgi_data = {'cmd': '15', |
| 886 |
'host_name': host, |
| 887 |
'author': author, |
| 888 |
'comment': comment, |
| 889 |
'submit': 'Add', |
| 890 |
'notify': int(notify), |
| 891 |
'service_description': s, |
| 892 |
'force_check': '1', |
| 893 |
'persistent': int(persistent), |
| 894 |
# following not needed in 18.10, required in wich version ? |
| 895 |
'persistant': int(persistent), |
| 896 |
'sticky': int(sticky), |
| 897 |
'o': 'svcd', |
| 898 |
'en': '1'} |
| 899 |
if self.centreon_version < 2.7: |
| 900 |
cgi_data['p'] = '20215' |
| 901 |
else: |
| 902 |
cgi_data['p'] = '20201' |
| 903 |
cgi_data['centreon_token'] = self.centreon_token |
| 904 |
|
| 905 |
# in case of a meta-service, extract the 'rsd' field from the service name : |
| 906 |
if host == '_Module_Meta': |
| 907 |
m = re.search(r'^.+ \((?P<rsd>.+)\)$', s) |
| 908 |
if m: |
| 909 |
rsd = m.group('rsd') |
| 910 |
if self.centreon_version < 2.8: |
| 911 |
cgi_data = {'p': '20206', |
| 912 |
'o': 'meta', |
| 913 |
'cmd': '70', |
| 914 |
'select[' + host + ';' + rsd + ']': '1', |
| 915 |
'limit': '0'} |
| 916 |
elif self.centreon_version in [2.8, 18.10]: |
| 917 |
cgi_data['service_description'] = rsd |
| 918 |
|
| 919 |
# POST, for some strange reason only working if giveback is 'raw' |
| 920 |
raw = self.fetch_url(self.urls_centreon['main'], cgi_data=cgi_data, giveback='raw') |
| 921 |
del raw |
| 922 |
except: |
| 923 |
self.error(sys.exc_info()) |
| 924 |
|
| 925 |
|
| 926 |
def _set_recheck(self, host, service): |
| 927 |
''' |
| 928 |
host and service ids are needed to tell Centreon what whe want |
| 929 |
''' |
| 930 |
try: |
| 931 |
# decision about host or service - they have different URLs |
| 932 |
# Meta |
| 933 |
if host == '_Module_Meta': |
| 934 |
if conf.debug_mode: |
| 935 |
self.debug(server=self.get_name(), debug='Recheck on a Meta service, more work to be done') |
| 936 |
m = re.search(r'^.+ \((?P<rsd>.+)\)$', service) |
| 937 |
if m: |
| 938 |
rsd = m.group('rsd') |
| 939 |
if self.centreon_version < 2.8: |
| 940 |
url = self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p': '20206','o': 'meta','cmd': '3','select[' + host + ';' + rsd + ']': '1','limit':'0'}) |
| 941 |
else: |
| 942 |
url = self.urls_centreon['main'] + '?' + urllib.parse.urlencode({'p': '202','o': 'svc','cmd': '3','select[' + host + ';' + rsd + ']': '1','limit':'1','centreon_token':self.centreon_token}) |
| 943 |
|
| 944 |
elif service == '': |
| 945 |
# ... it can only be a host, so check all his services and there is a command for that |
| 946 |
host_id = self._get_host_id(host) |
| 947 |
|
| 948 |
if self.centreon_version < 2.7: |
| 949 |
url = self.urls_centreon['xml_hostSendCommand'] + '?' + urllib.parse.urlencode({'cmd':'host_schedule_check', 'actiontype':1,'host_id':host_id,'sid':self.SID}) |
| 950 |
else: |
| 951 |
url = self.urls_centreon['xml_hostSendCommand'] + '?' + urllib.parse.urlencode({'cmd':'host_schedule_check', 'actiontype':1,'host_id':host_id}) |
| 952 |
del host_id |
| 953 |
|
| 954 |
else: |
| 955 |
# service @ host |
| 956 |
host_id, service_id = self._get_host_and_service_id(host, service) |
| 957 |
|
| 958 |
# Starting from 19.04 this must be in POST |
| 959 |
if self.centreon_version < 19.04: |
| 960 |
# fill and encode URL data |
| 961 |
cgi_data = urllib.parse.urlencode({'cmd':'service_schedule_check', 'actiontype':1,\ |
| 962 |
'host_id':host_id, 'service_id':service_id, 'sid':self.SID}) |
| 963 |
|
| 964 |
url = self.urls_centreon['xml_serviceSendCommand'] + '?' + cgi_data |
| 965 |
del host_id, service_id |
| 966 |
else: |
| 967 |
cgi_data = {'cmd': 'service_schedule_check', |
| 968 |
'host_id': host_id, |
| 969 |
'service_id': service_id, |
| 970 |
'actiontype': '0'} |
| 971 |
del host_id, service_id |
| 972 |
|
| 973 |
if self.centreon_version < 19.04: |
| 974 |
# execute GET request |
| 975 |
raw = self.fetch_url(url, giveback='raw') |
| 976 |
del raw |
| 977 |
else: |
| 978 |
# running remote cgi command with POST method, for some strange reason only working if |
| 979 |
# giveback is 'raw' |
| 980 |
raw = self.fetch_url(self.urls_centreon['xml_serviceSendCommand'], cgi_data=cgi_data, giveback='raw') |
| 981 |
del raw |
| 982 |
except: |
| 983 |
self.error(sys.exc_info()) |
| 984 |
|
| 985 |
|
| 986 |
def _set_downtime(self, host, service, author, comment, fixed, start_time, end_time, hours, minutes): |
| 987 |
''' |
| 988 |
gets actual host and service ids and apply them to downtime cgi |
| 989 |
''' |
| 990 |
try: |
| 991 |
# duration unit is minute |
| 992 |
duration = (hours * 60) + minutes |
| 993 |
# need cmdPopup.php needs boolean |
| 994 |
if fixed == 1: |
| 995 |
fixed = 'true' |
| 996 |
else: |
| 997 |
fixed = 'false' |
| 998 |
|
| 999 |
# Host downtime |
| 1000 |
if service == '': |
| 1001 |
if self.centreon_version < 19.04: |
| 1002 |
cgi_data = {'cmd':75,\ |
| 1003 |
'duration':duration,\ |
| 1004 |
'duration_scale':'m',\ |
| 1005 |
'start':start_time,\ |
| 1006 |
'end':end_time,\ |
| 1007 |
'comment':comment,\ |
| 1008 |
'fixed':fixed,\ |
| 1009 |
'downtimehostservice':'true',\ |
| 1010 |
'author':author,\ |
| 1011 |
'sid':self.SID,\ |
| 1012 |
'select['+host+']':1 |
| 1013 |
} |
| 1014 |
# Params has changed starting from 19.04 |
| 1015 |
else: |
| 1016 |
cgi_data = {'cmd':75, |
| 1017 |
'duration':duration, |
| 1018 |
'duration_scale':'m', |
| 1019 |
'comment':comment, |
| 1020 |
'start':start_time, |
| 1021 |
'end':end_time, |
| 1022 |
'host_or_centreon_time':0, |
| 1023 |
'fixed':fixed, |
| 1024 |
'downtimehostservice':'true', |
| 1025 |
'author':author, |
| 1026 |
'resources':'["'+host+'"]' |
| 1027 |
} |
| 1028 |
|
| 1029 |
# Service downtime |
| 1030 |
else: |
| 1031 |
# Centreon 2.8 only, in case of a meta-service, extract the 'rsd' field from the service name : |
| 1032 |
if host == '_Module_Meta' and self.centreon_version in [2.8, 18.10]: |
| 1033 |
m = re.search(r'^.+ \((?P<rsd>.+)\)$', service) |
| 1034 |
if m: |
| 1035 |
rsd = m.group('rsd') |
| 1036 |
service = rsd |
| 1037 |
if self.centreon_version < 19.04: |
| 1038 |
cgi_data = {'cmd':74,\ |
| 1039 |
'duration':duration,\ |
| 1040 |
'duration_scale':'m',\ |
| 1041 |
'start':start_time,\ |
| 1042 |
'end':end_time,\ |
| 1043 |
'comment':comment,\ |
| 1044 |
'fixed':fixed,\ |
| 1045 |
'downtimehostservice':0,\ |
| 1046 |
'author':author,\ |
| 1047 |
'sid':self.SID,\ |
| 1048 |
'select['+host+';'+service+']':1 |
| 1049 |
} |
| 1050 |
|
| 1051 |
# Params has changed starting from 19.04 |
| 1052 |
else: |
| 1053 |
cgi_data = {'cmd':74, |
| 1054 |
'duration':duration, |
| 1055 |
'duration_scale':'m', |
| 1056 |
'comment':comment, |
| 1057 |
'start':start_time, |
| 1058 |
'end':end_time, |
| 1059 |
'host_or_centreon_time':0, |
| 1060 |
'fixed':fixed, |
| 1061 |
'downtimehostservice':0, |
| 1062 |
'author':author, |
| 1063 |
'resources':'["'+host+'%3B'+service+'"]' |
| 1064 |
} |
| 1065 |
|
| 1066 |
if self.centreon_version < 19.04: |
| 1067 |
# This request must be done in a GET, so just encode the parameters and fetch |
| 1068 |
raw = self.fetch_url(self.urls_centreon['external_cmd_cmdPopup'] + '?' + urllib.parse.urlencode(cgi_data), giveback="raw") |
| 1069 |
del raw |
| 1070 |
# Starting from 19.04, must be POST |
| 1071 |
else: |
| 1072 |
# Do it in POST |
| 1073 |
raw = self.fetch_url(self.urls_centreon['external_cmd_cmdPopup'], cgi_data=cgi_data, giveback='raw') |
| 1074 |
del raw |
| 1075 |
|
| 1076 |
except: |
| 1077 |
self.error(sys.exc_info()) |
| 1078 |
|
| 1079 |
|
| 1080 |
def _check_session(self): |
| 1081 |
if conf.debug_mode: |
| 1082 |
self.debug(server=self.get_name(), debug='Checking session status') |
| 1083 |
if 'url_centreon' not in self.__dict__: |
| 1084 |
self.init_config() |
| 1085 |
if self.centreon_version: |
| 1086 |
try: |
| 1087 |
if self.centreon_version >= 18.10: |
| 1088 |
result = self.fetch_url(self.urls_centreon['keepAlive'], giveback='raw') |
| 1089 |
self.raw, self.error, self.status_code = result.result, result.error, result.status_code |
| 1090 |
# Return 200 & null a session is open |
| 1091 |
if conf.debug_mode: |
| 1092 |
self.debug(server=self.get_name(), debug='Session status : ' + self.raw + ', http code : ' + str(self.status_code)) |
| 1093 |
# 401 if no valid session is present |
| 1094 |
if self.status_code == 401: |
| 1095 |
self.SID = self._get_sid().result |
| 1096 |
if conf.debug_mode: |
| 1097 |
self.debug(server=self.get_name(), debug='Session renewed') |
| 1098 |
|
| 1099 |
else: |
| 1100 |
result = self.fetch_url(self.urls_centreon['autologoutXMLresponse'], giveback='xml') |
| 1101 |
xmlobj, error, status_code = result.result, result.error, result.status_code |
| 1102 |
self.session_state = xmlobj.find("state").text.lower() |
| 1103 |
if conf.debug_mode: |
| 1104 |
self.debug(server=self.get_name(), debug='Session status : ' + self.session_state) |
| 1105 |
if self.session_state == "nok": |
| 1106 |
self.SID = self._get_sid().result |
| 1107 |
if conf.debug_mode: |
| 1108 |
self.debug(server=self.get_name(), debug='Session renewed') |
| 1109 |
except: |
| 1110 |
import traceback |
| 1111 |
traceback.print_exc(file=sys.stdout) |
| 1112 |
result, error = self.error(sys.exc_info()) |
| 1113 |
return Result(result=result, error=error) |
| 1114 |
else: |
| 1115 |
return Result(result='ERROR', |
| 1116 |
error='Cannot detect Centreon version') |