-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsite.py
335 lines (281 loc) · 10.6 KB
/
site.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# Copyright: (c) 2024, Lars Getwan <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
---
module: site
short_description: Manage distributed monitoring in Checkmk.
# If this is part of a collection, you need to use semantic versioning,
# i.e. the version is of the form "2.5.0" and not "2.4".
version_added: "5.3.0"
description:
- Manage distributed monitoring within Checkmk.
extends_documentation_fragment:
- checkmk.general.common
- checkmk.general.site_options
author:
- Lars Getwan (@lgetwan)
"""
EXAMPLES = r"""
- name: "Add a remote site with configuration replication."
checkmk.general.site:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
site_id: "myremotesite"
site_connection:
site_config:
status_connection:
connection:
socket_type: tcp
port: 6557
encrypted: true
host: localhost
verify: true
proxy:
use_livestatus_daemon: "direct"
connect_timeout: 2
status_host:
status_host_set: "disabled"
url_prefix: "/myremotesite/"
configuration_connection:
enable_replication: true
url_of_remote_site: "http://localhost/myremotesite/check_mk/"
basic_settings:
site_id: "myremotesite"
customer: "provider"
alias: "My Remote Site"
state: "present"
- name: "Log into a remote site."
checkmk.general.site:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
site_id: "myremotesite"
site_connection:
authentication:
username: "myremote_admin"
password: "highly_secret"
state: "login"
- name: "Log out from a remote site."
checkmk.general.site:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
site_id: "myremotesite"
state: "logout"
- name: "Delete a remote site."
checkmk.general.site:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
site_id: "myremotesite"
state: "absent"
"""
RETURN = r"""
message:
description: The output message that the module generates. Contains the API response details in case of an error.
type: str
returned: always
sample: 'Site connection created.'
"""
import json
# https://docs.ansible.com/ansible/latest/dev_guide/testing/sanity/import.html
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.checkmk.general.plugins.module_utils.api import CheckmkAPI
from ansible_collections.checkmk.general.plugins.module_utils.logger import Logger
from ansible_collections.checkmk.general.plugins.module_utils.site import (
SiteConnection,
SiteEndpoints,
SiteHTTPCodes,
TargetAPI,
module_args,
)
from ansible_collections.checkmk.general.plugins.module_utils.types import RESULT
from ansible_collections.checkmk.general.plugins.module_utils.utils import (
exit_module,
remove_null_value_keys,
)
from ansible_collections.checkmk.general.plugins.module_utils.version import (
CheckmkVersion,
)
class SiteAPI(CheckmkAPI):
def __init__(self, module):
super().__init__(module)
self._verify_compatibility()
self.module = module
self.params = self.module.params
self.state = self.params.get("state")
def _get_endpoint(self, target_api, site_id=""):
if target_api == TargetAPI.CREATE:
return SiteEndpoints.create
if target_api in [TargetAPI.GET, TargetAPI.UPDATE]:
return "%s/%s" % (SiteEndpoints.default, site_id)
if target_api in [TargetAPI.LOGIN]:
return "%s/%s/actions/login/invoke" % (
SiteEndpoints.default,
site_id,
)
if target_api in [TargetAPI.LOGOUT]:
return "%s/%s/actions/logout/invoke" % (
SiteEndpoints.default,
site_id,
)
if target_api in [TargetAPI.DELETE]:
return "%s/%s/actions/delete/invoke" % (
SiteEndpoints.default,
site_id,
)
def get(self, site_id):
logger.debug(
"get endpoint: %s" % self._get_endpoint(TargetAPI.GET, site_id=site_id)
)
result = self._fetch(
code_mapping=SiteHTTPCodes.get,
endpoint=self._get_endpoint(TargetAPI.GET, site_id=site_id),
)
logger.debug("get data: %s" % str(result))
if result.http_code == 404:
return None
result = result._replace(content=json.loads(result.content))
return result
def create(self, site_connection):
logger.debug("create endpoint: %s" % self._get_endpoint(TargetAPI.CREATE))
logger.debug("create data: %s" % site_connection.get_api_data(TargetAPI.CREATE))
return self._fetch(
code_mapping=SiteHTTPCodes.create,
endpoint=self._get_endpoint(TargetAPI.CREATE),
data=site_connection.get_api_data(TargetAPI.CREATE),
method="POST",
)
def update(self, site_connection, desired_site_connection):
vorher = site_connection.site_config
site_connection.merge_with(desired_site_connection)
nachher = site_connection.site_config
logger.debug("update endpoint: %s" % self._get_endpoint(TargetAPI.UPDATE))
logger.debug("update data: %s" % site_connection.get_api_data(TargetAPI.UPDATE))
return self._fetch(
code_mapping=SiteHTTPCodes.update,
endpoint=self._get_endpoint(
TargetAPI.UPDATE, site_id=site_connection.site_id
),
data=site_connection.get_api_data(TargetAPI.UPDATE),
method="PUT",
)
def login(self, site_connection):
logger.debug(
"login endpoint: %s"
% self._get_endpoint(TargetAPI.LOGIN, site_id=site_connection.site_id)
)
logger.debug("login data: %s" % site_connection.get_api_data(TargetAPI.LOGIN))
return self._fetch(
code_mapping=SiteHTTPCodes.login,
endpoint=self._get_endpoint(
TargetAPI.LOGIN, site_id=site_connection.site_id
),
data=site_connection.get_api_data(TargetAPI.LOGIN),
method="POST",
)
def logout(self, site_connection):
logger.debug(
"logout endpoint: %s"
% self._get_endpoint(TargetAPI.LOGOUT, site_id=site_connection.site_id)
)
logger.debug("logout data: %s" % site_connection.get_api_data(TargetAPI.LOGOUT))
return self._fetch(
code_mapping=SiteHTTPCodes.logout,
endpoint=self._get_endpoint(
TargetAPI.LOGOUT, site_id=site_connection.site_id
),
method="POST",
)
def delete(self, site_connection):
logger.debug(
"delete endpoint: %s"
% self._get_endpoint(TargetAPI.DELETE, site_id=site_connection.site_id)
)
logger.debug("delete data: %s" % site_connection.get_api_data(TargetAPI.DELETE))
return self._fetch(
code_mapping=SiteHTTPCodes.delete,
endpoint=self._get_endpoint(
TargetAPI.DELETE, site_id=site_connection.site_id
),
method="POST",
)
def _verify_compatibility(self):
if self.getversion() <= CheckmkVersion("2.2.0"):
exit_module(
msg="Site management is only available for Checkmk versions starting with 2.2.0.",
failed=True,
)
logger = Logger()
def run_module():
# define available arguments/parameters a user can pass to the module
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
logger.set_loglevel(module._verbosity)
remove_null_value_keys(module.params)
site_id = module.params.get("site_id")
site_api = SiteAPI(module)
desired_site_connection = SiteConnection.from_module_params(module.params)
existing_site_connection = SiteConnection.from_api(site_api.get(site_id))
if desired_site_connection.state == "present":
if existing_site_connection and existing_site_connection.state == "present":
differences = existing_site_connection.diff(desired_site_connection)
if differences:
result = site_api.update(
existing_site_connection, desired_site_connection
)
result = result._replace(
msg="%s\nUpdated: %s" % (result.msg, ", ".join(differences))
)
else:
result = RESULT(
http_code=0,
msg="Site connection already exists with the desired parameters.",
content="",
etag="",
failed=False,
changed=False,
)
else:
result = site_api.create(desired_site_connection)
exit_module(module, result=result)
elif desired_site_connection.state == "absent":
if existing_site_connection and existing_site_connection.state == "present":
result = site_api.delete(existing_site_connection)
exit_module(module, result=result)
else:
exit_module(module, msg="Site connection already absent.")
elif desired_site_connection.state == "login":
if not existing_site_connection:
exit_module(module, msg="Site does not exist", failed=True)
if not existing_site_connection.logged_in():
result = site_api.login(desired_site_connection)
exit_module(module, result=result)
else:
exit_module(module, msg="Already logged in to site.")
elif desired_site_connection.state == "logout":
if not existing_site_connection:
exit_module(module, msg="Site does not exist", failed=True)
if existing_site_connection.logged_in():
result = site_api.logout(desired_site_connection)
exit_module(module, result=result)
else:
exit_module(module, msg="Already logged out from site.")
else:
exit_module(
module,
msg="Unexpected target state %s" % desired_site_connection.state,
failed=True,
)
def main():
run_module()
if __name__ == "__main__":
main()