-
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 monitoring tools which are located in the main de.thjom.java.systemd
package.
An instance of type de.thjom.java.systemd.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. 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 addDefaultHandlers()
method will activate this feature, calling the removeDefaultHandlers()
method will deactivate it. Without attaching the monitors default handlers 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 unit monitor which monitors the systemd units "avahi-daemon.service" and "polkit.service". The first one is configured via its full name and the second one is added directly by using the manager, which means it must be known to the system daemon at that time.
import de.thjom.java.systemd.Manager;
import de.thjom.java.systemd.Systemd;
import de.thjom.java.systemd.UnitNameMonitor;
import org.freedesktop.dbus.exceptions.DBusException;
try {
Manager manager = Systemd.get().getManager();
UnitNameMonitor unitMonitor = new UnitNameMonitor(manager);
unitMonitor.addUnits("avahi-daemon.service");
unitMonitor.addUnits(manager.getService("polkit"));
List<Unit> units = unitMonitor.getMonitoredUnits();
}
catch (DBusException e) {
// ...
}
An instance of type de.thjom.java.systemd.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 addDefaultHandlers()
method will activate this feature, calling the removeDefaultHandlers()
method will deactivate it. Without attaching the monitors default handlers 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 unit monitor which monitors all systemd units of types "service" and "mount" 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.UnitTypeMonitor;
import de.thjom.java.systemd.UnitTypeMonitor.MonitoredType;
import org.freedesktop.dbus.exceptions.DBusException;
try {
Manager manager = Systemd.get().getManager();
UnitTypeMonitor unitMonitor = new UnitTypeMonitor(manager);
unitMonitor.addMonitoredTypes(MonitoredType.MOUNT, MonitoredType.SERVICE);
List<Unit> units = unitMonitor.getMonitoredUnits();
}
catch (DBusException e) {
// ...
}
For monitoring of all available unit types use the MonitoredType.values()
method like shown below.
UnitTypeMonitor unitMonitor = new UnitTypeMonitor(manager);
unitMonitor.addMonitoredTypes(MonitoredType.values());