Merge ~canonical-is-bootstack/charm-nagios:rebase/bug/1894912 into charm-nagios:master

Proposed by Alvaro Uria
Status: Merged
Merged at revision: 0a14c13c7d7b02865ef2b43776e4991ced246499
Proposed branch: ~canonical-is-bootstack/charm-nagios:rebase/bug/1894912
Merge into: charm-nagios:master
Diff against target: 112 lines (+58/-3)
4 files modified
config.yaml (+26/-0)
hooks/templates/traps.tmpl (+2/-2)
hooks/upgrade_charm.py (+9/-1)
tests/functional/test_config.py (+21/-0)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Approve
Review via email: mp+393600@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Alvaro Uria (aluria) wrote :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index 54fd58d..1b6d739 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -289,6 +289,32 @@ options:
6 description: |
7 Defines the IP or Host Name to send snmp traps to. Leave blank (empty) to disable
8 the traps functionality.
9+ traps_service_notification_options:
10+ default: "w,u,c,r"
11+ type: string
12+ description: |
13+ This directive is used to define the service states for which notifications
14+ can be sent out to the default traps contact. Valid options are a combination of one
15+ or more of the following:
16+ w = notify on WARNING service states,
17+ u = notify on UNKNOWN service states,
18+ c = notify on CRITICAL service states,
19+ r = notify on service recoveries (OK states),
20+ f = notify when the service starts and stops flapping.
21+ If you specify n (none) as an option, the contact will not receive any type of service notifications.
22+ traps_host_notification_options:
23+ default: "d,r"
24+ type: string
25+ description: |
26+ This directive is used to define the host states for which notifications
27+ can be sent out to the default traps contact. Valid options are a combination of one
28+ or more of the following:
29+ d = notify on DOWN host states,
30+ u = notify on UNREACHABLE host states,
31+ r = notify on host recoveries (UP states),
32+ f = notify when the host starts and stops flapping,
33+ s = send notifications when host or service scheduled downtime starts and ends.
34+ If you specify n (none) as an option, the contact will not receive any type of host notifications.
35 extra_contacts:
36 default: ''
37 type: string
38diff --git a/hooks/templates/traps.tmpl b/hooks/templates/traps.tmpl
39index d1874e6..c850684 100644
40--- a/hooks/templates/traps.tmpl
41+++ b/hooks/templates/traps.tmpl
42@@ -14,8 +14,8 @@ define contact{
43 alias Management Station
44 service_notification_period 24x7
45 host_notification_period 24x7
46- service_notification_options w,u,c,r,f,s
47- host_notification_options d,u,r,f,s
48+ service_notification_options {{ traps_service_notification_options }}
49+ host_notification_options {{ traps_host_notification_options }}
50 service_notification_commands send-service-trap
51 host_notification_commands send-host-trap
52 }
53diff --git a/hooks/upgrade_charm.py b/hooks/upgrade_charm.py
54index 91b4b69..ecf4c68 100755
55--- a/hooks/upgrade_charm.py
56+++ b/hooks/upgrade_charm.py
57@@ -280,7 +280,15 @@ def enable_traps_config():
58 if "managementstation" not in contactgroup_members:
59 forced_contactgroup_members.append("managementstation")
60
61- template_values = {"send_traps_to": send_traps_to}
62+ template_values = {
63+ "send_traps_to": send_traps_to,
64+ "traps_service_notification_options": hookenv.config(
65+ "traps_service_notification_options"
66+ ),
67+ "traps_host_notification_options": hookenv.config(
68+ "traps_host_notification_options"
69+ ),
70+ }
71
72 with open("hooks/templates/traps.tmpl", "r") as f:
73 template_def = f.read()
74diff --git a/tests/functional/test_config.py b/tests/functional/test_config.py
75index 6bc36a6..5b5423f 100644
76--- a/tests/functional/test_config.py
77+++ b/tests/functional/test_config.py
78@@ -68,6 +68,17 @@ async def enable_pagerduty(unit):
79 yield app_config["pagerduty_path"]["value"]
80
81
82+@pytest.fixture()
83+async def enable_snmp_traps(unit):
84+ """Set send_traps_to before first test, then disable after last test.
85+
86+ :param Agent unit: unit from the fixture
87+ """
88+ async with config(unit, "send_traps_to", "127.0.0.1", ""):
89+ app_config = await unit.application.get_config()
90+ yield app_config["send_traps_to"]["value"]
91+
92+
93 @pytest.fixture
94 async def set_extra_contacts(unit):
95 """Set extra contacts."""
96@@ -129,6 +140,16 @@ async def test_pager_duty(unit, enable_pagerduty, file_stat):
97 assert stat["size"] != 0, "pagerduty_config wasn't a non-zero sized file"
98
99
100+async def test_snmp_traps(unit, enable_snmp_traps, file_stat, file_contents):
101+ traps_cfg_path = "/etc/nagios3/conf.d/traps.cfg"
102+ stat = await file_stat(traps_cfg_path, unit.u)
103+ assert stat["size"] != 0, "snmp traps config wasn't a non-zero sized file"
104+ traps_cfg_content = await file_contents(traps_cfg_path, unit.u)
105+ assert (
106+ enable_snmp_traps in traps_cfg_content
107+ ), "snmp traps target missing from traps cfg"
108+
109+
110 async def test_extra_contacts(auth, unit, set_extra_contacts):
111 contancts_url = (
112 "http://%s/cgi-bin/nagios3/config.cgi?type=contacts" % unit.u.public_address

Subscribers

People subscribed via source and target branches

to all changes: