-
Notifications
You must be signed in to change notification settings - Fork 0
Cookbook for Configurations
We are using a Python-specific configuration file format (based on the configparser module) that contains different sections that are named inside square brackets [ and ], and contain keys and values separated by an equal-sign. Lines that begin with a semicolon are comments and can help to document the configurations.
In addition you can use some variable substitution using a special syntax (the basic interpolation functionality of the configparser). This syntax looks as follows:
[my config]
home_dir = /home/pi
data_file = %(home_dir)s/data.txt
Starting with the two characters %(, followed by the key itself and then the two characters )s you instruct the parser to substitute this string with the contents of the key (or variable). In our example, this leads to the value for data_file being /home/pi/data.txt.
The one minor drawback is that using the percentage-sign verbatim now needs an escape i.e., if you want to have a %you now have to use two percentage-signs %%. But this is a minor price to pay for the additional functionality.
This page contains recipes for the different configuration types that you can use as a template to write your own configurations. These recipes have been tested, but always check and verify that they work in your environment as well.
[internet connectivity]
data_file = /home/pi/connectivity.txt
sh_cmd = /bin/date >> %(data_file)s
type = command
command = /bin/ping -c 1 -t 10 8.8.8.8
repair = /bin/sh -c "%(sh_cmd)s"
fallback = /bin/sh -c "%(sh_cmd)s"
sleep time = 20
timeout = 30
To check the internet connectivity we use a ping to the Google DNS server 8.8.8.8 with a timeout of 10 seconds. As command for both repair and fallback action we call a shell that appends the current date to the file /home/pi/connectivity.txt. We choose a sleep time of 20 seconds. This, together with the 10 second timeout, adds up to 30 seconds. Two times this interval is the interval for calling first the repair action, and then the fallback action.
This means that as long as the ping is not successful, every minute will be logged with an entry in the file connectivity.txt.
This recipe builds heavily on the previous example and simply adds a second configuration that checks whether we are connected to a network at all.
[internet connectivity]
data_file = /home/pi/connectivity.txt
sh_cmd = /bin/date +'Internet: %%F %%R' >> %(data_file)s
type = command
command = /bin/ping -c 1 -t 10 8.8.8.8
repair = /bin/sh -c "%(sh_cmd)s"
fallback = /bin/sh -c "%(sh_cmd)s"
sleep time = 20
timeout = 30
[local connectivity]
data_file = /home/pi/connectivity.txt
sh_cmd = /bin/date +'Local: %%F %%R' >> %(data_file)s
type = connectivity
connectivity = 1.2.3.4
repair = /bin/sh -c "%(sh_cmd)s"
fallback = /bin/sh -c "%(sh_cmd)s"
sleep time = 20
timeout = 30
We use a very similar configuration for the internet connectivity. But we add our own format to the output of the date program, and that uses the percentage-sign, which we have to escape by typing it twice. This allows us to log information from both configurations into one file.
For checking the local connectivity we use the built-in connectivity configuration type. Repair and fallback action are the same as in the internect connectivity typy.