#!/usr/bin/perl -w ################## # # HIP monitor # # see http://www.daveyp.com/blog/index.php/archives/164/ # ############# # # Knocked together by Dave Pattern # If you spot any bugs, please let me know! # ############# # # version 1.00 - last updated 23/Jan/2006 # # ######### # # (cc) 2007 # # http://creativecommons.org/licenses/by-nc-sa/2.5/ # ##### use strict; use Mail::Sender; use LWP::UserAgent; ### some configuration options... my $sendEmailTriggerHome = 2; my $sendEmailTriggerSearch = 1; my @emailList = qw( j.smith@my.edu a.person@my.edu ); my $hipServerHome = "http://webcat.hud.ac.uk/ipac20/ipac.jsp?profile=cls#focus"; my $hipServerSearch = "http://webcat.hud.ac.uk/ipac20/ipac.jsp?session=11695J0Q62D37.11137&menu=search&aspect=subtab33&npp=10&ipp=20&spp=20&profile=cls&ri=&index=.GW&term=business&aspect=subtab33&x=0&y=0"; my $hipSearchType = "html"; my $logTemp = "./serverlogtemp.txt"; my $smtpServer = '192.168.10.20'; my $fromAddress = 'a.n.other@my.edu'; my $httpProxy = ''; ### open temp file and read previous results... my %info = ( ); if( open( IN, $logTemp ) ) { while( ) { chomp; my( $p, $v ) = split( /\t/, $_, 2 ); $info{$p} = $v; } close( IN ); } ### check home page is available... { my $ua = LWP::UserAgent->new; $ua->timeout( 10 ); if( $httpProxy ) { $ua->proxy( 'http', $httpProxy ) } my $response = $ua->get( $hipServerHome ); my $status = $response->status_line; if( $response->is_success ) { $info{home_error} = 0; } else { $info{home_error}++; if( $info{home_error} == $sendEmailTriggerHome ) { sendEmail( "HIP Home Page Error", $status, $sendEmailTriggerHome ); } } } ### check search page is returning results... { my $ua = LWP::UserAgent->new; $ua->timeout( 10 ); if( $httpProxy ) { $ua->proxy( 'http', $httpProxy ) } my $response = $ua->get( $hipServerSearch ); my $status = $response->status_line; if( $response->is_success ) { my $results = 0; my $content = $response->content; if( lc( $hipSearchType ) eq 'html' ) { if( $content =~ /\(\d+?)\<\/b\>\ \;titles matched/ ) { $results = $1; } } if( lc( $hipSearchType ) eq 'xml' ) { if( $content =~ /\(\d+?)\<\/hits\>/ ) { $results = $1; } } unless( $results ) { $info{search_error}++; if( $info{search_error} == $sendEmailTriggerSearch ) { sendEmail( "HIP Search Page Error", $status, $sendEmailTriggerSearch ); } } if( $results ) { $info{search_error} = 0; } } else { } } ### save info to temp log..... open( OUT, ">$logTemp" ); foreach( sort keys %info ) { print OUT "$_\t$info{$_}\n"; print "\t$_ = $info{$_}\n"; } close( OUT ); sub sendEmail { my $subject = shift; my $status = shift; my $trigger = shift; my $message = qq($subject\n\nThe trigger value of $trigger was reached!\n\nThe last HTTP status response recevied was:\n$status\n\n); foreach my $email ( @emailList ) { print qq(sending "$subject" to $email\n); my $sender = new Mail::Sender { smtp => $smtpServer, from => $fromAddress, }; if ($sender->MailMsg( { smtp => $smtpServer, from => $fromAddress, replyto => $fromAddress, to => $email, subject => $subject, msg => $message, priority => 1, } ) < 0) { } else { } $sender->Close; } }