|
Using PERL to
Monitor Website / Server Status
Website monitor in perl sample
#1
This is as basic as you can
get. Two simple scripts, one to run in your website and another
run from any remote website. This uses 2 websites, one to monitor
the other. The only check is the visual display showing the
current status.
In the site that is going to
be monitored, place the response script in any web directory that
can execute perl.
This program goes back to the
"hello world" concept. Just a few short lines to say
"I am OK".
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "I AM WORKING!";
exit;
This simple script is all you
need to tell the monitoring script that the website is in fact
working.
The next script you need is
the monitoring script. In this example, the script is run at the
website level using a meta refresh to execute the script every 5
minutes from a standard browser window. No files are logged, just
a visual confirmation in the browser showing the server is
working.
At first thought, you might
say how does this help. I could just check the website myself.
This allows you to check
multiple sites, multiple servers and keep a server running with a
status window showing all of your websites or servers at one time.
In more a practical configuration I would recomend using one of
the more developed examples with logging and or e-mail
notification. This will give you one easy option automating the
checking of multiple sites at regular intervals with just a few
lines of code.
For the monitoring script we
will use LWP::UserAgent to fetch the website data. Then print the
response to the browser.
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'GET',"http://yourwebsite.com/mystatus.cgi";
$res = $ua->request($req);
If the website gives us back
the message "I AM WORKING!", then we know it is ok. If
the website returns a response other than the message we
specified then the server may be up, but the website is not
working the way we expect. It could be out of bandwidth, closed
by the isp or just on its way to a server crash.
if ($res->content =~
/I AM WORKING/i){print "SERVER OK";}
else {print "WARNING! There appears to be a problem, FIX ME!";}
That is not exactly how the
script is set up, but it expains what we are trying to do.
Getting the server to tell us it is ok and we can relax our
paranoia.
Download the 2 scripts needed:
mystatus1.cgi
servermonitor1.cgi
Installation
Upload (ASC/TEXT Format) the
mystatus1.cgi to your website or each website you want to
monitor, chmod 0755 and that is all. To see if the script is
working, access the script with your browser.
Configure the servermonitor1.cgi
script, adding in all the websites you want to check. Upload
servercheck1.cgi to a remote website or home server, chmod 0755.
Then just open it in a web browser and watch it go.
Check the other sample
monitoring scripts for more detailed versions of logging and e-mail
notification. This script is about the least likely for you to
use. But it is also the easiest to show what the concept is we
are trying to achive.
Although it is nice to
download working programs and install them, our goal is to give
you the foundation to build bigger and better tools. To do that,
we need to make you understand the basic concept of how the
program works. This over simplified script is one you can
understand, but might fall short of your true goal. Sample 2 and Sample 3 will give you
more developed options.
Limitations of Sample
1. The script depends on the meta refresh and the
browser is subject to stalling. If the refresh stops, the script
stops checking the websites. There is also no record of what the
server status was, so there is no way to know if the website was
up an hour ago.
|