Skip to content

Command Type

jbaumann edited this page Jan 6, 2022 · 7 revisions

Mandatory Options

The command type is used by setting the type accordingly:

[my new command]
type = command
command = check command
repair = repair command

The command type needs the following options to be set:

Option Explanation
command This command is executed to check whether the watched service is working as intended
repair This command is executed when the check was unsuccessful for longer than the set timeout

It is a good idea to use commands that execute as fast as possible to ensure a timely reaction. Adapt your sleep time and your timeout according to the maximum time the command might need.

Testing a Command

One very important design decision for this configuration type is that an error (one that leads to an exception in Python) in one of the commands for checking, repairing for the fallback action leads to a direct termination of the configuration. This means that you should test your commands thoroughly before using them in a production environment.

All commands have to use the full path to the executable to ensure that it can be found. Otherwise some errors might occur that are very hard to find. The command has to return the value 0 to signal a successful execution, and the value 1 to signal that something went wrong. Most Linux commands do this already. Command-line parameters can be used without limitation.

Testing whether a command conforms to this is pretty simple. You execute the command on the command-line and after its execution, check its return value with the command

> echo $?
0
>

If you want to check e.g., the network connectivity with the configuration type (it is actually better to use either the network or the connectivity type, but let us do this for the sake of the demonstration), you can use a ping command. To limit its execution time we use the option -c (for the number of pings) and the option -t (for the maximum time). First we check the successful execution:

> /bin/ping -c 1 -t 10 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=119 time=12.2 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 12.238/12.238/12.238/0.000 ms
> echo $?
0

And in the next step we verify that for the unsuccessful execution we get the correct response as well:

> /bin/ping -c 1 -t 10 1.2.3.4
PING 1.2.3.4 (1.2.3.4) 56(84) bytes of data.
From 62.155.245.81 icmp_seq=1 Destination Net Unreachable

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

> echo $?
1
>

This leads us to the following definition for the command option:

command = /bin/ping -c 1 -t 10 8.8.8.8

The next step is the repair action. We simply restart our networking with a systemctl command:

repair = sudo systemctl restart networking

Example

[my new command]
type = command
command = /bin/ping -c 1 -t 10 8.8.8.8
repair = sudo systemctl restart networking

Options overwriting defaults

The following options overwrite default values from the [general] section, if need be:

Option Explanation
fallback This is the fallback action that is executed if repairing was unsuccessful
sleep time The daemon sleeps between checks of the current configuration for this time in seconds.
timeout The timeout defines the maximum allowed time between successful checks. If this is exceeded, then a repair action or a fallback action is executed

Clone this wiki locally