| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is largely copied from the Nagios module included in the |
| 4 |
# Func project. Original copyright follows: |
| 5 |
# |
| 6 |
# func-nagios - Schedule downtime and enables/disable notifications |
| 7 |
# Copyright 2011, Red Hat, Inc. |
| 8 |
# Tim Bielawa <[email protected]> |
| 9 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 10 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
module: nagios |
| 16 |
short_description: Perform common tasks in Nagios related to downtime and notifications |
| 17 |
description: |
| 18 |
- 'The C(nagios) module has two basic functions: scheduling downtime and toggling alerts for services or hosts.' |
| 19 |
- The C(nagios) module is not idempotent. |
| 20 |
- All actions require the O(host) parameter to be given explicitly. In playbooks you can use the C({{inventory_hostname}}) |
| 21 |
variable to refer to the host the playbook is currently running on. |
| 22 |
- The module executes commands and needs to be run directly on the Nagios server |
| 23 |
with a user that has appropriate access rights. It does not use Nagios' HTTP API. |
| 24 |
- Searches for a I(nagios.cfg) in I(/etc/nagios), I(/etc/nagios2), I(/etc/nagios3), I(/usr/local/etc/nagios), |
| 25 |
I(/usr/local/groundwork/nagios/etc), I(/omd/sites/oppy/tmp/nagios), I(/usr/local/nagios/etc), |
| 26 |
I(/usr/local/nagios), I(/opt/nagios/etc), and I(/opt/nagios), |
| 27 |
or a I(icinga.cfg) in I(/etc/icinga) and I(/usr/local/icinga/etc). |
| 28 |
(The Nagios configuration file should be readable by the Ansible user.) |
| 29 |
extends_documentation_fragment: |
| 30 |
- community.general._attributes |
| 31 |
attributes: |
| 32 |
check_mode: |
| 33 |
support: none |
| 34 |
diff_mode: |
| 35 |
support: none |
| 36 |
options: |
| 37 |
action: |
| 38 |
description: |
| 39 |
- Action to take. |
| 40 |
- The V(acknowledge) and V(forced_check) actions were added in community.general 1.2.0. |
| 41 |
required: true |
| 42 |
choices: |
| 43 |
- downtime |
| 44 |
- delete_downtime |
| 45 |
- enable_alerts |
| 46 |
- disable_alerts |
| 47 |
- silence |
| 48 |
- unsilence |
| 49 |
- silence_nagios |
| 50 |
- unsilence_nagios |
| 51 |
- command |
| 52 |
- servicegroup_service_downtime |
| 53 |
- servicegroup_host_downtime |
| 54 |
- acknowledge |
| 55 |
- forced_check |
| 56 |
type: str |
| 57 |
host: |
| 58 |
description: |
| 59 |
- Host to operate on in Nagios. |
| 60 |
type: str |
| 61 |
cmdfile: |
| 62 |
description: |
| 63 |
- Path to the nagios I(command file) (FIFO pipe). Only required if auto-detection fails. |
| 64 |
type: str |
| 65 |
author: |
| 66 |
description: |
| 67 |
- Author to leave downtime comments as. Only used when O(action) is V(downtime) or V(acknowledge). |
| 68 |
type: str |
| 69 |
default: Ansible |
| 70 |
comment: |
| 71 |
description: |
| 72 |
- Comment when O(action) is V(downtime) or V(acknowledge). |
| 73 |
type: str |
| 74 |
default: Scheduling downtime |
| 75 |
start: |
| 76 |
description: |
| 77 |
- When downtime should start, in C(time_t) format (epoch seconds). |
| 78 |
version_added: '0.2.0' |
| 79 |
type: str |
| 80 |
minutes: |
| 81 |
description: |
| 82 |
- Minutes to schedule downtime for. |
| 83 |
- Only usable with O(action=downtime). |
| 84 |
type: int |
| 85 |
default: 30 |
| 86 |
services: |
| 87 |
description: |
| 88 |
- What to manage downtime/alerts for. Separate multiple services with commas. |
| 89 |
- 'B(Required) option when O(action) is one of: V(downtime), V(acknowledge), V(forced_check), V(enable_alerts), V(disable_alerts).' |
| 90 |
- When specifying what O(services) to handle there is a special service value, V(host), which handles alerts/downtime/acknowledge |
| 91 |
for the I(host itself), for example O(services=host). This keyword may not be given with other services at the same |
| 92 |
time. B(Setting alerts/downtime/acknowledge for a host does not affect alerts/downtime/acknowledge for any of the |
| 93 |
services running on it.) To schedule downtime for all O(services) on particular host use keyword V(all), for example |
| 94 |
O(services=all). |
| 95 |
- Before community.general 11.2.0, one could specify multiple services at once by separating them with commas, for example |
| 96 |
O(services=httpd,nfs,puppet). Since community.general 11.2.0, there can be spaces around the commas, and an actual |
| 97 |
list can be provided. |
| 98 |
aliases: ["service"] |
| 99 |
type: list |
| 100 |
elements: str |
| 101 |
servicegroup: |
| 102 |
description: |
| 103 |
- The Servicegroup we want to set downtimes/alerts for. |
| 104 |
- B(Required) option when using the V(servicegroup_service_downtime) and V(servicegroup_host_downtime) O(action). |
| 105 |
type: str |
| 106 |
command: |
| 107 |
description: |
| 108 |
- The raw command to send to Nagios, which should not include the submitted time header or the line-feed. |
| 109 |
- B(Required) option when O(action=command). |
| 110 |
type: str |
| 111 |
|
| 112 |
author: "Tim Bielawa (@tbielawa)" |
| 113 |
""" |
| 114 |
|
| 115 |
EXAMPLES = r""" |
| 116 |
- name: Set 30 minutes of apache downtime |
| 117 |
community.general.nagios: |
| 118 |
action: downtime |
| 119 |
minutes: 30 |
| 120 |
service: httpd |
| 121 |
host: '{{ inventory_hostname }}' |
| 122 |
|
| 123 |
- name: Schedule an hour of HOST downtime |
| 124 |
community.general.nagios: |
| 125 |
action: downtime |
| 126 |
minutes: 60 |
| 127 |
service: host |
| 128 |
host: '{{ inventory_hostname }}' |
| 129 |
|
| 130 |
- name: Schedule an hour of HOST downtime starting at 2019-04-23T02:00:00+00:00 |
| 131 |
community.general.nagios: |
| 132 |
action: downtime |
| 133 |
start: 1555984800 |
| 134 |
minutes: 60 |
| 135 |
service: host |
| 136 |
host: '{{ inventory_hostname }}' |
| 137 |
|
| 138 |
- name: Schedule an hour of HOST downtime, with a comment describing the reason |
| 139 |
community.general.nagios: |
| 140 |
action: downtime |
| 141 |
minutes: 60 |
| 142 |
service: host |
| 143 |
host: '{{ inventory_hostname }}' |
| 144 |
comment: Rebuilding machine |
| 145 |
|
| 146 |
- name: Schedule downtime for ALL services on HOST |
| 147 |
community.general.nagios: |
| 148 |
action: downtime |
| 149 |
minutes: 45 |
| 150 |
service: all |
| 151 |
host: '{{ inventory_hostname }}' |
| 152 |
|
| 153 |
- name: Schedule downtime for a few services |
| 154 |
community.general.nagios: |
| 155 |
action: downtime |
| 156 |
services: frob,foobar,qeuz |
| 157 |
host: '{{ inventory_hostname }}' |
| 158 |
|
| 159 |
- name: Set 30 minutes downtime for all services in servicegroup foo |
| 160 |
community.general.nagios: |
| 161 |
action: servicegroup_service_downtime |
| 162 |
minutes: 30 |
| 163 |
servicegroup: foo |
| 164 |
host: '{{ inventory_hostname }}' |
| 165 |
|
| 166 |
- name: Set 30 minutes downtime for all host in servicegroup foo |
| 167 |
community.general.nagios: |
| 168 |
action: servicegroup_host_downtime |
| 169 |
minutes: 30 |
| 170 |
servicegroup: foo |
| 171 |
host: '{{ inventory_hostname }}' |
| 172 |
|
| 173 |
- name: Delete all downtime for a given host |
| 174 |
community.general.nagios: |
| 175 |
action: delete_downtime |
| 176 |
host: '{{ inventory_hostname }}' |
| 177 |
service: all |
| 178 |
|
| 179 |
- name: Delete all downtime for HOST with a particular comment |
| 180 |
community.general.nagios: |
| 181 |
action: delete_downtime |
| 182 |
host: '{{ inventory_hostname }}' |
| 183 |
service: host |
| 184 |
comment: Planned maintenance |
| 185 |
|
| 186 |
- name: Acknowledge an HOST with a particular comment |
| 187 |
community.general.nagios: |
| 188 |
action: acknowledge |
| 189 |
service: host |
| 190 |
host: '{{ inventory_hostname }}' |
| 191 |
comment: 'power outage - see casenr 12345' |
| 192 |
|
| 193 |
- name: Acknowledge an active service problem for the httpd service with a particular comment |
| 194 |
community.general.nagios: |
| 195 |
action: acknowledge |
| 196 |
service: httpd |
| 197 |
host: '{{ inventory_hostname }}' |
| 198 |
comment: 'service crashed - see casenr 12345' |
| 199 |
|
| 200 |
- name: Reset a passive service check for snmp trap |
| 201 |
community.general.nagios: |
| 202 |
action: forced_check |
| 203 |
service: snmp |
| 204 |
host: '{{ inventory_hostname }}' |
| 205 |
|
| 206 |
- name: Force an active service check for the httpd service |
| 207 |
community.general.nagios: |
| 208 |
action: forced_check |
| 209 |
service: httpd |
| 210 |
host: '{{ inventory_hostname }}' |
| 211 |
|
| 212 |
- name: Force an active service check for all services of a particular host |
| 213 |
community.general.nagios: |
| 214 |
action: forced_check |
| 215 |
service: all |
| 216 |
host: '{{ inventory_hostname }}' |
| 217 |
|
| 218 |
- name: Force an active service check for a particular host |
| 219 |
community.general.nagios: |
| 220 |
action: forced_check |
| 221 |
service: host |
| 222 |
host: '{{ inventory_hostname }}' |
| 223 |
|
| 224 |
- name: Enable SMART disk alerts |
| 225 |
community.general.nagios: |
| 226 |
action: enable_alerts |
| 227 |
service: smart |
| 228 |
host: '{{ inventory_hostname }}' |
| 229 |
|
| 230 |
- name: Disable httpd and nfs alerts |
| 231 |
community.general.nagios: |
| 232 |
action: disable_alerts |
| 233 |
service: |
| 234 |
- httpd |
| 235 |
- nfs |
| 236 |
host: '{{ inventory_hostname }}' |
| 237 |
|
| 238 |
- name: Disable HOST alerts |
| 239 |
community.general.nagios: |
| 240 |
action: disable_alerts |
| 241 |
service: host |
| 242 |
host: '{{ inventory_hostname }}' |
| 243 |
|
| 244 |
- name: Silence ALL alerts |
| 245 |
community.general.nagios: |
| 246 |
action: silence |
| 247 |
host: '{{ inventory_hostname }}' |
| 248 |
|
| 249 |
- name: Unsilence all alerts |
| 250 |
community.general.nagios: |
| 251 |
action: unsilence |
| 252 |
host: '{{ inventory_hostname }}' |
| 253 |
|
| 254 |
- name: Shut up nagios |
| 255 |
community.general.nagios: |
| 256 |
action: silence_nagios |
| 257 |
|
| 258 |
- name: Annoy me negios |
| 259 |
community.general.nagios: |
| 260 |
action: unsilence_nagios |
| 261 |
|
| 262 |
- name: Command something |
| 263 |
community.general.nagios: |
| 264 |
action: command |
| 265 |
command: DISABLE_FAILURE_PREDICTION |
| 266 |
""" |
| 267 |
|
| 268 |
import os.path |
| 269 |
import stat |
| 270 |
import time |
| 271 |
|
| 272 |
from ansible.module_utils.basic import AnsibleModule |
| 273 |
|
| 274 |
|
| 275 |
def which_cmdfile(): |
| 276 |
locations = [ |
| 277 |
# rhel |
| 278 |
"/etc/nagios/nagios.cfg", |
| 279 |
# debian |
| 280 |
"/etc/nagios3/nagios.cfg", |
| 281 |
# older debian |
| 282 |
"/etc/nagios2/nagios.cfg", |
| 283 |
# bsd, solaris |
| 284 |
"/usr/local/etc/nagios/nagios.cfg", |
| 285 |
# groundwork it monitoring |
| 286 |
"/usr/local/groundwork/nagios/etc/nagios.cfg", |
| 287 |
# open monitoring distribution |
| 288 |
"/omd/sites/oppy/tmp/nagios/nagios.cfg", |
| 289 |
# ??? |
| 290 |
"/usr/local/nagios/etc/nagios.cfg", |
| 291 |
"/usr/local/nagios/nagios.cfg", |
| 292 |
"/opt/nagios/etc/nagios.cfg", |
| 293 |
"/opt/nagios/nagios.cfg", |
| 294 |
# icinga on debian/ubuntu |
| 295 |
"/etc/icinga/icinga.cfg", |
| 296 |
# icinga installed from source (default location) |
| 297 |
"/usr/local/icinga/etc/icinga.cfg", |
| 298 |
] |
| 299 |
|
| 300 |
for path in locations: |
| 301 |
if os.path.exists(path): |
| 302 |
for line in open(path): |
| 303 |
if line.startswith("command_file"): |
| 304 |
return line.split("=")[1].strip() |
| 305 |
|
| 306 |
return None |
| 307 |
|
| 308 |
|
| 309 |
def main(): |
| 310 |
ACTION_CHOICES = [ |
| 311 |
"downtime", |
| 312 |
"delete_downtime", |
| 313 |
"silence", |
| 314 |
"unsilence", |
| 315 |
"enable_alerts", |
| 316 |
"disable_alerts", |
| 317 |
"silence_nagios", |
| 318 |
"unsilence_nagios", |
| 319 |
"command", |
| 320 |
"servicegroup_host_downtime", |
| 321 |
"servicegroup_service_downtime", |
| 322 |
"acknowledge", |
| 323 |
"forced_check", |
| 324 |
] |
| 325 |
|
| 326 |
module = AnsibleModule( |
| 327 |
argument_spec=dict( |
| 328 |
action=dict(type="str", required=True, choices=ACTION_CHOICES), |
| 329 |
author=dict(type="str", default="Ansible"), |
| 330 |
comment=dict(type="str", default="Scheduling downtime"), |
| 331 |
host=dict(type="str"), |
| 332 |
servicegroup=dict(type="str"), |
| 333 |
start=dict(type="str"), |
| 334 |
minutes=dict(type="int", default=30), |
| 335 |
cmdfile=dict(type="str", default=which_cmdfile()), |
| 336 |
services=dict(type="list", elements="str", aliases=["service"]), |
| 337 |
command=dict(type="str"), |
| 338 |
), |
| 339 |
required_if=[ |
| 340 |
("action", "downtime", ["host", "services"]), |
| 341 |
("action", "delete_downtime", ["host", "services"]), |
| 342 |
("action", "silence", ["host"]), |
| 343 |
("action", "unsilence", ["host"]), |
| 344 |
("action", "enable_alerts", ["host", "services"]), |
| 345 |
("action", "disable_alerts", ["host", "services"]), |
| 346 |
("action", "command", ["command"]), |
| 347 |
("action", "servicegroup_host_downtime", ["host", "servicegroup"]), |
| 348 |
("action", "servicegroup_service_downtime", ["host", "servicegroup"]), |
| 349 |
("action", "acknowledge", ["host", "services"]), |
| 350 |
("action", "forced_check", ["host", "services"]), |
| 351 |
], |
| 352 |
) |
| 353 |
|
| 354 |
if not module.params["cmdfile"]: |
| 355 |
module.fail_json(msg="unable to locate nagios.cfg") |
| 356 |
|
| 357 |
ansible_nagios = Nagios(module, **module.params) |
| 358 |
if module.check_mode: |
| 359 |
module.exit_json(changed=True) |
| 360 |
else: |
| 361 |
ansible_nagios.act() |
| 362 |
|
| 363 |
|
| 364 |
class Nagios: |
| 365 |
""" |
| 366 |
Perform common tasks in Nagios related to downtime and |
| 367 |
notifications. |
| 368 |
|
| 369 |
The complete set of external commands Nagios handles is documented |
| 370 |
on their website: |
| 371 |
|
| 372 |
http://old.nagios.org/developerinfo/externalcommands/commandlist.php |
| 373 |
|
| 374 |
Note that in the case of `schedule_svc_downtime`, |
| 375 |
`enable_svc_notifications`, and `disable_svc_notifications`, the |
| 376 |
service argument should be passed as a list. |
| 377 |
""" |
| 378 |
|
| 379 |
def __init__(self, module, **kwargs): |
| 380 |
self.module = module |
| 381 |
self.action = kwargs["action"] |
| 382 |
self.author = kwargs["author"] |
| 383 |
self.comment = kwargs["comment"] |
| 384 |
self.host = kwargs["host"] |
| 385 |
self.servicegroup = kwargs["servicegroup"] |
| 386 |
if kwargs["start"] is not None: |
| 387 |
self.start = int(kwargs["start"]) |
| 388 |
else: |
| 389 |
self.start = None |
| 390 |
self.minutes = kwargs["minutes"] |
| 391 |
self.cmdfile = kwargs["cmdfile"] |
| 392 |
self.command = kwargs["command"] |
| 393 |
|
| 394 |
if kwargs["services"] is None: |
| 395 |
self.services = kwargs["services"] |
| 396 |
elif len(kwargs["services"]) == 1 and kwargs["services"][0] in ["host", "all"]: |
| 397 |
self.services = kwargs["services"][0] |
| 398 |
else: |
| 399 |
self.services = kwargs["services"] |
| 400 |
|
| 401 |
self.command_results = [] |
| 402 |
|
| 403 |
def _now(self): |
| 404 |
""" |
| 405 |
The time in seconds since 12:00:00AM Jan 1, 1970 |
| 406 |
""" |
| 407 |
|
| 408 |
return int(time.time()) |
| 409 |
|
| 410 |
def _write_command(self, cmd): |
| 411 |
""" |
| 412 |
Write the given command to the Nagios command file |
| 413 |
""" |
| 414 |
|
| 415 |
if not os.path.exists(self.cmdfile): |
| 416 |
self.module.fail_json(msg="nagios command file does not exist", cmdfile=self.cmdfile) |
| 417 |
if not stat.S_ISFIFO(os.stat(self.cmdfile).st_mode): |
| 418 |
self.module.fail_json(msg="nagios command file is not a fifo file", cmdfile=self.cmdfile) |
| 419 |
try: |
| 420 |
with open(self.cmdfile, "w") as fp: |
| 421 |
fp.write(cmd) |
| 422 |
fp.flush() |
| 423 |
self.command_results.append(cmd.strip()) |
| 424 |
except OSError: |
| 425 |
self.module.fail_json(msg="unable to write to nagios command file", cmdfile=self.cmdfile) |
| 426 |
|
| 427 |
def _fmt_dt_str(self, cmd, host, duration, author=None, comment=None, start=None, svc=None, fixed=1, trigger=0): |
| 428 |
""" |
| 429 |
Format an external-command downtime string. |
| 430 |
|
| 431 |
cmd - Nagios command ID |
| 432 |
host - Host schedule downtime on |
| 433 |
duration - Minutes to schedule downtime for |
| 434 |
author - Name to file the downtime as |
| 435 |
comment - Reason for running this command (upgrade, reboot, etc) |
| 436 |
start - Start of downtime in seconds since 12:00AM Jan 1 1970 |
| 437 |
Default is to use the entry time (now) |
| 438 |
svc - Service to schedule downtime for, omit when for host downtime |
| 439 |
fixed - Start now if 1, start when a problem is detected if 0 |
| 440 |
trigger - Optional ID of event to start downtime from. Leave as 0 for |
| 441 |
fixed downtime. |
| 442 |
|
| 443 |
Syntax: [submitted] COMMAND;<host_name>;[<service_description>] |
| 444 |
<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>; |
| 445 |
<comment> |
| 446 |
""" |
| 447 |
|
| 448 |
entry_time = self._now() |
| 449 |
if start is None: |
| 450 |
start = entry_time |
| 451 |
|
| 452 |
hdr = f"[{entry_time}] {cmd};{host};" |
| 453 |
duration_s = duration * 60 |
| 454 |
end = start + duration_s |
| 455 |
|
| 456 |
if not author: |
| 457 |
author = self.author |
| 458 |
|
| 459 |
if not comment: |
| 460 |
comment = self.comment |
| 461 |
|
| 462 |
if svc is not None: |
| 463 |
dt_args = [svc, str(start), str(end), str(fixed), str(trigger), str(duration_s), author, comment] |
| 464 |
else: |
| 465 |
# Downtime for a host if no svc specified |
| 466 |
dt_args = [str(start), str(end), str(fixed), str(trigger), str(duration_s), author, comment] |
| 467 |
|
| 468 |
dt_arg_str = ";".join(dt_args) |
| 469 |
dt_str = f"{hdr}{dt_arg_str}\n" |
| 470 |
|
| 471 |
return dt_str |
| 472 |
|
| 473 |
def _fmt_ack_str(self, cmd, host, author=None, comment=None, svc=None, sticky=0, notify=1, persistent=0): |
| 474 |
""" |
| 475 |
Format an external-command acknowledge string. |
| 476 |
|
| 477 |
cmd - Nagios command ID |
| 478 |
host - Host schedule downtime on |
| 479 |
author - Name to file the downtime as |
| 480 |
comment - Reason for running this command (upgrade, reboot, etc) |
| 481 |
svc - Service to schedule downtime for, omit when for host downtime |
| 482 |
sticky - the acknowledgement will remain until the host returns to an UP state if set to 1 |
| 483 |
notify - a notification will be sent out to contacts |
| 484 |
persistent - survive across restarts of the Nagios process |
| 485 |
|
| 486 |
Syntax: [submitted] COMMAND;<host_name>;[<service_description>] |
| 487 |
<sticky>;<notify>;<persistent>;<author>;<comment> |
| 488 |
""" |
| 489 |
|
| 490 |
entry_time = self._now() |
| 491 |
hdr = f"[{entry_time}] {cmd};{host};" |
| 492 |
|
| 493 |
if not author: |
| 494 |
author = self.author |
| 495 |
|
| 496 |
if not comment: |
| 497 |
comment = self.comment |
| 498 |
|
| 499 |
if svc is not None: |
| 500 |
ack_args = [svc, str(sticky), str(notify), str(persistent), author, comment] |
| 501 |
else: |
| 502 |
# Downtime for a host if no svc specified |
| 503 |
ack_args = [str(sticky), str(notify), str(persistent), author, comment] |
| 504 |
|
| 505 |
ack_arg_str = ";".join(ack_args) |
| 506 |
ack_str = f"{hdr}{ack_arg_str}\n" |
| 507 |
|
| 508 |
return ack_str |
| 509 |
|
| 510 |
def _fmt_dt_del_str(self, cmd, host, svc=None, start=None, comment=None): |
| 511 |
""" |
| 512 |
Format an external-command downtime deletion string. |
| 513 |
|
| 514 |
cmd - Nagios command ID |
| 515 |
host - Host to remove scheduled downtime from |
| 516 |
comment - Reason downtime was added (upgrade, reboot, etc) |
| 517 |
start - Start of downtime in seconds since 12:00AM Jan 1 1970 |
| 518 |
svc - Service to remove downtime for, omit to remove all downtime for the host |
| 519 |
|
| 520 |
Syntax: [submitted] COMMAND;<host_name>; |
| 521 |
[<service_desription>];[<start_time>];[<comment>] |
| 522 |
""" |
| 523 |
|
| 524 |
entry_time = self._now() |
| 525 |
hdr = f"[{entry_time}] {cmd};{host};" |
| 526 |
|
| 527 |
if comment is None: |
| 528 |
comment = self.comment |
| 529 |
|
| 530 |
dt_del_args = [] |
| 531 |
if svc is not None: |
| 532 |
dt_del_args.append(svc) |
| 533 |
else: |
| 534 |
dt_del_args.append("") |
| 535 |
|
| 536 |
if start is not None: |
| 537 |
dt_del_args.append(str(start)) |
| 538 |
else: |
| 539 |
dt_del_args.append("") |
| 540 |
|
| 541 |
if comment is not None: |
| 542 |
dt_del_args.append(comment) |
| 543 |
else: |
| 544 |
dt_del_args.append("") |
| 545 |
|
| 546 |
dt_del_arg_str = ";".join(dt_del_args) |
| 547 |
dt_del_str = f"{hdr}{dt_del_arg_str}\n" |
| 548 |
|
| 549 |
return dt_del_str |
| 550 |
|
| 551 |
def _fmt_chk_str(self, cmd, host, svc=None, start=None): |
| 552 |
""" |
| 553 |
Format an external-command forced host or service check string. |
| 554 |
|
| 555 |
cmd - Nagios command ID |
| 556 |
host - Host to check service from |
| 557 |
svc - Service to check |
| 558 |
start - check time |
| 559 |
|
| 560 |
Syntax: [submitted] COMMAND;<host_name>;[<service_description>];<check_time> |
| 561 |
""" |
| 562 |
|
| 563 |
entry_time = self._now() |
| 564 |
hdr = f"[{entry_time}] {cmd};{host};" |
| 565 |
|
| 566 |
if start is None: |
| 567 |
start = entry_time + 3 |
| 568 |
|
| 569 |
if svc is None: |
| 570 |
chk_args = [str(start)] |
| 571 |
else: |
| 572 |
chk_args = [svc, str(start)] |
| 573 |
|
| 574 |
chk_arg_str = ";".join(chk_args) |
| 575 |
chk_str = f"{hdr}{chk_arg_str}\n" |
| 576 |
|
| 577 |
return chk_str |
| 578 |
|
| 579 |
def _fmt_notif_str(self, cmd, host=None, svc=None): |
| 580 |
""" |
| 581 |
Format an external-command notification string. |
| 582 |
|
| 583 |
cmd - Nagios command ID. |
| 584 |
host - Host to en/disable notifications on.. A value is not required |
| 585 |
for global downtime |
| 586 |
svc - Service to schedule downtime for. A value is not required |
| 587 |
for host downtime. |
| 588 |
|
| 589 |
Syntax: [submitted] COMMAND;<host_name>[;<service_description>] |
| 590 |
""" |
| 591 |
|
| 592 |
entry_time = self._now() |
| 593 |
notif_str = f"[{entry_time}] {cmd}" |
| 594 |
if host is not None: |
| 595 |
notif_str += f";{host}" |
| 596 |
|
| 597 |
if svc is not None: |
| 598 |
notif_str += f";{svc}" |
| 599 |
|
| 600 |
notif_str += "\n" |
| 601 |
|
| 602 |
return notif_str |
| 603 |
|
| 604 |
def schedule_svc_downtime(self, host, services=None, minutes=30, start=None): |
| 605 |
""" |
| 606 |
This command is used to schedule downtime for a particular |
| 607 |
service. |
| 608 |
|
| 609 |
During the specified downtime, Nagios will not send |
| 610 |
notifications out about the service. |
| 611 |
|
| 612 |
Syntax: SCHEDULE_SVC_DOWNTIME;<host_name>;<service_description> |
| 613 |
<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>; |
| 614 |
<comment> |
| 615 |
""" |
| 616 |
|
| 617 |
cmd = "SCHEDULE_SVC_DOWNTIME" |
| 618 |
|
| 619 |
if services is None: |
| 620 |
services = [] |
| 621 |
|
| 622 |
for service in services: |
| 623 |
dt_cmd_str = self._fmt_dt_str(cmd, host, minutes, start=start, svc=service) |
| 624 |
self._write_command(dt_cmd_str) |
| 625 |
|
| 626 |
def schedule_host_downtime(self, host, minutes=30, start=None): |
| 627 |
""" |
| 628 |
This command is used to schedule downtime for a particular |
| 629 |
host. |
| 630 |
|
| 631 |
During the specified downtime, Nagios will not send |
| 632 |
notifications out about the host. |
| 633 |
|
| 634 |
Syntax: SCHEDULE_HOST_DOWNTIME;<host_name>;<start_time>;<end_time>; |
| 635 |
<fixed>;<trigger_id>;<duration>;<author>;<comment> |
| 636 |
""" |
| 637 |
|
| 638 |
cmd = "SCHEDULE_HOST_DOWNTIME" |
| 639 |
dt_cmd_str = self._fmt_dt_str(cmd, host, minutes, start=start) |
| 640 |
self._write_command(dt_cmd_str) |
| 641 |
|
| 642 |
def acknowledge_svc_problem(self, host, services=None): |
| 643 |
""" |
| 644 |
This command is used to acknowledge a particular |
| 645 |
service problem. |
| 646 |
|
| 647 |
By acknowledging the current problem, future notifications |
| 648 |
for the same servicestate are disabled |
| 649 |
|
| 650 |
Syntax: ACKNOWLEDGE_SVC_PROBLEM;<host_name>;<service_description>; |
| 651 |
<sticky>;<notify>;<persistent>;<author>;<comment> |
| 652 |
""" |
| 653 |
|
| 654 |
cmd = "ACKNOWLEDGE_SVC_PROBLEM" |
| 655 |
|
| 656 |
if services is None: |
| 657 |
services = [] |
| 658 |
|
| 659 |
for service in services: |
| 660 |
ack_cmd_str = self._fmt_ack_str(cmd, host, svc=service) |
| 661 |
self._write_command(ack_cmd_str) |
| 662 |
|
| 663 |
def acknowledge_host_problem(self, host): |
| 664 |
""" |
| 665 |
This command is used to acknowledge a particular |
| 666 |
host problem. |
| 667 |
|
| 668 |
By acknowledging the current problem, future notifications |
| 669 |
for the same servicestate are disabled |
| 670 |
|
| 671 |
Syntax: ACKNOWLEDGE_HOST_PROBLEM;<host_name>;<sticky>;<notify>; |
| 672 |
<persistent>;<author>;<comment> |
| 673 |
""" |
| 674 |
|
| 675 |
cmd = "ACKNOWLEDGE_HOST_PROBLEM" |
| 676 |
ack_cmd_str = self._fmt_ack_str(cmd, host) |
| 677 |
self._write_command(ack_cmd_str) |
| 678 |
|
| 679 |
def schedule_forced_host_check(self, host): |
| 680 |
""" |
| 681 |
This command schedules a forced active check for a particular host. |
| 682 |
|
| 683 |
Syntax: SCHEDULE_FORCED_HOST_CHECK;<host_name>;<check_time> |
| 684 |
""" |
| 685 |
|
| 686 |
cmd = "SCHEDULE_FORCED_HOST_CHECK" |
| 687 |
|
| 688 |
chk_cmd_str = self._fmt_chk_str(cmd, host, svc=None) |
| 689 |
self._write_command(chk_cmd_str) |
| 690 |
|
| 691 |
def schedule_forced_host_svc_check(self, host): |
| 692 |
""" |
| 693 |
This command schedules a forced active check for all services |
| 694 |
associated with a particular host. |
| 695 |
|
| 696 |
Syntax: SCHEDULE_FORCED_HOST_SVC_CHECKS;<host_name>;<check_time> |
| 697 |
""" |
| 698 |
|
| 699 |
cmd = "SCHEDULE_FORCED_HOST_SVC_CHECKS" |
| 700 |
|
| 701 |
chk_cmd_str = self._fmt_chk_str(cmd, host, svc=None) |
| 702 |
self._write_command(chk_cmd_str) |
| 703 |
|
| 704 |
def schedule_forced_svc_check(self, host, services=None): |
| 705 |
""" |
| 706 |
This command schedules a forced active check for a particular |
| 707 |
service. |
| 708 |
|
| 709 |
Syntax: SCHEDULE_FORCED_SVC_CHECK;<host_name>;<service_description>;<check_time> |
| 710 |
""" |
| 711 |
|
| 712 |
cmd = "SCHEDULE_FORCED_SVC_CHECK" |
| 713 |
|
| 714 |
if services is None: |
| 715 |
services = [] |
| 716 |
|
| 717 |
for service in services: |
| 718 |
chk_cmd_str = self._fmt_chk_str(cmd, host, svc=service) |
| 719 |
self._write_command(chk_cmd_str) |
| 720 |
|
| 721 |
def schedule_host_svc_downtime(self, host, minutes=30, start=None): |
| 722 |
""" |
| 723 |
This command is used to schedule downtime for |
| 724 |
all services associated with a particular host. |
| 725 |
|
| 726 |
During the specified downtime, Nagios will not send |
| 727 |
notifications out about the host. |
| 728 |
|
| 729 |
SCHEDULE_HOST_SVC_DOWNTIME;<host_name>;<start_time>;<end_time>; |
| 730 |
<fixed>;<trigger_id>;<duration>;<author>;<comment> |
| 731 |
""" |
| 732 |
|
| 733 |
cmd = "SCHEDULE_HOST_SVC_DOWNTIME" |
| 734 |
dt_cmd_str = self._fmt_dt_str(cmd, host, minutes, start=start) |
| 735 |
self._write_command(dt_cmd_str) |
| 736 |
|
| 737 |
def delete_host_downtime(self, host, services=None, comment=None): |
| 738 |
""" |
| 739 |
This command is used to remove scheduled downtime for a particular |
| 740 |
host. |
| 741 |
|
| 742 |
Syntax: DEL_DOWNTIME_BY_HOST_NAME;<host_name>; |
| 743 |
[<service_desription>];[<start_time>];[<comment>] |
| 744 |
""" |
| 745 |
|
| 746 |
cmd = "DEL_DOWNTIME_BY_HOST_NAME" |
| 747 |
|
| 748 |
if services is None: |
| 749 |
dt_del_cmd_str = self._fmt_dt_del_str(cmd, host, comment=comment) |
| 750 |
self._write_command(dt_del_cmd_str) |
| 751 |
else: |
| 752 |
for service in services: |
| 753 |
dt_del_cmd_str = self._fmt_dt_del_str(cmd, host, svc=service, comment=comment) |
| 754 |
self._write_command(dt_del_cmd_str) |
| 755 |
|
| 756 |
def schedule_hostgroup_host_downtime(self, hostgroup, minutes=30, start=None): |
| 757 |
""" |
| 758 |
This command is used to schedule downtime for all hosts in a |
| 759 |
particular hostgroup. |
| 760 |
|
| 761 |
During the specified downtime, Nagios will not send |
| 762 |
notifications out about the hosts. |
| 763 |
|
| 764 |
Syntax: SCHEDULE_HOSTGROUP_HOST_DOWNTIME;<hostgroup_name>;<start_time>; |
| 765 |
<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment> |
| 766 |
""" |
| 767 |
|
| 768 |
cmd = "SCHEDULE_HOSTGROUP_HOST_DOWNTIME" |
| 769 |
dt_cmd_str = self._fmt_dt_str(cmd, hostgroup, minutes, start=start) |
| 770 |
self._write_command(dt_cmd_str) |
| 771 |
|
| 772 |
def schedule_hostgroup_svc_downtime(self, hostgroup, minutes=30, start=None): |
| 773 |
""" |
| 774 |
This command is used to schedule downtime for all services in |
| 775 |
a particular hostgroup. |
| 776 |
|
| 777 |
During the specified downtime, Nagios will not send |
| 778 |
notifications out about the services. |
| 779 |
|
| 780 |
Note that scheduling downtime for services does not |
| 781 |
automatically schedule downtime for the hosts those services |
| 782 |
are associated with. |
| 783 |
|
| 784 |
Syntax: SCHEDULE_HOSTGROUP_SVC_DOWNTIME;<hostgroup_name>;<start_time>; |
| 785 |
<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment> |
| 786 |
""" |
| 787 |
|
| 788 |
cmd = "SCHEDULE_HOSTGROUP_SVC_DOWNTIME" |
| 789 |
dt_cmd_str = self._fmt_dt_str(cmd, hostgroup, minutes, start=start) |
| 790 |
self._write_command(dt_cmd_str) |
| 791 |
|
| 792 |
def schedule_servicegroup_host_downtime(self, servicegroup, minutes=30, start=None): |
| 793 |
""" |
| 794 |
This command is used to schedule downtime for all hosts in a |
| 795 |
particular servicegroup. |
| 796 |
|
| 797 |
During the specified downtime, Nagios will not send |
| 798 |
notifications out about the hosts. |
| 799 |
|
| 800 |
Syntax: SCHEDULE_SERVICEGROUP_HOST_DOWNTIME;<servicegroup_name>; |
| 801 |
<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>; |
| 802 |
<comment> |
| 803 |
""" |
| 804 |
|
| 805 |
cmd = "SCHEDULE_SERVICEGROUP_HOST_DOWNTIME" |
| 806 |
dt_cmd_str = self._fmt_dt_str(cmd, servicegroup, minutes, start=start) |
| 807 |
self._write_command(dt_cmd_str) |
| 808 |
|
| 809 |
def schedule_servicegroup_svc_downtime(self, servicegroup, minutes=30, start=None): |
| 810 |
""" |
| 811 |
This command is used to schedule downtime for all services in |
| 812 |
a particular servicegroup. |
| 813 |
|
| 814 |
During the specified downtime, Nagios will not send |
| 815 |
notifications out about the services. |
| 816 |
|
| 817 |
Note that scheduling downtime for services does not |
| 818 |
automatically schedule downtime for the hosts those services |
| 819 |
are associated with. |
| 820 |
|
| 821 |
Syntax: SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;<servicegroup_name>; |
| 822 |
<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>; |
| 823 |
<comment> |
| 824 |
""" |
| 825 |
|
| 826 |
cmd = "SCHEDULE_SERVICEGROUP_SVC_DOWNTIME" |
| 827 |
dt_cmd_str = self._fmt_dt_str(cmd, servicegroup, minutes, start=start) |
| 828 |
self._write_command(dt_cmd_str) |
| 829 |
|
| 830 |
def disable_host_svc_notifications(self, host): |
| 831 |
""" |
| 832 |
This command is used to prevent notifications from being sent |
| 833 |
out for all services on the specified host. |
| 834 |
|
| 835 |
Note that this command does not disable notifications from |
| 836 |
being sent out about the host. |
| 837 |
|
| 838 |
Syntax: DISABLE_HOST_SVC_NOTIFICATIONS;<host_name> |
| 839 |
""" |
| 840 |
|
| 841 |
cmd = "DISABLE_HOST_SVC_NOTIFICATIONS" |
| 842 |
notif_str = self._fmt_notif_str(cmd, host) |
| 843 |
self._write_command(notif_str) |
| 844 |
|
| 845 |
def disable_host_notifications(self, host): |
| 846 |
""" |
| 847 |
This command is used to prevent notifications from being sent |
| 848 |
out for the specified host. |
| 849 |
|
| 850 |
Note that this command does not disable notifications for |
| 851 |
services associated with this host. |
| 852 |
|
| 853 |
Syntax: DISABLE_HOST_NOTIFICATIONS;<host_name> |
| 854 |
""" |
| 855 |
|
| 856 |
cmd = "DISABLE_HOST_NOTIFICATIONS" |
| 857 |
notif_str = self._fmt_notif_str(cmd, host) |
| 858 |
self._write_command(notif_str) |
| 859 |
|
| 860 |
def disable_svc_notifications(self, host, services=None): |
| 861 |
""" |
| 862 |
This command is used to prevent notifications from being sent |
| 863 |
out for the specified service. |
| 864 |
|
| 865 |
Note that this command does not disable notifications from |
| 866 |
being sent out about the host. |
| 867 |
|
| 868 |
Syntax: DISABLE_SVC_NOTIFICATIONS;<host_name>;<service_description> |
| 869 |
""" |
| 870 |
|
| 871 |
cmd = "DISABLE_SVC_NOTIFICATIONS" |
| 872 |
|
| 873 |
if services is None: |
| 874 |
services = [] |
| 875 |
|
| 876 |
for service in services: |
| 877 |
notif_str = self._fmt_notif_str(cmd, host, svc=service) |
| 878 |
self._write_command(notif_str) |
| 879 |
|
| 880 |
def disable_servicegroup_host_notifications(self, servicegroup): |
| 881 |
""" |
| 882 |
This command is used to prevent notifications from being sent |
| 883 |
out for all hosts in the specified servicegroup. |
| 884 |
|
| 885 |
Note that this command does not disable notifications for |
| 886 |
services associated with hosts in this service group. |
| 887 |
|
| 888 |
Syntax: DISABLE_SERVICEGROUP_HOST_NOTIFICATIONS;<servicegroup_name> |
| 889 |
""" |
| 890 |
|
| 891 |
cmd = "DISABLE_SERVICEGROUP_HOST_NOTIFICATIONS" |
| 892 |
notif_str = self._fmt_notif_str(cmd, servicegroup) |
| 893 |
self._write_command(notif_str) |
| 894 |
|
| 895 |
def disable_servicegroup_svc_notifications(self, servicegroup): |
| 896 |
""" |
| 897 |
This command is used to prevent notifications from being sent |
| 898 |
out for all services in the specified servicegroup. |
| 899 |
|
| 900 |
Note that this does not prevent notifications from being sent |
| 901 |
out about the hosts in this servicegroup. |
| 902 |
|
| 903 |
Syntax: DISABLE_SERVICEGROUP_SVC_NOTIFICATIONS;<servicegroup_name> |
| 904 |
""" |
| 905 |
|
| 906 |
cmd = "DISABLE_SERVICEGROUP_SVC_NOTIFICATIONS" |
| 907 |
notif_str = self._fmt_notif_str(cmd, servicegroup) |
| 908 |
self._write_command(notif_str) |
| 909 |
|
| 910 |
def disable_hostgroup_host_notifications(self, hostgroup): |
| 911 |
""" |
| 912 |
Disables notifications for all hosts in a particular |
| 913 |
hostgroup. |
| 914 |
|
| 915 |
Note that this does not disable notifications for the services |
| 916 |
associated with the hosts in the hostgroup - see the |
| 917 |
DISABLE_HOSTGROUP_SVC_NOTIFICATIONS command for that. |
| 918 |
|
| 919 |
Syntax: DISABLE_HOSTGROUP_HOST_NOTIFICATIONS;<hostgroup_name> |
| 920 |
""" |
| 921 |
|
| 922 |
cmd = "DISABLE_HOSTGROUP_HOST_NOTIFICATIONS" |
| 923 |
notif_str = self._fmt_notif_str(cmd, hostgroup) |
| 924 |
self._write_command(notif_str) |
| 925 |
|
| 926 |
def disable_hostgroup_svc_notifications(self, hostgroup): |
| 927 |
""" |
| 928 |
Disables notifications for all services associated with hosts |
| 929 |
in a particular hostgroup. |
| 930 |
|
| 931 |
Note that this does not disable notifications for the hosts in |
| 932 |
the hostgroup - see the DISABLE_HOSTGROUP_HOST_NOTIFICATIONS |
| 933 |
command for that. |
| 934 |
|
| 935 |
Syntax: DISABLE_HOSTGROUP_SVC_NOTIFICATIONS;<hostgroup_name> |
| 936 |
""" |
| 937 |
|
| 938 |
cmd = "DISABLE_HOSTGROUP_SVC_NOTIFICATIONS" |
| 939 |
notif_str = self._fmt_notif_str(cmd, hostgroup) |
| 940 |
self._write_command(notif_str) |
| 941 |
|
| 942 |
def enable_host_notifications(self, host): |
| 943 |
""" |
| 944 |
Enables notifications for a particular host. |
| 945 |
|
| 946 |
Note that this command does not enable notifications for |
| 947 |
services associated with this host. |
| 948 |
|
| 949 |
Syntax: ENABLE_HOST_NOTIFICATIONS;<host_name> |
| 950 |
""" |
| 951 |
|
| 952 |
cmd = "ENABLE_HOST_NOTIFICATIONS" |
| 953 |
notif_str = self._fmt_notif_str(cmd, host) |
| 954 |
self._write_command(notif_str) |
| 955 |
|
| 956 |
def enable_host_svc_notifications(self, host): |
| 957 |
""" |
| 958 |
Enables notifications for all services on the specified host. |
| 959 |
|
| 960 |
Note that this does not enable notifications for the host. |
| 961 |
|
| 962 |
Syntax: ENABLE_HOST_SVC_NOTIFICATIONS;<host_name> |
| 963 |
""" |
| 964 |
|
| 965 |
cmd = "ENABLE_HOST_SVC_NOTIFICATIONS" |
| 966 |
notif_str = self._fmt_notif_str(cmd, host) |
| 967 |
nagios_return = self._write_command(notif_str) |
| 968 |
|
| 969 |
if nagios_return: |
| 970 |
return notif_str |
| 971 |
else: |
| 972 |
return "Fail: could not write to the command file" |
| 973 |
|
| 974 |
def enable_svc_notifications(self, host, services=None): |
| 975 |
""" |
| 976 |
Enables notifications for a particular service. |
| 977 |
|
| 978 |
Note that this does not enable notifications for the host. |
| 979 |
|
| 980 |
Syntax: ENABLE_SVC_NOTIFICATIONS;<host_name>;<service_description> |
| 981 |
""" |
| 982 |
|
| 983 |
cmd = "ENABLE_SVC_NOTIFICATIONS" |
| 984 |
|
| 985 |
if services is None: |
| 986 |
services = [] |
| 987 |
|
| 988 |
nagios_return = True |
| 989 |
return_str_list = [] |
| 990 |
for service in services: |
| 991 |
notif_str = self._fmt_notif_str(cmd, host, svc=service) |
| 992 |
nagios_return = self._write_command(notif_str) and nagios_return |
| 993 |
return_str_list.append(notif_str) |
| 994 |
|
| 995 |
if nagios_return: |
| 996 |
return return_str_list |
| 997 |
else: |
| 998 |
return "Fail: could not write to the command file" |
| 999 |
|
| 1000 |
def enable_hostgroup_host_notifications(self, hostgroup): |
| 1001 |
""" |
| 1002 |
Enables notifications for all hosts in a particular hostgroup. |
| 1003 |
|
| 1004 |
Note that this command does not enable notifications for |
| 1005 |
services associated with the hosts in this hostgroup. |
| 1006 |
|
| 1007 |
Syntax: ENABLE_HOSTGROUP_HOST_NOTIFICATIONS;<hostgroup_name> |
| 1008 |
""" |
| 1009 |
|
| 1010 |
cmd = "ENABLE_HOSTGROUP_HOST_NOTIFICATIONS" |
| 1011 |
notif_str = self._fmt_notif_str(cmd, hostgroup) |
| 1012 |
nagios_return = self._write_command(notif_str) |
| 1013 |
|
| 1014 |
if nagios_return: |
| 1015 |
return notif_str |
| 1016 |
else: |
| 1017 |
return "Fail: could not write to the command file" |
| 1018 |
|
| 1019 |
def enable_hostgroup_svc_notifications(self, hostgroup): |
| 1020 |
""" |
| 1021 |
Enables notifications for all services that are associated |
| 1022 |
with hosts in a particular hostgroup. |
| 1023 |
|
| 1024 |
Note that this does not enable notifications for the hosts in |
| 1025 |
this hostgroup. |
| 1026 |
|
| 1027 |
Syntax: ENABLE_HOSTGROUP_SVC_NOTIFICATIONS;<hostgroup_name> |
| 1028 |
""" |
| 1029 |
|
| 1030 |
cmd = "ENABLE_HOSTGROUP_SVC_NOTIFICATIONS" |
| 1031 |
notif_str = self._fmt_notif_str(cmd, hostgroup) |
| 1032 |
nagios_return = self._write_command(notif_str) |
| 1033 |
|
| 1034 |
if nagios_return: |
| 1035 |
return notif_str |
| 1036 |
else: |
| 1037 |
return "Fail: could not write to the command file" |
| 1038 |
|
| 1039 |
def enable_servicegroup_host_notifications(self, servicegroup): |
| 1040 |
""" |
| 1041 |
Enables notifications for all hosts that have services that |
| 1042 |
are members of a particular servicegroup. |
| 1043 |
|
| 1044 |
Note that this command does not enable notifications for |
| 1045 |
services associated with the hosts in this servicegroup. |
| 1046 |
|
| 1047 |
Syntax: ENABLE_SERVICEGROUP_HOST_NOTIFICATIONS;<servicegroup_name> |
| 1048 |
""" |
| 1049 |
|
| 1050 |
cmd = "ENABLE_SERVICEGROUP_HOST_NOTIFICATIONS" |
| 1051 |
notif_str = self._fmt_notif_str(cmd, servicegroup) |
| 1052 |
nagios_return = self._write_command(notif_str) |
| 1053 |
|
| 1054 |
if nagios_return: |
| 1055 |
return notif_str |
| 1056 |
else: |
| 1057 |
return "Fail: could not write to the command file" |
| 1058 |
|
| 1059 |
def enable_servicegroup_svc_notifications(self, servicegroup): |
| 1060 |
""" |
| 1061 |
Enables notifications for all services that are members of a |
| 1062 |
particular servicegroup. |
| 1063 |
|
| 1064 |
Note that this does not enable notifications for the hosts in |
| 1065 |
this servicegroup. |
| 1066 |
|
| 1067 |
Syntax: ENABLE_SERVICEGROUP_SVC_NOTIFICATIONS;<servicegroup_name> |
| 1068 |
""" |
| 1069 |
|
| 1070 |
cmd = "ENABLE_SERVICEGROUP_SVC_NOTIFICATIONS" |
| 1071 |
notif_str = self._fmt_notif_str(cmd, servicegroup) |
| 1072 |
nagios_return = self._write_command(notif_str) |
| 1073 |
|
| 1074 |
if nagios_return: |
| 1075 |
return notif_str |
| 1076 |
else: |
| 1077 |
return "Fail: could not write to the command file" |
| 1078 |
|
| 1079 |
def silence_host(self, host): |
| 1080 |
""" |
| 1081 |
This command is used to prevent notifications from being sent |
| 1082 |
out for the host and all services on the specified host. |
| 1083 |
|
| 1084 |
This is equivalent to calling disable_host_svc_notifications |
| 1085 |
and disable_host_notifications. |
| 1086 |
|
| 1087 |
Syntax: DISABLE_HOST_SVC_NOTIFICATIONS;<host_name> |
| 1088 |
Syntax: DISABLE_HOST_NOTIFICATIONS;<host_name> |
| 1089 |
""" |
| 1090 |
|
| 1091 |
cmd = ["DISABLE_HOST_SVC_NOTIFICATIONS", "DISABLE_HOST_NOTIFICATIONS"] |
| 1092 |
nagios_return = True |
| 1093 |
return_str_list = [] |
| 1094 |
for c in cmd: |
| 1095 |
notif_str = self._fmt_notif_str(c, host) |
| 1096 |
nagios_return = self._write_command(notif_str) and nagios_return |
| 1097 |
return_str_list.append(notif_str) |
| 1098 |
|
| 1099 |
if nagios_return: |
| 1100 |
return return_str_list |
| 1101 |
else: |
| 1102 |
return "Fail: could not write to the command file" |
| 1103 |
|
| 1104 |
def unsilence_host(self, host): |
| 1105 |
""" |
| 1106 |
This command is used to enable notifications for the host and |
| 1107 |
all services on the specified host. |
| 1108 |
|
| 1109 |
This is equivalent to calling enable_host_svc_notifications |
| 1110 |
and enable_host_notifications. |
| 1111 |
|
| 1112 |
Syntax: ENABLE_HOST_SVC_NOTIFICATIONS;<host_name> |
| 1113 |
Syntax: ENABLE_HOST_NOTIFICATIONS;<host_name> |
| 1114 |
""" |
| 1115 |
|
| 1116 |
cmd = ["ENABLE_HOST_SVC_NOTIFICATIONS", "ENABLE_HOST_NOTIFICATIONS"] |
| 1117 |
nagios_return = True |
| 1118 |
return_str_list = [] |
| 1119 |
for c in cmd: |
| 1120 |
notif_str = self._fmt_notif_str(c, host) |
| 1121 |
nagios_return = self._write_command(notif_str) and nagios_return |
| 1122 |
return_str_list.append(notif_str) |
| 1123 |
|
| 1124 |
if nagios_return: |
| 1125 |
return return_str_list |
| 1126 |
else: |
| 1127 |
return "Fail: could not write to the command file" |
| 1128 |
|
| 1129 |
def silence_nagios(self): |
| 1130 |
""" |
| 1131 |
This command is used to disable notifications for all hosts and services |
| 1132 |
in nagios. |
| 1133 |
|
| 1134 |
This is a 'SHUT UP, NAGIOS' command |
| 1135 |
""" |
| 1136 |
cmd = "DISABLE_NOTIFICATIONS" |
| 1137 |
self._write_command(self._fmt_notif_str(cmd)) |
| 1138 |
|
| 1139 |
def unsilence_nagios(self): |
| 1140 |
""" |
| 1141 |
This command is used to enable notifications for all hosts and services |
| 1142 |
in nagios. |
| 1143 |
|
| 1144 |
This is a 'OK, NAGIOS, GO'' command |
| 1145 |
""" |
| 1146 |
cmd = "ENABLE_NOTIFICATIONS" |
| 1147 |
self._write_command(self._fmt_notif_str(cmd)) |
| 1148 |
|
| 1149 |
def nagios_cmd(self, cmd): |
| 1150 |
""" |
| 1151 |
This sends an arbitrary command to nagios |
| 1152 |
|
| 1153 |
It prepends the submitted time and appends a \n |
| 1154 |
|
| 1155 |
You just have to provide the properly formatted command |
| 1156 |
""" |
| 1157 |
|
| 1158 |
pre = f"[{int(time.time())}]" |
| 1159 |
|
| 1160 |
post = "\n" |
| 1161 |
cmdstr = f"{pre} {cmd}{post}" |
| 1162 |
self._write_command(cmdstr) |
| 1163 |
|
| 1164 |
def act(self): |
| 1165 |
""" |
| 1166 |
Figure out what you want to do from ansible, and then do the |
| 1167 |
needful (at the earliest). |
| 1168 |
""" |
| 1169 |
# host or service downtime? |
| 1170 |
if self.action == "downtime": |
| 1171 |
if self.services == "host": |
| 1172 |
self.schedule_host_downtime(self.host, minutes=self.minutes, start=self.start) |
| 1173 |
elif self.services == "all": |
| 1174 |
self.schedule_host_svc_downtime(self.host, minutes=self.minutes, start=self.start) |
| 1175 |
else: |
| 1176 |
self.schedule_svc_downtime(self.host, services=self.services, minutes=self.minutes, start=self.start) |
| 1177 |
|
| 1178 |
elif self.action == "acknowledge": |
| 1179 |
if self.services == "host": |
| 1180 |
self.acknowledge_host_problem(self.host) |
| 1181 |
else: |
| 1182 |
self.acknowledge_svc_problem(self.host, services=self.services) |
| 1183 |
|
| 1184 |
elif self.action == "delete_downtime": |
| 1185 |
if self.services == "host": |
| 1186 |
self.delete_host_downtime(self.host) |
| 1187 |
elif self.services == "all": |
| 1188 |
self.delete_host_downtime(self.host, comment="") |
| 1189 |
else: |
| 1190 |
self.delete_host_downtime(self.host, services=self.services) |
| 1191 |
|
| 1192 |
elif self.action == "forced_check": |
| 1193 |
if self.services == "host": |
| 1194 |
self.schedule_forced_host_check(self.host) |
| 1195 |
elif self.services == "all": |
| 1196 |
self.schedule_forced_host_svc_check(self.host) |
| 1197 |
else: |
| 1198 |
self.schedule_forced_svc_check(self.host, services=self.services) |
| 1199 |
|
| 1200 |
elif self.action == "servicegroup_host_downtime": |
| 1201 |
if self.servicegroup: |
| 1202 |
self.schedule_servicegroup_host_downtime( |
| 1203 |
servicegroup=self.servicegroup, minutes=self.minutes, start=self.start |
| 1204 |
) |
| 1205 |
elif self.action == "servicegroup_service_downtime": |
| 1206 |
if self.servicegroup: |
| 1207 |
self.schedule_servicegroup_svc_downtime( |
| 1208 |
servicegroup=self.servicegroup, minutes=self.minutes, start=self.start |
| 1209 |
) |
| 1210 |
|
| 1211 |
# toggle the host AND service alerts |
| 1212 |
elif self.action == "silence": |
| 1213 |
self.silence_host(self.host) |
| 1214 |
|
| 1215 |
elif self.action == "unsilence": |
| 1216 |
self.unsilence_host(self.host) |
| 1217 |
|
| 1218 |
# toggle host/svc alerts |
| 1219 |
elif self.action == "enable_alerts": |
| 1220 |
if self.services == "host": |
| 1221 |
self.enable_host_notifications(self.host) |
| 1222 |
elif self.services == "all": |
| 1223 |
self.enable_host_svc_notifications(self.host) |
| 1224 |
else: |
| 1225 |
self.enable_svc_notifications(self.host, services=self.services) |
| 1226 |
|
| 1227 |
elif self.action == "disable_alerts": |
| 1228 |
if self.services == "host": |
| 1229 |
self.disable_host_notifications(self.host) |
| 1230 |
elif self.services == "all": |
| 1231 |
self.disable_host_svc_notifications(self.host) |
| 1232 |
else: |
| 1233 |
self.disable_svc_notifications(self.host, services=self.services) |
| 1234 |
elif self.action == "silence_nagios": |
| 1235 |
self.silence_nagios() |
| 1236 |
|
| 1237 |
elif self.action == "unsilence_nagios": |
| 1238 |
self.unsilence_nagios() |
| 1239 |
|
| 1240 |
else: # self.action == 'command' |
| 1241 |
self.nagios_cmd(self.command) |
| 1242 |
|
| 1243 |
self.module.exit_json(nagios_commands=self.command_results, changed=True) |
| 1244 |
|
| 1245 |
|
| 1246 |
if __name__ == "__main__": |
| 1247 |
main() |