|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8; py-indent-offset: 4 -*- |
| 3 | + |
| 4 | +# Copyright: (c) 2022 - 2025, |
| 5 | +# Michael Sekania & |
| 6 | +# Robin Gierse <robin.gierse@checkmk.com> & |
| 7 | +# Max Sickora <max.sickora@checkmk.com> & |
| 8 | +# Lars Getwan <lars.getwan@checkmk.com> |
| 9 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 10 | + |
| 11 | +from __future__ import absolute_import, division, print_function |
| 12 | + |
| 13 | +__metaclass__ = type |
| 14 | + |
| 15 | +import json |
| 16 | +import time |
| 17 | + |
| 18 | +from ansible_collections.checkmk.general.plugins.module_utils.api import CheckmkAPI |
| 19 | +from ansible_collections.checkmk.general.plugins.module_utils.discovery import ( |
| 20 | + HTTP_CODES, |
| 21 | + HTTP_CODES_BULK, |
| 22 | + HTTP_CODES_BULK_SC, |
| 23 | + HTTP_CODES_SC, |
| 24 | + Discovery, |
| 25 | +) |
| 26 | +from ansible_collections.checkmk.general.plugins.module_utils.types import ( |
| 27 | + generate_result, |
| 28 | +) |
| 29 | + |
| 30 | +COMPATIBLE_MODES = [ |
| 31 | + "new", |
| 32 | + "remove", |
| 33 | + "fix_all", |
| 34 | + "only_host_labels", |
| 35 | + "only_service_labels", |
| 36 | + "tabula_rasa", |
| 37 | + "refresh", |
| 38 | + "monitor_undecided_services", |
| 39 | +] |
| 40 | + |
| 41 | +MONITOR_UNDECIDED_SERVICES = [ |
| 42 | + "new", |
| 43 | + "fix_all", |
| 44 | + "refresh", |
| 45 | + "tabula_rasa", |
| 46 | + "monitor_undecided_services", |
| 47 | +] |
| 48 | + |
| 49 | +REMOVE_VANISHED_SERVICES = ["remove", "fix_all", "refresh", "tabula_rasa"] |
| 50 | + |
| 51 | +UPDATE_SERVICE_LABELS = [ |
| 52 | + "only_service_labels", |
| 53 | + "fix_all", |
| 54 | + "refresh", |
| 55 | + "tabula_rasa", |
| 56 | +] |
| 57 | + |
| 58 | +UPDATE_HOST_LABELS = [ |
| 59 | + "new", |
| 60 | + "fix_all", |
| 61 | + "only_host_labels", |
| 62 | + "refresh", |
| 63 | + "tabula_rasa", |
| 64 | +] |
| 65 | + |
| 66 | +SUPPORTED_VERSIONS = { |
| 67 | + "min": "2.6.0", |
| 68 | + "max": "3.0.0p99", |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +class ServiceDiscoveryAPI(CheckmkAPI): |
| 73 | + def post(self): |
| 74 | + mode = self.params.get("state") |
| 75 | + if mode not in COMPATIBLE_MODES: |
| 76 | + return generate_result( |
| 77 | + msg="State %s is not supported with this Checkmk version." % mode |
| 78 | + ) |
| 79 | + |
| 80 | + if mode == "monitor_undecided_services": |
| 81 | + return generate_result( |
| 82 | + msg="State %s is only supported in bulk mode." % mode |
| 83 | + ) |
| 84 | + |
| 85 | + data = { |
| 86 | + "host_name": self.params.get("host_name"), |
| 87 | + "mode": mode, |
| 88 | + } |
| 89 | + |
| 90 | + return self._fetch( |
| 91 | + code_mapping=HTTP_CODES, |
| 92 | + endpoint="domain-types/service_discovery_run/actions/start/invoke", |
| 93 | + data=data, |
| 94 | + method="POST", |
| 95 | + logger=self.logger, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +class ServiceBulkDiscoveryAPI(CheckmkAPI): |
| 100 | + def post(self): |
| 101 | + mode = self.params.get("state") |
| 102 | + if mode not in COMPATIBLE_MODES: |
| 103 | + return generate_result( |
| 104 | + msg="State %s is not supported with this Checkmk version." % mode |
| 105 | + ) |
| 106 | + |
| 107 | + options = { |
| 108 | + "monitor_undecided_services": False, |
| 109 | + "remove_vanished_services": False, |
| 110 | + "update_service_labels": False, |
| 111 | + "update_host_labels": False, |
| 112 | + } |
| 113 | + |
| 114 | + if self.params.get("state") in MONITOR_UNDECIDED_SERVICES: |
| 115 | + options["monitor_undecided_services"] = True |
| 116 | + |
| 117 | + if self.params.get("state") in REMOVE_VANISHED_SERVICES: |
| 118 | + options["remove_vanished_services"] = True |
| 119 | + |
| 120 | + if self.params.get("state") in UPDATE_SERVICE_LABELS: |
| 121 | + options["update_service_labels"] = True |
| 122 | + |
| 123 | + if self.params.get("state") in UPDATE_HOST_LABELS: |
| 124 | + options["update_host_labels"] = True |
| 125 | + |
| 126 | + data = { |
| 127 | + "hostnames": self.params.get("hosts", []), |
| 128 | + "options": options, |
| 129 | + "do_full_scan": self.params.get("do_full_scan", True), |
| 130 | + "bulk_size": self.params.get("bulk_size", 1), |
| 131 | + "ignore_errors": self.params.get("ignore_errors", True), |
| 132 | + } |
| 133 | + |
| 134 | + return self._fetch( |
| 135 | + code_mapping=HTTP_CODES_BULK, |
| 136 | + endpoint="domain-types/discovery_run/actions/bulk-discovery-start/invoke", |
| 137 | + data=data, |
| 138 | + method="POST", |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +class ServiceCompletionAPI(CheckmkAPI): |
| 143 | + def get(self): |
| 144 | + data = {} |
| 145 | + |
| 146 | + return self._fetch( |
| 147 | + code_mapping=HTTP_CODES_SC, |
| 148 | + endpoint=( |
| 149 | + "objects/service_discovery_run/" |
| 150 | + + self.params.get("host_name") |
| 151 | + + "/actions/wait-for-completion/invoke" |
| 152 | + ), |
| 153 | + data=data, |
| 154 | + method="GET", |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +class ServiceCompletionBulkAPI(CheckmkAPI): |
| 159 | + def get(self, job_id): |
| 160 | + data = {} |
| 161 | + |
| 162 | + return self._fetch( |
| 163 | + code_mapping=HTTP_CODES_BULK_SC, |
| 164 | + endpoint=("objects/background_job/%s" % job_id), |
| 165 | + data=data, |
| 166 | + method="GET", |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | +class Discovery300(Discovery): |
| 171 | + def __init__(self, module, logger): |
| 172 | + super().__init__(module, logger) |
| 173 | + |
| 174 | + self.discovery_single = ServiceDiscoveryAPI(self.module, self.logger) |
| 175 | + self.discovery_bulk = ServiceBulkDiscoveryAPI(self.module, self.logger) |
| 176 | + self.completion_single = ServiceCompletionAPI(self.module, self.logger) |
| 177 | + self.completion_bulk = ServiceCompletionBulkAPI(self.module, self.logger) |
| 178 | + |
| 179 | + self.discovery_api = self._discovery_api() |
| 180 | + self.service_completion_api = self._service_completion_api() |
| 181 | + |
| 182 | + self.supported_versions = SUPPORTED_VERSIONS |
| 183 | + |
| 184 | + def _discovery_api(self): |
| 185 | + if self.single_mode: |
| 186 | + return self.discovery_single |
| 187 | + |
| 188 | + return self.discovery_bulk |
| 189 | + |
| 190 | + def _service_completion_api(self): |
| 191 | + if self.single_mode: |
| 192 | + return self.completion_single |
| 193 | + |
| 194 | + return self.completion_bulk |
| 195 | + |
| 196 | + def _wait_for_completion(self, what, job_id=None): |
| 197 | + now = time.time() |
| 198 | + |
| 199 | + if self.timeout > 0: |
| 200 | + deadline = now + self.timeout |
| 201 | + else: |
| 202 | + deadline = 0 # In case of infinite timeout |
| 203 | + |
| 204 | + if self.bulk_mode and not job_id: |
| 205 | + return generate_result( |
| 206 | + msg=( |
| 207 | + "Unable to get job_id for bulk service discovery. " |
| 208 | + "Will not wait for job to finish" |
| 209 | + ), |
| 210 | + failed=False, |
| 211 | + changed=True, |
| 212 | + ) |
| 213 | + |
| 214 | + while True: |
| 215 | + now = time.time() |
| 216 | + if self.timeout > 0 and now > deadline: |
| 217 | + return generate_result( |
| 218 | + msg="Timeout reached while waiting for %s discovery" % what |
| 219 | + ) |
| 220 | + |
| 221 | + if self.single_mode: |
| 222 | + result = self.service_completion_api.get() |
| 223 | + # For single mode, there's a forwarding, but _fetch() doesn't support that. |
| 224 | + if result.http_code != 302: |
| 225 | + break |
| 226 | + |
| 227 | + else: |
| 228 | + # For bulk mode, the completion api shows the state of the job |
| 229 | + result = self.service_completion_api.get(job_id) |
| 230 | + if not json.loads(result.content).get("extensions").get("active"): |
| 231 | + break |
| 232 | + |
| 233 | + time.sleep(3) |
| 234 | + |
| 235 | + return result |
| 236 | + |
| 237 | + def start_discovery(self): |
| 238 | + if self.wait_for_previous and self.single_mode: |
| 239 | + # For bulk mode, it's neither possible nor necessary to wait for the previous |
| 240 | + # discovery, as jobs are now allowed to run in parallel |
| 241 | + result = self._wait_for_completion("previous") |
| 242 | + if result.failed: |
| 243 | + return result |
| 244 | + |
| 245 | + result = self.discovery_api.post() |
| 246 | + if result.failed: |
| 247 | + return result |
| 248 | + |
| 249 | + if self.wait_for_completion: |
| 250 | + if self.single_mode and result.http_code != 200: |
| 251 | + result = self._wait_for_completion("current") |
| 252 | + elif self.bulk_mode: |
| 253 | + job_id = json.loads(result.content).get("id") |
| 254 | + result = self._wait_for_completion("current", job_id=job_id) |
| 255 | + |
| 256 | + return result |
0 commit comments