~openstack-charmers-archive/charms/trusty/ceilometer/next

« back to all changes in this revision

Viewing changes to files/nrpe-external-master/check_upstart_job

  • Committer: root
  • Date: 2014-10-30 03:30:35 UTC
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: root+boostack-staging@canonical.com-20141030033035-ofdq627c9xnncy89
[bradm] initial nrpe checks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
#
 
4
# Copyright 2012, 2013 Canonical Ltd.
 
5
#
 
6
# Author: Paul Collins <paul.collins@canonical.com>
 
7
#
 
8
# Based on http://www.eurion.net/python-snippets/snippet/Upstart%20service%20status.html
 
9
#
 
10
 
 
11
import sys
 
12
 
 
13
import dbus
 
14
 
 
15
 
 
16
class Upstart(object):
 
17
    def __init__(self):
 
18
        self._bus = dbus.SystemBus()
 
19
        self._upstart = self._bus.get_object('com.ubuntu.Upstart',
 
20
                                             '/com/ubuntu/Upstart')
 
21
    def get_job(self, job_name):
 
22
        path = self._upstart.GetJobByName(job_name,
 
23
                                          dbus_interface='com.ubuntu.Upstart0_6')
 
24
        return self._bus.get_object('com.ubuntu.Upstart', path)
 
25
 
 
26
    def get_properties(self, job):
 
27
        path = job.GetInstance([], dbus_interface='com.ubuntu.Upstart0_6.Job')
 
28
        instance = self._bus.get_object('com.ubuntu.Upstart', path)
 
29
        return instance.GetAll('com.ubuntu.Upstart0_6.Instance',
 
30
                               dbus_interface=dbus.PROPERTIES_IFACE)
 
31
 
 
32
    def get_job_instances(self, job_name):
 
33
        job = self.get_job(job_name)
 
34
        paths = job.GetAllInstances([], dbus_interface='com.ubuntu.Upstart0_6.Job')
 
35
        return [self._bus.get_object('com.ubuntu.Upstart', path) for path in paths]
 
36
 
 
37
    def get_job_instance_properties(self, job):
 
38
        return job.GetAll('com.ubuntu.Upstart0_6.Instance',
 
39
                          dbus_interface=dbus.PROPERTIES_IFACE)
 
40
 
 
41
try:
 
42
    upstart = Upstart()
 
43
    try:
 
44
        job = upstart.get_job(sys.argv[1])        
 
45
        props = upstart.get_properties(job)
 
46
 
 
47
        if props['state'] == 'running':
 
48
            print 'OK: %s is running' % sys.argv[1]
 
49
            sys.exit(0)
 
50
        else:
 
51
            print 'CRITICAL: %s is not running' % sys.argv[1]
 
52
            sys.exit(2)
 
53
 
 
54
    except dbus.DBusException as e:
 
55
        instances = upstart.get_job_instances(sys.argv[1])
 
56
        propses = [upstart.get_job_instance_properties(instance) for instance in instances]
 
57
        states = dict([(props['name'], props['state']) for props in propses])
 
58
        if len(states) != states.values().count('running'):
 
59
            not_running = []
 
60
            for name in states.keys():
 
61
                if states[name] != 'running':
 
62
                    not_running.append(name)
 
63
            print 'CRITICAL: %d instances of %s not running: %s' % \
 
64
                (len(not_running), sys.argv[1], not_running.join(', '))
 
65
            sys.exit(2)
 
66
        else:
 
67
            print 'OK: %d instances of %s running' % (len(states), sys.argv[1])
 
68
 
 
69
except dbus.DBusException as e:
 
70
    print 'CRITICAL: failed to get properties of \'%s\' from upstart' % sys.argv[1]
 
71
    sys.exit(2)
 
72