Debian Codegraph @

Nagstamon/servers/Centreon/__init__.py

raw
1 # Nagstamon - Nagios status monitor for your desktop
2 # Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
18 import json
19
20 from . import CentreonAPI
21 from ..Generic import GenericServer
22 from Nagstamon.config import conf
23
24 class CentreonServer(GenericServer):
25 """
26 Use this class as switch
27 """
28 TYPE = 'Centreon'
29 def __init__(self, **kwds):
30 # like all servers we need to initialize Generic
31 GenericServer.__init__(self, **kwds)
32 # due to not being initialized right now we need to access config directly to get this instance's config
33 server_conf = conf.servers.get(kwds.get('name'))
34 if server_conf.enabled:
35 self.authentication = conf.servers[self.get_name()].authentication
36 # because auf being very early in init process the property ignore_cert is not known yet
37 # add it here to be able to fetch URL and ignore certs if activated
38 self.ignore_cert = conf.servers[self.get_name()].ignore_cert
39 self.custom_cert_use = conf.servers[self.get_name()].custom_cert_use
40 # This URL exists at least from Centreon 22.x - if not accessible it must be legacy
41 versions_raw = self.fetch_url(f'{conf.servers[self.get_name()].monitor_cgi_url}/api/latest/platform/versions', no_auth=True, giveback='raw')
42 self.debug(server='[' + self.get_name() + ']', debug='Page retrieval to detect the version, status code : %s' % (str(versions_raw.status_code)))
43 if versions_raw.status_code == 200:
44 try:
45 data = json.loads(versions_raw.result)
46 except Exception as e:
47 self.debug(server='[' + self.get_name() + ']', debug='Page retrieval to detect the version, ERROR when decoding JSON')
48 self.enable = False
49 self.isChecking = False
50 return None
51
52 ver_major = int(data["web"]["major"])
53 ver_minor = int(data["web"]["minor"])
54 # API V2 is usable only after 21.04 (not tested), ressources endpoint is buggy in 20.10
55 if ver_major >= 21:
56 self.debug(server='[' + self.get_name() + ']', debug='Loading class API, Centreon version : ' + str(ver_major) + '.' + str(ver_minor))
57 from .CentreonAPI import CentreonServer as CentreonServerReal
58 elif ver_major < 21:
59 # Fallback to Legacy
60 self.debug(server='[' + self.get_name() + ']', debug='Loading class LEGACY, Centreon version : ' + str(ver_major) + '.' + str(ver_minor))
61 from .CentreonLegacy import CentreonServer as CentreonServerReal
62 else:
63 self.debug(server='[' + self.get_name() + ']', debug='Something wrong happens with Centreon, version detected : ' + str(ver_major) + '.' + str(ver_minor))
64 self.enable = False
65 self.isChecking = False
66 return None
67 else:
68 # Try to check if only it’s a Centreon
69 versions_raw = self.fetch_url(f'{server_conf.monitor_cgi_url}/main.php', no_auth=True, giveback='raw')
70 if versions_raw.status_code == 200:
71 from .CentreonLegacy import CentreonServer as CentreonServerReal
72 self.debug(server='[' + self.get_name() + ']', debug='Loading class LEGACY, Centreon version will be checked later')
73 else:
74 self.debug(server='[' + self.get_name() + ']', debug='The URL given is not a Centreon')
75 self.enable = False
76 self.isChecking = False
77 return None
78
79 # kind of mad but helps the servers/__init__.py to detect if there is any other class to be used
80 self.ClassServerReal = CentreonServerReal