1
"""Compatibility with the nrpe-external-master charm"""
2
# Copyright 2012 Canonical Ltd.
5
# Matthew Wedgwood <matthew.wedgwood@canonical.com>
15
from charmhelpers.core.hookenv import (
23
from charmhelpers.core.host import service
25
# This module adds compatibility with the nrpe-external-master and plain nrpe
26
# subordinate charms. To use it in your charm:
28
# 1. Update metadata.yaml
32
# nrpe-external-master:
33
# interface: nrpe-external-master
41
# interface: local-monitors
45
# 2. Add the following to config.yaml
51
# Used by the nrpe subordinate charms.
52
# A string that will be prepended to instance name to set the host name
53
# in nagios. So for instance the hostname would be something like:
55
# If you're running multiple environments with the same services in them
56
# this allows you to differentiate between them.
58
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
60
# 4. Update your hooks.py with something like this:
62
# from charmsupport.nrpe import NRPE
64
# def update_nrpe_config():
65
# nrpe_compat = NRPE()
66
# nrpe_compat.add_check(
67
# shortname = "myservice",
68
# description = "Check MyService",
69
# check_cmd = "check_http -w 2 -c 10 http://localhost"
71
# nrpe_compat.add_check(
73
# "Check for widget failures",
74
# check_cmd = "/srv/myapp/scripts/widget_check"
78
# def config_changed():
80
# update_nrpe_config()
82
# def nrpe_external_master_relation_changed():
83
# update_nrpe_config()
85
# def local_monitors_relation_changed():
86
# update_nrpe_config()
88
# 5. ln -s hooks.py nrpe-external-master-relation-changed
89
# ln -s hooks.py local-monitors-relation-changed
92
class CheckException(Exception):
97
shortname_re = '[A-Za-z0-9-_]+$'
98
service_template = ("""
99
#---------------------------------------------------
100
# This file is Juju managed
101
#---------------------------------------------------
104
host_name {nagios_hostname}
105
service_description {nagios_hostname}[{shortname}] """
107
check_command check_nrpe!{command}
108
servicegroups {nagios_servicegroup}
112
def __init__(self, shortname, description, check_cmd):
113
super(Check, self).__init__()
114
# XXX: could be better to calculate this from the service name
115
if not re.match(self.shortname_re, shortname):
116
raise CheckException("shortname must match {}".format(
118
self.shortname = shortname
119
self.command = "check_{}".format(shortname)
120
# Note: a set of invalid characters is defined by the
121
# Nagios server config
122
# The default is: illegal_object_name_chars=`~!$%^&*"|'<>?,()=
123
self.description = description
124
self.check_cmd = self._locate_cmd(check_cmd)
126
def _locate_cmd(self, check_cmd):
129
os.path.join(os.environ['CHARM_DIR'],
130
'files/nrpe-external-master'),
131
'/usr/lib/nagios/plugins',
133
parts = shlex.split(check_cmd)
134
for path in search_path:
135
if os.path.exists(os.path.join(path, parts[0])):
136
command = os.path.join(path, parts[0])
138
command += " " + " ".join(parts[1:])
140
log('Check command not found: {}'.format(parts[0]))
143
def write(self, nagios_context, hostname):
144
nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
146
with open(nrpe_check_file, 'w') as nrpe_check_config:
147
nrpe_check_config.write("# check {}\n".format(self.shortname))
148
nrpe_check_config.write("command[{}]={}\n".format(
149
self.command, self.check_cmd))
151
if not os.path.exists(NRPE.nagios_exportdir):
152
log('Not writing service config as {} is not accessible'.format(
153
NRPE.nagios_exportdir))
155
self.write_service_config(nagios_context, hostname)
157
def write_service_config(self, nagios_context, hostname):
158
for f in os.listdir(NRPE.nagios_exportdir):
159
if re.search('.*{}.cfg'.format(self.command), f):
160
os.remove(os.path.join(NRPE.nagios_exportdir, f))
163
'nagios_hostname': hostname,
164
'nagios_servicegroup': nagios_context,
165
'description': self.description,
166
'shortname': self.shortname,
167
'command': self.command,
169
nrpe_service_text = Check.service_template.format(**templ_vars)
170
nrpe_service_file = '{}/service__{}_{}.cfg'.format(
171
NRPE.nagios_exportdir, hostname, self.command)
172
with open(nrpe_service_file, 'w') as nrpe_service_config:
173
nrpe_service_config.write(str(nrpe_service_text))
176
subprocess.call(self.check_cmd)
180
nagios_logdir = '/var/log/nagios'
181
nagios_exportdir = '/var/lib/nagios/export'
182
nrpe_confdir = '/etc/nagios/nrpe.d'
185
super(NRPE, self).__init__()
186
self.config = config()
187
self.nagios_context = self.config['nagios_context']
188
self.unit_name = local_unit().replace('/', '-')
189
self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
192
def add_check(self, *args, **kwargs):
193
self.checks.append(Check(*args, **kwargs))
197
nagios_uid = pwd.getpwnam('nagios').pw_uid
198
nagios_gid = grp.getgrnam('nagios').gr_gid
200
log("Nagios user not set up, nrpe checks not updated")
203
if not os.path.exists(NRPE.nagios_logdir):
204
os.mkdir(NRPE.nagios_logdir)
205
os.chown(NRPE.nagios_logdir, nagios_uid, nagios_gid)
208
monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
209
for nrpecheck in self.checks:
210
nrpecheck.write(self.nagios_context, self.hostname)
211
nrpe_monitors[nrpecheck.shortname] = {
212
"command": nrpecheck.command,
215
service('restart', 'nagios-nrpe-server')
217
for rid in relation_ids("local-monitors"):
218
relation_set(relation_id=rid, monitors=yaml.dump(monitors))