Skip to content

Commit

Permalink
nagios: force an active service check for all services of a particula…
Browse files Browse the repository at this point in the history
…r host or for the host itself (#998) (#1003)

* Update nagios.py

Force an active service check for all services of a particular host or for the host itself

* Create 998-nagios-added_forced_check_for_all_services_or_host.yml

Added fragment

(cherry picked from commit 9b24b7a)

Co-authored-by: trumbaut <[email protected]>
  • Loading branch information
patchback[bot] and trumbaut authored Sep 29, 2020
1 parent c563813 commit 0baceda
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- nagios - rename the ``service_check`` action to ``forced_check`` since we now are able to check both a particular service, all services of a particular host and the host itself (https://github.com/ansible-collections/community.general/pull/998).
- nagios - add the ``host`` and ``all`` values for the ``forced_check`` action (https://github.com/ansible-collections/community.general/pull/998).
84 changes: 66 additions & 18 deletions plugins/modules/monitoring/nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
- Action to take.
- servicegroup options were added in 2.0.
- delete_downtime options were added in 2.2.
- The C(acknowledge) and C(service_check) actions were added in community.general 1.2.0.
- The C(acknowledge) and C(forced_check) actions were added in community.general 1.2.0.
required: true
choices: [ "downtime", "delete_downtime", "enable_alerts", "disable_alerts", "silence", "unsilence",
"silence_nagios", "unsilence_nagios", "command", "servicegroup_service_downtime",
"servicegroup_host_downtime", "acknowledge", "service_check" ]
"servicegroup_host_downtime", "acknowledge", "forced_check" ]
host:
description:
- Host to operate on in Nagios.
Expand Down Expand Up @@ -69,7 +69,7 @@
description:
- What to manage downtime/alerts for. Separate multiple services with commas.
C(service) is an alias for C(services).
B(Required) option when using the C(downtime), C(acknowledge), C(service_check), C(enable_alerts), and C(disable_alerts) actions.
B(Required) option when using the C(downtime), C(acknowledge), C(forced_check), C(enable_alerts), and C(disable_alerts) actions.
aliases: [ "service" ]
required: true
servicegroup:
Expand Down Expand Up @@ -157,26 +157,43 @@
service: host
comment: Planned maintenance
- name: acknowledge an HOST with a particular comment
- name: Acknowledge an HOST with a particular comment
community.general.nagios:
action: acknowledge
service: host
host: '{{ inventory_hostname }}'
comment: 'power outage - see casenr 12345'
- name: acknowledge an active service problem for the httpd service with a particular comment
- name: Acknowledge an active service problem for the httpd service with a particular comment
community.general.nagios:
action: acknowledge
service: httpd
host: '{{ inventory_hostname }}'
comment: 'service crashed - see casenr 12345'
- name: acknowledge an passive service problem for snmp trap with a particular comment
- name: Reset a passive service check for snmp trap
community.general.nagios:
action: service_check
action: forced_check
service: snmp
host: '{{ inventory_hostname }}'
comment: 'switch problem - see casenr 12345'
- name: Force an active service check for the httpd service
community.general.nagios:
action: forced_check
service: httpd
host: '{{ inventory_hostname }}'
- name: Force an active service check for all services of a particular host
community.general.nagios:
action: forced_check
service: all
host: '{{ inventory_hostname }}'
- name: Force an active service check for a particular host
community.general.nagios:
action: forced_check
service: host
host: '{{ inventory_hostname }}'
- name: Enable SMART disk alerts
community.general.nagios:
Expand Down Expand Up @@ -279,7 +296,7 @@ def main():
'servicegroup_host_downtime',
'servicegroup_service_downtime',
'acknowledge',
'service_check',
'forced_check',
]

module = AnsibleModule(
Expand Down Expand Up @@ -354,7 +371,7 @@ def main():
module.fail_json(msg='no service selected to acknowledge')

##################################################################
if action == 'service_check':
if action == 'forced_check':
# Make sure there's an actual service selected
if not services:
module.fail_json(msg='no service selected to check')
Expand Down Expand Up @@ -569,7 +586,7 @@ def _fmt_dt_del_str(self, cmd, host, svc=None, start=None, comment=None):

def _fmt_chk_str(self, cmd, host, svc=None, start=None):
"""
Format an external-command downtime deletion string.
Format an external-command forced host or service check string.
cmd - Nagios command ID
host - Host to check service from
Expand All @@ -585,7 +602,10 @@ def _fmt_chk_str(self, cmd, host, svc=None, start=None):
if start is None:
start = entry_time + 3

chk_args = [svc, str(start)]
if svc is None:
chk_args = [str(start)]
else:
chk_args = [svc, str(start)]

chk_arg_str = ";".join(chk_args)
chk_str = hdr + chk_arg_str + "\n"
Expand Down Expand Up @@ -692,12 +712,35 @@ def acknowledge_host_problem(self, host):
ack_cmd_str = self._fmt_ack_str(cmd, host)
self._write_command(ack_cmd_str)

def schedule_forced_svc_check(self, host, services=None):
def schedule_forced_host_check(self, host):
"""
This command is used to check a particular
service .
This command schedules a forced active check for a particular host.
Schedules a forced active check of a particular service
Syntax: SCHEDULE_FORCED_HOST_CHECK;<host_name>;<check_time>
"""

cmd = "SCHEDULE_FORCED_HOST_CHECK"

chk_cmd_str = self._fmt_chk_str(cmd, host, svc=None)
self._write_command(chk_cmd_str)

def schedule_forced_host_svc_check(self, host):
"""
This command schedules a forced active check for all services
associated with a particular host.
Syntax: SCHEDULE_FORCED_HOST_SVC_CHECKS;<host_name>;<check_time>
"""

cmd = "SCHEDULE_FORCED_HOST_SVC_CHECKS"

chk_cmd_str = self._fmt_chk_str(cmd, host, svc=None)
self._write_command(chk_cmd_str)

def schedule_forced_svc_check(self, host, services=None):
"""
This command schedules a forced active check for a particular
service.
Syntax: SCHEDULE_FORCED_SVC_CHECK;<host_name>;<service_description>;<check_time>
"""
Expand Down Expand Up @@ -1193,8 +1236,13 @@ def act(self):
else:
self.delete_host_downtime(self.host, services=self.services)

elif self.action == 'service_check':
self.schedule_forced_svc_check(self.host, services=self.services)
elif self.action == 'forced_check':
if self.services == 'host':
self.schedule_forced_host_check(self.host)
elif self.services == 'all':
self.schedule_forced_host_svc_check(self.host)
else:
self.schedule_forced_svc_check(self.host, services=self.services)

elif self.action == "servicegroup_host_downtime":
if self.servicegroup:
Expand Down

0 comments on commit 0baceda

Please sign in to comment.