← Back to team overview

nagios-charmers team mailing list archive

[Merge] ~jfguedez/charm-nagios:bug/1894912 into charm-nagios:master

 

Jose Guedez has proposed merging ~jfguedez/charm-nagios:bug/1894912 into charm-nagios:master.

Requested reviews:
  Nagios Charm developers (nagios-charmers)
Related bugs:
  Bug #1894912 in Nagios Charm: "make *_notification_options configurable for SNMP traps"
  https://bugs.launchpad.net/charm-nagios/+bug/1894912

For more details, see:
https://code.launchpad.net/~jfguedez/charm-nagios/+git/charm-nagios/+merge/390591
-- 
Your team Nagios Charm developers is requested to review the proposed merge of ~jfguedez/charm-nagios:bug/1894912 into charm-nagios:master.
diff --git a/config.yaml b/config.yaml
index 54fd58d..1b6d739 100644
--- a/config.yaml
+++ b/config.yaml
@@ -289,6 +289,32 @@ options:
         description: |
             Defines the IP or Host Name to send snmp traps to. Leave blank (empty) to disable
             the traps functionality.
+    traps_service_notification_options:
+        default: "w,u,c,r"
+        type: string
+        description: |
+            This directive is used to define the service states for which notifications
+            can be sent out to the default traps contact.  Valid options are a combination of one
+            or more of the following:
+            w = notify on WARNING service states,
+            u = notify on UNKNOWN service states,
+            c = notify on CRITICAL service states,
+            r = notify on service recoveries (OK states),
+            f = notify when the service starts and stops flapping.
+            If you specify n (none) as an option, the contact will not receive any type of service notifications.
+    traps_host_notification_options:
+        default: "d,r"
+        type: string
+        description: |
+            This directive is used to define the host states for which notifications
+            can be sent out to the default traps contact.  Valid options are a combination of one
+            or more of the following:
+            d = notify on DOWN host states,
+            u = notify on UNREACHABLE host states,
+            r = notify on host recoveries (UP states),
+            f = notify when the host starts and stops flapping,
+            s = send notifications when host or service scheduled downtime starts and ends.
+            If you specify n (none) as an option, the contact will not receive any type of host notifications.
     extra_contacts:
         default: ''
         type: string
diff --git a/hooks/templates/traps.tmpl b/hooks/templates/traps.tmpl
index d1874e6..c850684 100644
--- a/hooks/templates/traps.tmpl
+++ b/hooks/templates/traps.tmpl
@@ -14,8 +14,8 @@ define contact{
         alias                           Management Station
         service_notification_period     24x7
         host_notification_period        24x7
-        service_notification_options    w,u,c,r,f,s
-        host_notification_options       d,u,r,f,s
+        service_notification_options    {{ traps_service_notification_options }}
+        host_notification_options       {{ traps_host_notification_options }}
         service_notification_commands   send-service-trap
         host_notification_commands      send-host-trap
 }
diff --git a/hooks/upgrade_charm.py b/hooks/upgrade_charm.py
index ca410ef..025a335 100755
--- a/hooks/upgrade_charm.py
+++ b/hooks/upgrade_charm.py
@@ -272,7 +272,15 @@ def enable_traps_config():
     if "managementstation" not in contactgroup_members:
         forced_contactgroup_members.append("managementstation")
 
-    template_values = {"send_traps_to": send_traps_to}
+    template_values = {
+        "send_traps_to": send_traps_to,
+        "traps_service_notification_options": hookenv.config(
+            "traps_service_notification_options"
+        ),
+        "traps_host_notification_options": hookenv.config(
+            "traps_host_notification_options"
+        ),
+    }
 
     with open("hooks/templates/traps.tmpl", "r") as f:
         template_def = f.read()
diff --git a/tests/functional/test_config.py b/tests/functional/test_config.py
index 6bc36a6..5b5423f 100644
--- a/tests/functional/test_config.py
+++ b/tests/functional/test_config.py
@@ -68,6 +68,17 @@ async def enable_pagerduty(unit):
         yield app_config["pagerduty_path"]["value"]
 
 
+@pytest.fixture()
+async def enable_snmp_traps(unit):
+    """Set send_traps_to before first test, then disable after last test.
+
+    :param Agent unit:              unit from the fixture
+    """
+    async with config(unit, "send_traps_to", "127.0.0.1", ""):
+        app_config = await unit.application.get_config()
+        yield app_config["send_traps_to"]["value"]
+
+
 @pytest.fixture
 async def set_extra_contacts(unit):
     """Set extra contacts."""
@@ -129,6 +140,16 @@ async def test_pager_duty(unit, enable_pagerduty, file_stat):
     assert stat["size"] != 0, "pagerduty_config wasn't a non-zero sized file"
 
 
+async def test_snmp_traps(unit, enable_snmp_traps, file_stat, file_contents):
+    traps_cfg_path = "/etc/nagios3/conf.d/traps.cfg"
+    stat = await file_stat(traps_cfg_path, unit.u)
+    assert stat["size"] != 0, "snmp traps config wasn't a non-zero sized file"
+    traps_cfg_content = await file_contents(traps_cfg_path, unit.u)
+    assert (
+        enable_snmp_traps in traps_cfg_content
+    ), "snmp traps target missing from traps cfg"
+
+
 async def test_extra_contacts(auth, unit, set_extra_contacts):
     contancts_url = (
         "http://%s/cgi-bin/nagios3/config.cgi?type=contacts"; % unit.u.public_address

Follow ups