1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
5
# charm-helpers is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# charm-helpers is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
1
17
"""Compatibility with the nrpe-external-master charm"""
2
18
# Copyright 2012 Canonical Ltd.
55
72
# If you're running multiple environments with the same services in them
56
73
# this allows you to differentiate between them.
74
# nagios_servicegroups:
78
# A comma-separated list of nagios servicegroups.
79
# If left empty, the nagios_context will be used as the servicegroup
58
81
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
126
149
def _locate_cmd(self, check_cmd):
129
os.path.join(os.environ['CHARM_DIR'],
130
'files/nrpe-external-master'),
131
151
'/usr/lib/nagios/plugins',
152
'/usr/local/lib/nagios/plugins',
133
154
parts = shlex.split(check_cmd)
134
155
for path in search_path:
140
161
log('Check command not found: {}'.format(parts[0]))
143
def write(self, nagios_context, hostname):
164
def write(self, nagios_context, hostname, nagios_servicegroups=None):
144
165
nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
146
167
with open(nrpe_check_file, 'w') as nrpe_check_config:
152
173
log('Not writing service config as {} is not accessible'.format(
153
174
NRPE.nagios_exportdir))
155
self.write_service_config(nagios_context, hostname)
176
self.write_service_config(nagios_context, hostname,
177
nagios_servicegroups)
157
def write_service_config(self, nagios_context, hostname):
179
def write_service_config(self, nagios_context, hostname,
180
nagios_servicegroups=None):
158
181
for f in os.listdir(NRPE.nagios_exportdir):
159
182
if re.search('.*{}.cfg'.format(self.command), f):
160
183
os.remove(os.path.join(NRPE.nagios_exportdir, f))
185
if not nagios_servicegroups:
186
nagios_servicegroups = nagios_context
163
189
'nagios_hostname': hostname,
164
'nagios_servicegroup': nagios_context,
190
'nagios_servicegroup': nagios_servicegroups,
165
191
'description': self.description,
166
192
'shortname': self.shortname,
167
193
'command': self.command,
181
207
nagios_exportdir = '/var/lib/nagios/export'
182
208
nrpe_confdir = '/etc/nagios/nrpe.d'
210
def __init__(self, hostname=None):
185
211
super(NRPE, self).__init__()
186
212
self.config = config()
187
213
self.nagios_context = self.config['nagios_context']
214
if 'nagios_servicegroups' in self.config:
215
self.nagios_servicegroups = self.config['nagios_servicegroups']
217
self.nagios_servicegroups = 'juju'
188
218
self.unit_name = local_unit().replace('/', '-')
189
self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
220
self.hostname = hostname
222
self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
192
225
def add_check(self, *args, **kwargs):
207
240
nrpe_monitors = {}
208
241
monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
209
242
for nrpecheck in self.checks:
210
nrpecheck.write(self.nagios_context, self.hostname)
243
nrpecheck.write(self.nagios_context, self.hostname,
244
self.nagios_servicegroups)
211
245
nrpe_monitors[nrpecheck.shortname] = {
212
246
"command": nrpecheck.command,
217
251
for rid in relation_ids("local-monitors"):
218
252
relation_set(relation_id=rid, monitors=yaml.dump(monitors))
255
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
257
Query relation with nrpe subordinate, return the nagios_host_context
259
:param str relation_name: Name of relation nrpe sub joined to
261
for rel in relations_of_type(relation_name):
262
if 'nagios_hostname' in rel:
263
return rel['nagios_host_context']
266
def get_nagios_hostname(relation_name='nrpe-external-master'):
268
Query relation with nrpe subordinate, return the nagios_hostname
270
:param str relation_name: Name of relation nrpe sub joined to
272
for rel in relations_of_type(relation_name):
273
if 'nagios_hostname' in rel:
274
return rel['nagios_hostname']
277
def get_nagios_unit_name(relation_name='nrpe-external-master'):
279
Return the nagios unit name prepended with host_context if needed
281
:param str relation_name: Name of relation nrpe sub joined to
283
host_context = get_nagios_hostcontext(relation_name)
285
unit = "%s:%s" % (host_context, local_unit())
291
def add_init_service_checks(nrpe, services, unit_name):
293
Add checks for each service in list
295
:param NRPE nrpe: NRPE object to add check to
296
:param list services: List of services to check
297
:param str unit_name: Unit name to use in check description
300
upstart_init = '/etc/init/%s.conf' % svc
301
sysv_init = '/etc/init.d/%s' % svc
302
if os.path.exists(upstart_init):
305
description='process check {%s}' % unit_name,
306
check_cmd='check_upstart_job %s' % svc
308
elif os.path.exists(sysv_init):
309
cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
310
cron_file = ('*/5 * * * * root '
311
'/usr/local/lib/nagios/plugins/check_exit_status.pl '
312
'-s /etc/init.d/%s status > '
313
'/var/lib/nagios/service-check-%s.txt\n' % (svc,
316
f = open(cronpath, 'w')
321
description='process check {%s}' % unit_name,
322
check_cmd='check_status_file.py -f '
323
'/var/lib/nagios/service-check-%s.txt' % svc,