-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path__init__.py
159 lines (137 loc) · 4.8 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
class Webservice(object):
"""
Class for call Centreon Web Rest webservices
"""
__instance = None
def __new__(cls):
"""
Constructor with singleton for webservices
"""
if Webservice.__instance is None:
Webservice.__instance = object.__new__(cls)
Webservice.__instance.url = None
Webservice.__instance.authuser = None
Webservice.__instance.authpass = None
Webservice.__instance.auth_token = None
Webservice.__instance.check_ssl = True
return Webservice.__instance
def load(self, url, username, password, check_ssl=True):
"""
Load configuration for webservices
:param url: The Centreon Web URL
:type url: String
:param username: The username for connect to Centreon Web webservices
:type username: String
:param password: The password for connect to Centreon Web webservices
:type password: String
"""
self.url = url
self.authuser = username
self.authpass = password
self.check_ssl = check_ssl
def isLoaded(self):
"""
Test if webservices configuration is loaded
:return: If the webservices configuration is loaded
:rtype: Boolean
"""
if self.url is None:
return False
return True
def auth(self):
"""
Authenticate to the webservices
"""
request = requests.post(
self.url + '/api/index.php?action=authenticate',
data={
'username': self.authuser,
'password': self.authpass
},
verify=self.check_ssl
)
request.raise_for_status()
data = request.json()
self.auth_token = data['authToken']
def call_clapi(self, action=None, obj=None, values=None):
"""
Call an endpoint of Centreon Web for clapi wrapper
:param action: The clapi action
:type action: String
:param obj: The clapi object
:type obj: String
:param values: The values for the call
:type values: mixed
:return: The response of call
:rtype: dict
"""
if self.auth_token is None:
self.auth()
data = {}
if action is not None:
data['action'] = action
if obj is not None:
data['object'] = obj
if values is not None:
data['values'] = values
request = requests.post(
self.url + '/api/index.php?action=action&object=centreon_clapi',
headers={
'Content-Type': 'application/json',
'centreon-auth-token': self.auth_token
},
data=json.dumps(data),
verify=self.check_ssl
)
request.raise_for_status()
return request.json()
def centreon_realtime(self, action=None, obj=None, values=None):
if self.auth_token is None:
self.auth()
data = {}
obj_supported = ('hosts', 'services')
if action is not None:
data['action'] = action
if obj is not None:
if obj in obj_supported:
data['object'] = obj
else:
raise ValueError("Only support <hosts> or <services>")
if not self.check_ssl:
# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
requests.packages.urllib3\
.disable_warnings(InsecureRequestWarning)
request = requests.get(
self.url + '/api/index.php?object=centreon_realtime_'
+ obj + '&action=' + action,
headers={
'Content-Type': 'application/json',
'centreon-auth-token': self.auth_token
},
params=values,
verify=self.check_ssl
)
request.raise_for_status()
return request.json()
@staticmethod
def getInstance(url=None, username=None, password=None, check_ssl=True):
"""
Get an unique instance of the webservices
:param url: The Centreon Web URL
:type url: String
:param username: The username for connect to Centreon Web webservices
:type username: String
:param password: The password for connect to Centreon Web webservices
:type password: String
"""
instance = Webservice()
if instance.isLoaded():
return instance
if url is None or username is None or password is None:
raise KeyError('Missing parameters to load the Webservice')
instance.load(url, username, password, check_ssl)
return instance