Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

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

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
toroblanco2002
Asked:
toroblanco2002
  • 6
  • 5
1 Solution
 
VryaliCommented:
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
 
VryaliCommented:
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
 
toroblanco2002Author Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
VryaliCommented:
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
 
toroblanco2002Author Commented:
Ok, as soon as I get out from work I will try this and post back.  Thanks again
0
 
VryaliCommented:
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
 
toroblanco2002Author Commented:
Ok I'm trying to close now under your solution  let me know if I did it rigth.
Thanks
0
 
toroblanco2002Author Commented:
Sorry I think I put it on the wrong one again
Can you object it again so I can accept the rigth solution
0
 
VryaliCommented:
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
 
toroblanco2002Author Commented:
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
 
toroblanco2002Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now