Last active
February 5, 2021 05:17
-
-
Save gwww/07d0619ef430a9c37e7af38918d73b97 to your computer and use it in GitHub Desktop.
Monitor OPNSense log file for link DOWN. For integration with Monit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
lastlog_file="/home/admin/link-down/lastlog.txt" | |
history_file="/home/admin/link-down/history.txt" | |
down_logs="clog /var/log/system.log | grep 'link state changed to DOWN$'" | |
last_log=`eval $down_logs | tail -1` | |
if [ ! -e $lastlog_file ]; then | |
echo "$last_log" >$lastlog_file | |
touch -A -235959 $lastlog_file | |
echo "`date` Created $lastlog_file" >>$history_file | |
exit 0 | |
fi | |
read last_saved_log <$lastlog_file | |
if [ "$last_saved_log" = "$last_log" ]; then | |
# echo "`date` No new DOWN events." >>$history_file | |
exit 0 | |
fi | |
if [ "$last_log" = "" ]; then | |
echo "`date` Log file rolled/rotated or cleared." >>$history_file | |
rm $lastlog_file | |
touch $lastlog_file | |
exit 0 | |
fi | |
# Limit reports to no more than once per 480 minutes (6 hours) | |
last_event_time=`find /home/admin/link-down -name lastlog.txt -mmin +480` | |
if [ "$last_event_time" = "" ]; then | |
echo "`date` Quelched: $last_log" >>$history_file | |
# Update lastlog but preserve mtime from existing lastlog file | |
echo "$last_log" >$lastlog_file.new | |
touch -r $lastlog_file $lastlog_file.new | |
mv $lastlog_file.new $lastlog_file | |
exit 0 | |
fi | |
echo "`date` DOWN event detected: $last_log" >>$history_file | |
echo "`date` $last_saved_log" >>$history_file | |
echo "$last_log" >$lastlog_file | |
echo Router link DOWN event | |
eval $down_logs | tail -5 | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment