#!/usr/bin/python3 # Nagios plugin to check the system state according to systemd. from __future__ import division, print_function import posixpath import subprocess import sys try: import dbus except ImportError: print("UNKNOWN: Please install python-dbus") sys.exit(3) bus = dbus.SystemBus() proxy = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') def get_system_state(): """Get system state.""" properties = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties') system_state = properties.Get('org.freedesktop.systemd1.Manager', 'SystemState') return system_state def get_failed_units(): """Get a list of failed system units.""" manager = dbus.Interface(proxy, 'org.freedesktop.systemd1.Manager') units = manager.ListUnits() 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('OK: {}'.format(system_state)) sys.exit(0) else: print('CRIT: {}, failed: {}'.format(system_state, " ".join(get_failed_units()))) sys.exit(2) # vim:tw=120: