Topic: How to monitor and log if a host is alive

Hello,

This is my 1st message here, i'm new to OpenWRT.
I have some knowledge in networking but more or less nothing in linux.

I have Openwrt on a PC, to run Overthebox product from OVH.

Overthebox gives me the way to aggregate WAN connection (adsl, 4G) to get redundancy and to increase my bandwidth.

My WAN routers are the box provided by the ISP and an USB dongle for 4G connectivity.

As i have a lot of problems with my ISP, i would like to monitor and log the link failures.

My idea is to send a ping (interval to determine, around 5 seconds) to the WAN IP interface of the box.
If the IP is dead => write the date and time in a log file
When the IP is back alive => write date and time

2 questions :
-is it the wrong way to monitor and log these failures ?
-how to implement it ?

Thanks a lot

Re: How to monitor and log if a host is alive

you certainly could write a shell script to do this, openwrt also has keepalived that can automatically switch routers when one goes down.  Not really sure what you are wanting to do at the end.

If you are only wanting to monitor the connection there are tons of windows based freeware out there that does this already.

Re: How to monitor and log if a host is alive

the goal is to have a log file where i can find when my connection state changed :

something like that :

2017-05-18-21h42-down
2017-05-18-21h45-up
...

i guess there are lot of freewares that could do that on windows, but the goal is to have it running on OpenWRT, as it's always running, while my Windows PC could be switch off

I've found this :
/viewtopic.php?pid=178368#p178368

i think it's almost the same as i want, but i have no idea how to use this script
i guess i have to create a file whith these lines,but i don't know how to run it, how to have it started automatically at startup, how to check the logfile

4 (edited by WWTK 2017-05-18 21:09:12)

Re: How to monitor and log if a host is alive

copy and paste it to a file on your pc
scp it to your router /tmp
chmod to make it executable
copy/move it somewhere permanent
add it to /etc/rc.local so it runs every time router boots redirecting output to a logfile you choose (make sure you don't fill the filesystem and crash the router)


those are the basic steps anyways

5 (edited by lb_le_coyote 2017-05-19 08:58:53)

Re: How to monitor and log if a host is alive

OK, solved, thanks a lot WWTK, that was the missing point.

I've changed the script to reach my goal.
Here it is :

#!/bin/sh

host1="109.xx.yy.zz"
aliveDate="0"

while [ 1 ]
do
date=`date "+%Y-%m-%d`
if ! [ $(ping -q -c 2 -w 2 $host1 2>&1 | grep "[1-2] received" | sed "s/.*\([1-2]\) received.*/1/") ] ; then
if [ $aliveDate == "1" ]; then
echo Alive, at $actualDate >> /perso/log_wan_$date.txt
aliveDate="0"
fi
actualDate="$(date)"
echo Not alive, $actualDate  >> /perso/log_wan_$date.txt
else
actualDate="$(date)"
if [ $aliveDate == "0" ] ; then
echo Alive, at $actualDate >> /perso/log_wan_$date.txt
aliveDate="1"
fi
fi
sleep 5
done


It gerenates a new log file every day