Solved

Need Bash script to monitor and alert via email on a Buffalo router

Posted on 2010-11-24
11
893 Views
Last Modified: 2013-12-26
Hi Guys

I'm using a buffalo router at home with a dd-wrt firmware buffalo router model: WZR-HP-G300NH .
If you are familiar with dd-wrt firmware, you know you can run scripts from the router so I need a bash or python script to ping one of my computer every 10 min to check is the interface is up   and if is down to send me an email.  Just to let you know I have no programing experience but I'm interested to learn Linux shell and python if anyone can recommend me the best books out there to learn Shell and Python scripts I will really appreciated.  I hope this is not too complex to accomplish.  I know I can easily accomplish this using nagios or other mon systems but when I'm not home the only thing that is on is my router also I don't think nagios can be run withing my router.  I also browse the dd-wrt site but did not find anything similar of what I'm looking for or may be I missed the article.  Thank you so much in advance guys.
0
Comment
Question by:toroblanco2002
  • 6
  • 5
11 Comments
 
LVL 3

Expert Comment

by:Vryali
ID: 34210875
The best way, in my opinion, especially if you're trying to learn some of the bits about Linux, would be to use a simple shell script and then put it into a cronjob (rather than have it run perpetually, though that'd work too).

That said, you can do a cronjob that runs every 10 minutes like so:

Run the command crontab -e (This brings you into the crontab editor).  It uses vi, so if you aren't familiar with that, you may want to look at a cheat sheet.  Hitting 'i' puts you in insert mode, then you'd want to add the following line:

10,20,30,40,50 * * * * /home/toroblanco/script/monitor.sh

Then hit the escape key and type ':wq' without the quotes (That says to write to the crontab and then quit the editor).  You'd changet that last argument to whatever the path is to your script, by the by.

The wikipedia link for cron is pretty good: http://en.wikipedia.org/wiki/Cron

I can't recommend any books on shell scripting, as honestly, I've never found one.  The best way to learn in my experience has just been to do it and struggle through it.  That said, here's a link that should help: http://linuxconfig.org/Bash_scripting_Tutorial

When I want to learn a new language, I use the following website to give myself 'something to do' such that I can use it: http://projecteuler.net/

Finally, the actual script that I named, for example, monitor.sh

#!/bin/bash
ping -c 3 somepcname
if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M' : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
else
    date '+%Y-%m-%d %H:%M:%S Connection Available' >> /var/log/monitor.log
fi

Cheers =)
0
 
LVL 3

Accepted Solution

by:
Vryali earned 500 total points
ID: 34210897
Realized I made a mistake in my edits of the script I posted, let me fix that:

#!/bin/bash
ping -c 3 somepcname
if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
else
    date '+%m/%d %H:%M : Connection Available' >> /var/log/monitor.log
fi

Also, a bit of a further explanation on the bash script, since if you're new I image it's not self-explanatory

#!/bin/bash
This declares the shell to use to run the script.  It's simply good practice, though in general scripts will work on most modern systems without it.

ping -c 3 somepcname
Ping your PC 3 times, nothing special here.

if [[ $? != 0]]; then
This is (obviously) a basic if check, and the evaluated expression breaks down like so:
$? - the return value of the last command to run, in this case ping
!= - ! is the not operator, so this is 'not equal to'
0 - value returned by ping for an error/unsuccessful ping.

Next I just run the date command to put the date in a format I like, with a message that I then pipe to a log file I called /var/log/monitor.log.  Do note that, when using >, one > means overwrite, >> means append.

mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
This is a basic use of the mail command.  -s means subject, then the address to send to, and then an input pipe (<) that says to use /var/log/monitor for the body of the message.

The else statement is not needed, you can very safely cut that out and just have the following if you wanted

if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
fi

fi
In shell scripting, when you end a case statement or if statement, you specify this by typing it backwards (if and fi, case and esac).

Should do it =]
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34213428
Thank you I check this when I get home. Can I copy and paste this on the cron tab via the web interface of my router? Or I have to ssh to it?  Also do I have to make the script executable first? Chmod +x
0
 
LVL 3

Expert Comment

by:Vryali
ID: 34213643
Yes, you'll need to make it executable.  I'm not very familiar with the web interface, but you could try that first.  If it didn't it should work from the ssh prompt just fine.
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34213718
Ok, as soon as I get out from work I will try this and post back.  Thanks again
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 3

Expert Comment

by:Vryali
ID: 34215730
I believe you may have misclosed this?  I gave you a viable solution, some useful links, and you thanked me for it :)  need to close it under my solution for me to get points.
0
 
LVL 3

Author Closing Comment

by:toroblanco2002
ID: 34217373
Ok I'm trying to close now under your solution  let me know if I did it rigth.
Thanks
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34217402
Sorry I think I put it on the wrong one again
Can you object it again so I can accept the rigth solution
0
 
LVL 3

Expert Comment

by:Vryali
ID: 34218579
You got it right mate, cheers =]

If for some reason you run into problems with it, post up here and let me know the specific error or problem you have.  Best of luck.
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34218643
Thank you.  I have another question open maybe you know something about it. I'm to ssh from work to my router. and getting a time out connection error.   thanks
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34218655
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
In the world of WAN, QoS is a pretty important topic for most, if not all, networks. Some WAN technologies have QoS mechanisms built in, but others, such as some L2 WAN's, don't have QoS control in the provider cloud.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now