Use rnpe functions from charmhelpers

This commit is contained in:
Liam Young
2015-01-12 12:04:01 +00:00
parent fae197d899
commit f2d918924f
5 changed files with 120 additions and 59 deletions

View File

@@ -18,6 +18,7 @@ from charmhelpers.core.hookenv import (
log, log,
relation_ids, relation_ids,
relation_set, relation_set,
relations_of_type,
) )
from charmhelpers.core.host import service from charmhelpers.core.host import service
@@ -54,6 +55,12 @@ from charmhelpers.core.host import service
# juju-myservice-0 # juju-myservice-0
# If you're running multiple environments with the same services in them # If you're running multiple environments with the same services in them
# this allows you to differentiate between them. # this allows you to differentiate between them.
# nagios_servicegroups:
# default: ""
# type: string
# description: |
# A comma-separated list of nagios servicegroups.
# If left empty, the nagios_context will be used as the servicegroup
# #
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master # 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
# #
@@ -125,9 +132,6 @@ define service {{
def _locate_cmd(self, check_cmd): def _locate_cmd(self, check_cmd):
search_path = ( search_path = (
'/',
os.path.join(os.environ['CHARM_DIR'],
'files/nrpe-external-master'),
'/usr/lib/nagios/plugins', '/usr/lib/nagios/plugins',
'/usr/local/lib/nagios/plugins', '/usr/local/lib/nagios/plugins',
) )
@@ -141,7 +145,7 @@ define service {{
log('Check command not found: {}'.format(parts[0])) log('Check command not found: {}'.format(parts[0]))
return '' return ''
def write(self, nagios_context, hostname): def write(self, nagios_context, hostname, nagios_servicegroups=None):
nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format( nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
self.command) self.command)
with open(nrpe_check_file, 'w') as nrpe_check_config: with open(nrpe_check_file, 'w') as nrpe_check_config:
@@ -153,16 +157,21 @@ define service {{
log('Not writing service config as {} is not accessible'.format( log('Not writing service config as {} is not accessible'.format(
NRPE.nagios_exportdir)) NRPE.nagios_exportdir))
else: else:
self.write_service_config(nagios_context, hostname) self.write_service_config(nagios_context, hostname,
nagios_servicegroups)
def write_service_config(self, nagios_context, hostname): def write_service_config(self, nagios_context, hostname,
nagios_servicegroups=None):
for f in os.listdir(NRPE.nagios_exportdir): for f in os.listdir(NRPE.nagios_exportdir):
if re.search('.*{}.cfg'.format(self.command), f): if re.search('.*{}.cfg'.format(self.command), f):
os.remove(os.path.join(NRPE.nagios_exportdir, f)) os.remove(os.path.join(NRPE.nagios_exportdir, f))
if not nagios_servicegroups:
nagios_servicegroups = nagios_context
templ_vars = { templ_vars = {
'nagios_hostname': hostname, 'nagios_hostname': hostname,
'nagios_servicegroup': nagios_context, 'nagios_servicegroup': nagios_servicegroups,
'description': self.description, 'description': self.description,
'shortname': self.shortname, 'shortname': self.shortname,
'command': self.command, 'command': self.command,
@@ -186,6 +195,10 @@ class NRPE(object):
super(NRPE, self).__init__() super(NRPE, self).__init__()
self.config = config() self.config = config()
self.nagios_context = self.config['nagios_context'] self.nagios_context = self.config['nagios_context']
if 'nagios_servicegroups' in self.config:
self.nagios_servicegroups = self.config['nagios_servicegroups']
else:
self.nagios_servicegroups = 'juju'
self.unit_name = local_unit().replace('/', '-') self.unit_name = local_unit().replace('/', '-')
if hostname: if hostname:
self.hostname = hostname self.hostname = hostname
@@ -211,7 +224,8 @@ class NRPE(object):
nrpe_monitors = {} nrpe_monitors = {}
monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}} monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
for nrpecheck in self.checks: for nrpecheck in self.checks:
nrpecheck.write(self.nagios_context, self.hostname) nrpecheck.write(self.nagios_context, self.hostname,
self.nagios_servicegroups)
nrpe_monitors[nrpecheck.shortname] = { nrpe_monitors[nrpecheck.shortname] = {
"command": nrpecheck.command, "command": nrpecheck.command,
} }
@@ -220,3 +234,75 @@ class NRPE(object):
for rid in relation_ids("local-monitors"): for rid in relation_ids("local-monitors"):
relation_set(relation_id=rid, monitors=yaml.dump(monitors)) relation_set(relation_id=rid, monitors=yaml.dump(monitors))
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
"""
Query relation with nrpe subordinate, return the nagios_host_context
:param str relation_name: Name of relation nrpe sub joined to
"""
for rel in relations_of_type(relation_name):
if 'nagios_hostname' in rel:
return rel['nagios_host_context']
def get_nagios_hostname(relation_name='nrpe-external-master'):
"""
Query relation with nrpe subordinate, return the nagios_hostname
:param str relation_name: Name of relation nrpe sub joined to
"""
for rel in relations_of_type(relation_name):
if 'nagios_hostname' in rel:
return rel['nagios_hostname']
def get_nagios_unit_name(relation_name='nrpe-external-master'):
"""
Return the nagios unit name prepended with host_context if needed
:param str relation_name: Name of relation nrpe sub joined to
"""
host_context = get_nagios_hostcontext(relation_name)
if host_context:
unit = "%s:%s" % (host_context, local_unit())
else:
unit = local_unit()
return unit
def add_init_service_checks(nrpe, services, unit_name):
"""
Add checks for each service in list
:param NRPE nrpe: NRPE object to add check to
:param list services: List of services to check
:param str unit_name: Unit name to use in check description
"""
for svc in services:
upstart_init = '/etc/init/%s.conf' % svc
sysv_init = '/etc/init.d/%s' % svc
if os.path.exists(upstart_init):
nrpe.add_check(
shortname=svc,
description='process check {%s}' % unit_name,
check_cmd='check_upstart_job %s' % svc
)
elif os.path.exists(sysv_init):
cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
cron_file = ('*/5 * * * * root '
'/usr/local/lib/nagios/plugins/check_exit_status.pl '
'-s /etc/init.d/%s status > '
'/var/lib/nagios/service-check-%s.txt\n' % (svc,
svc)
)
f = open(cronpath, 'w')
f.write(cron_file)
f.close()
nrpe.add_check(
shortname=svc,
description='process check {%s}' % unit_name,
check_cmd='check_status_file.py -f '
'/var/lib/nagios/service-check-%s.txt' % svc,
)

View File

@@ -2,7 +2,8 @@
Functions for managing volumes in juju units. One volume is supported per unit. Functions for managing volumes in juju units. One volume is supported per unit.
Subordinates may have their own storage, provided it is on its own partition. Subordinates may have their own storage, provided it is on its own partition.
Configuration stanzas: Configuration stanzas::
volume-ephemeral: volume-ephemeral:
type: boolean type: boolean
default: true default: true
@@ -20,7 +21,8 @@ Configuration stanzas:
is 'true' and no volume-map value is set. Use 'juju set' to set a is 'true' and no volume-map value is set. Use 'juju set' to set a
value and 'juju resolved' to complete configuration. value and 'juju resolved' to complete configuration.
Usage: Usage::
from charmsupport.volumes import configure_volume, VolumeConfigurationError from charmsupport.volumes import configure_volume, VolumeConfigurationError
from charmsupport.hookenv import log, ERROR from charmsupport.hookenv import log, ERROR
def post_mount_hook(): def post_mount_hook():
@@ -34,6 +36,7 @@ Usage:
after_change=post_mount_hook) after_change=post_mount_hook)
except VolumeConfigurationError: except VolumeConfigurationError:
log('Storage could not be configured', ERROR) log('Storage could not be configured', ERROR)
''' '''
# XXX: Known limitations # XXX: Known limitations

View File

@@ -53,6 +53,7 @@ UBUNTU_OPENSTACK_RELEASE = OrderedDict([
('saucy', 'havana'), ('saucy', 'havana'),
('trusty', 'icehouse'), ('trusty', 'icehouse'),
('utopic', 'juno'), ('utopic', 'juno'),
('vivid', 'kilo'),
]) ])
@@ -64,6 +65,7 @@ OPENSTACK_CODENAMES = OrderedDict([
('2013.2', 'havana'), ('2013.2', 'havana'),
('2014.1', 'icehouse'), ('2014.1', 'icehouse'),
('2014.2', 'juno'), ('2014.2', 'juno'),
('2015.1', 'kilo'),
]) ])
# The ugly duckling # The ugly duckling
@@ -84,6 +86,7 @@ SWIFT_CODENAMES = OrderedDict([
('2.0.0', 'juno'), ('2.0.0', 'juno'),
('2.1.0', 'juno'), ('2.1.0', 'juno'),
('2.2.0', 'juno'), ('2.2.0', 'juno'),
('2.2.1', 'kilo'),
]) ])
DEFAULT_LOOPBACK_SIZE = '5G' DEFAULT_LOOPBACK_SIZE = '5G'
@@ -289,6 +292,9 @@ def configure_installation_source(rel):
'juno': 'trusty-updates/juno', 'juno': 'trusty-updates/juno',
'juno/updates': 'trusty-updates/juno', 'juno/updates': 'trusty-updates/juno',
'juno/proposed': 'trusty-proposed/juno', 'juno/proposed': 'trusty-proposed/juno',
'kilo': 'trusty-updates/kilo',
'kilo/updates': 'trusty-updates/kilo',
'kilo/proposed': 'trusty-proposed/kilo',
} }
try: try:

View File

@@ -64,9 +64,16 @@ CLOUD_ARCHIVE_POCKETS = {
'trusty-juno/updates': 'trusty-updates/juno', 'trusty-juno/updates': 'trusty-updates/juno',
'trusty-updates/juno': 'trusty-updates/juno', 'trusty-updates/juno': 'trusty-updates/juno',
'juno/proposed': 'trusty-proposed/juno', 'juno/proposed': 'trusty-proposed/juno',
'juno/proposed': 'trusty-proposed/juno',
'trusty-juno/proposed': 'trusty-proposed/juno', 'trusty-juno/proposed': 'trusty-proposed/juno',
'trusty-proposed/juno': 'trusty-proposed/juno', 'trusty-proposed/juno': 'trusty-proposed/juno',
# Kilo
'kilo': 'trusty-updates/kilo',
'trusty-kilo': 'trusty-updates/kilo',
'trusty-kilo/updates': 'trusty-updates/kilo',
'trusty-updates/kilo': 'trusty-updates/kilo',
'kilo/proposed': 'trusty-proposed/kilo',
'trusty-kilo/proposed': 'trusty-proposed/kilo',
'trusty-proposed/kilo': 'trusty-proposed/kilo',
} }
# The order of this list is very important. Handlers should be listed in from # The order of this list is very important. Handlers should be listed in from

View File

@@ -44,7 +44,6 @@ from charmhelpers.core.hookenv import (
relation_ids, relation_ids,
relation_get, relation_get,
related_units, related_units,
relations_of_type,
log, log,
DEBUG, DEBUG,
INFO, INFO,
@@ -80,7 +79,7 @@ from charmhelpers.contrib.network.ip import (
) )
from charmhelpers.contrib.openstack.context import ADDRESS_TYPES from charmhelpers.contrib.openstack.context import ADDRESS_TYPES
from charmhelpers.contrib.charmsupport.nrpe import NRPE from charmhelpers.contrib.charmsupport import nrpe
extra_pkgs = [ extra_pkgs = [
"haproxy", "haproxy",
@@ -142,7 +141,6 @@ def config_changed():
keystone_joined(relid=r_id) keystone_joined(relid=r_id)
@hooks.hook('identity-service-relation-joined') @hooks.hook('identity-service-relation-joined')
def keystone_joined(relid=None): def keystone_joined(relid=None):
if not is_elected_leader(SWIFT_HA_RES): if not is_elected_leader(SWIFT_HA_RES):
@@ -497,52 +495,13 @@ def configure_https():
@hooks.hook('nrpe-external-master-relation-joined', @hooks.hook('nrpe-external-master-relation-joined',
'nrpe-external-master-relation-changed') 'nrpe-external-master-relation-changed')
def update_nrpe_config(): def update_nrpe_config():
# python-dbus is used by check_upstart_job
apt_install('python-dbus') apt_install('python-dbus')
# Find out if nrpe set nagios_hostname hostname = nrpe.get_nagios_hostname()
hostname = None current_unit = nrpe.get_nagios_unit_name()
host_context = None nrpe_setup = nrpe.NRPE(hostname=hostname)
for rel in relations_of_type('nrpe-external-master'): nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
if 'nagios_hostname' in rel: nrpe_setup.write()
hostname = rel['nagios_hostname']
host_context = rel['nagios_host_context']
break
nrpe = NRPE(hostname=hostname)
if host_context:
current_unit = "%s:%s" % (host_context, local_unit())
else:
current_unit = local_unit()
services_to_monitor = services()
for service in services_to_monitor:
upstart_init = '/etc/init/%s.conf' % service
sysv_init = '/etc/init.d/%s' % service
if os.path.exists(upstart_init):
nrpe.add_check(
shortname=service,
description='process check {%s}' % current_unit,
check_cmd='check_upstart_job %s' % service,
)
elif os.path.exists(sysv_init):
cronpath = '/etc/cron.d/nagios-service-check-%s' % service
cron_entry = ('*/5 * * * * root '
'/usr/local/lib/nagios/plugins/check_exit_status.pl '
'-s /etc/init.d/%s status > '
'/var/lib/nagios/service-check-%s.txt\n' % (service,
service)
)
f = open(cronpath, 'w')
f.write(cron_entry)
f.close()
nrpe.add_check(
shortname=service,
description='process check {%s}' % current_unit,
check_cmd='check_status_file.py -f '
'/var/lib/nagios/service-check-%s.txt' % service,
)
nrpe.write()
def main(): def main():