-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcheckmk.py
269 lines (228 loc) · 9.44 KB
/
checkmk.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
# Copyright: (c) 2024, Max Sickora <max.sickora@checkmk.com>
# 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 = """
name: checkmk
author: Max Sickora (@max-checkmk)
short_description: Dynamic Inventory Source or Checkmk
description:
- Get hosts from any checkmk site.
- Generate groups based on tag groups or sites in Checkmk.
extends_documentation_fragment: [checkmk.general.common]
options:
plugin:
description: Name of the plugin. Should always be C(checkmk.general.checkmk).
type: string
required: true
choices: ['checkmk.general.checkmk']
groupsources:
description:
- List of sources for grouping
- Possible sources are C(sites) and C(hosttags)
type: list
elements: str
required: false
"""
EXAMPLES = """
# To get started, you need to create a file called `checkmk.yml`, which contains
# one of the example blocks below and use it as your inventory source.
# E.g., with `ansible-inventory -i checkmk.yml --graph`.
# Group all hosts based on both tag groups and sites:
plugin: checkmk.general.checkmk
server_url: "http://hostname/"
site: "sitename"
automation_user: "cmkadmin"
automation_secret: "******"
validate_certs: False
groupsources: ["hosttags", "sites"]
"""
import json
import re
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.utils.display import Display
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
)
display = Display()
class InventoryModule(BaseInventoryPlugin):
"""Host inventory parser for ansible using Checkmk as source."""
NAME = "checkmk.general.checkmk"
def __init__(self):
super(InventoryModule, self).__init__()
self.plugin = None
self.server_url = None
self.site = None
self.user = None
self.secret = None
self.validate_certs = None
self.groupsources = []
self.hosttaggroups = []
self.tags = []
self.groups = []
self.hosts = []
self.sites = []
def convertname(self, name):
"""Removes empty space and changes bad chars to _"""
regex = r"[^A-Za-z0-9\_]"
return re.sub(regex, "_", name.replace(" ", ""))
def verify_file(self, path):
"""return true/false if this is possibly a valid file for this plugin to consume"""
valid = False
if super(InventoryModule, self).verify_file(path):
# base class verifies that file exists and is readable by current user
if path.endswith(("checkmk.yaml", "checkmk.yml")):
self.display.vvv("Inventory source file verified")
valid = True
else:
self.display.vvv(
"Inventory source file doesn't end with 'checkmk.yaml' or 'checkmk.yml'"
)
return valid
def _generate_groups(self):
if self.groupsources:
if "hosttags" in self.groupsources:
hosttags = []
for hosttaggroups in self.hosttaggroups:
if len(hosttaggroups.get("tags")) > 1:
hosttags += [
(
"tag_" + hosttaggroups.get("id") + "_" + tag.get("id")
if tag.get("id")
else "tag_" + hosttaggroups.get("id") + "_None"
)
for tag in (hosttaggroups.get("tags"))
]
else:
# If the tag group has only one choice, we have to generate TWO groups,
# one for hosts that have this tag set and one for hosts that have it unset
hosttags.append("tag_" + hosttaggroups.get("id") + "_None")
hosttags.append(
"tag_"
+ hosttaggroups.get("id")
+ "_"
+ hosttaggroups.get("tags")[0].get("id")
)
self.groups.extend(hosttags)
if "sites" in self.groupsources:
sites = ["site_" + site.get("id") for site in self.sites]
self.groups.extend(sites)
def parse(self, inventory, loader, path, cache=False):
super(InventoryModule, self).parse(inventory, loader, path, cache)
config = self._read_config_data(path)
try:
self.plugin = self.get_option("plugin")
self.server_url = self.get_option("server_url")
self.site = self.get_option("site")
self.user = self.get_option("automation_user")
self.secret = self.get_option("automation_secret")
self.validate_certs = self.get_option("validate_certs")
self.groupsources = self.get_option("groupsources")
except Exception as e:
raise AnsibleParserError("All correct options required: {}".format(e))
api = CheckMKLookupAPI(
site_url=self.get_option("server_url") + "/" + self.get_option("site"),
user=self.get_option("automation_user"),
secret=self.get_option("automation_secret"),
validate_certs=self.get_option("validate_certs"),
)
self.hosttaggroups = self._get_taggroups(api)
self.tags = [("tag_" + tag.get("id")) for tag in self.hosttaggroups]
self.sites = self._get_sites(api)
self.hosts = self._get_hosts(api)
self._generate_groups()
self._populate()
def _populate(self):
"""Return the hosts and groups"""
for group in self.groups:
self.inventory.add_group(self.convertname(group))
for host in self.hosts:
self.inventory.add_host(host["id"])
self.inventory.set_variable(host["id"], "ipaddress", host["ipaddress"])
self.inventory.set_variable(host["id"], "folder", host["folder"])
if self.groupsources:
if "hosttags" in self.groupsources:
for host in self.hosts:
for tag in self.tags:
if host.get("tags").get(tag):
self.inventory.add_child(
tag + "_" + self.convertname(host.get("tags").get(tag)),
host.get("id"),
)
else:
self.inventory.add_child(tag + "_None", host.get("id"))
if "sites" in self.groupsources:
for host in self.hosts:
self.inventory.add_child(
"site_" + self.convertname(host.get("site")), host.get("id")
)
def _get_hosts(self, api):
response = json.loads(
api.get(
"/domain-types/host_config/collections/all",
{"effective_attributes": True},
)
)
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)
hosts = [
{
"id": host.get("id"),
"title": host.get("extensions").get("title"),
"ipaddress": host.get("extensions").get("attributes").get("ipaddress"),
"folder": host.get("extensions").get("folder"),
"site": host.get("extensions").get("effective_attributes").get("site"),
"tags": {
taggroup: tag
for taggroup, tag in host.get("extensions")
.get("effective_attributes")
.items()
if taggroup in self.tags
},
}
for host in (response.get("value"))
]
return hosts
def _get_taggroups(self, api):
response = json.loads(api.get("/domain-types/host_tag_group/collections/all"))
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)
hosttaggroups = [
{
"id": hosttaggroup.get("id"),
"tags": hosttaggroup.get("extensions").get("tags"),
}
for hosttaggroup in (response.get("value"))
]
return hosttaggroups
def _get_sites(self, api):
response = json.loads(api.get("/domain-types/site_connection/collections/all"))
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)
sites = [
{"id": site.get("id"), "customer": site.get("extensions").get("customer")}
for site in (response.get("value"))
]
return sites