From 0baceda7f6e05cd92976b3e1a9543dc669b6d5f9 Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Tue, 29 Sep 2020 14:31:05 +0200
Subject: [PATCH] nagios: force an active service check for all services of a
 particular 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 9b24b7a96991ed1e2f7d41b84e613e1c8a8359d7)

Co-authored-by: trumbaut <thomas@rumbaut.be>
---
 ..._forced_check_for_all_services_or_host.yml |  3 +
 plugins/modules/monitoring/nagios.py          | 84 +++++++++++++++----
 2 files changed, 69 insertions(+), 18 deletions(-)
 create mode 100644 changelogs/fragments/998-nagios-added_forced_check_for_all_services_or_host.yml

diff --git a/changelogs/fragments/998-nagios-added_forced_check_for_all_services_or_host.yml b/changelogs/fragments/998-nagios-added_forced_check_for_all_services_or_host.yml
new file mode 100644
index 0000000000..bfa1397e08
--- /dev/null
+++ b/changelogs/fragments/998-nagios-added_forced_check_for_all_services_or_host.yml
@@ -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).
diff --git a/plugins/modules/monitoring/nagios.py b/plugins/modules/monitoring/nagios.py
index 6c02b02d1f..379279717d 100644
--- a/plugins/modules/monitoring/nagios.py
+++ b/plugins/modules/monitoring/nagios.py
@@ -33,11 +33,11 @@ options:
       - 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.
@@ -69,7 +69,7 @@ options:
     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:
@@ -157,26 +157,43 @@ EXAMPLES = '''
     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:
@@ -279,7 +296,7 @@ def main():
         'servicegroup_host_downtime',
         'servicegroup_service_downtime',
         'acknowledge',
-        'service_check',
+        'forced_check',
     ]
 
     module = AnsibleModule(
@@ -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')
@@ -569,7 +586,7 @@ class Nagios(object):
 
     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
@@ -585,7 +602,10 @@ class Nagios(object):
         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"
@@ -692,12 +712,35 @@ class Nagios(object):
         ack_cmd_str = self._fmt_ack_str(cmd, host)
         self._write_command(ack_cmd_str)
 
+    def schedule_forced_host_check(self, host):
+        """
+        This command schedules a forced active check for a particular host.
+
+        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 is used to check a particular
-        service .
-
-        Schedules a forced active check of a particular service
+        This command schedules a forced active check for a particular
+        service.
 
         Syntax: SCHEDULE_FORCED_SVC_CHECK;<host_name>;<service_description>;<check_time>
         """
@@ -1193,8 +1236,13 @@ class Nagios(object):
             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: