Merge lp:~jjo/charms/trusty/nova-compute/add-neutron-plugin-nrpe-checks-lp1530227 into lp:~openstack-charmers-archive/charms/trusty/nova-compute/next

Proposed by JuanJo Ciarlante on 2016-01-07
Status: Work in progress
Proposed branch: lp:~jjo/charms/trusty/nova-compute/add-neutron-plugin-nrpe-checks-lp1530227
Merge into: lp:~openstack-charmers-archive/charms/trusty/nova-compute/next
Diff against target: 175 lines (+108/-2)
2 files modified
hooks/charmhelpers/contrib/charmsupport/nrpe.py (+100/-2)
hooks/nova_compute_hooks.py (+8/-0)
To merge this branch: bzr merge lp:~jjo/charms/trusty/nova-compute/add-neutron-plugin-nrpe-checks-lp1530227
Reviewer Review Type Date Requested Status
OpenStack Charmers 2016-01-07 Pending
Review via email: mp+281930@code.launchpad.net
To post a comment you must log in.

Unmerged revisions

188. By JuanJo Ciarlante on 2016-01-07

[jjo] WIP: add NRPE support via NRPESet passed at relation time thru neutron-plugin relation lp#1530227

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/charmhelpers/contrib/charmsupport/nrpe.py'
2--- hooks/charmhelpers/contrib/charmsupport/nrpe.py 2016-01-04 21:29:58 +0000
3+++ hooks/charmhelpers/contrib/charmsupport/nrpe.py 2016-01-07 21:39:25 +0000
4@@ -39,7 +39,11 @@
5 relations_of_type,
6 )
7
8-from charmhelpers.core.host import service
9+from charmhelpers.core.host import (
10+ service,
11+ rsync,
12+)
13+NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
14
15 # This module adds compatibility with the nrpe-external-master and plain nrpe
16 # subordinate charms. To use it in your charm:
17@@ -217,6 +221,38 @@
18 subprocess.call(self.check_cmd)
19
20
21+class Cron(object):
22+ def __init__(self, filename, cron_freq, cron_user, cron_cmd):
23+ super(Cron, self).__init__()
24+ self.filename = filename
25+ self.cron_freq = cron_freq
26+ self.cron_user = cron_user
27+ self.cron_cmd = cron_cmd
28+
29+ def write(self):
30+ cron_filename = '/etc/cron.d/{}'.format(self.filename)
31+ log("cron.write: {}".format(cron_filename))
32+ with open(cron_filename, 'w') as cron_file:
33+ cron_file.write("# cron {}\n".format(self.filename))
34+ cron_file.write("{} {} {}\n".format(self.cron_freq,
35+ self.cron_user,
36+ self.cron_cmd))
37+
38+class NagiosPlugin(object):
39+ def __init__(self, filename):
40+ super(NagiosPlugin, self).__init__()
41+ self.filename = filename
42+
43+ def write(self):
44+ if not os.path.exists(NAGIOS_PLUGINS):
45+ os.makedirs(NAGIOS_PLUGINS)
46+ if os.path.exists(self.filename):
47+ log("NagiosPlugin.write: {} {}".format(self.filename, NAGIOS_PLUGINS))
48+ rsync(self.filename, NAGIOS_PLUGINS)
49+ else:
50+ log("SKIPPED: NagiosPlugin.write: {} {}".format(self.filename, NAGIOS_PLUGINS))
51+
52+
53 class NRPE(object):
54 nagios_logdir = '/var/log/nagios'
55 nagios_exportdir = '/var/lib/nagios/export'
56@@ -240,6 +276,8 @@
57 else:
58 self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
59 self.checks = []
60+ self.crons = []
61+ self.nagios_plugins = []
62
63 def add_check(self, *args, **kwargs):
64 self.checks.append(Check(*args, **kwargs))
65@@ -260,6 +298,24 @@
66 check = Check(*args, **kwargs)
67 check.remove(self.hostname)
68
69+ def add_cron(self, *args, **kwargs):
70+ self.crons.append(Cron(*args, **kwargs))
71+
72+ def add_nagios_plugin(self, *args, **kwargs):
73+ self.nagios_plugins.append(NagiosPlugin(*args, **kwargs))
74+
75+ def add_from_config(self, config_key):
76+ saved_config = config()
77+ nrpe_set = NRPESet(saved_config.get(config_key))
78+ log('NRPE.add_from_config: nrpe_set={}'.format(str(nrpe_set)))
79+ if nrpe_set:
80+ for check in nrpe_set.checks:
81+ self.add_check(*check)
82+ for cron in nrpe_set.crons:
83+ self.add_cron(*cron)
84+ for nagios_plugins in nrpe_set.nagios_plugins:
85+ self.add_nagios_plugin(*nagios_plugins)
86+
87 def write(self):
88 try:
89 nagios_uid = pwd.getpwnam('nagios').pw_uid
90@@ -281,6 +337,12 @@
91 "command": nrpecheck.command,
92 }
93
94+ for cron in self.crons:
95+ cron.write()
96+
97+ for nagios_plugins in self.nagios_plugins:
98+ nagios_plugins.write()
99+
100 service('restart', 'nagios-nrpe-server')
101
102 monitor_ids = relation_ids("local-monitors") + \
103@@ -368,7 +430,6 @@
104 Copy the nrpe checks into place
105
106 """
107- NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
108 nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',
109 'charmhelpers', 'contrib', 'openstack',
110 'files')
111@@ -396,3 +457,40 @@
112 shortname='haproxy_queue',
113 description='Check HAProxy queue depth {%s}' % unit_name,
114 check_cmd='check_haproxy_queue_depth.sh')
115+
116+class NRPESet:
117+ checks = []
118+ crons = []
119+ nagios_plugins = []
120+ def __init__(self, yaml_str='null'):
121+ init_values = None
122+ if yaml_str:
123+ init_values = yaml.safe_load(yaml_str)
124+ log('NRPESet: init_values={}'.format(init_values))
125+ if type(init_values) == type({}):
126+ self.checks = init_values.get('checks', [])
127+ self.crons = init_values.get('crons', [])
128+ self.nagios_plugins = init_values.get('nagios_plugins', [])
129+
130+ def add_check(self, shortname, description, check_cmd):
131+ log('NRPESet: add_check({})'.format((shortname, description, check_cmd)))
132+ self.checks.append((shortname, description, check_cmd))
133+
134+ def add_init_service_checks(self, services, unit_name=None):
135+ if not unit_name:
136+ unit_name = local_unit().replace('/', '-')
137+ log('NRPESet: add_init_service_checks({}, {})'.format(services, unit_name))
138+ add_init_service_checks(self, services, unit_name)
139+
140+ def add_cron(self, filename, cron_freq, cron_user, cron_cmd):
141+ log('NRPESet: add_cron({}, ...)'.format(filename))
142+ self.crons.append((filename, cron_freq, cron_user, cron_cmd))
143+
144+ def add_nagios_plugin(self, filename):
145+ log('NRPESet: add_nagios_plugin({})'.format(filename))
146+ self.nagios_plugins.append((filename,))
147+
148+ def __str__(self):
149+ return yaml.safe_dump({'checks': self.checks,
150+ 'crons': self.crons,
151+ 'nagios_plugins': self.nagios_plugins})
152
153=== modified file 'hooks/nova_compute_hooks.py'
154--- hooks/nova_compute_hooks.py 2015-11-25 12:48:50 +0000
155+++ hooks/nova_compute_hooks.py 2016-01-07 21:39:25 +0000
156@@ -378,6 +378,7 @@
157 current_unit = nrpe.get_nagios_unit_name()
158 nrpe_setup = nrpe.NRPE(hostname=hostname)
159 nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
160+ nrpe_setup.add_from_config('neutron-plugin-nrpe-checks')
161 nrpe_setup.write()
162
163
164@@ -396,6 +397,13 @@
165 apt_install('nova-api-metadata', fatal=True)
166 else:
167 apt_purge('nova-api-metadata', fatal=True)
168+
169+ saved_config = config()
170+ saved_config['neutron-plugin-nrpe-checks'] = settings.get('nrpe-checks')
171+ saved_config.save()
172+ if is_relation_made("nrpe-external-master"):
173+ update_nrpe_config()
174+
175 CONFIGS.write(NOVA_CONF)
176
177

Subscribers

People subscribed via source and target branches