So short tutorial:
Mender monitor (monitor client) is a Mender add-on, which is loosely coupled
with the Mender client to provide monitoring of device events related to three
subsystems:
- D-Bus
- Logs
- init system services (systemd)
The daemon is designed to be a lightweight edge processing oriented monitoring solution, which
The functionality is mostly tied up into three files:
mender-monitorctl
mender-monitorctl
provides the user interface for enabling/disabling monitor
services, run mender-monitorctl -h
for more information.
mender-monitord
mender-monitord
is the main entrance to the monitor client, and is essentially
a wrapper for the daemon.sh
script
daemon.sh
This is an implementation of a monitoring client which periodically calls all
the enabled/*.sh
files from the monitor.d
directory.
Each file that exists in the enabled
directory is included (sourced) to set
the environment for the call to the implementation of the monitoring. The first
element of the file name, using _
as separator, determines the monitoring
subsystem. In the above example of enabled/service_menderconnect.sh
, the file
will be sourced and monitor.d/service.sh
will be called to perform the
monitoring.
Configuration
See src/config/config.sh
.
Functionality
The monitor daemon functions as follows. To enable monitoring of a log file,
D-Bus bus or systemd service, create an appropriate shell file in the enabled
directory. Then install it with the mender-monitorctl
utility, which then
automatically enables files in the available directory through a symlink, which
should be familiar to users of Unix like utilites. Example:
#tree /etc/mender-monitor/monitor.d/
/etc/mender-monitor/monitor.d
├── available
│ ├── log_auth.sh
│ ├── log_messages.sh
│ ├── service_apache.sh
│ ├── service_cron.sh
│ ├── service_sshd.sh
│ └── sshd_service.sh
├── enabled
│ ├── log_auth.sh -> /etc/mender-monitor/monitor.d/available/log_auth.sh
│ ├── log_messages.sh -> /etc/mender-monitor/monitor.d/available/log_messages.sh
│ ├── service_apache.sh -> /etc/mender-monitor/monitor.d/available/service_apache.sh
│ ├── service_cron.sh -> /etc/mender-monitor/monitor.d/available/service_cron.sh
│ └── service_sshd.sh -> /etc/mender-monitor/monitor.d/available/service_sshd.sh
├── log.sh
└── service.sh
2 directories, 15 files
where for instance the cron
monitoring entry in the monitor.d
directory is:
#cat /tmp/etc/mender-monitor/monitor.d/enabled/service_cron.sh
# This file was autogenerated by Monitoring daemon based on the configuration
SERVICE_NAME=cron
SERVICE_TYPE=systemd
Support for interval_s
is coming, currently all services share 1s sleep interval
between runs.
The three sub-systems supported
Currently the monitor client supports three different sub-systems.
Log file subsystem
You can configure log files monitoring in a similar way as with the systemd services.
Let’s assume you want to raise an alert every time session opened for user root by
appears in /var/log/auth.log
file. To this end you need to create the enable file
and use log.sh
subsystem. The file can look like this:
SERVICE_NAME="auth"
LOG_PATTERN="session opened for user root by"
LOG_FILE="/var/log/auth.log"
With each iteration of the main loop daemon.sh
will call log.sh
to check
for a given pattern in a given file. The pattern is interpreted
as a grep extended regular expression.
systemd services subsystem
To enable systemd service monitoring all you need to do is to enable the service.sh monitor
for a given service. For example, to enable monitoring of cron systemd:
echo -ne 'SERVICE_NAME="cron"\nSERVICE_TYPE="systemd"' > monitor.d/enabled/service_cron.sh;
The “enable” directory file names format is: service_"name-of-the-service".sh"
.
The name of the service in the file name is arbitrary and does not have any other meaning
than just to distinguish the file in the monitor.d/enabled
directory. The files are read
by the daemon.sh
.
D-Bus subsystem
You can configure D-Bus monitoring in a similar way as with the systemd
services, and log files. Let’s assume you want to raise an alert every time
u-power
raises a signal. To this end you need to create the enable file and use
the dbus.sh
subsystem. The file can look like this:
DBUS_NAME="u-power"
DBUS_PATTERN="some pattern to match on | (empty to match on all notifications)"
DBUS_WATCH_EXPRESSION="type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path=/org/freedesktop/UPower/devices/battery_BAT0 | (empty to match on all interfaces)"
Shell API for the Monitoring backend
monitor-lib.sh
provides a single call to send an alert via the backend
with the help of monitor_send_alert
. You can find an example usage
in the monitor.d/service.sh
.