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
=== modified file 'config.yaml'
--- config.yaml 2015-07-20 11:52:19 +0000
+++ config.yaml 2017-11-07 11:14:30 +0000
@@ -89,3 +89,19 @@
89 for memory-intensive tasks is increased, but there is more risk of memory89 for memory-intensive tasks is increased, but there is more risk of memory
90 overload. This option is ignored when the charm is deployed to a local90 overload. This option is ignored when the charm is deployed to a local
91 environment.91 environment.
92 nagios_context:
93 default: "juju"
94 type: string
95 description: |
96 Used by the nrpe subordinate charms.
97 A string that will be prepended to instance name to set the host name
98 in nagios. So for instance the hostname would be something like:
99 juju-myservice-0
100 If you're running multiple environments with the same services in them
101 this allows you to differentiate between them.
102 nagios_servicegroups:
103 default: ""
104 type: string
105 description: |
106 A comma-separated list of nagios servicegroups.
107 If left empty, the nagios_context will be used as the servicegroup
92108
=== added directory 'files'
=== added file 'files/check_redis.sh'
--- files/check_redis.sh 1970-01-01 00:00:00 +0000
+++ files/check_redis.sh 2017-11-07 11:14:30 +0000
@@ -0,0 +1,7 @@
1#!/bin/sh
2
3# Check the redis server is running.
4if redis-cli -h $1 -p $2 ping; then
5 exit 0
6fi
7exit 2
08
=== added symlink 'hooks/nrpe-external-master-relation-changed'
=== target is u'generic-hook'
=== added symlink 'hooks/nrpe-external-master-relation-joined'
=== target is u'generic-hook'
=== modified file 'hooks/relations.py'
--- hooks/relations.py 2015-05-25 10:42:20 +0000
+++ hooks/relations.py 2017-11-07 11:14:30 +0000
@@ -40,3 +40,13 @@
40 name = 'slave'40 name = 'slave'
41 interface = 'redis'41 interface = 'redis'
42 required_keys = ['hostname', 'port']42 required_keys = ['hostname', 'port']
43
44
45class NRPERelation(helpers.RelationContext):
46 """Relation data provider feeding NRPE service configuration."""
47
48 name = 'nrpe-external-master'
49 interface = 'nrpe-external-master'
50 required_keys = [
51 "nagios_hostname",
52 "nagios_host_context"]
4353
=== modified file 'hooks/services.py'
--- hooks/services.py 2015-07-17 12:29:09 +0000
+++ hooks/services.py 2017-11-07 11:14:30 +0000
@@ -42,6 +42,8 @@
42 master_relation = relations.MasterRelation()42 master_relation = relations.MasterRelation()
43 slave_relation = relations.SlaveRelation()43 slave_relation = relations.SlaveRelation()
44 slave_relation_ready = slave_relation.is_ready()44 slave_relation_ready = slave_relation.is_ready()
45 nrpe_relation = relations.NRPERelation()
46 nrpe_relation_ready = nrpe_relation.is_ready()
4547
46 # Set up the service manager.48 # Set up the service manager.
47 manager = base.ServiceManager([49 manager = base.ServiceManager([
@@ -100,6 +102,24 @@
100102
101 # Callables called when it is time to stop the service.103 # Callables called when it is time to stop the service.
102 'stop': [service_stop],104 'stop': [service_stop],
103 }105 },
106 {
107 # The name of the nrpe "service".
108 'service': 'nrpe',
109
110 # Data (contexts) required to configure the service.
111 'required_data': [config, nrpe_relation_ready],
112
113 # Callables called when required data is ready.
114 'data_ready': [
115 serviceutils.install_redis_check(config),
116 ],
117
118 # Callables called when it is time to start the service.
119 'start': [],
120
121 # Callables called when it is time to stop the service.
122 'stop': [],
123 },
104 ])124 ])
105 manager.manage()125 manager.manage()
106126
=== modified file 'hooks/serviceutils.py'
--- hooks/serviceutils.py 2015-07-17 12:49:32 +0000
+++ hooks/serviceutils.py 2017-11-07 11:14:30 +0000
@@ -7,7 +7,11 @@
7registering callables in the services framework manager.7registering callables in the services framework manager.
8"""8"""
99
10import os
11import shutil
12
10from charmhelpers import fetch13from charmhelpers import fetch
14from charmhelpers.contrib.charmsupport import nrpe
11from charmhelpers.core import (15from charmhelpers.core import (
12 hookenv,16 hookenv,
13 host,17 host,
@@ -157,3 +161,28 @@
157 for relation_id in hookenv.relation_ids(name):161 for relation_id in hookenv.relation_ids(name):
158 hookutils.log('Updating data for relation {}.'.format(name))162 hookutils.log('Updating data for relation {}.'.format(name))
159 hookenv.relation_set(relation_id, relation.provide_data())163 hookenv.relation_set(relation_id, relation.provide_data())
164
165
166_nagios_plugin_path = '/usr/local/lib/nagios/plugins'
167
168def install_redis_check(config):
169 """Write redis NRPE check."""
170 def callback(service_name):
171 path = _nagios_plugin_path
172 host.mkdir(path)
173 src = os.path.join(hookenv.charm_dir(), 'files', 'check_redis.sh')
174 check_file = os.path.join(path, 'check_redis.sh')
175 shutil.copy(src, check_file)
176 check = '{} {} {}'.format(
177 check_file,
178 hookenv.unit_private_ip(),
179 config["port"])
180
181 nrpe_ = nrpe.NRPE()
182 nrpe_.add_check(
183 shortname='redis',
184 description='Check Redis service',
185 check_cmd=check,
186 )
187 nrpe_.write()
188 return callback
160189
=== modified file 'metadata.yaml'
--- metadata.yaml 2015-05-25 09:08:47 +0000
+++ metadata.yaml 2017-11-07 11:14:30 +0000
@@ -16,6 +16,9 @@
16 interface: redis16 interface: redis
17 db:17 db:
18 interface: redis18 interface: redis
19 nrpe-external-master:
20 interface: nrpe-external-master
21 scope: container
19requires:22requires:
20 slave:23 slave:
21 interface: redis24 interface: redis
2225
=== modified file 'unit_tests/test_relations.py'
--- unit_tests/test_relations.py 2015-05-25 15:34:53 +0000
+++ unit_tests/test_relations.py 2017-11-07 11:14:30 +0000
@@ -52,3 +52,19 @@
52 }52 }
53 self.assertEqual(expected_data, data)53 self.assertEqual(expected_data, data)
54 mock_unit_get.assert_called_once_with('private-address')54 mock_unit_get.assert_called_once_with('private-address')
55
56
57class TestNRPERelation(unittest.TestCase):
58
59 def setUp(self):
60 relation_ids_path = 'charmhelpers.core.hookenv.relation_ids'
61 with mock.patch(relation_ids_path, mock.MagicMock()):
62 self.relation = relations.NRPERelation()
63
64 def test_relation(self):
65 self.assertEqual(self.relation.name, 'nrpe-external-master')
66 self.assertEqual(self.relation.interface, 'nrpe-external-master')
67 self.assertEqual(
68 self.relation.required_keys,
69 ["nagios_hostname",
70 "nagios_host_context"])
5571
=== modified file 'unit_tests/test_services.py'
--- unit_tests/test_services.py 2015-05-25 15:34:53 +0000
+++ unit_tests/test_services.py 2017-11-07 11:14:30 +0000
@@ -25,5 +25,6 @@
25 self.assertEqual(1, mock_manager.call_count)25 self.assertEqual(1, mock_manager.call_count)
26 definitions = mock_manager.call_args[0][0]26 definitions = mock_manager.call_args[0][0]
27 service_names = [i['service'] for i in definitions]27 service_names = [i['service'] for i in definitions]
28 self.assertEqual(['redis-master', 'redis-slave'], service_names)28 self.assertEqual(['redis-master', 'redis-slave', 'nrpe'],
29 service_names)
29 mock_config.assert_called_once_with()30 mock_config.assert_called_once_with()
3031
=== modified file 'unit_tests/test_serviceutils.py'
--- unit_tests/test_serviceutils.py 2015-07-17 12:53:23 +0000
+++ unit_tests/test_serviceutils.py 2017-11-07 11:14:30 +0000
@@ -3,7 +3,10 @@
33
4import contextlib4import contextlib
5from pkg_resources import resource_filename5from pkg_resources import resource_filename
6import os
7import shutil
6import sys8import sys
9import tempfile
7import unittest10import unittest
811
9import mock12import mock
@@ -494,3 +497,38 @@
494 callback('service')497 callback('service')
495 mock_log.assert_called_once_with('Setting up sysctl state.')498 mock_log.assert_called_once_with('Setting up sysctl state.')
496 mock_write_sysctl.assert_called_once_with(overcommit_memory=False)499 mock_write_sysctl.assert_called_once_with(overcommit_memory=False)
500
501
502class TestInstallRedisCheck(unittest.TestCase):
503
504 def setUp(self):
505 self.charm_dir = tempfile.mkdtemp()
506 self.dest = tempfile.mkdtemp()
507 self.old_nagios_plugin_path = serviceutils._nagios_plugin_path
508 serviceutils._nagios_plugin_path = self.dest
509
510 def tearDown(self):
511 serviceutils._nagios_plugin_path = self.old_nagios_plugin_path
512 shutil.rmtree(self.dest)
513 shutil.rmtree(self.charm_dir)
514
515 @mock.patch('charmhelpers.contrib.charmsupport.nrpe.NRPE')
516 @mock.patch('charmhelpers.core.hookenv.charm_dir')
517 @mock.patch('charmhelpers.core.hookenv.unit_private_ip')
518 @mock.patch('charmhelpers.core.host.mkdir')
519 def test_install_redis_check(self, mock_mkdir, mock_unit_private_ip,
520 mock_charm_dir, mock_nrpe):
521 files_path = os.path.join(self.charm_dir, 'files')
522 os.makedirs(files_path)
523 with open(os.path.join(files_path, 'check_redis.sh'), 'w') as f:
524 f.write('TEST1234')
525 mock_charm_dir.return_value = self.charm_dir
526 mock_unit_private_ip.return_value = '127.0.0.1'
527 serviceutils.install_redis_check({"port": 1111})("")
528 with open(os.path.join(self.dest, 'check_redis.sh')) as f:
529 self.assertEqual(f.read(), 'TEST1234')
530 mock_nrpe().add_check.assert_called_with(
531 shortname='redis',
532 description='Check Redis service',
533 check_cmd='{}/check_redis.sh 127.0.0.1 1111'.format(self.dest))
534 mock_nrpe().write.assert_called_with()

Subscribers

People subscribed via source and target branches