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
=== modified file 'hooks/charmhelpers/contrib/charmsupport/nrpe.py'
--- hooks/charmhelpers/contrib/charmsupport/nrpe.py 2016-01-04 21:29:58 +0000
+++ hooks/charmhelpers/contrib/charmsupport/nrpe.py 2016-01-07 21:39:25 +0000
@@ -39,7 +39,11 @@
39 relations_of_type,39 relations_of_type,
40)40)
4141
42from charmhelpers.core.host import service42from charmhelpers.core.host import (
43 service,
44 rsync,
45)
46NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
4347
44# This module adds compatibility with the nrpe-external-master and plain nrpe48# This module adds compatibility with the nrpe-external-master and plain nrpe
45# subordinate charms. To use it in your charm:49# subordinate charms. To use it in your charm:
@@ -217,6 +221,38 @@
217 subprocess.call(self.check_cmd)221 subprocess.call(self.check_cmd)
218222
219223
224class Cron(object):
225 def __init__(self, filename, cron_freq, cron_user, cron_cmd):
226 super(Cron, self).__init__()
227 self.filename = filename
228 self.cron_freq = cron_freq
229 self.cron_user = cron_user
230 self.cron_cmd = cron_cmd
231
232 def write(self):
233 cron_filename = '/etc/cron.d/{}'.format(self.filename)
234 log("cron.write: {}".format(cron_filename))
235 with open(cron_filename, 'w') as cron_file:
236 cron_file.write("# cron {}\n".format(self.filename))
237 cron_file.write("{} {} {}\n".format(self.cron_freq,
238 self.cron_user,
239 self.cron_cmd))
240
241class NagiosPlugin(object):
242 def __init__(self, filename):
243 super(NagiosPlugin, self).__init__()
244 self.filename = filename
245
246 def write(self):
247 if not os.path.exists(NAGIOS_PLUGINS):
248 os.makedirs(NAGIOS_PLUGINS)
249 if os.path.exists(self.filename):
250 log("NagiosPlugin.write: {} {}".format(self.filename, NAGIOS_PLUGINS))
251 rsync(self.filename, NAGIOS_PLUGINS)
252 else:
253 log("SKIPPED: NagiosPlugin.write: {} {}".format(self.filename, NAGIOS_PLUGINS))
254
255
220class NRPE(object):256class NRPE(object):
221 nagios_logdir = '/var/log/nagios'257 nagios_logdir = '/var/log/nagios'
222 nagios_exportdir = '/var/lib/nagios/export'258 nagios_exportdir = '/var/lib/nagios/export'
@@ -240,6 +276,8 @@
240 else:276 else:
241 self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)277 self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
242 self.checks = []278 self.checks = []
279 self.crons = []
280 self.nagios_plugins = []
243281
244 def add_check(self, *args, **kwargs):282 def add_check(self, *args, **kwargs):
245 self.checks.append(Check(*args, **kwargs))283 self.checks.append(Check(*args, **kwargs))
@@ -260,6 +298,24 @@
260 check = Check(*args, **kwargs)298 check = Check(*args, **kwargs)
261 check.remove(self.hostname)299 check.remove(self.hostname)
262300
301 def add_cron(self, *args, **kwargs):
302 self.crons.append(Cron(*args, **kwargs))
303
304 def add_nagios_plugin(self, *args, **kwargs):
305 self.nagios_plugins.append(NagiosPlugin(*args, **kwargs))
306
307 def add_from_config(self, config_key):
308 saved_config = config()
309 nrpe_set = NRPESet(saved_config.get(config_key))
310 log('NRPE.add_from_config: nrpe_set={}'.format(str(nrpe_set)))
311 if nrpe_set:
312 for check in nrpe_set.checks:
313 self.add_check(*check)
314 for cron in nrpe_set.crons:
315 self.add_cron(*cron)
316 for nagios_plugins in nrpe_set.nagios_plugins:
317 self.add_nagios_plugin(*nagios_plugins)
318
263 def write(self):319 def write(self):
264 try:320 try:
265 nagios_uid = pwd.getpwnam('nagios').pw_uid321 nagios_uid = pwd.getpwnam('nagios').pw_uid
@@ -281,6 +337,12 @@
281 "command": nrpecheck.command,337 "command": nrpecheck.command,
282 }338 }
283339
340 for cron in self.crons:
341 cron.write()
342
343 for nagios_plugins in self.nagios_plugins:
344 nagios_plugins.write()
345
284 service('restart', 'nagios-nrpe-server')346 service('restart', 'nagios-nrpe-server')
285347
286 monitor_ids = relation_ids("local-monitors") + \348 monitor_ids = relation_ids("local-monitors") + \
@@ -368,7 +430,6 @@
368 Copy the nrpe checks into place430 Copy the nrpe checks into place
369431
370 """432 """
371 NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
372 nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',433 nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',
373 'charmhelpers', 'contrib', 'openstack',434 'charmhelpers', 'contrib', 'openstack',
374 'files')435 'files')
@@ -396,3 +457,40 @@
396 shortname='haproxy_queue',457 shortname='haproxy_queue',
397 description='Check HAProxy queue depth {%s}' % unit_name,458 description='Check HAProxy queue depth {%s}' % unit_name,
398 check_cmd='check_haproxy_queue_depth.sh')459 check_cmd='check_haproxy_queue_depth.sh')
460
461class NRPESet:
462 checks = []
463 crons = []
464 nagios_plugins = []
465 def __init__(self, yaml_str='null'):
466 init_values = None
467 if yaml_str:
468 init_values = yaml.safe_load(yaml_str)
469 log('NRPESet: init_values={}'.format(init_values))
470 if type(init_values) == type({}):
471 self.checks = init_values.get('checks', [])
472 self.crons = init_values.get('crons', [])
473 self.nagios_plugins = init_values.get('nagios_plugins', [])
474
475 def add_check(self, shortname, description, check_cmd):
476 log('NRPESet: add_check({})'.format((shortname, description, check_cmd)))
477 self.checks.append((shortname, description, check_cmd))
478
479 def add_init_service_checks(self, services, unit_name=None):
480 if not unit_name:
481 unit_name = local_unit().replace('/', '-')
482 log('NRPESet: add_init_service_checks({}, {})'.format(services, unit_name))
483 add_init_service_checks(self, services, unit_name)
484
485 def add_cron(self, filename, cron_freq, cron_user, cron_cmd):
486 log('NRPESet: add_cron({}, ...)'.format(filename))
487 self.crons.append((filename, cron_freq, cron_user, cron_cmd))
488
489 def add_nagios_plugin(self, filename):
490 log('NRPESet: add_nagios_plugin({})'.format(filename))
491 self.nagios_plugins.append((filename,))
492
493 def __str__(self):
494 return yaml.safe_dump({'checks': self.checks,
495 'crons': self.crons,
496 'nagios_plugins': self.nagios_plugins})
399497
=== modified file 'hooks/nova_compute_hooks.py'
--- hooks/nova_compute_hooks.py 2015-11-25 12:48:50 +0000
+++ hooks/nova_compute_hooks.py 2016-01-07 21:39:25 +0000
@@ -378,6 +378,7 @@
378 current_unit = nrpe.get_nagios_unit_name()378 current_unit = nrpe.get_nagios_unit_name()
379 nrpe_setup = nrpe.NRPE(hostname=hostname)379 nrpe_setup = nrpe.NRPE(hostname=hostname)
380 nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)380 nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
381 nrpe_setup.add_from_config('neutron-plugin-nrpe-checks')
381 nrpe_setup.write()382 nrpe_setup.write()
382383
383384
@@ -396,6 +397,13 @@
396 apt_install('nova-api-metadata', fatal=True)397 apt_install('nova-api-metadata', fatal=True)
397 else:398 else:
398 apt_purge('nova-api-metadata', fatal=True)399 apt_purge('nova-api-metadata', fatal=True)
400
401 saved_config = config()
402 saved_config['neutron-plugin-nrpe-checks'] = settings.get('nrpe-checks')
403 saved_config.save()
404 if is_relation_made("nrpe-external-master"):
405 update_nrpe_config()
406
399 CONFIGS.write(NOVA_CONF)407 CONFIGS.write(NOVA_CONF)
400408
401409

Subscribers

People subscribed via source and target branches