Hello again,
Today's entry harkens back to a post we did a while ago regarding checking your network status using CGI. Although the Perl script we're going to run through today is somewhat similar, there are more than a few notable differences.
This Perl script should be able to be run on pretty much any flavor of Linux or Unix that supports Perl and simple Berkeley Sockets. It's also meant to be run out of cron, so that you can keep Web Server error reports (or, hopefully, a lack thereof) coming on a dependable basis. This script doesn't rely on anything in the *nix "environment" that should make it fail, so a simple entry in your crontab, like:
0,30 * * * * /wherever/you/keep/this/wsMonitor.pl
should check your Web Server every 30 minutes.
Another way you can do this (which is explored in more detail in our previous post on alternatives to using the nohup command) is to run the command (either from an init script or straight from the command line) in a backgrounded subshell, with slight wrapper script around the code, like so:
---> Sample Wrapper Shell Code <---#!/bin/sh
while :
do
/wherever/you/keep/this/wsMonitor.pl
sleep 1800
done
---> End Sample Wrapper Shell Code <---
and then kick it off with a command line similar to this (again, either from an init script or directly from your shell prompt):
host # (/full/path/to/your/wrapper_script.sh &)
Okay, now back to the Perl script itself ;) You'll note that it works very simply by sending a string (whatever you want it to be - Maybe something meaningful, like "This is just us testing," to make it easy to keep out of your Web Server access reporting) to your Web Server and sending out an email if the Web Server doesn't respond. Note that this Perl script doesn't care what kind of response it gets, since it's not sending a valid HTTP request; it only checks to make sure that the Web Server is receiving the same amount of characters we send it in our test string. It will also send a slightly different mail message if it can't open the port at all.
I've commented on each line for all the stuff you might need (or want) to modify. Hopefully this script finds you, and your Web Server, well :)
Best wishes,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/usr/bin/perl
# wsMonitor.pl - Make Sure Our Web Server Is Answering On Port 80
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use Socket;
require 5.002;
$mailer = "/usr/lib/sendmail"; # Your path to your local mail program
$remote = "webserver.xyz.com"; # Whatever host you want to check
$port = 80; # You can also check any other port, since we aren't looking for a particular response
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
&mail_out unless $port;
$iaddr = inet_aton($remote);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(BINGER, PF_INET, SOCK_STREAM, $proto) or &fail_mail;
connect(BINGER, $paddr) or &fail_mail;
$hey = send(BINGER, "TESTING TESTING 1 2 3", 21) or &fail_mail; # Note: Change the number after your message to the amount of characters being sent in between the quotes preceding it.
# In this case "TESTING TESTING 1 2 3" comes out to 21 characters, including the whitespace
exit;
sub fail_mail {
open (MAIL, "| $mailer -t") || die "Can't Open $mailer: $!\n";
print MAIL "To: interested_party\@xyz.com\n";
print MAIL "From: socketWatch\@monitorhost\n";
print MAIL "Subject: Possible problem with $remote!!!\n";
print MAIL "------------------------\n";
print MAIL "$remote incomplete Socket transmission:\n";
print MAIL "Returned $hey from 21...\n"; # Note that this 21 is the same as the 21 referenced in the socket send command above
close (MAIL);
exit;
}
sub mail_out {
open (MAIL, "| $mailer -t") || die "Can't Open $mailer: $!\n";
print MAIL "To: interested_parties\@pager.xyz.com\n";
print MAIL "From: socketWatch\@$remote\n";
print MAIL "Subject: Possible problem with $remote!!!\n";
print MAIL "------------------------\n";
print MAIL "$remote may be down: $!\n";
close (MAIL);
exit;
}
, Mike
linux unix internet technology
Friday, March 21, 2008
Checking Whether Your Web Server Is Up Using Perl Sockets Programming
Sunday, October 21, 2007
Crontab And Your Environment
Hey There,
This is a question that gets asked by everyone, at some point or another. My script works fine when I run it at the command line, but it never executes correctly when I run it in cron. Why not?
This usually boils down to a few reasons:
1. The cron entry is vague (Cron, at least in the old days) used to be very strict. That is, it ran in a totally different environment than the environment the user had while logged into the shell. It's always good practice to type
* * * * * /usr/bin/ls
rather than
* * * * * ls
2. That same rule applies for the inside of your scripts. Absolute paths to executables should be used as opposed to just the names of the executables (excluding shell built-ins, of course). So, again:
/bin/grep
rather than
grep
3. The easiest way you can make sure that you have same environment in cron as you have when running any script as the regular user is to "source" the environment into the script by adding a line like:
. /etc/profile
. /home/user/.profile
to the top of your script (below the #! line). The literal dot, space, filename patterns tells your shell to read in all variables in that named file, so you could run your cron job with the same environment as when you test it manually, which might avoid issues caused by points 1 and 2 above.
4. Finally, some newer versions of cron have configuration files (like /etc/default/cron or /etc/sysconfig/cron) where you can add additional environment variables that you always want set when you run your cron jobs. You can also do this in some newer implementations of cron by adding environment variable lines as the first lines of your crontab. Like:
PATH=/usr/bin:/usr/sbin
If all else fails, points 1 and 2 should almost always fix your problem :)
Have a great night :)
, Mike
linux unix internet technology
Posted by
Mike Golvach
at
7:53 PM
abolute path, administration, advice, cron, crontab, Environment, linux, profile, scripting, source, technology, tips, tricks, unix
Sunday, October 14, 2007
Using Cron To Send Out Raw Email
Hey there,
It's been a while since work hasn't gotten in the way of my blog-baby here ;) Unfortunately, I've been on-call and it's taken every moment of my time. I'll be handing off the pager here in just about an hour. Yay!!
Got a question about how to use "cron" to send out emails without calling a separate script and using a linux/unix built-in mailer. It's actually very simple to do; it just looks complicated.
For instance, if you wanted to send out an email every 30 minutes on the hour and half hour, with a Subject of "Your Regular Email" and a body with just a few lines in it, like:
"Howdy,
Hope you like this email!
, Mike"
You could just add this entry to your crontab:
0,30 * * * * (echo "Subject: Your Regular Email";echo;echo "Howdy";echo " Hope you like this email";echo;echo " , Mike")|/usr/lib/sendmail you@email.com
It's a simple concept - You create the email on the left side of the pipe (|) and use sendmail on the right side to mail that out to the address (you@email.com).
The most important thing to remember is to write out the entire contents of your email in a subshell (in parentheses). If you wrote all those echo statements outside of a subshell, only the last echo would get passed to Sendmail, and you'd get an email with no subject and a body that said:
" , Mike"
Generally, you only send mail this way for really simple stuff, but it can be as complicated as you like :)
, Mike
linux unix internet technology
Posted by
Mike Golvach
at
4:15 AM
administration, advice, cron, crontab, email, executable script, linux, raw email, scripting, sendmail, technology, tips, unix





