Merge lp:~martin-hilton/charms/trusty/redis/000-nrpe-external-master into lp:~juju-gui/charms/trusty/redis/trunk

Proposed by Martin Hilton
Status: Merged
Merged at revision: 10
Proposed branch: lp:~martin-hilton/charms/trusty/redis/000-nrpe-external-master
Merge into: lp:~juju-gui/charms/trusty/redis/trunk
Diff against target: 246 lines (+142/-2)
9 files modified
config.yaml (+16/-0)
files/check_redis.sh (+7/-0)
hooks/relations.py (+10/-0)
hooks/services.py (+21/-1)
hooks/serviceutils.py (+29/-0)
metadata.yaml (+3/-0)
unit_tests/test_relations.py (+16/-0)
unit_tests/test_services.py (+2/-1)
unit_tests/test_serviceutils.py (+38/-0)
To merge this branch: bzr merge lp:~martin-hilton/charms/trusty/redis/000-nrpe-external-master
Reviewer Review Type Date Requested Status
Francesco Banconi Approve
Review via email: mp+333312@code.launchpad.net

Description of the change

Add an nrpe-external-master relation

To post a comment you must log in.
Revision history for this message
Uros Jovanovic (uros-jovanovic) wrote :

LGTM

I assume check_redis.sh has been tested ...

Revision history for this message
Francesco Banconi (frankban) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2015-07-20 11:52:19 +0000
3+++ config.yaml 2017-11-07 11:14:30 +0000
4@@ -89,3 +89,19 @@
5 for memory-intensive tasks is increased, but there is more risk of memory
6 overload. This option is ignored when the charm is deployed to a local
7 environment.
8+ nagios_context:
9+ default: "juju"
10+ type: string
11+ description: |
12+ Used by the nrpe subordinate charms.
13+ A string that will be prepended to instance name to set the host name
14+ in nagios. So for instance the hostname would be something like:
15+ juju-myservice-0
16+ If you're running multiple environments with the same services in them
17+ this allows you to differentiate between them.
18+ nagios_servicegroups:
19+ default: ""
20+ type: string
21+ description: |
22+ A comma-separated list of nagios servicegroups.
23+ If left empty, the nagios_context will be used as the servicegroup
24
25=== added directory 'files'
26=== added file 'files/check_redis.sh'
27--- files/check_redis.sh 1970-01-01 00:00:00 +0000
28+++ files/check_redis.sh 2017-11-07 11:14:30 +0000
29@@ -0,0 +1,7 @@
30+#!/bin/sh
31+
32+# Check the redis server is running.
33+if redis-cli -h $1 -p $2 ping; then
34+ exit 0
35+fi
36+exit 2
37
38=== added symlink 'hooks/nrpe-external-master-relation-changed'
39=== target is u'generic-hook'
40=== added symlink 'hooks/nrpe-external-master-relation-joined'
41=== target is u'generic-hook'
42=== modified file 'hooks/relations.py'
43--- hooks/relations.py 2015-05-25 10:42:20 +0000
44+++ hooks/relations.py 2017-11-07 11:14:30 +0000
45@@ -40,3 +40,13 @@
46 name = 'slave'
47 interface = 'redis'
48 required_keys = ['hostname', 'port']
49+
50+
51+class NRPERelation(helpers.RelationContext):
52+ """Relation data provider feeding NRPE service configuration."""
53+
54+ name = 'nrpe-external-master'
55+ interface = 'nrpe-external-master'
56+ required_keys = [
57+ "nagios_hostname",
58+ "nagios_host_context"]
59
60=== modified file 'hooks/services.py'
61--- hooks/services.py 2015-07-17 12:29:09 +0000
62+++ hooks/services.py 2017-11-07 11:14:30 +0000
63@@ -42,6 +42,8 @@
64 master_relation = relations.MasterRelation()
65 slave_relation = relations.SlaveRelation()
66 slave_relation_ready = slave_relation.is_ready()
67+ nrpe_relation = relations.NRPERelation()
68+ nrpe_relation_ready = nrpe_relation.is_ready()
69
70 # Set up the service manager.
71 manager = base.ServiceManager([
72@@ -100,6 +102,24 @@
73
74 # Callables called when it is time to stop the service.
75 'stop': [service_stop],
76- }
77+ },
78+ {
79+ # The name of the nrpe "service".
80+ 'service': 'nrpe',
81+
82+ # Data (contexts) required to configure the service.
83+ 'required_data': [config, nrpe_relation_ready],
84+
85+ # Callables called when required data is ready.
86+ 'data_ready': [
87+ serviceutils.install_redis_check(config),
88+ ],
89+
90+ # Callables called when it is time to start the service.
91+ 'start': [],
92+
93+ # Callables called when it is time to stop the service.
94+ 'stop': [],
95+ },
96 ])
97 manager.manage()
98
99=== modified file 'hooks/serviceutils.py'
100--- hooks/serviceutils.py 2015-07-17 12:49:32 +0000
101+++ hooks/serviceutils.py 2017-11-07 11:14:30 +0000
102@@ -7,7 +7,11 @@
103 registering callables in the services framework manager.
104 """
105
106+import os
107+import shutil
108+
109 from charmhelpers import fetch
110+from charmhelpers.contrib.charmsupport import nrpe
111 from charmhelpers.core import (
112 hookenv,
113 host,
114@@ -157,3 +161,28 @@
115 for relation_id in hookenv.relation_ids(name):
116 hookutils.log('Updating data for relation {}.'.format(name))
117 hookenv.relation_set(relation_id, relation.provide_data())
118+
119+
120+_nagios_plugin_path = '/usr/local/lib/nagios/plugins'
121+
122+def install_redis_check(config):
123+ """Write redis NRPE check."""
124+ def callback(service_name):
125+ path = _nagios_plugin_path
126+ host.mkdir(path)
127+ src = os.path.join(hookenv.charm_dir(), 'files', 'check_redis.sh')
128+ check_file = os.path.join(path, 'check_redis.sh')
129+ shutil.copy(src, check_file)
130+ check = '{} {} {}'.format(
131+ check_file,
132+ hookenv.unit_private_ip(),
133+ config["port"])
134+
135+ nrpe_ = nrpe.NRPE()
136+ nrpe_.add_check(
137+ shortname='redis',
138+ description='Check Redis service',
139+ check_cmd=check,
140+ )
141+ nrpe_.write()
142+ return callback
143
144=== modified file 'metadata.yaml'
145--- metadata.yaml 2015-05-25 09:08:47 +0000
146+++ metadata.yaml 2017-11-07 11:14:30 +0000
147@@ -16,6 +16,9 @@
148 interface: redis
149 db:
150 interface: redis
151+ nrpe-external-master:
152+ interface: nrpe-external-master
153+ scope: container
154 requires:
155 slave:
156 interface: redis
157
158=== modified file 'unit_tests/test_relations.py'
159--- unit_tests/test_relations.py 2015-05-25 15:34:53 +0000
160+++ unit_tests/test_relations.py 2017-11-07 11:14:30 +0000
161@@ -52,3 +52,19 @@
162 }
163 self.assertEqual(expected_data, data)
164 mock_unit_get.assert_called_once_with('private-address')
165+
166+
167+class TestNRPERelation(unittest.TestCase):
168+
169+ def setUp(self):
170+ relation_ids_path = 'charmhelpers.core.hookenv.relation_ids'
171+ with mock.patch(relation_ids_path, mock.MagicMock()):
172+ self.relation = relations.NRPERelation()
173+
174+ def test_relation(self):
175+ self.assertEqual(self.relation.name, 'nrpe-external-master')
176+ self.assertEqual(self.relation.interface, 'nrpe-external-master')
177+ self.assertEqual(
178+ self.relation.required_keys,
179+ ["nagios_hostname",
180+ "nagios_host_context"])
181
182=== modified file 'unit_tests/test_services.py'
183--- unit_tests/test_services.py 2015-05-25 15:34:53 +0000
184+++ unit_tests/test_services.py 2017-11-07 11:14:30 +0000
185@@ -25,5 +25,6 @@
186 self.assertEqual(1, mock_manager.call_count)
187 definitions = mock_manager.call_args[0][0]
188 service_names = [i['service'] for i in definitions]
189- self.assertEqual(['redis-master', 'redis-slave'], service_names)
190+ self.assertEqual(['redis-master', 'redis-slave', 'nrpe'],
191+ service_names)
192 mock_config.assert_called_once_with()
193
194=== modified file 'unit_tests/test_serviceutils.py'
195--- unit_tests/test_serviceutils.py 2015-07-17 12:53:23 +0000
196+++ unit_tests/test_serviceutils.py 2017-11-07 11:14:30 +0000
197@@ -3,7 +3,10 @@
198
199 import contextlib
200 from pkg_resources import resource_filename
201+import os
202+import shutil
203 import sys
204+import tempfile
205 import unittest
206
207 import mock
208@@ -494,3 +497,38 @@
209 callback('service')
210 mock_log.assert_called_once_with('Setting up sysctl state.')
211 mock_write_sysctl.assert_called_once_with(overcommit_memory=False)
212+
213+
214+class TestInstallRedisCheck(unittest.TestCase):
215+
216+ def setUp(self):
217+ self.charm_dir = tempfile.mkdtemp()
218+ self.dest = tempfile.mkdtemp()
219+ self.old_nagios_plugin_path = serviceutils._nagios_plugin_path
220+ serviceutils._nagios_plugin_path = self.dest
221+
222+ def tearDown(self):
223+ serviceutils._nagios_plugin_path = self.old_nagios_plugin_path
224+ shutil.rmtree(self.dest)
225+ shutil.rmtree(self.charm_dir)
226+
227+ @mock.patch('charmhelpers.contrib.charmsupport.nrpe.NRPE')
228+ @mock.patch('charmhelpers.core.hookenv.charm_dir')
229+ @mock.patch('charmhelpers.core.hookenv.unit_private_ip')
230+ @mock.patch('charmhelpers.core.host.mkdir')
231+ def test_install_redis_check(self, mock_mkdir, mock_unit_private_ip,
232+ mock_charm_dir, mock_nrpe):
233+ files_path = os.path.join(self.charm_dir, 'files')
234+ os.makedirs(files_path)
235+ with open(os.path.join(files_path, 'check_redis.sh'), 'w') as f:
236+ f.write('TEST1234')
237+ mock_charm_dir.return_value = self.charm_dir
238+ mock_unit_private_ip.return_value = '127.0.0.1'
239+ serviceutils.install_redis_check({"port": 1111})("")
240+ with open(os.path.join(self.dest, 'check_redis.sh')) as f:
241+ self.assertEqual(f.read(), 'TEST1234')
242+ mock_nrpe().add_check.assert_called_with(
243+ shortname='redis',
244+ description='Check Redis service',
245+ check_cmd='{}/check_redis.sh 127.0.0.1 1111'.format(self.dest))
246+ mock_nrpe().write.assert_called_with()

Subscribers

People subscribed via source and target branches