| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright: (c) 2022, ONODERA Masaru <[email protected]> |
| 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_housekeeping |
| 14 |
|
| 15 |
short_description: Update Zabbix housekeeping |
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows you to modify Zabbix housekeeping setting. |
| 19 |
|
| 20 |
author: |
| 21 |
- ONODERA Masaru(@masa-orca) |
| 22 |
|
| 23 |
requirements: |
| 24 |
- "python >= 3.9" |
| 25 |
|
| 26 |
version_added: 1.6.0 |
| 27 |
|
| 28 |
options: |
| 29 |
hk_events_mode: |
| 30 |
description: |
| 31 |
- Internal housekeeping for events and alerts will be enabled if C(true). |
| 32 |
required: false |
| 33 |
type: bool |
| 34 |
hk_events_trigger: |
| 35 |
description: |
| 36 |
- Storage period of trigger data (e.g. 365d). |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
hk_events_service: |
| 40 |
description: |
| 41 |
- Storage period of service data (e.g. 365d). |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
hk_events_internal: |
| 45 |
description: |
| 46 |
- Storage period of internal data (e.g. 365d). |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
hk_events_discovery: |
| 50 |
description: |
| 51 |
- Storage period of network discovery (e.g. 365d). |
| 52 |
required: false |
| 53 |
type: str |
| 54 |
hk_events_autoreg: |
| 55 |
description: |
| 56 |
- Storage period of autoregistration data (e.g. 365d). |
| 57 |
required: false |
| 58 |
type: str |
| 59 |
hk_services_mode: |
| 60 |
description: |
| 61 |
- Internal housekeeping for services will be enabled if C(true). |
| 62 |
required: false |
| 63 |
type: bool |
| 64 |
hk_services: |
| 65 |
description: |
| 66 |
- Storage period of services data (e.g. 365d). |
| 67 |
required: false |
| 68 |
type: str |
| 69 |
hk_audit_mode: |
| 70 |
description: |
| 71 |
- Internal housekeeping for audit will be enabled if C(true). |
| 72 |
required: false |
| 73 |
type: bool |
| 74 |
hk_audit: |
| 75 |
description: |
| 76 |
- Storage period of audit data (e.g. 365d). |
| 77 |
required: false |
| 78 |
type: str |
| 79 |
hk_sessions_mode: |
| 80 |
description: |
| 81 |
- Internal housekeeping for sessions will be enabled if C(true). |
| 82 |
required: false |
| 83 |
type: bool |
| 84 |
hk_sessions: |
| 85 |
description: |
| 86 |
- Storage period of sessions data (e.g. 365d). |
| 87 |
required: false |
| 88 |
type: str |
| 89 |
hk_history_mode: |
| 90 |
description: |
| 91 |
- Internal housekeeping for history will be enabled if C(true). |
| 92 |
required: false |
| 93 |
type: bool |
| 94 |
hk_history_global: |
| 95 |
description: |
| 96 |
- Overriding history period of each items will be enabled if C(true). |
| 97 |
required: false |
| 98 |
type: bool |
| 99 |
hk_history: |
| 100 |
description: |
| 101 |
- Storage priod of history data (e.g. 365d). |
| 102 |
required: false |
| 103 |
type: str |
| 104 |
hk_trends_mode: |
| 105 |
description: |
| 106 |
- Internal housekeeping for trends will be enabled if C(true). |
| 107 |
required: false |
| 108 |
type: bool |
| 109 |
hk_trends_global: |
| 110 |
description: |
| 111 |
- Overriding trend period of each items will be enabled if C(true). |
| 112 |
required: false |
| 113 |
type: bool |
| 114 |
hk_trends: |
| 115 |
description: |
| 116 |
- Storage priod of trends data (e.g. 365d). |
| 117 |
required: false |
| 118 |
type: str |
| 119 |
compression_status: |
| 120 |
description: |
| 121 |
- TimescaleDB compression for history and trends will be enabled if C(true). |
| 122 |
required: false |
| 123 |
type: bool |
| 124 |
compress_older: |
| 125 |
description: |
| 126 |
- Compress history and trends records older than this period if I(compression_status=true). |
| 127 |
required: false |
| 128 |
type: str |
| 129 |
|
| 130 |
extends_documentation_fragment: |
| 131 |
- community.zabbix.zabbix |
| 132 |
""" |
| 133 |
|
| 134 |
EXAMPLES = """ |
| 135 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 136 |
- name: Set credentials to access Zabbix Server API |
| 137 |
ansible.builtin.set_fact: |
| 138 |
ansible_user: Admin |
| 139 |
ansible_httpapi_pass: zabbix |
| 140 |
|
| 141 |
# If you want to use API token to be authenticated by Zabbix Server |
| 142 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 143 |
- name: Set API token |
| 144 |
ansible.builtin.set_fact: |
| 145 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 146 |
|
| 147 |
- name: Update housekeeping all parameter |
| 148 |
# set task level variables as we change ansible_connection plugin here |
| 149 |
vars: |
| 150 |
ansible_network_os: community.zabbix.zabbix |
| 151 |
ansible_connection: httpapi |
| 152 |
ansible_httpapi_port: 443 |
| 153 |
ansible_httpapi_use_ssl: true |
| 154 |
ansible_httpapi_validate_certs: false |
| 155 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 156 |
ansible_host: zabbix-example-fqdn.org |
| 157 |
community.zabbix.zabbix_housekeeping: |
| 158 |
login_user: Admin |
| 159 |
login_password: secret |
| 160 |
hk_events_mode: yes |
| 161 |
hk_events_trigger: 365d |
| 162 |
hk_events_service: 365d |
| 163 |
hk_events_internal: 365d |
| 164 |
hk_events_discovery: 365d |
| 165 |
hk_events_autoreg: 365d |
| 166 |
hk_services_mode: yes |
| 167 |
hk_services: 365d |
| 168 |
hk_audit_mode: yes |
| 169 |
hk_audit: 365d |
| 170 |
hk_sessions_mode: yes |
| 171 |
hk_sessions: 365d |
| 172 |
hk_history_mode: yes |
| 173 |
hk_history_global: yes |
| 174 |
hk_history: 365d |
| 175 |
hk_trends_mode: yes |
| 176 |
hk_trends_global: yes |
| 177 |
hk_trends: 365d |
| 178 |
compression_status: off |
| 179 |
compress_older: 7d |
| 180 |
""" |
| 181 |
|
| 182 |
RETURN = """ |
| 183 |
msg: |
| 184 |
description: The result of the operation |
| 185 |
returned: success |
| 186 |
type: str |
| 187 |
sample: "Successfully update housekeeping setting" |
| 188 |
""" |
| 189 |
|
| 190 |
import re |
| 191 |
|
| 192 |
from ansible.module_utils.basic import AnsibleModule |
| 193 |
|
| 194 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 195 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 196 |
|
| 197 |
|
| 198 |
class Housekeeping(ZabbixBase): |
| 199 |
# get housekeeping setting |
| 200 |
def get_housekeeping(self): |
| 201 |
try: |
| 202 |
return self._zapi.housekeeping.get({"output": "extend"}) |
| 203 |
except Exception as e: |
| 204 |
self._module.fail_json(msg="Failed to get housekeeping setting: %s" % e) |
| 205 |
|
| 206 |
# Check parameter about time is valid. |
| 207 |
def check_time_parameter(self, key_name, value): |
| 208 |
match_result = re.match("^[0-9]+[smhdw]$", value) |
| 209 |
if not match_result: |
| 210 |
self._module.fail_json(msg="Invalid value for %s! Please set value like 365d." % key_name) |
| 211 |
|
| 212 |
# update housekeeping setting |
| 213 |
def update_housekeeping( |
| 214 |
self, |
| 215 |
current_housekeeping, |
| 216 |
hk_events_mode, |
| 217 |
hk_events_trigger, |
| 218 |
hk_events_service, |
| 219 |
hk_events_internal, |
| 220 |
hk_events_discovery, |
| 221 |
hk_events_autoreg, |
| 222 |
hk_services_mode, |
| 223 |
hk_services, |
| 224 |
hk_audit_mode, |
| 225 |
hk_audit, |
| 226 |
hk_sessions_mode, |
| 227 |
hk_sessions, |
| 228 |
hk_history_mode, |
| 229 |
hk_history_global, |
| 230 |
hk_history, |
| 231 |
hk_trends_mode, |
| 232 |
hk_trends_global, |
| 233 |
hk_trends, |
| 234 |
compression_status, |
| 235 |
compress_older): |
| 236 |
try: |
| 237 |
params = {} |
| 238 |
|
| 239 |
if isinstance(hk_events_mode, bool): |
| 240 |
params["hk_events_mode"] = str(int(hk_events_mode)) |
| 241 |
|
| 242 |
if hk_events_trigger: |
| 243 |
self.check_time_parameter("hk_events_trigger", hk_events_trigger) |
| 244 |
params["hk_events_trigger"] = hk_events_trigger |
| 245 |
|
| 246 |
if hk_events_service: |
| 247 |
self.check_time_parameter("hk_events_service", hk_events_service) |
| 248 |
params["hk_events_service"] = hk_events_service |
| 249 |
|
| 250 |
if hk_events_internal: |
| 251 |
self.check_time_parameter("hk_events_internal", hk_events_internal) |
| 252 |
params["hk_events_internal"] = hk_events_internal |
| 253 |
|
| 254 |
if hk_events_discovery: |
| 255 |
self.check_time_parameter("hk_events_discovery", hk_events_discovery) |
| 256 |
params["hk_events_discovery"] = hk_events_discovery |
| 257 |
|
| 258 |
if hk_events_autoreg: |
| 259 |
self.check_time_parameter("hk_events_autoreg", hk_events_autoreg) |
| 260 |
params["hk_events_autoreg"] = hk_events_autoreg |
| 261 |
|
| 262 |
if isinstance(hk_services_mode, bool): |
| 263 |
params["hk_services_mode"] = str(int(hk_services_mode)) |
| 264 |
|
| 265 |
if hk_services: |
| 266 |
self.check_time_parameter("hk_services", hk_services) |
| 267 |
params["hk_services"] = hk_services |
| 268 |
|
| 269 |
if isinstance(hk_audit_mode, bool): |
| 270 |
params["hk_audit_mode"] = str(int(hk_audit_mode)) |
| 271 |
|
| 272 |
if hk_audit: |
| 273 |
self.check_time_parameter("hk_audit", hk_audit) |
| 274 |
params["hk_audit"] = hk_audit |
| 275 |
|
| 276 |
if isinstance(hk_sessions_mode, bool): |
| 277 |
params["hk_sessions_mode"] = str(int(hk_sessions_mode)) |
| 278 |
|
| 279 |
if hk_sessions: |
| 280 |
self.check_time_parameter("hk_sessions", hk_sessions) |
| 281 |
params["hk_sessions"] = hk_sessions |
| 282 |
|
| 283 |
if isinstance(hk_history_mode, bool): |
| 284 |
params["hk_history_mode"] = str(int(hk_history_mode)) |
| 285 |
|
| 286 |
if isinstance(hk_history_global, bool): |
| 287 |
params["hk_history_global"] = str(int(hk_history_global)) |
| 288 |
|
| 289 |
if hk_history: |
| 290 |
self.check_time_parameter("hk_history", hk_history) |
| 291 |
params["hk_history"] = hk_history |
| 292 |
|
| 293 |
if isinstance(hk_trends_mode, bool): |
| 294 |
params["hk_trends_mode"] = str(int(hk_trends_mode)) |
| 295 |
|
| 296 |
if isinstance(hk_trends_global, bool): |
| 297 |
params["hk_trends_global"] = str(int(hk_trends_global)) |
| 298 |
|
| 299 |
if hk_trends: |
| 300 |
self.check_time_parameter("hk_trends", hk_trends) |
| 301 |
params["hk_trends"] = hk_trends |
| 302 |
|
| 303 |
if isinstance(compression_status, bool): |
| 304 |
params["compression_status"] = str(int(compression_status)) |
| 305 |
|
| 306 |
if compress_older: |
| 307 |
self.check_time_parameter("compress_older", compress_older) |
| 308 |
params["compress_older"] = compress_older |
| 309 |
|
| 310 |
future_housekeeping = current_housekeeping.copy() |
| 311 |
future_housekeeping.update(params) |
| 312 |
|
| 313 |
if future_housekeeping != current_housekeeping: |
| 314 |
if self._module.check_mode: |
| 315 |
self._module.exit_json(changed=True) |
| 316 |
|
| 317 |
self._zapi.housekeeping.update(params) |
| 318 |
self._module.exit_json(changed=True, result="Successfully update housekeeping setting") |
| 319 |
else: |
| 320 |
self._module.exit_json(changed=False, result="Housekeeping setting is already up to date") |
| 321 |
except Exception as e: |
| 322 |
self._module.fail_json(msg="Failed to update housekeeping setting, Exception: %s" % e) |
| 323 |
|
| 324 |
|
| 325 |
def main(): |
| 326 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 327 |
argument_spec.update(dict( |
| 328 |
hk_events_mode=dict(type="bool"), |
| 329 |
hk_events_trigger=dict(type="str"), |
| 330 |
hk_events_service=dict(type="str"), |
| 331 |
hk_events_internal=dict(type="str"), |
| 332 |
hk_events_discovery=dict(type="str"), |
| 333 |
hk_events_autoreg=dict(type="str"), |
| 334 |
hk_services_mode=dict(type="bool"), |
| 335 |
hk_services=dict(type="str"), |
| 336 |
hk_audit_mode=dict(type="bool"), |
| 337 |
hk_audit=dict(type="str"), |
| 338 |
hk_sessions_mode=dict(type="bool"), |
| 339 |
hk_sessions=dict(type="str"), |
| 340 |
hk_history_mode=dict(type="bool"), |
| 341 |
hk_history_global=dict(type="bool"), |
| 342 |
hk_history=dict(type="str"), |
| 343 |
hk_trends_mode=dict(type="bool"), |
| 344 |
hk_trends_global=dict(type="bool"), |
| 345 |
hk_trends=dict(type="str"), |
| 346 |
compression_status=dict(type="bool"), |
| 347 |
compress_older=dict(type="str") |
| 348 |
)) |
| 349 |
module = AnsibleModule( |
| 350 |
argument_spec=argument_spec, |
| 351 |
supports_check_mode=True |
| 352 |
) |
| 353 |
|
| 354 |
hk_events_mode = module.params["hk_events_mode"] |
| 355 |
hk_events_trigger = module.params["hk_events_trigger"] |
| 356 |
hk_events_service = module.params["hk_events_service"] |
| 357 |
hk_events_internal = module.params["hk_events_internal"] |
| 358 |
hk_events_discovery = module.params["hk_events_discovery"] |
| 359 |
hk_events_autoreg = module.params["hk_events_autoreg"] |
| 360 |
hk_services_mode = module.params["hk_services_mode"] |
| 361 |
hk_services = module.params["hk_services"] |
| 362 |
hk_audit_mode = module.params["hk_audit_mode"] |
| 363 |
hk_audit = module.params["hk_audit"] |
| 364 |
hk_sessions_mode = module.params["hk_sessions_mode"] |
| 365 |
hk_sessions = module.params["hk_sessions"] |
| 366 |
hk_history_mode = module.params["hk_history_mode"] |
| 367 |
hk_history_global = module.params["hk_history_global"] |
| 368 |
hk_history = module.params["hk_history"] |
| 369 |
hk_trends_mode = module.params["hk_trends_mode"] |
| 370 |
hk_trends_global = module.params["hk_trends_global"] |
| 371 |
hk_trends = module.params["hk_trends"] |
| 372 |
compression_status = module.params["compression_status"] |
| 373 |
compress_older = module.params["compress_older"] |
| 374 |
|
| 375 |
housekeeping = Housekeeping(module) |
| 376 |
|
| 377 |
current_housekeeping = housekeeping.get_housekeeping() |
| 378 |
housekeeping.update_housekeeping( |
| 379 |
current_housekeeping, |
| 380 |
hk_events_mode, |
| 381 |
hk_events_trigger, |
| 382 |
hk_events_service, |
| 383 |
hk_events_internal, |
| 384 |
hk_events_discovery, |
| 385 |
hk_events_autoreg, |
| 386 |
hk_services_mode, |
| 387 |
hk_services, |
| 388 |
hk_audit_mode, |
| 389 |
hk_audit, |
| 390 |
hk_sessions_mode, |
| 391 |
hk_sessions, |
| 392 |
hk_history_mode, |
| 393 |
hk_history_global, |
| 394 |
hk_history, |
| 395 |
hk_trends_mode, |
| 396 |
hk_trends_global, |
| 397 |
hk_trends, |
| 398 |
compression_status, |
| 399 |
compress_older |
| 400 |
) |
| 401 |
|
| 402 |
|
| 403 |
if __name__ == "__main__": |
| 404 |
main() |