Logging your internet connection state in OpenBSD with ifstated
Sometimes your internet connection goes down…
… and when it does, you might not know about it, such as if you are out. It can be handy to collect dates and times for when this happens. This is especially true if you have a SLA (service level agreement) with your ISP and are able to get compensation.
Initially, I set about writing a script to search through
ifconfig(8)
, when I stumbled upon
ifstated(4)
. This is a daemon that ships
since OpenBSD 3.8 which does “stuff” when interfaces change.
Specifically, you can set up a complete state machine and configure the transition criteria. It even has init points so temporary transition states are not required!
This is possibly one of the simplest use cases imaginable, one can configure it
for more complex for situations involving multi-homed connections, DMZs, and
carp(4)
virtual interfaces that might be part
of multiple redundant connections.
Here, I have logging working. I also thought it would be useful to use the built
in piezo transducer to buzz, but alas, /dev/speaker
does not work on sparc64
architecture. It may work on Raspberry Pi, and should work on i386 and amd64
architectures.
Backup /etc/ifstated.conf
and paste this in there:
# monitor pppoe0 and log on changes
# inspired by http://openbsd-archive.7691.n7.nabble.com/ifstated-recipe-for-down-gt-up-td71811.html
# state transitions:
# auto ---- detect down --> pppoe0-down
# auto ---- detect up ----> pppoe0-up
# pppoe0-up ---- detect down --> pppoe0-down
# pppoe0-down ---- detect up ----> pppoe0-up
init-state auto
pppoe0_up="pppoe0.link.up"
pppoe0_down="pppoe0.link.down"
state auto {
run "logger -t ifstated pppoe0: AUTO!"
if ($pppoe0_up) set-state pppoe0-up
if ($pppoe0_down) set-state pppoe0-down
}
state pppoe0-up {
init {
# TODO: play a beep. consider something like
run "echo 'AAA' > /dev/speaker" # does not work on sparc64
run "logger -t ifstated pppoe0: UP!"
}
if ($pppoe0_down) set-state pppoe0-down
}
state pppoe0-down {
init {
# TODO: play a beep. consider something like
run "echo 'AAA' > /dev/speaker" # does not work on sparc64
run "logger -t ifstated pppoe0: DOWN!"
}
if ($pppoe0_up) set-state pppoe0-up
}
Configure and tweak as appropriate, and test it out using ifstated -d
or
ifstated -dv
/ ifstated -dvv
with increased verbosity. -d
sends output to
STDOUT
, and -v
adds more and more verbose output.
Once happy, start the daemon for realzies with rcctl start ifstated
and to
enable it at boot rcctl enable ifstated
.