Skip to content

Instantly share code, notes, and snippets.

@hatak
Created December 9, 2010 05:41
Show Gist options
  • Save hatak/734378 to your computer and use it in GitHub Desktop.
Save hatak/734378 to your computer and use it in GitHub Desktop.
check http-status from url list
#!/usr/bin/perl
#===============================================================================
#
# FILE: checker.pl
#
# USAGE: ./checker.pl [filename]
#
# DESCRIPTION: check http-status from url list
#
# OPTIONS: ---
# REQUIREMENTS: libwww-perl
# BUGS: ---
# NOTES: ---
# AUTHOR: HATAKEYAMA Hisashi (hatak), [email protected]
# COMPANY:
# VERSION: 0.01
# CREATED: 2010/12/01 13:00:00
# REVISION: ---
#
# Last Change: 2010/12/09 14:48:10 .
#===============================================================================
use strict;
use warnings;
use vars qw($VERSION);
our $VERSION = '0.01';
use HTTP::Status qw(status_message);
use constant SKIP_EXPECTED_RESULTS => 0;
use constant RESULT_FORMAT => "\t%s is %s (%s [%d])\n";
use constant AGENT => 'http-statuschecker-0.01';
main();
exit;
sub main {
_usage() if @ARGV != 1;
my $filename = $ARGV[0];
my $sites = _get_from_files($filename);
my $checker = HTTP::StatusChecker->new({agent => AGENT});
my @result;
my $unexpected = 0;
for my $site (@$sites) {
$checker->check($site);
next if $site->is_expected && SKIP_EXPECTED_RESULTS;
push @result, sprintf RESULT_FORMAT, $site->url, (($site->is_expected) ? 'OK' : 'NG'), status_message($site->status), $site->status;
++$unexpected unless $site->is_expected;
}
print '['._get_time().'] '.scalar @$sites.' sites checked complete.'."\n" if !SKIP_EXPECTED_RESULTS or $unexpected;
print '!!! FOUND UNEXPECTED STATUS : '.scalar $unexpected.' !!!'."\n" if $unexpected;
print @result if scalar @result > 0;
}
sub _get_time {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
return sprintf '%4d/%02d/%02d %02d:%02d:%02s', $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
}
sub _get_from_files {
my $filename = shift;
open my $FH, '<'.$filename || die "Error: $filename $!\n";
my @lines = <$FH>;
close $FH;
my @sites;
for my $line (@lines) {
next if ($line =~ /^#/);
chomp $line;
my @values = split /,/, $line;
die "Error: illigal format file" if (scalar @values != 2);
push @sites, HTTP::StatusChecker::Site->new(@values);
}
return \@sites;
}
sub _usage {
print <<EOF;
HTTP StatusChecker --- Check http site status from url list.
Usage : ./checker.pl [filename]
Please write to the file in CSV format.
statuscode,url
example
200,http://www.example.com/
401,http://www.example.com/admin/
EOF
exit;
}
package HTTP::StatusChecker;
use LWP::UserAgent;
use HTTP::Request;
sub new {
my $class = shift;
my $args = shift || {};
$class = ref $class if ref $class;
my $useragent = LWP::UserAgent->new();
$useragent->agent($args->{agent}) if $args->{agent};
return bless {
useragent => $useragent,
}, $class;
}
sub useragent {
my $self = shift;
if(@_) {
$self->{useragent} = $_[0];
}
return $self->{useragent};
}
sub check {
my $self = shift;
my $site = shift;
my $req = HTTP::Request->new(GET => $site->url);
my $res = $self->useragent->request($req);
$site->status($res->code);
return $site->is_expected;
}
package HTTP::StatusChecker::Site;
sub new {
my ($class, $expected, $url) = @_;
$class = ref $class if ref $class;
return bless {
expected => $expected,
url => $url,
status => '',
}, $class;
}
sub expected {
my $self = shift;
if(@_) {
$self->{expected} = $_[0];
}
return $self->{expected};
}
sub status {
my $self = shift;
if(@_) {
$self->{status} = $_[0];
}
return $self->{status};
}
sub url {
my $self = shift;
if(@_) {
$self->{url} = $_[0];
}
return $self->{url};
}
sub is_expected {
my $self = shift;
return ($self->status == $self->expected) ? 1 : 0;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment