OpenWrt is an operating system / embedded operating system based on the Linux kernel and using a command-line interface (ash shell), and a web interface (LuCI) has endless extension possibilities for power users
In OpenWrt Monitor network uptime and alert you the downtime can be detected by this script. It will send email notification when a host or site is down
cd /etc
Preview
- [Uptime Monitor] 192.168.1.1 is back up! - Good News -- 192.168.1.1 is back up
- [Uptime Monitor] 192.168.1.1 is down! - Bad News -- 192.168.1.1 is down
Pre-requisites
- fping - similiar to ping, but much better performing when pinging multiple hosts. "How to Install Fping a Ping alternative on Linux"
- msmtp - required for sending email "Send Email with Msmtp on OpenWrt"
Install the script
Step 1: Create the folder and scriptcd /etc
mkdir cronscript cd cronscript touch uptime_monitor.sh vi uptime_monitor.shStep 2: In vi, You need add this script.
#!/bin/ash # Uptime monitoring for OpenWrt # Tested on OpenWrt Attitude Adjustment 12.09 # Written By justhrun posted on <http://www.techrapid.co.uk> # ---------------------------------------------------------- # Last Modified : 02/08/2014 # ---------------------------------------------------------- TCHECK=`date "+%Y-%m-%d %H:%M:%S"` HOSTS="192.168.1.1 192.168.1.4 www.google.com" xping=/usr/bin/fping flog=/tmp/log/hostcheck.log FPATH=/tmp/log for myHost in ${HOSTS} do filenya=${FPATH}/${myHost}.txt if ${xping} -q -t750 ${myHost} ; then ## host alive, check then remove file host.txt if [ -f ${filenya} ]; then echo "${TCHECK} : ${myHost} is BACK UP" >> ${flog} echo -e "Subject: [Uptime Monitor] $myHost is back up!\r\n\nGood News -- $myHost is back up" |msmtp --from=default -t destination.address@gmail.com rm -f ${filenya} fi else ## host down, check then create file host.txt if [ ! -f ${filenya} ]; then ## not exist, host is dwon echo "${TCHECK} : ${myHost} is DOWN" >> ${flog} echo -e "Subject: [Uptime Monitor] $myHost is down!\r\n\nBad News -- $myHost is down" |msmtp --from=default -t destination.address@gmail.com touch ${filenya} else ## exist, still down, logging don't send email echo "${TCHECK} : ${myHost} is still DOWN" >> ${flog} fi fi done #-EOFAlso you can get the script by wget, run following commands:
cd /etc/cronscript wget http://www.mediafire.com/view/l0xjxk7p9s3p2c5/uptime_monitoring.sh
Configure the script
Step 3: Find and replace which line on the script to your own# This is address or host that you want to monitor HOSTS="192.168.1.1 192.168.1.4 www.google.com" # Path to fping xping=/usr/bin/fping # Path where summary log will be created flog=/tmp/log/hostcheck.log # Path where host log will be created FPATH=/tmp/log # Specify Initial target timeout in millisecond for ping to hosts -t750 # Specify Subject, Message Body, and Destination Address mail to send when host is backup echo -e "Subject: [Uptime Monitor] $myHost is back up!\r\n\nGood News -- $myHost is back up" |msmtp --from=default -t destination.address@gmail.com # Specify Subject, Message Body, and Destination Address mail to send when host is down echo -e "Subject: [Uptime Monitor] $myHost is down!\r\n\nBad News -- $myHost is down" |msmtp --from=default -t destination.address@gmail.comStep 4: Test the script to debug the code To test the script run following command:
sh uptime_monitor.shIf there are no error at testing of the script you can continue to the next step Step 5: Run the script automatically with Cron
- Select tabs System - Scheduled Tasks
- Now add it to the column
*/15 * * * * /etc/cronscript/uptime_monitor.shIt will run the script every 15 minutes or notify you which host is down every 15 Minutes. If you beginner at cron timing you can visit this site: How to Run Cron Every 5 Minutes, Seconds, Hours, Days, Months
Troubleshooting
- OpenWrt report a host when the host is not down- Slow response time from the host try to increase -t750 to a larger value
References
- Thanks to justhrun on Kaskus for contributing the code
- Jetpack Monitor - Mail's template
- Image by Kishjar on Flickr / CC BY 2.0
Comments
Post a Comment