My friend gave me a topic, which is to use Perl to implement a Web site monitoring script.
The main implementation of this script is a few points, each interval for a period of time to obtain the response status of the site, the Web site respond time.
If an error is answered, the answer status code is sent in the form of a message.
First, the answer state is processed.
This can be implemented using Lwp::useragent.
Implementation is simple, by looking at the CPAN example you can see HTTP://SEARCH.CPAN.ORG/~MSCHILLI/LIBWWW-PERL-6.08/LIB/LWP/USERAGENT.PM
requireLWP::useragent;my $ua= lwp::useragent->new;$ua->timeout (Ten);$ua-Env_proxy;my $response=$ua->get ('http://search.cpan.org/'); if($response-is_success) { Print $response->decoded_content;#or whatever}Else { die $response-Status_line;}
It is easy to understand, at first is a new Lwp::useragent object, set the timeout time to 10 seconds, environment variable get proxy settings, and then go to get the answer of the webpage, the corresponding $response->is_success is true when the access is successful. Otherwise, an incorrect status code is returned.
It should be explained here that the early lwp::useragent seemed to support HTTP only, not HTTPS, if the protocol using HTTPS requires another LWP::P Rotocol::https, there will be an error when the module is not installed. Modules are installed directly by CPAN, command line cpan then install Lwp::useragent,install LWP::P Rotocol::https.
Next deal with the response time of the website, found that Lwp::useragent does not seem to have this processing, so the use of net::P ing this module, the same first use is first used by CPAN to install. Because there are many modules in the CPAN, the modules may have a better choice.
The following is an example given in CPAN: http://search.cpan.org/~smpeters/Net-Ping-2.41/lib/Net/Ping.pm
#High precision syntax (requires time::hires) $p= Net::P ing->new (); $p-hires (); ($ret,$duration,$ip) =$p->ping ($host,5.5); printf("$host [IP: $ip] is alive (Packet return time:%.2f ms) \ n", +*$duration) if $ret; $p-Close();
Net::P ing->new ([$proto [, $def _timeout [, $bytes [, $device [, $tos [, $ttl]]]]);
The default value of the $proto is TCP, and the others are "UDP", "ICMP", "Stream", "syn", or "external".
Here I use SYN this, when this SYN protocol is specified, will send a TCP SYN packet to the site and return immediately, the SYN packet is sent successfully return true, send failed to return falase, because do not need to be like TCP three handshake, The returned value may be indeterminate, but it is more appropriate to send packet to multiple addresses because of the fast response.
($ret, $duration, $ip) = $p->ping ($host, 5.5);
Return success then RET will be true, get a packet return time duration, and the domain name corresponding IP address, 5.5 means the Timeout,default value is 5 seconds.
$p-Hires ();
This function needs to use the Time::hires module, in order to return the microsecond response time, it is necessary to use this function, so duration get the value is a microsecond level of floating point value, so *1000 convert it into milliseconds units.
It's also a long-sealed draft. But here are some of the Perl libraries, so just be a piece of information.
A Perl-based Web site monitoring script