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.
150
175
log('Not writing service config as {} is not accessible'.format(
151
176
NRPE.nagios_exportdir))
153
self.write_service_config(nagios_context, hostname)
178
self.write_service_config(nagios_context, hostname,
179
nagios_servicegroups)
155
def write_service_config(self, nagios_context, hostname):
181
def write_service_config(self, nagios_context, hostname,
182
nagios_servicegroups):
156
183
for f in os.listdir(NRPE.nagios_exportdir):
157
184
if re.search('.*{}.cfg'.format(self.command), f):
158
185
os.remove(os.path.join(NRPE.nagios_exportdir, f))
161
188
'nagios_hostname': hostname,
162
'nagios_servicegroup': nagios_context,
189
'nagios_servicegroup': nagios_servicegroups,
163
190
'description': self.description,
164
191
'shortname': self.shortname,
165
192
'command': self.command,
208
239
nrpe_monitors = {}
209
240
monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
210
241
for nrpecheck in self.checks:
211
nrpecheck.write(self.nagios_context, self.hostname)
242
nrpecheck.write(self.nagios_context, self.hostname,
243
self.nagios_servicegroups)
212
244
nrpe_monitors[nrpecheck.shortname] = {
213
245
"command": nrpecheck.command,
216
248
service('restart', 'nagios-nrpe-server')
218
for rid in relation_ids("local-monitors"):
250
monitor_ids = relation_ids("local-monitors") + \
251
relation_ids("nrpe-external-master")
252
for rid in monitor_ids:
219
253
relation_set(relation_id=rid, monitors=yaml.dump(monitors))
256
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
258
Query relation with nrpe subordinate, return the nagios_host_context
260
:param str relation_name: Name of relation nrpe sub joined to
262
for rel in relations_of_type(relation_name):
263
if 'nagios_hostname' in rel:
264
return rel['nagios_host_context']
267
def get_nagios_hostname(relation_name='nrpe-external-master'):
269
Query relation with nrpe subordinate, return the nagios_hostname
271
:param str relation_name: Name of relation nrpe sub joined to
273
for rel in relations_of_type(relation_name):
274
if 'nagios_hostname' in rel:
275
return rel['nagios_hostname']
278
def get_nagios_unit_name(relation_name='nrpe-external-master'):
280
Return the nagios unit name prepended with host_context if needed
282
:param str relation_name: Name of relation nrpe sub joined to
284
host_context = get_nagios_hostcontext(relation_name)
286
unit = "%s:%s" % (host_context, local_unit())
292
def add_init_service_checks(nrpe, services, unit_name):
294
Add checks for each service in list
296
:param NRPE nrpe: NRPE object to add check to
297
:param list services: List of services to check
298
:param str unit_name: Unit name to use in check description
301
upstart_init = '/etc/init/%s.conf' % svc
302
sysv_init = '/etc/init.d/%s' % svc
303
if os.path.exists(upstart_init):
306
description='process check {%s}' % unit_name,
307
check_cmd='check_upstart_job %s' % svc
309
elif os.path.exists(sysv_init):
310
cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
311
cron_file = ('*/5 * * * * root '
312
'/usr/local/lib/nagios/plugins/check_exit_status.pl '
313
'-s /etc/init.d/%s status > '
314
'/var/lib/nagios/service-check-%s.txt\n' % (svc,
317
f = open(cronpath, 'w')
322
description='process check {%s}' % unit_name,
323
check_cmd='check_status_file.py -f '
324
'/var/lib/nagios/service-check-%s.txt' % svc,
328
def copy_nrpe_checks():
330
Copy the nrpe checks into place
333
NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
334
nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',
335
'charmhelpers', 'contrib', 'openstack',
338
if not os.path.exists(NAGIOS_PLUGINS):
339
os.makedirs(NAGIOS_PLUGINS)
340
for fname in glob.glob(os.path.join(nrpe_files_dir, "check_*")):
341
if os.path.isfile(fname):
343
os.path.join(NAGIOS_PLUGINS, os.path.basename(fname)))
346
def add_haproxy_checks(nrpe, unit_name):
348
Add checks for each service in list
350
:param NRPE nrpe: NRPE object to add check to
351
:param str unit_name: Unit name to use in check description
354
shortname='haproxy_servers',
355
description='Check HAProxy {%s}' % unit_name,
356
check_cmd='check_haproxy.sh')
358
shortname='haproxy_queue',
359
description='Check HAProxy queue depth {%s}' % unit_name,
360
check_cmd='check_haproxy_queue_depth.sh')