Use dbus-python to query system state and failed units
This commit is contained in:
parent
ece5647c25
commit
e95167262b
2 changed files with 35 additions and 23 deletions
|
@ -12,5 +12,7 @@ these commands:
|
||||||
$ systemctl list-units
|
$ systemctl list-units
|
||||||
$ systemctl status
|
$ systemctl status
|
||||||
|
|
||||||
|
Requires python-dbus.
|
||||||
|
|
||||||
License: Public Domain. Keep my name in it if you like.
|
License: Public Domain. Keep my name in it if you like.
|
||||||
Author: Mike Gerber 2015
|
Author: Mike Gerber 2015, 2016
|
||||||
|
|
|
@ -7,29 +7,39 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def find_systemctl():
|
|
||||||
systemctls = [ '/bin/systemctl', '/usr/bin/systemctl' ]
|
|
||||||
for systemctl in systemctls:
|
|
||||||
if posixpath.exists(systemctl):
|
|
||||||
return systemctl
|
|
||||||
|
|
||||||
|
|
||||||
systemctl = find_systemctl()
|
|
||||||
if not systemctl:
|
|
||||||
print('System WARN: systemctl not found')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
state = subprocess.check_output([systemctl, 'is-system-running'])
|
import dbus
|
||||||
state = state.strip()
|
except ImportError:
|
||||||
except subprocess.CalledProcessError as e:
|
print("UNKNOWN: Please install python-dbus")
|
||||||
state = e.output.strip()
|
sys.exit(3)
|
||||||
print('System NOT OK: {}'.format(state))
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
if state in ['running', 'starting']:
|
|
||||||
print('System OK: {}'.format(state))
|
bus = dbus.SystemBus()
|
||||||
|
proxy = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
|
||||||
|
|
||||||
|
|
||||||
|
def get_system_state():
|
||||||
|
"""Get system state."""
|
||||||
|
properties_manager = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
|
||||||
|
system_state = properties_manager.Get('org.freedesktop.systemd1.Manager', 'SystemState')
|
||||||
|
return system_state
|
||||||
|
|
||||||
|
|
||||||
|
def get_failed_units():
|
||||||
|
"""Get a list of failed system units."""
|
||||||
|
units = proxy.ListUnits(dbus_interface='org.freedesktop.systemd1.Manager')
|
||||||
|
failed_units = []
|
||||||
|
for u in units:
|
||||||
|
service, _, loaded, active, state = u[:5]
|
||||||
|
if active == 'failed':
|
||||||
|
failed_units.append(service)
|
||||||
|
return failed_units
|
||||||
|
|
||||||
|
|
||||||
|
system_state = get_system_state()
|
||||||
|
if system_state in ['running', 'starting']:
|
||||||
|
print('System OK: {}'.format(system_state))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
print('System NOT OK: {}'.format(state))
|
print('System NOT OK: {}, failed: {}'.format(system_state, " ".join(get_failed_units())))
|
||||||
sys.exit(0)
|
sys.exit(2)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue