~tribaal/charms/trusty/ceph-osd/ceph-zap-fix

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2015-02-24 11:00:08 UTC
  • Revision ID: edward.hope-morley@canonical.com-20150224110008-i2ilnxx56b7o794g
[trivial] charmhelpers sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import pwd
25
25
import grp
26
26
import os
 
27
import glob
 
28
import shutil
27
29
import re
28
30
import shlex
29
31
import yaml
161
163
        log('Check command not found: {}'.format(parts[0]))
162
164
        return ''
163
165
 
164
 
    def write(self, nagios_context, hostname, nagios_servicegroups=None):
 
166
    def write(self, nagios_context, hostname, nagios_servicegroups):
165
167
        nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
166
168
            self.command)
167
169
        with open(nrpe_check_file, 'w') as nrpe_check_config:
177
179
                                      nagios_servicegroups)
178
180
 
179
181
    def write_service_config(self, nagios_context, hostname,
180
 
                             nagios_servicegroups=None):
 
182
                             nagios_servicegroups):
181
183
        for f in os.listdir(NRPE.nagios_exportdir):
182
184
            if re.search('.*{}.cfg'.format(self.command), f):
183
185
                os.remove(os.path.join(NRPE.nagios_exportdir, f))
184
186
 
185
 
        if not nagios_servicegroups:
186
 
            nagios_servicegroups = nagios_context
187
 
 
188
187
        templ_vars = {
189
188
            'nagios_hostname': hostname,
190
189
            'nagios_servicegroup': nagios_servicegroups,
211
210
        super(NRPE, self).__init__()
212
211
        self.config = config()
213
212
        self.nagios_context = self.config['nagios_context']
214
 
        if 'nagios_servicegroups' in self.config:
 
213
        if 'nagios_servicegroups' in self.config and self.config['nagios_servicegroups']:
215
214
            self.nagios_servicegroups = self.config['nagios_servicegroups']
216
215
        else:
217
 
            self.nagios_servicegroups = 'juju'
 
216
            self.nagios_servicegroups = self.nagios_context
218
217
        self.unit_name = local_unit().replace('/', '-')
219
218
        if hostname:
220
219
            self.hostname = hostname
322
321
                check_cmd='check_status_file.py -f '
323
322
                          '/var/lib/nagios/service-check-%s.txt' % svc,
324
323
            )
 
324
 
 
325
 
 
326
def copy_nrpe_checks():
 
327
    """
 
328
    Copy the nrpe checks into place
 
329
 
 
330
    """
 
331
    NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
 
332
    nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks',
 
333
                                  'charmhelpers', 'contrib', 'openstack',
 
334
                                  'files')
 
335
 
 
336
    if not os.path.exists(NAGIOS_PLUGINS):
 
337
        os.makedirs(NAGIOS_PLUGINS)
 
338
    for fname in glob.glob(os.path.join(nrpe_files_dir, "check_*")):
 
339
        if os.path.isfile(fname):
 
340
            shutil.copy2(fname,
 
341
                         os.path.join(NAGIOS_PLUGINS, os.path.basename(fname)))
 
342
 
 
343
 
 
344
def add_haproxy_checks(nrpe, unit_name):
 
345
    """
 
346
    Add checks for each service in list
 
347
 
 
348
    :param NRPE nrpe: NRPE object to add check to
 
349
    :param str unit_name: Unit name to use in check description
 
350
    """
 
351
    nrpe.add_check(
 
352
        shortname='haproxy_servers',
 
353
        description='Check HAProxy {%s}' % unit_name,
 
354
        check_cmd='check_haproxy.sh')
 
355
    nrpe.add_check(
 
356
        shortname='haproxy_queue',
 
357
        description='Check HAProxy queue depth {%s}' % unit_name,
 
358
        check_cmd='check_haproxy_queue_depth.sh')