4
# Copyright 2012, 2013 Canonical Ltd.
6
# Author: Paul Collins <paul.collins@canonical.com>
8
# Based on http://www.eurion.net/python-snippets/snippet/Upstart%20service%20status.html
16
class Upstart(object):
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)
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)
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]
37
def get_job_instance_properties(self, job):
38
return job.GetAll('com.ubuntu.Upstart0_6.Instance',
39
dbus_interface=dbus.PROPERTIES_IFACE)
44
job = upstart.get_job(sys.argv[1])
45
props = upstart.get_properties(job)
47
if props['state'] == 'running':
48
print 'OK: %s is running' % sys.argv[1]
51
print 'CRITICAL: %s is not running' % sys.argv[1]
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'):
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(', '))
67
print 'OK: %d instances of %s running' % (len(states), sys.argv[1])
69
except dbus.DBusException as e:
70
print 'CRITICAL: failed to get properties of \'%s\' from upstart' % sys.argv[1]