~aisrael/charms/trusty/ubuntu-repository-cache/fix-test

« back to all changes in this revision

Viewing changes to lib/charmhelpers/contrib/charmsupport/nrpe.py

  • Committer: José Antonio Rey
  • Date: 2015-06-04 16:40:15 UTC
  • mfrom: (193.2.4 update_charm-helpers-2)
  • Revision ID: jose@ubuntu.com-20150604164015-cta82se8e14sn62k
Daniel Watkins 2015-06-02 Revert "storage.linux.util: Fix swapped is_device_mounted regex logic (lp:1370053)"
Daniel Watkins 2015-06-03 Update charm-helpers.
Daniel Watkins 2015-06-03 Remove superseded patches.
Daniel Watkins 2015-06-03 Refresh and apply remaining patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
1
17
"""Compatibility with the nrpe-external-master charm"""
2
18
# Copyright 2012 Canonical Ltd.
3
19
#
8
24
import pwd
9
25
import grp
10
26
import os
 
27
import glob
 
28
import shutil
11
29
import re
12
30
import shlex
13
31
import yaml
18
36
    log,
19
37
    relation_ids,
20
38
    relation_set,
 
39
    relations_of_type,
21
40
)
22
41
 
23
42
from charmhelpers.core.host import service
54
73
#            juju-myservice-0
55
74
#        If you're running multiple environments with the same services in them
56
75
#        this allows you to differentiate between them.
 
76
#    nagios_servicegroups:
 
77
#      default: ""
 
78
#      type: string
 
79
#      description: |
 
80
#        A comma-separated list of nagios servicegroups.
 
81
#        If left empty, the nagios_context will be used as the servicegroup
57
82
#
58
83
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
59
84
#
138
163
        log('Check command not found: {}'.format(parts[0]))
139
164
        return ''
140
165
 
141
 
    def write(self, nagios_context, hostname):
 
166
    def write(self, nagios_context, hostname, nagios_servicegroups):
142
167
        nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
143
168
            self.command)
144
169
        with open(nrpe_check_file, 'w') as nrpe_check_config:
150
175
            log('Not writing service config as {} is not accessible'.format(
151
176
                NRPE.nagios_exportdir))
152
177
        else:
153
 
            self.write_service_config(nagios_context, hostname)
 
178
            self.write_service_config(nagios_context, hostname,
 
179
                                      nagios_servicegroups)
154
180
 
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))
159
186
 
160
187
        templ_vars = {
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,
183
210
        super(NRPE, self).__init__()
184
211
        self.config = config()
185
212
        self.nagios_context = self.config['nagios_context']
 
213
        if 'nagios_servicegroups' in self.config and self.config['nagios_servicegroups']:
 
214
            self.nagios_servicegroups = self.config['nagios_servicegroups']
 
215
        else:
 
216
            self.nagios_servicegroups = self.nagios_context
186
217
        self.unit_name = local_unit().replace('/', '-')
187
218
        if hostname:
188
219
            self.hostname = hostname
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,
214
246
            }
215
247
 
216
248
        service('restart', 'nagios-nrpe-server')
217
249
 
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))
 
254
 
 
255
 
 
256
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
 
257
    """
 
258
    Query relation with nrpe subordinate, return the nagios_host_context
 
259
 
 
260
    :param str relation_name: Name of relation nrpe sub joined to
 
261
    """
 
262
    for rel in relations_of_type(relation_name):
 
263
        if 'nagios_hostname' in rel:
 
264
            return rel['nagios_host_context']
 
265
 
 
266
 
 
267
def get_nagios_hostname(relation_name='nrpe-external-master'):
 
268
    """
 
269
    Query relation with nrpe subordinate, return the nagios_hostname
 
270
 
 
271
    :param str relation_name: Name of relation nrpe sub joined to
 
272
    """
 
273
    for rel in relations_of_type(relation_name):
 
274
        if 'nagios_hostname' in rel:
 
275
            return rel['nagios_hostname']
 
276
 
 
277
 
 
278
def get_nagios_unit_name(relation_name='nrpe-external-master'):
 
279
    """
 
280
    Return the nagios unit name prepended with host_context if needed
 
281
 
 
282
    :param str relation_name: Name of relation nrpe sub joined to
 
283
    """
 
284
    host_context = get_nagios_hostcontext(relation_name)
 
285
    if host_context:
 
286
        unit = "%s:%s" % (host_context, local_unit())
 
287
    else:
 
288
        unit = local_unit()
 
289
    return unit
 
290
 
 
291
 
 
292
def add_init_service_checks(nrpe, services, unit_name):
 
293
    """
 
294
    Add checks for each service in list
 
295
 
 
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
 
299
    """
 
300
    for svc in services:
 
301
        upstart_init = '/etc/init/%s.conf' % svc
 
302
        sysv_init = '/etc/init.d/%s' % svc
 
303
        if os.path.exists(upstart_init):
 
304
            nrpe.add_check(
 
305
                shortname=svc,
 
306
                description='process check {%s}' % unit_name,
 
307
                check_cmd='check_upstart_job %s' % svc
 
308
            )
 
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,
 
315
                                                                     svc)
 
316
                         )
 
317
            f = open(cronpath, 'w')
 
318
            f.write(cron_file)
 
319
            f.close()
 
320
            nrpe.add_check(
 
321
                shortname=svc,
 
322
                description='process check {%s}' % unit_name,
 
323
                check_cmd='check_status_file.py -f '
 
324
                          '/var/lib/nagios/service-check-%s.txt' % svc,
 
325
            )
 
326
 
 
327
 
 
328
def copy_nrpe_checks():
 
329
    """
 
330
    Copy the nrpe checks into place
 
331
 
 
332
    """
 
333
    NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
 
334
    nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',
 
335
                                  'charmhelpers', 'contrib', 'openstack',
 
336
                                  'files')
 
337
 
 
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):
 
342
            shutil.copy2(fname,
 
343
                         os.path.join(NAGIOS_PLUGINS, os.path.basename(fname)))
 
344
 
 
345
 
 
346
def add_haproxy_checks(nrpe, unit_name):
 
347
    """
 
348
    Add checks for each service in list
 
349
 
 
350
    :param NRPE nrpe: NRPE object to add check to
 
351
    :param str unit_name: Unit name to use in check description
 
352
    """
 
353
    nrpe.add_check(
 
354
        shortname='haproxy_servers',
 
355
        description='Check HAProxy {%s}' % unit_name,
 
356
        check_cmd='check_haproxy.sh')
 
357
    nrpe.add_check(
 
358
        shortname='haproxy_queue',
 
359
        description='Check HAProxy queue depth {%s}' % unit_name,
 
360
        check_cmd='check_haproxy_queue_depth.sh')