Merge ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master into ~livepatch-charmers/charm-canonical-livepatch:master

Proposed by Barry Price
Status: Merged
Merged at revision: ad6b19b1a62736d33de7e124eb6f4f42859fc03c
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 224 lines (+51/-67)
4 files modified
README.md (+0/-6)
config.yaml (+14/-2)
reactive/canonical_livepatch.py (+11/-57)
tests/99-autogen (+26/-2)
Reviewer Review Type Date Requested Status
Barry Price Abstain
Stuart Bishop (community) Approve
Review via email: mp+315749@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

Also upped the timeout for checks to an hour to cover sub-optimal internet connections and/or cloud environments.

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

Mostly looks good.

I think the interface:nrpe-external-master needs to stay though, commented on inline. I suspect when you tested you had nrpe relation hooks hanging around from a previous build.

review: Approve
Revision history for this message
Barry Price (barryprice) wrote :

Local tests are now passing once again, but I want to reinstate the amulet test for nagios_context/servicegroups now those have been reinstated. Will likely pick this up again Monday.

review: Needs Fixing
Revision history for this message
Barry Price (barryprice) wrote :

All tests are now passing.

Revision history for this message
Barry Price (barryprice) :
review: Abstain

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/README.md b/README.md
2index 4cf16a5..8ac1e4a 100644
3--- a/README.md
4+++ b/README.md
5@@ -24,10 +24,6 @@ Or via juju config (Juju 2.x)
6
7 juju config canonical-livepatch livepatch_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8
9-## Known Limitations and Issues
10-
11-The Nagios nrpe check integration code is a work in progress (but is fully functional)
12-
13 # Configuration
14
15 The livepatch_key setting must be configured in order for the software to function correctly - see above.
16@@ -36,8 +32,6 @@ By default, the livepatch software will attempt to retrieve its patch data direc
17
18 Likewise, if you need to use a proxy server to access the Snap Store, snap_proxy can also be set to the address of your proxy server.
19
20-We set the Nagios servicegroups to 'livepatch' by default, this can also be reconfigured to suit your needs.
21-
22 # Contact Information
23
24 This charm is maintained here, by the livepatch-charmers team:
25diff --git a/config.yaml b/config.yaml
26index 40694b6..298611b 100644
27--- a/config.yaml
28+++ b/config.yaml
29@@ -13,7 +13,19 @@ options:
30 description: |
31 The address of a proxy server to use for livepatch traffic
32 e.g. http://proxy.example.com:3128
33+ nagios_context:
34+ default: "juju"
35+ type: string
36+ description: |
37+ Used by the nrpe-external-master subordinate charm.
38+ A string that will be prepended to instance name to set the host name
39+ in nagios. So for instance the hostname would be something like:
40+ juju-myservice-0
41+ If you're running multiple environments with the same services in them
42+ this allows you to differentiate between them.
43 nagios_servicegroups:
44- default: "livepatch"
45+ default: ""
46 type: string
47- description: "Nagios servicegroup(s) for alerts."
48+ description: >
49+ A comma-separated list of nagios servicegroups.
50+ If left empty, the nagios_context will be used as the servicegroup.
51diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
52index 102df07..4c12fb0 100644
53--- a/reactive/canonical_livepatch.py
54+++ b/reactive/canonical_livepatch.py
55@@ -1,11 +1,11 @@
56 from charms.reactive import when, when_not, set_state
57 from charmhelpers.core.host import write_file
58 from charmhelpers.core import hookenv
59+from charmhelpers.contrib.charmsupport import nrpe
60 from subprocess import check_call, check_output, CalledProcessError
61 from time import sleep
62 from os import path
63 from yaml import load, dump
64-from glob import glob
65
66
67 def file_to_units(local_path, unit_path):
68@@ -102,30 +102,6 @@ def restart_livepatch():
69 check_call(cmd, universal_newlines=True)
70
71
72-def update_nagios_servicedef(service_name, service_value):
73- for service_file in glob("/var/lib/nagios/export/service__*_check_canonical-livepatch.cfg"):
74- with open(service_file, "r+") as fh:
75- output = ''
76- for line in fh:
77- if line.startswith(' {} '.format(service_name)):
78- els = line.split(' ')
79- new_els = []
80- for e in els:
81- if e == '':
82- new_els.append(e)
83- elif e == service_name:
84- new_els.append(e)
85- else:
86- new_els.append(service_value)
87- new_line = ' '.join(new_els) + "\n"
88- output += new_line
89- else:
90- output += line
91- fh.seek(0)
92- fh.write(output)
93- fh.truncate()
94-
95-
96 @when('snap.installed.canonical-livepatch')
97 @when_not('canonical-livepatch.connected')
98 def canonical_livepatch_connect():
99@@ -184,24 +160,14 @@ def update_livepatch_proxy():
100 hookenv.status_set('active', 'Ready')
101
102
103+@when('snap.installed.canonical-livepatch')
104 @when('nrpe-external-master.available')
105 def init_nagios_checks(nagios):
106 hookenv.status_set('maintenance', 'Configuring Nagios checks')
107
108- # We are a subordinate, try to find the primary hostname
109- nagios_hostname = None
110- for hostfile in glob("/var/lib/nagios/export/host__*"):
111- with open(hostfile) as fh:
112- for line in fh:
113- if 'host_name' in line:
114- nagios_hostname = line.split()[1]
115-
116- # Otherwise, fall back to our generated hostname, probably wrong!
117- if nagios_hostname is None:
118- nagios_hostname = hookenv.local_unit()
119-
120- config = hookenv.config()
121- nagios_servicegroups = config.get('nagios_servicegroups')
122+ # Ask charmhelpers.contrib.charmsupport's nrpe to work out our hostname
123+ hostname = nrpe.get_nagios_hostname()
124+ nrpe_setup = nrpe.NRPE(hostname=hostname)
125
126 # install nagios support files
127 file_to_units(
128@@ -219,23 +185,11 @@ def init_nagios_checks(nagios):
129 '/var/lib/nagios/canonical-livepatch-status.txt',
130 )
131
132- # use the interface to create the check
133- nagios.add_check(["/usr/lib/nagios/plugins/check_canonical-livepatch.py", ],
134- name="check_canonical-livepatch",
135- description="Verify canonical-livepatch is working",
136- servicegroups=nagios_servicegroups,
137- unit=nagios_hostname)
138-
139- # clean up after https://github.com/cmars/nrpe-external-master-interface/issues/5
140- update_nagios_servicedef('host_name', nagios_hostname)
141+ # use charmhelpers to create the check
142+ nrpe_setup.add_check('check_canonical-livepatch',
143+ 'Verify canonical-livepatch is working',
144+ '/usr/lib/nagios/plugins/check_canonical-livepatch.py'
145+ )
146
147+ nrpe_setup.write()
148 hookenv.status_set('active', 'Ready')
149-
150-
151-@when('config.changed.nagios_servicegroups')
152-def update_nagios_servicegroups():
153- hookenv.status_set('maintenance', 'Configuring Nagios checks')
154-
155- config = hookenv.config()
156- nagios_servicegroups = config.get('nagios_servicegroups')
157- update_nagios_servicedef('servicegroups', nagios_servicegroups)
158diff --git a/tests/99-autogen b/tests/99-autogen
159index 1527d4f..c57bbf4 100755
160--- a/tests/99-autogen
161+++ b/tests/99-autogen
162@@ -2,6 +2,7 @@
163
164 import amulet
165 import unittest
166+from time import sleep
167
168
169 class TestDeployment(unittest.TestCase):
170@@ -26,7 +27,7 @@ class TestDeployment(unittest.TestCase):
171 cls.deployment.relate('canonical-livepatch:nrpe-external-master', 'nrpe:nrpe-external-master')
172
173 try:
174- cls.deployment.setup(timeout=900)
175+ cls.deployment.setup(timeout=3600)
176 cls.deployment.sentry.wait()
177 except amulet.helpers.TimeoutError:
178 amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")
179@@ -59,21 +60,44 @@ class TestDeployment(unittest.TestCase):
180 output, exit_code = livepatch.run('stat {}'.format(path))
181 self.assertEqual(exit_code, 0)
182
183+ def test_nagios_context_change(self):
184+ livepatch = self.deployment.sentry['canonical-livepatch'][0]
185+
186+ test_context_name = 'amulet1'
187+
188+ # set context name
189+ self.deployment.configure('canonical-livepatch', {
190+ 'nagios_context': test_context_name,
191+ })
192+
193+ # nrpe updates can take a while
194+ sleep(30)
195+
196+ # confirm it showed up in the right place
197+ output, exit_code = livepatch.run(
198+ 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_context_name)
199+ )
200+ self.assertEqual(exit_code, 0)
201+
202 def test_nagios_servicegroup_change(self):
203 livepatch = self.deployment.sentry['canonical-livepatch'][0]
204
205- test_servicegroup_name = 'amulet'
206+ test_servicegroup_name = 'amulet2'
207
208 # set servicegroup name
209 self.deployment.configure('canonical-livepatch', {
210 'nagios_servicegroups': test_servicegroup_name,
211 })
212
213+ # nrpe updates can take a while
214+ sleep(30)
215+
216 # confirm it showed up in the right place
217 output, exit_code = livepatch.run(
218 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_servicegroup_name)
219 )
220 self.assertEqual(exit_code, 0)
221
222+
223 if __name__ == '__main__':
224 unittest.main()

Subscribers

People subscribed via source and target branches