-
Notifications
You must be signed in to change notification settings - Fork 6
Unit monitoring
This library is suited for monitoring systems which are based on systemd. It therefore comes with some handy monitoring tools which are located in the de.thjom.java.systemd.tools package.
An instance of type de.thjom.java.systemd.tools.UnitNameMonitor is able to monitor a set of discrete systemd units. Units which shall be monitored can be added to the monitor instance either directly (by a Unit object) or by their name (e.g. "avahi-daemon.service"). The latter option is useful in case units are not yet known to the systemd daemon, but however already expected.
An instance of type de.thjom.java.systemd.tools.UnitTypeMonitor is able to monitor all units of a configurable unit type (e.g. "mount" or "service"). Units being currently monitored can be obtained via the getMonitoredUnits() method. The class is thread-safe regarding concurrent access.
The class is capable of getting notified by the systemd manager about changes on unit deployment. Calling the attach() method will activate this feature, calling the detach() method will deactivate it. Without attaching the monitor to the managers notification signals the monitor won't be updated automatically. In that case manual refreshing is required which can be done via the refresh() method.
The following code listing configures a basic service monitor which monitors all systemd units of type "service" which are currently deployed and known to the system daemon.
import de.thjom.java.systemd.Manager;
import de.thjom.java.systemd.Systemd;
import de.thjom.java.systemd.tools.UnitTypeMonitor;
import org.freedesktop.dbus.exceptions.DBusException;
try {
Manager manager = Systemd.get().getManager();
UnitTypeMonitor serviceMonitor = new UnitTypeMonitor(manager, MonitoredType.SERVICE);
List<Unit> units = serviceMonitor.getMonitoredUnits();
}
catch (DBusException e) {
// ...
}