Merge ~paulgear/charm-nrpe/+git/nrpe-charm:master into ~nrpe-charmers/charm-nrpe:master

Proposed by Paul Gear
Status: Merged
Merged at revision: 10198d38461b550b406ca945947533318fd71c8b
Proposed branch: ~paulgear/charm-nrpe/+git/nrpe-charm:master
Merge into: ~nrpe-charmers/charm-nrpe:master
Diff against target: 87 lines (+30/-9)
2 files modified
hooks/nrpe_utils.py (+28/-2)
hooks/services.py (+2/-7)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
William Grant Approve
Review via email: mp+323818@code.launchpad.net

Description of the change

I spent a sizable chunk of a large ticket a few months back debugging why adding a relation made the nrpe charm generate incorrect hostnames when it had previously had correct ones. I think this is due to a timing issue because of when the nagios hostname is obtained vs. when it is used, and this change seems to help, but I'm not familiar enough with the services framework to know whether this is just luck.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

This seems like a reasonable change. I'm not sure why the host definition would need a rewrite on nrpe-config, but it empirically has seemed to be the case in the past, and neither change can make things worse.

review: Approve
Revision history for this message
Paul Gear (paulgear) wrote :

This branch is still buggy; I've tested it and it causes host files to be removed on upgrade - still trying to work out why.

Revision history for this message
Paul Gear (paulgear) wrote :

I've pushed a new revision which fixes the host export file removal issue, and tested deploy on xenial and upgrade-charm on trusty & xenial.

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

'ick. I'd forgotten service manager.

review: Approve
Revision history for this message
Paul Gear (paulgear) wrote :

This might fix lp:1671707

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/nrpe_utils.py b/hooks/nrpe_utils.py
2index 89f8092..ae83158 100644
3--- a/hooks/nrpe_utils.py
4+++ b/hooks/nrpe_utils.py
5@@ -4,8 +4,10 @@ import glob
6
7 from charmhelpers import fetch
8 from charmhelpers.core import host
9-from charmhelpers.core.templating import render
10 from charmhelpers.core import hookenv
11+from charmhelpers.core.services import helpers
12+from charmhelpers.core.services.base import ManagerCallback
13+from charmhelpers.core.templating import render
14
15 import nrpe_helpers
16
17@@ -115,7 +117,7 @@ def process_local_monitors():
18
19 def update_nrpe_external_master_relation(service_name):
20 """
21- Send updated nagsios_hostname to charms attached to nrpe_external_master
22+ Send updated nagios_hostname to charms attached to nrpe_external_master
23 relation.
24 """
25 principle_relation = nrpe_helpers.PrincipleRelation()
26@@ -134,3 +136,27 @@ def update_monitor_relation(service_name):
27 relation_id=rid,
28 relation_settings=monitor_relation.provide_data()
29 )
30+
31+
32+class ExportManagerCallback(ManagerCallback):
33+
34+ """
35+ This class exists in order to defer lookup of nagios_hostname()
36+ until the template is ready to be rendered. This should reduce the
37+ incidence of incorrectly-rendered hostnames in /var/lib/nagios/exports.
38+ See charmhelpers.core.services.base.ManagerCallback and
39+ charmhelpers.core.services.helpers.TemplateCallback for more background.
40+ """
41+
42+ def __call__(self, manager, service_name, event_name):
43+ nag_hostname = nrpe_helpers.PrincipleRelation().nagios_hostname()
44+ target = '/var/lib/nagios/export/host__{}.cfg'.format(nag_hostname)
45+ renderer = helpers.render_template(
46+ source='export_host.cfg.tmpl',
47+ target=target,
48+ perms=0o644,
49+ )
50+ renderer(manager, service_name, event_name)
51+
52+
53+create_host_export_fragment = ExportManagerCallback()
54diff --git a/hooks/services.py b/hooks/services.py
55index 9034981..543bcc6 100644
56--- a/hooks/services.py
57+++ b/hooks/services.py
58@@ -8,7 +8,6 @@ import nrpe_helpers
59
60 def manage():
61 config = hookenv.config()
62- nag_hostname = nrpe_helpers.PrincipleRelation().nagios_hostname()
63 manager = ServiceManager([
64 {
65 'service': 'nrpe-install',
66@@ -28,6 +27,7 @@ def manage():
67 'data_ready': [
68 nrpe_utils.update_nrpe_external_master_relation,
69 nrpe_utils.update_monitor_relation,
70+ nrpe_utils.create_host_export_fragment,
71 nrpe_utils.render_nrped_files,
72 helpers.render_template(
73 source='nrpe.tmpl',
74@@ -51,12 +51,7 @@ def manage():
75 source='rsync-juju.d.tmpl',
76 target='/etc/rsync-juju.d/010-nrpe-external-master.conf'
77 ),
78- helpers.render_template(
79- source='export_host.cfg.tmpl',
80- target='/var/lib/nagios/export/'
81- 'host__{}.cfg'.format(nag_hostname),
82- perms=0o644,
83- ),
84+ nrpe_utils.create_host_export_fragment,
85 ],
86 'start': [nrpe_utils.restart_rsync],
87 },

Subscribers

People subscribed via source and target branches