Merge lp:~chad.smith/charms/trusty/nrpe/trunk into lp:charms/trusty/nrpe

Proposed by Chad Smith
Status: Merged
Merged at revision: 39
Proposed branch: lp:~chad.smith/charms/trusty/nrpe/trunk
Merge into: lp:charms/trusty/nrpe
Diff against target: 231 lines (+107/-12)
7 files modified
hooks/nrpe_helpers.py (+7/-0)
hooks/nrpe_utils.py (+13/-0)
hooks/services.py (+1/-0)
tests/10-tests (+8/-8)
tests/11-monitors-configurations (+2/-2)
tests/13-monitors-config (+2/-2)
tests/14-basic-nrpe-external-master (+74/-0)
To merge this branch: bzr merge lp:~chad.smith/charms/trusty/nrpe/trunk
Reviewer Review Type Date Requested Status
Review Queue (community) automated testing Needs Fixing
Chad Smith (community) Abstain
Stuart Bishop (community) Approve
Review via email: mp+284986@code.launchpad.net

Description of the change

This fixes lp:1532281.

When setting nagions_host_type = "host" on nrpe, all related principals need to write the proper hostname in their service files.

Principals related via nrpe-external-master already look in the nrpe-external-master relation for nagios_hostname and nagios_host_context before writing services__ files via calls to charmhelpers/contrib/charmsupport/nrpe:get_nagios_hostname and get_nagios_context.

This branch publishes these two values: nagios_hostname and nagios_host_context as expected by charmhelpers utility functions.

Principals calling get_nagios_hostname will properly write a the host_name in their nagios service__ files matching the host_name definition created by nrpe. This way principals and subordinate nrpe stay synchronized.

to test:
juju deploy local
./tests/14-basic-nrpe-external-master

To post a comment you must log in.
45. By Chad Smith

add comment referencing charmhelpers

Revision history for this message
Stuart Bishop (stub) wrote :

All looks good to me, and tests are fine. Thanks for fixing the unrelated tests too (for Juju 1.25 compatibility).

review: Approve
Revision history for this message
Stuart Bishop (stub) wrote :

There is still a test failing, unless run against a freshly bootstrapped environment, due to a hard coded unit number. This is still landable, as *all* the tests currently fail on trunk unless run against a freshly bootstrapped environment.

$ tests/10-tests
2016-02-04 11:20:37 Starting deployment of local
2016-02-04 11:20:37 Deploying services...
2016-02-04 11:20:37 Deploying service mysql using cs:trusty/mysql-33
2016-02-04 11:20:40 Deploying service nrpe using /tmp/charmqWiN9t/trusty/trunk
2016-02-04 11:21:48 Config specifies num units for subordinate: nrpe
2016-02-04 11:22:14 Adding relations...
2016-02-04 11:22:14 Adding relation nrpe:monitors <-> nagios:monitors
2016-02-04 11:22:14 Adding relation nrpe:local-monitors <-> mysql:local-monitors
2016-02-04 11:23:19 Exposing service 'nagios'
2016-02-04 11:23:19 Deployment complete in 162.00 seconds
.............E..
======================================================================
ERROR: test_nagios_host_context (__main__.TestDeployment)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/10-tests", line 227, in test_nagios_host_context
    self.check_nrpe_setting(**test_config)
  File "tests/10-tests", line 42, in check_nrpe_setting
    nrpe_contents = self.nrpe_sentry.file_contents(filename)
  File "/usr/lib/python2.7/dist-packages/amulet/sentry.py", line 92, in file_contents
    raise IOError(output)
IOError: cat: /var/lib/nagios/export/host__bob-mysql-0.cfg: No such file or directory

----------------------------------------------------------------------
Ran 16 tests in 755.998s

Revision history for this message
Chad Smith (chad.smith) :
review: Abstain
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-lxc/2451/

review: Needs Fixing (automated testing)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/nrpe_helpers.py'
2--- hooks/nrpe_helpers.py 2015-04-21 18:28:48 +0000
3+++ hooks/nrpe_helpers.py 2016-02-03 22:51:16 +0000
4@@ -155,6 +155,13 @@
5 if 'monitors' in self[self.name][0]:
6 return yaml.load(self[self.name][0]['monitors'])
7
8+ def provide_data(self):
9+ # Provide this data to principals because get_nagios_hostname expects
10+ # them in charmhelpers/contrib/charmsupport/nrpe when writing principal
11+ # service__* files
12+ return {'nagios_hostname': self.nagios_hostname(),
13+ 'nagios_host_context': hookenv.config('nagios_host_context')}
14+
15
16 class NagiosInfo(dict):
17 def __init__(self):
18
19=== modified file 'hooks/nrpe_utils.py'
20--- hooks/nrpe_utils.py 2015-07-13 13:02:56 +0000
21+++ hooks/nrpe_utils.py 2016-02-03 22:51:16 +0000
22@@ -120,6 +120,19 @@
23 )
24
25
26+def update_nrpe_external_master_relation(service_name):
27+ """
28+ Send updated nagsios_hostname to charms attached to nrpe_external_master
29+ relation.
30+ """
31+ principle_relation = nrpe_helpers.PrincipleRelation()
32+ for rid in hookenv.relation_ids('nrpe-external-master'):
33+ hookenv.relation_set(
34+ relation_id=rid,
35+ relation_settings=principle_relation.provide_data()
36+ )
37+
38+
39 def update_monitor_relation(service_name):
40 """ Send updated monitor yaml to charms attached to monitor relation """
41 monitor_relation = nrpe_helpers.MonitorsRelation()
42
43=== modified file 'hooks/services.py'
44--- hooks/services.py 2015-07-08 23:43:42 +0000
45+++ hooks/services.py 2016-02-03 22:51:16 +0000
46@@ -26,6 +26,7 @@
47 nrpe_helpers.NagiosInfo(),
48 ],
49 'data_ready': [
50+ nrpe_utils.update_nrpe_external_master_relation,
51 nrpe_utils.update_monitor_relation,
52 nrpe_utils.render_nrped_files,
53 helpers.render_template(
54
55=== modified file 'tests/10-tests'
56--- tests/10-tests 2015-03-23 09:45:10 +0000
57+++ tests/10-tests 2016-02-03 22:51:16 +0000
58@@ -35,7 +35,7 @@
59
60 def check_nrpe_setting(self, filename, expected_settings, juju_kv,
61 filedelim=None):
62- self.nrpe_sentry = self.deployment.sentry.unit['nrpe/0']
63+ self.nrpe_sentry = self.deployment.sentry['nrpe'][0]
64 if juju_kv:
65 self.deployment.configure('nrpe', juju_kv)
66 time.sleep(PAUSE_TIME)
67@@ -56,7 +56,7 @@
68 """ Check default monitor definitions are passed to Nagios """
69 self.deployment.configure('nrpe', {'monitors': ''})
70 time.sleep(PAUSE_TIME)
71- self.nrpe_sentry = self.deployment.sentry.unit['nrpe/0']
72+ self.nrpe_sentry = self.deployment.sentry['nrpe'][0]
73 relation_data = self.nrpe_sentry.relation(
74 'monitors',
75 'nagios:monitors',
76@@ -76,7 +76,7 @@
77
78 def test_monitors_relation_principle_monitors(self):
79 """ Check monitor definitions from principle are passed to Nagios """
80- self.nrpe_sentry = self.deployment.sentry.unit['nrpe/0']
81+ self.nrpe_sentry = self.deployment.sentry['nrpe'][0]
82 relation_data = self.nrpe_sentry.relation(
83 'monitors',
84 'nagios:monitors',
85@@ -134,7 +134,7 @@
86 'nrpe', {'monitors': yaml.dump(user_monitors)}
87 )
88 time.sleep(PAUSE_TIME)
89- self.nrpe_sentry = self.deployment.sentry.unit['nrpe/0']
90+ self.nrpe_sentry = self.deployment.sentry['nrpe'][0]
91 relation_data = self.nrpe_sentry.relation(
92 'monitors',
93 'nagios:monitors',
94@@ -155,8 +155,8 @@
95
96 def test_services(self):
97 """ Test basic services are running """
98- self.nagios_sentry = self.deployment.sentry.unit['nagios/0']
99- self.nrpe_sentry = self.deployment.sentry.unit['nrpe/0']
100+ self.nagios_sentry = self.deployment.sentry['nagios'][0]
101+ self.nrpe_sentry = self.deployment.sentry['nrpe'][0]
102 commands = {
103 self.nrpe_sentry: ['service nagios-nrpe-server status'],
104 self.nagios_sentry: ['service nagios3 status'],
105@@ -166,7 +166,7 @@
106 amulet.raise_status(amulet.FAIL, msg=ret)
107
108 def test_config_nagios_master(self):
109- unit = self.deployment.sentry.unit['nagios/0']
110+ unit = self.deployment.sentry['nagios'][0]
111 nagios_relation = unit.relation('monitors', 'nrpe:monitors')
112 ipaddr = nagios_relation['private-address']
113 test_config = {
114@@ -227,7 +227,7 @@
115 self.check_nrpe_setting(**test_config)
116
117 def test_nagios_hostname_type(self):
118- sentry = self.deployment.sentry.unit['nrpe/0']
119+ sentry = self.deployment.sentry['nrpe'][0]
120 hostname = sentry.run('hostname')[0]
121 test_config = {
122 'filename': '/var/lib/nagios/export/host__%s.cfg' % (hostname),
123
124=== modified file 'tests/11-monitors-configurations'
125--- tests/11-monitors-configurations 2015-04-22 17:40:54 +0000
126+++ tests/11-monitors-configurations 2016-02-03 22:51:16 +0000
127@@ -29,8 +29,8 @@
128 ##
129 # Set relationship aliases
130 ##
131-mysql_unit = d.sentry.unit['mysql/0']
132-nagios_unit = d.sentry.unit['nagios/0']
133+mysql_unit = d.sentry['mysql'][0]
134+nagios_unit = d.sentry['nagios'][0]
135
136
137 def test_nrpe_monitors_config():
138
139=== modified file 'tests/13-monitors-config'
140--- tests/13-monitors-config 2015-04-22 17:40:54 +0000
141+++ tests/13-monitors-config 2016-02-03 22:51:16 +0000
142@@ -41,8 +41,8 @@
143 ##
144 # Set relationship aliases
145 ##
146-mysql_unit = d.sentry.unit['mysql/0']
147-nagios_unit = d.sentry.unit['nagios/0']
148+mysql_unit = d.sentry['mysql'][0]
149+nagios_unit = d.sentry['nagios'][0]
150
151
152 def test_nrpe_monitors_config():
153
154=== added file 'tests/14-basic-nrpe-external-master'
155--- tests/14-basic-nrpe-external-master 1970-01-01 00:00:00 +0000
156+++ tests/14-basic-nrpe-external-master 2016-02-03 22:51:16 +0000
157@@ -0,0 +1,74 @@
158+#!/usr/bin/python
159+
160+import amulet
161+import re
162+import unittest
163+from charmhelpers.contrib.amulet.utils import (
164+ AmuletUtils,
165+)
166+autils = AmuletUtils()
167+
168+
169+class TestBasicNRPEExternalMasterDeployment(unittest.TestCase):
170+ @classmethod
171+ def setUpClass(cls):
172+ cls.deployment = amulet.Deployment(series='trusty')
173+ cls.deployment.add('mysql')
174+ cls.deployment.add('nrpe')
175+ cls.deployment.configure('nrpe', {'nagios_hostname_type': 'unit',
176+ 'nagios_host_context': 'mygroup'})
177+ cls.deployment.configure('mysql', {'dataset-size': '10%'})
178+ cls.deployment.relate('nrpe:nrpe-external-master',
179+ 'mysql:nrpe-external-master')
180+ try:
181+ cls.deployment.setup(timeout=900)
182+ cls.deployment.sentry.wait()
183+ except amulet.helpers.TimeoutError:
184+ msg = "Environment wasn't stood up in time"
185+ amulet.raise_status(amulet.SKIP, msg=msg)
186+ except:
187+ raise
188+
189+ def test_nrpe_external_master_relation(self):
190+ """
191+ Check nagios_hostname and nagions_host_context are passed to principals
192+ """
193+ nrpe_sentry = self.deployment.sentry['nrpe'][0]
194+ mysql = self.deployment.sentry['mysql'][0]
195+ relation = [
196+ 'nrpe-external-master',
197+ 'mysql:nrpe-external-master']
198+ nagios_hostname = "mygroup-{}".format(
199+ mysql.info["unit_name"].replace('/', '-'))
200+ expected = {
201+ 'private-address': autils.valid_ip,
202+ 'nagios_hostname': nagios_hostname,
203+ 'nagios_host_context': 'mygroup'}
204+ ret = autils.validate_relation_data(nrpe_sentry, relation, expected)
205+ if ret:
206+ message = autils.relation_error("nrpe to mysql principal", ret)
207+ amulet.raise_status(amulet.FAIL, msg=message)
208+
209+ def test_exported_nagiosconfig_nrpe_external_master_principal(self):
210+ """
211+ The hostname defined in exported nagios service files matches
212+ nagios_hostname from the nrpe-external-master relation.
213+ """
214+ mysql = self.deployment.sentry['mysql'][0]
215+ nagios_hostname = "mygroup-{}".format(
216+ mysql.info["unit_name"].replace('/', '-'))
217+ mysql_service_file = "service__mygroup-{}_check_mysql.cfg".format(
218+ mysql.info["unit_name"].replace('/', '-'))
219+ content = mysql.file_contents("/var/lib/nagios/export/{}".format(
220+ mysql_service_file))
221+ for line in content.split('\n'):
222+ host_match = re.match('.*host_name\s+([-\w]+)',line)
223+ if host_match:
224+ service_hostname = host_match.groups()[0]
225+ if service_hostname != nagios_hostname:
226+ message = 'Invalid host_name {} in {}. Expected {}'.format(
227+ service_hostname, mysql_service_file, nagios_hostname)
228+ amulet.raise_status(amulet.FAIL, msg=message)
229+
230+if __name__ == '__main__':
231+ unittest.main()

Subscribers

People subscribed via source and target branches

to all changes: