~gnuoy/charms/trusty/ceph/1453940-stable

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Liam Young
  • Date: 2015-01-12 17:22:02 UTC
  • mfrom: (86.2.8 ceph)
  • Revision ID: liam.young@canonical.com-20150112172202-0td3spsuykzmy36q
[gnuoy,r=james-page] Add support for nrpe

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    relation_set,
26
26
    remote_unit,
27
27
    Hooks, UnregisteredHookError,
28
 
    service_name
 
28
    service_name,
 
29
    relations_of_type
29
30
)
30
31
from charmhelpers.core.host import (
31
32
    service_restart,
32
33
    umount,
33
34
    mkdir,
 
35
    write_file,
 
36
    rsync,
34
37
    cmp_pkgrevno
35
38
)
36
39
from charmhelpers.fetch import (
56
59
    process_requests
57
60
)
58
61
 
 
62
from charmhelpers.contrib.charmsupport import nrpe
 
63
 
59
64
hooks = Hooks()
60
65
 
 
66
NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
 
67
SCRIPTS_DIR = '/usr/local/bin'
 
68
STATUS_FILE = '/var/lib/nagios/cat-ceph-status.txt'
 
69
STATUS_CRONFILE = '/etc/cron.d/cat-ceph-health'
 
70
 
61
71
 
62
72
def install_upstart_scripts():
63
73
    # Only install upstart configurations for older versions
152
162
                        reformat_osd(), config('ignore-device-errors'))
153
163
        ceph.start_osds(get_devices())
154
164
 
 
165
    if relations_of_type('nrpe-external-master'):
 
166
        update_nrpe_config()
 
167
 
155
168
 
156
169
def get_mon_hosts():
157
170
    hosts = []
334
347
        ceph.start_osds(get_devices())
335
348
 
336
349
 
 
350
@hooks.hook('nrpe-external-master-relation-joined')
 
351
@hooks.hook('nrpe-external-master-relation-changed')
 
352
def update_nrpe_config():
 
353
    # python-dbus is used by check_upstart_job
 
354
    apt_install('python-dbus')
 
355
    log('Refreshing nagios checks')
 
356
    if os.path.isdir(NAGIOS_PLUGINS):
 
357
        rsync(os.path.join(os.getenv('CHARM_DIR'), 'files', 'nagios',
 
358
                           'check_ceph_status.py'),
 
359
              os.path.join(NAGIOS_PLUGINS, 'check_ceph_status.py'))
 
360
 
 
361
    script = os.path.join(SCRIPTS_DIR, 'collect_ceph_status.sh')
 
362
    rsync(os.path.join(os.getenv('CHARM_DIR'), 'files',
 
363
                       'nagios', 'collect_ceph_status.sh'),
 
364
          script)
 
365
    cronjob = "{} root {}\n".format('*/5 * * * *', script)
 
366
    write_file(STATUS_CRONFILE, cronjob)
 
367
 
 
368
    # Find out if nrpe set nagios_hostname
 
369
    hostname = nrpe.get_nagios_hostname()
 
370
    current_unit = nrpe.get_nagios_unit_name()
 
371
    nrpe_setup = nrpe.NRPE(hostname=hostname)
 
372
    nrpe_setup.add_check(
 
373
        shortname="ceph",
 
374
        description='Check Ceph health {%s}' % current_unit,
 
375
        check_cmd='check_ceph_status.py -f {}'.format(STATUS_FILE)
 
376
    )
 
377
    nrpe_setup.write()
 
378
 
 
379
 
337
380
if __name__ == '__main__':
338
381
    try:
339
382
        hooks.execute(sys.argv)