| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2019, OVH SAS |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
--- |
| 13 |
module: zabbix_service |
| 14 |
short_description: Create/update/delete Zabbix service |
| 15 |
description: |
| 16 |
- Create/update/delete Zabbix service. |
| 17 |
author: |
| 18 |
- "Emmanuel Riviere (@emriver)" |
| 19 |
- "Evgeny Yurchenko (@BGmot)" |
| 20 |
requirements: |
| 21 |
- "python >= 3.9" |
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Name of Zabbix service |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
algorithm: |
| 29 |
description: |
| 30 |
- Status calculation rule. Only applicable if child services exists. |
| 31 |
- " - C(status_to_ok), set status to OK with" |
| 32 |
- " - C(most_crit_if_all_children), most critical if all children have problems" |
| 33 |
- " - C(most_crit_of_child_serv), most critical of child services with" |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
choices: ["status_to_ok", "most_crit_if_all_children", "most_crit_of_child_serv"] |
| 37 |
default: status_to_ok |
| 38 |
state: |
| 39 |
description: |
| 40 |
- "State: present - create/update service; absent - delete service." |
| 41 |
required: false |
| 42 |
choices: [present, absent] |
| 43 |
default: "present" |
| 44 |
type: str |
| 45 |
sortorder: |
| 46 |
description: |
| 47 |
- Position of the service used for sorting. |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
weight: |
| 51 |
description: |
| 52 |
- Service weight. |
| 53 |
required: false |
| 54 |
default: "0" |
| 55 |
type: str |
| 56 |
description: |
| 57 |
description: |
| 58 |
- Description of the service. |
| 59 |
required: false |
| 60 |
type: str |
| 61 |
tags: |
| 62 |
description: |
| 63 |
- Service tags to be created for the service. |
| 64 |
required: false |
| 65 |
type: list |
| 66 |
elements: dict |
| 67 |
suboptions: |
| 68 |
tag: |
| 69 |
description: |
| 70 |
- Service tag name. |
| 71 |
required: true |
| 72 |
type: str |
| 73 |
value: |
| 74 |
description: |
| 75 |
- Service tag value. |
| 76 |
required: false |
| 77 |
type: str |
| 78 |
problem_tags: |
| 79 |
description: |
| 80 |
- Problem tags to be created for the service. |
| 81 |
required: false |
| 82 |
type: list |
| 83 |
elements: dict |
| 84 |
suboptions: |
| 85 |
tag: |
| 86 |
description: |
| 87 |
- Problem tag name. |
| 88 |
required: true |
| 89 |
type: str |
| 90 |
operator: |
| 91 |
description: |
| 92 |
- Mapping condition operator. |
| 93 |
- C(equals) |
| 94 |
- C(like) |
| 95 |
choices: ["equals", "like"] |
| 96 |
required: false |
| 97 |
default: "equals" |
| 98 |
type: str |
| 99 |
value: |
| 100 |
description: |
| 101 |
- Problem tag value. |
| 102 |
required: false |
| 103 |
default: "" |
| 104 |
type: str |
| 105 |
parents: |
| 106 |
description: |
| 107 |
- Parent services to be linked to the service. |
| 108 |
required: false |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
children: |
| 112 |
description: |
| 113 |
- Child services to be linked to the service. |
| 114 |
required: false |
| 115 |
type: list |
| 116 |
elements: str |
| 117 |
propagation_rule: |
| 118 |
description: |
| 119 |
- Status propagation value. Must be set together with propagation_rule. |
| 120 |
- C(as_is) propagate service status as is - without any changes |
| 121 |
- C(increase) increase the propagated status by a given propagation_value (by 1 to 5 severities) |
| 122 |
- C(decrease) decrease the propagated status by a given propagation_value (by 1 to 5 severities) |
| 123 |
- C(ignore) ignore this service - the status is not propagated to the parent service at all |
| 124 |
- C(fixed) set fixed service status using a given propagation_value |
| 125 |
- Required with C(propagation_value) |
| 126 |
required: false |
| 127 |
type: str |
| 128 |
default: as_is |
| 129 |
propagation_value: |
| 130 |
description: |
| 131 |
- Status propagation value. Must be set together with propagation_rule. |
| 132 |
- "Possible values when I(propagation_rule=as_is or ignore):" |
| 133 |
- " - C(not_classified)" |
| 134 |
- "Possible values when I(propagation_rule=increase or decrease):" |
| 135 |
- " - C(information)" |
| 136 |
- " - C(warning)" |
| 137 |
- " - C(average)" |
| 138 |
- " - C(high)" |
| 139 |
- " - C(disaster)" |
| 140 |
- "Possible values when I(propagation_rule=fixed):" |
| 141 |
- " - C(ok)" |
| 142 |
- " - C(not_classified)" |
| 143 |
- " - C(information)" |
| 144 |
- " - C(warning)" |
| 145 |
- " - C(average)" |
| 146 |
- " - C(high)" |
| 147 |
- " - C(disaster)" |
| 148 |
- Required with C(propagation_rule) |
| 149 |
required: false |
| 150 |
type: str |
| 151 |
default: not_classified |
| 152 |
status_rules: |
| 153 |
description: |
| 154 |
- Status rules for the service. |
| 155 |
required: false |
| 156 |
type: list |
| 157 |
elements: dict |
| 158 |
suboptions: |
| 159 |
type: |
| 160 |
description: |
| 161 |
- Condition for setting (New status) status. |
| 162 |
- C(at_least_n_child_services_have_status_or_above) if at least (N) child services have (Status) status or above |
| 163 |
- C(at_least_npct_child_services_have_status_or_above) if at least (N%) of child services have (Status) status or above |
| 164 |
- C(less_than_n_child_services_have_status_or_below) if less than (N) child services have (Status) status or below |
| 165 |
- C(less_than_npct_child_services_have_status_or_below) if less than (N%) of child services have (Status) status or below |
| 166 |
- C(weight_child_services_with_status_or_above_at_least_w) if weight of child services with (Status) status or above is at least (W) |
| 167 |
- C(weight_child_services_with_status_or_above_at_least_npct) if weight of child services with (Status) status or above is at least (N%) |
| 168 |
- C(weight_child_services_with_status_or_below_less_w) if weight of child services with (Status) status or below is less than (W) |
| 169 |
- C(weight_child_services_with_status_or_below_less_npct) if weight of child services with (Status) status or below is less than (N%) |
| 170 |
required: true |
| 171 |
type: str |
| 172 |
limit_value: |
| 173 |
description: |
| 174 |
- "Limit value: N, N% or W" |
| 175 |
- "Possible values: 1-100000 for N and W, 1-100 for N%" |
| 176 |
required: true |
| 177 |
type: int |
| 178 |
limit_status: |
| 179 |
description: |
| 180 |
- Limit status. |
| 181 |
- C(ok) OK |
| 182 |
- C(not_classified) Not classified |
| 183 |
- C(information) Information |
| 184 |
- C(warning) Warning |
| 185 |
- C(average) Average |
| 186 |
- C(high) High |
| 187 |
- C(disaster) Disaster |
| 188 |
required: true |
| 189 |
type: str |
| 190 |
new_status: |
| 191 |
description: |
| 192 |
- New status value. |
| 193 |
- C(not_classified) Not classified |
| 194 |
- C(information) Information |
| 195 |
- C(warning) Warning |
| 196 |
- C(average) Average |
| 197 |
- C(high) High |
| 198 |
- C(disaster) Disaster |
| 199 |
required: true |
| 200 |
type: str |
| 201 |
|
| 202 |
extends_documentation_fragment: |
| 203 |
- community.zabbix.zabbix |
| 204 |
|
| 205 |
""" |
| 206 |
|
| 207 |
EXAMPLES = """ |
| 208 |
--- |
| 209 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 210 |
- name: Set credentials to access Zabbix Server API |
| 211 |
ansible.builtin.set_fact: |
| 212 |
ansible_user: Admin |
| 213 |
ansible_httpapi_pass: zabbix |
| 214 |
|
| 215 |
# If you want to use API token to be authenticated by Zabbix Server |
| 216 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 217 |
- name: Set API token |
| 218 |
ansible.builtin.set_fact: |
| 219 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 220 |
|
| 221 |
# Creates a new Zabbix service |
| 222 |
- name: Create Zabbix service monitoring Apache2 in DCs in Toronto area |
| 223 |
# set task level variables |
| 224 |
vars: |
| 225 |
ansible_network_os: community.zabbix.zabbix |
| 226 |
ansible_connection: httpapi |
| 227 |
ansible_httpapi_port: 443 |
| 228 |
ansible_httpapi_use_ssl: true |
| 229 |
ansible_httpapi_validate_certs: false |
| 230 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 231 |
ansible_host: zabbix-example-fqdn.org |
| 232 |
community.zabbix.zabbix_service: |
| 233 |
name: "apache2 service Toronto" |
| 234 |
description: Apache2 services in Toronto area |
| 235 |
sortorder: 0 |
| 236 |
propagation_rule: increase |
| 237 |
propagation_value: warning |
| 238 |
weight: 1 |
| 239 |
state: present |
| 240 |
tags: |
| 241 |
- tag: zabbix_service |
| 242 |
value: apache2 |
| 243 |
- tag: area |
| 244 |
value: Toronto |
| 245 |
problem_tags: |
| 246 |
- tag: service_name |
| 247 |
value: httpd |
| 248 |
- tag: area |
| 249 |
operator: like |
| 250 |
value: toronto |
| 251 |
status_rules: |
| 252 |
- type: at_least_n_child_services_have_status_or_above |
| 253 |
limit_value: 4242 |
| 254 |
limit_status: ok |
| 255 |
new_status: average |
| 256 |
|
| 257 |
- name: Create Zabbix service monitoring all Apache2 services |
| 258 |
# set task level variables as we change ansible_connection plugin here |
| 259 |
vars: |
| 260 |
ansible_network_os: community.zabbix.zabbix |
| 261 |
ansible_connection: httpapi |
| 262 |
ansible_httpapi_port: 443 |
| 263 |
ansible_httpapi_use_ssl: true |
| 264 |
ansible_httpapi_validate_certs: false |
| 265 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 266 |
ansible_host: zabbix-example-fqdn.org |
| 267 |
community.zabbix.zabbix_service: |
| 268 |
name: apache2 service |
| 269 |
description: Apache2 services |
| 270 |
tags: |
| 271 |
- tag: zabbix_service |
| 272 |
value: apache2 |
| 273 |
- tag: area |
| 274 |
value: global |
| 275 |
children: |
| 276 |
- "apache2 service Toronto" |
| 277 |
""" |
| 278 |
|
| 279 |
RETURN = """ |
| 280 |
--- |
| 281 |
""" |
| 282 |
|
| 283 |
|
| 284 |
from ansible.module_utils.basic import AnsibleModule |
| 285 |
|
| 286 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 287 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 288 |
|
| 289 |
|
| 290 |
class Service(ZabbixBase): |
| 291 |
def get_service(self, service_name): |
| 292 |
services = self._zapi.service.get({"selectParents": ["serviceid"], "selectChildren": ["serviceid"], "selectTags": "extend", |
| 293 |
"selectProblemTags": "extend", "selectStatusRules": "extend", "sortfield": "serviceid", |
| 294 |
"sortorder": "ASC", "filter": {"name": service_name}}) |
| 295 |
return services |
| 296 |
|
| 297 |
def delete_service(self, service_ids): |
| 298 |
if self._module.check_mode: |
| 299 |
self._module.exit_json(changed=True) |
| 300 |
self._zapi.service.delete([service_ids]) |
| 301 |
|
| 302 |
def dump_services(self, service_ids): |
| 303 |
services = self._zapi.service.get({"output": "extend", "filter": {"serviceid": service_ids}, "selectParents": "extend", |
| 304 |
"selectTags": "extend", "selectProblemTags": "extend", "selectChildren": "extend", |
| 305 |
"selectStatusRules": "extend"}) |
| 306 |
|
| 307 |
return services |
| 308 |
|
| 309 |
def construct_problem_tags(self, problem_tags): |
| 310 |
p_operators = {"equals": "0", "like": "2"} |
| 311 |
for problem_tag in problem_tags: |
| 312 |
if problem_tag["operator"] in list(p_operators.keys()): |
| 313 |
problem_tag["operator"] = p_operators[problem_tag["operator"]] |
| 314 |
else: |
| 315 |
problem_tag["operator"] = int(problem_tag["operator"]) |
| 316 |
if not problem_tag["tag"]: |
| 317 |
self._module.fail_json(msg='"tag" required in "problem_tags"') |
| 318 |
return problem_tags |
| 319 |
|
| 320 |
def construct_status_rules(self, status_rules): |
| 321 |
sr_type_map = {"at_least_n_child_services_have_status_or_above": "0", |
| 322 |
"at_least_npct_child_services_have_status_or_above": "1", |
| 323 |
"less_than_n_child_services_have_status_or_below": "2", |
| 324 |
"less_than_npct_child_services_have_status_or_below": "3", |
| 325 |
"weight_child_services_with_status_or_above_at_least_w": "4", |
| 326 |
"weight_child_services_with_status_or_above_at_least_npct": "5", |
| 327 |
"weight_child_services_with_status_or_below_less_w": "6", |
| 328 |
"weight_child_services_with_status_or_below_less_npct": "7"} |
| 329 |
for s_rule in status_rules: |
| 330 |
if "type" in s_rule: |
| 331 |
if s_rule["type"] not in sr_type_map: |
| 332 |
self._module.fail_json(msg="Wrong value for 'type' parameter in status rule.") |
| 333 |
s_rule["type"] = sr_type_map[s_rule["type"]] |
| 334 |
else: |
| 335 |
self._module.fail_json(msg="'type' is mandatory paremeter for status rule.") |
| 336 |
|
| 337 |
if "limit_value" in s_rule: |
| 338 |
lv = s_rule["limit_value"] |
| 339 |
if s_rule["type"] in ["0", "2", "4", "6"]: |
| 340 |
if int(lv) < 1 or int(lv) > 100000: |
| 341 |
self._module.fail_json(msg="'limit_value' for N and W must be between 1 and 100000 but provided %s" % lv) |
| 342 |
else: |
| 343 |
if int(lv) < 1 or int(lv) > 100: |
| 344 |
self._module.fail_json(msg="'limit_value' for N%% must be between 1 and 100 but provided %s" % lv) |
| 345 |
s_rule["limit_value"] = str(lv) |
| 346 |
else: |
| 347 |
self._module.fail_json(msg="'limit_value' is mandatory paremeter for status rule.") |
| 348 |
|
| 349 |
if "limit_status" in s_rule: |
| 350 |
sr_ls_map = {"ok": "-1", "not_classified": "0", "information": "1", "warning": "2", |
| 351 |
"average": "3", "high": "4", "disaster": 5} |
| 352 |
if s_rule["limit_status"] not in sr_ls_map: |
| 353 |
self._module.fail_json(msg="Wrong value for 'limit_status' parameter in status rule.") |
| 354 |
s_rule["limit_status"] = sr_ls_map[s_rule["limit_status"]] |
| 355 |
else: |
| 356 |
self._module.fail_json(msg="'limit_status' is mandatory paremeter for status rule.") |
| 357 |
|
| 358 |
if "new_status" in s_rule: |
| 359 |
sr_ns_map = {"not_classified": "0", "information": "1", "warning": "2", |
| 360 |
"average": "3", "high": "4", "disaster": "5"} |
| 361 |
if s_rule["new_status"] not in sr_ns_map: |
| 362 |
self._module.fail_json(msg="Wrong value for 'new_status' parameter in status rule.") |
| 363 |
s_rule["new_status"] = sr_ns_map[s_rule["new_status"]] |
| 364 |
else: |
| 365 |
self._module.fail_json(msg="'new_status' is mandatory paremeter for status rule.") |
| 366 |
|
| 367 |
return status_rules |
| 368 |
|
| 369 |
def create_service(self, service_name, sortorder, weight, algorithm, |
| 370 |
description, tags, problem_tags, parents, children, propagation_rule, propagation_value, status_rules): |
| 371 |
if self._module.check_mode: |
| 372 |
self._module.exit_json(changed=True) |
| 373 |
else: |
| 374 |
try: |
| 375 |
parameters = {"name": service_name} |
| 376 |
if sortorder is not None: |
| 377 |
parameters["sortorder"] = sortorder |
| 378 |
if weight is not None: |
| 379 |
parameters["weight"] = weight |
| 380 |
if algorithm is not None: |
| 381 |
parameters["algorithm"] = algorithm |
| 382 |
if description is not None: |
| 383 |
parameters["description"] = description |
| 384 |
if tags is not None: |
| 385 |
parameters["tags"] = tags |
| 386 |
if problem_tags is not None: |
| 387 |
parameters["problem_tags"] = problem_tags |
| 388 |
if parents is not None: |
| 389 |
parameters["parents"] = parents |
| 390 |
if children is not None: |
| 391 |
parameters["children"] = children |
| 392 |
if propagation_rule is not None: |
| 393 |
parameters["propagation_rule"] = propagation_rule |
| 394 |
if propagation_value is not None: |
| 395 |
parameters["propagation_value"] = propagation_value |
| 396 |
if status_rules is not None: |
| 397 |
parameters["status_rules"] = status_rules |
| 398 |
|
| 399 |
self._zapi.service.create(parameters) |
| 400 |
self._module.exit_json(changed=True, msg="Service %s created" % service_name) |
| 401 |
except Exception as e: |
| 402 |
self._module.fail_json(msg="Failed creating service %s: %s" % (service_name, e)) |
| 403 |
|
| 404 |
def check_all_properties(self, name, sortorder, weight, algorithm, description, tags, problem_tags, parents, |
| 405 |
children, propagation_rule, propagation_value, status_rules, service_exist): |
| 406 |
# raise Exception("%s ------ %s" % (parents, service_exist)) |
| 407 |
if sortorder and sortorder != service_exist['sortorder']: |
| 408 |
return True |
| 409 |
if weight and weight != service_exist['weight']: |
| 410 |
return True |
| 411 |
if algorithm and algorithm != service_exist['algorithm']: |
| 412 |
return True |
| 413 |
if description and description != service_exist['description']: |
| 414 |
return True |
| 415 |
if tags and tags != service_exist['tags']: |
| 416 |
return True |
| 417 |
if problem_tags != service_exist['problem_tags']: |
| 418 |
return True |
| 419 |
if parents and parents != service_exist['parents']: |
| 420 |
return True |
| 421 |
if children and children != service_exist['children']: |
| 422 |
return True |
| 423 |
if propagation_rule and propagation_rule != service_exist['propagation_rule']: |
| 424 |
return True |
| 425 |
if propagation_value and propagation_value != service_exist['propagation_value']: |
| 426 |
return True |
| 427 |
if status_rules and status_rules != service_exist['status_rules']: |
| 428 |
return True |
| 429 |
return False |
| 430 |
|
| 431 |
def update_service(self, service_id, name, sortorder, weight, algorithm, description, tags, problem_tags, |
| 432 |
parents, children, propagation_rule, propagation_value, status_rules, service_name): |
| 433 |
try: |
| 434 |
parameters = {"serviceid": service_id} |
| 435 |
if sortorder is not None: |
| 436 |
parameters["sortorder"] = sortorder |
| 437 |
if weight is not None: |
| 438 |
parameters["weight"] = weight |
| 439 |
if algorithm is not None: |
| 440 |
parameters["algorithm"] = algorithm |
| 441 |
if description is not None: |
| 442 |
parameters["description"] = description |
| 443 |
if tags is not None: |
| 444 |
parameters["tags"] = tags |
| 445 |
if problem_tags is not None: |
| 446 |
parameters["problem_tags"] = problem_tags |
| 447 |
if parents is not None: |
| 448 |
parameters["parents"] = parents |
| 449 |
if children is not None: |
| 450 |
parameters["children"] = children |
| 451 |
if propagation_rule is not None: |
| 452 |
parameters["propagation_rule"] = propagation_rule |
| 453 |
if propagation_value is not None: |
| 454 |
parameters["propagation_value"] = propagation_value |
| 455 |
if status_rules is not None: |
| 456 |
parameters["status_rules"] = status_rules |
| 457 |
|
| 458 |
self._zapi.service.update(parameters) |
| 459 |
self._module.exit_json(changed=True, msg="Service %s updated" % name) |
| 460 |
except Exception as e: |
| 461 |
self._module.fail_json(msg="Failed updating service %s: %s" % (service_name, e)) |
| 462 |
|
| 463 |
|
| 464 |
def main(): |
| 465 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 466 |
argument_spec.update(dict( |
| 467 |
name=dict(type="str", required=True), |
| 468 |
algorithm=dict(default="status_to_ok", required=False, choices=["status_to_ok", "most_crit_if_all_children", "most_crit_of_child_serv"]), |
| 469 |
sortorder=dict(type="str", required=True), |
| 470 |
weight=dict(default="0", type="str", required=False), |
| 471 |
state=dict(default="present", choices=["present", "absent"]), |
| 472 |
description=dict(type="str", required=False), |
| 473 |
tags=dict( |
| 474 |
type="list", |
| 475 |
required=False, |
| 476 |
elements="dict", |
| 477 |
options=dict( |
| 478 |
tag=dict( |
| 479 |
type="str", |
| 480 |
required=True |
| 481 |
), |
| 482 |
value=dict( |
| 483 |
type="str", |
| 484 |
required=False |
| 485 |
) |
| 486 |
) |
| 487 |
), |
| 488 |
problem_tags=dict( |
| 489 |
type="list", |
| 490 |
required=False, |
| 491 |
elements="dict", |
| 492 |
options=dict( |
| 493 |
tag=dict( |
| 494 |
type="str", |
| 495 |
required=True |
| 496 |
), |
| 497 |
operator=dict( |
| 498 |
type="str", |
| 499 |
required=False, |
| 500 |
choices=[ |
| 501 |
"equals", |
| 502 |
"like" |
| 503 |
], |
| 504 |
default="equals" |
| 505 |
), |
| 506 |
value=dict( |
| 507 |
type="str", |
| 508 |
required=False, |
| 509 |
default="" |
| 510 |
) |
| 511 |
) |
| 512 |
), |
| 513 |
parents=dict(type="list", required=False, elements="str"), |
| 514 |
children=dict(type="list", required=False, elements="str"), |
| 515 |
propagation_rule=dict(type="str", required=False, default="as_is"), |
| 516 |
propagation_value=dict(type="str", required=False, default="not_classified"), |
| 517 |
status_rules=dict( |
| 518 |
type="list", |
| 519 |
required=False, |
| 520 |
elements="dict", |
| 521 |
options=dict( |
| 522 |
type=dict( |
| 523 |
type="str", |
| 524 |
required=True |
| 525 |
), |
| 526 |
limit_value=dict( |
| 527 |
type="int", |
| 528 |
required=True |
| 529 |
), |
| 530 |
limit_status=dict( |
| 531 |
type="str", |
| 532 |
required=True |
| 533 |
), |
| 534 |
new_status=dict( |
| 535 |
type="str", |
| 536 |
required=True |
| 537 |
) |
| 538 |
) |
| 539 |
) |
| 540 |
)) |
| 541 |
module = AnsibleModule( |
| 542 |
argument_spec=argument_spec, |
| 543 |
supports_check_mode=True, |
| 544 |
required_together=[ |
| 545 |
('propagation_rule', 'propagation_value') |
| 546 |
] |
| 547 |
) |
| 548 |
|
| 549 |
name = module.params["name"] |
| 550 |
algorithm = module.params["algorithm"] |
| 551 |
sortorder = module.params["sortorder"] |
| 552 |
weight = module.params["weight"] |
| 553 |
state = module.params["state"] |
| 554 |
description = module.params["description"] |
| 555 |
tags = module.params["tags"] |
| 556 |
problem_tags = module.params["problem_tags"] |
| 557 |
parents = module.params["parents"] |
| 558 |
children = module.params["children"] |
| 559 |
propagation_rule = module.params["propagation_rule"] |
| 560 |
propagation_value = module.params["propagation_value"] |
| 561 |
status_rules = module.params["status_rules"] |
| 562 |
|
| 563 |
# Load service module |
| 564 |
service = Service(module) |
| 565 |
service_exist = service.get_service(name) |
| 566 |
service_id = "" |
| 567 |
if service_exist: |
| 568 |
service_id = service_exist[0]["serviceid"] |
| 569 |
|
| 570 |
algorithms = {"status_to_ok": "0", "most_crit_if_all_children": "1", "most_crit_of_child_serv": "2"} |
| 571 |
algorithm = algorithms[algorithm] |
| 572 |
|
| 573 |
if problem_tags: |
| 574 |
problem_tags = service.construct_problem_tags(problem_tags) |
| 575 |
else: |
| 576 |
problem_tags = [] |
| 577 |
|
| 578 |
if parents: |
| 579 |
p_service_ids = [] |
| 580 |
p_services = service._zapi.service.get({"output": ["serviceid"], "filter": {"name": parents}}) |
| 581 |
for p_service in p_services: |
| 582 |
p_service_ids.append({"serviceid": p_service["serviceid"]}) |
| 583 |
parents = p_service_ids |
| 584 |
|
| 585 |
if children: |
| 586 |
c_service_ids = [] |
| 587 |
c_services = service._zapi.service.get({"output": ["serviceid"], "filter": {"name": children}}) |
| 588 |
for c_service in c_services: |
| 589 |
c_service_ids.append({"serviceid": c_service["serviceid"]}) |
| 590 |
children = c_service_ids |
| 591 |
|
| 592 |
if status_rules: |
| 593 |
status_rules = service.construct_status_rules(status_rules) |
| 594 |
|
| 595 |
if propagation_rule: |
| 596 |
if propagation_value is None: |
| 597 |
module.fail_json(msg="If 'propagation_rule' is provided then 'propagation_value' must be provided too.") |
| 598 |
pr_map = {"as_is": "0", "increase": "1", "decrease": "2", "ignore": "3", "fixed": "4"} |
| 599 |
if propagation_rule not in pr_map: |
| 600 |
module.fail_json(msg="Wrong value for 'propagation_rule' parameter.") |
| 601 |
else: |
| 602 |
propagation_rule = pr_map[propagation_rule] |
| 603 |
|
| 604 |
if propagation_value: |
| 605 |
if propagation_rule is None: |
| 606 |
module.fail_json(msg="If 'propagation_value' is provided then 'propagation_rule' must be provided too.") |
| 607 |
pv_map = {"ok": "-1", "not_classified": "0", "information": "1", "warning": "2", |
| 608 |
"average": "3", "high": "4", "disaster": "5"} |
| 609 |
if propagation_value not in pv_map: |
| 610 |
module.fail_json(msg="Wrong value for 'propagation_value' parameter.") |
| 611 |
else: |
| 612 |
propagation_value = pv_map[propagation_value] |
| 613 |
|
| 614 |
# Delete service |
| 615 |
if state == "absent": |
| 616 |
if not service_id: |
| 617 |
module.exit_json(changed=False, msg="Service not found, no change: %s" % name) |
| 618 |
service.delete_service(service_id) |
| 619 |
module.exit_json(changed=True, result="Successfully deleted service(s) %s" % name) |
| 620 |
|
| 621 |
elif state == "present": |
| 622 |
# Does not exists going to create it |
| 623 |
if not service_id: |
| 624 |
service.create_service(name, sortorder, weight, algorithm, description, |
| 625 |
tags, problem_tags, parents, children, propagation_rule, propagation_value, status_rules) |
| 626 |
module.exit_json(changed=True, msg="Service %s created" % name) |
| 627 |
# Else we update it if exists |
| 628 |
else: |
| 629 |
# Check if parameters have changed |
| 630 |
if service.check_all_properties(name, sortorder, weight, algorithm, description, tags, problem_tags, parents, |
| 631 |
children, propagation_rule, propagation_value, status_rules, service_exist[0]): |
| 632 |
# Update service if a parameter is different |
| 633 |
service.update_service(service_id, name, sortorder, weight, algorithm, description, tags, problem_tags, |
| 634 |
parents, children, propagation_rule, propagation_value, status_rules, name) |
| 635 |
else: |
| 636 |
# No parameters changed, no update required. |
| 637 |
module.exit_json(changed=False) |
| 638 |
|
| 639 |
|
| 640 |
if __name__ == "__main__": |
| 641 |
main() |