#!/usr/bin/perl my @url_list = qw(http://www.xxxxx.de/ http://www.xxxxx.info/); my $sleep_minutes = 1; my $send_adress = 'webservice@xxxxxxx.de'; my $notiy_adress_1 = 'office@xxxxxxxxx.de'; my $notiy_adress_2 = 'me@xxxxxxxxxxx.de'; my $mail_prog = '/usr/sbin/sendmail -t'; my $base_dir = '/home/xxxxxx/xxxxxxx'; #------------------ Run as daemon ---------------------------------# #use Proc::Daemon; #Proc::Daemon::Init; #------------------------------------------------------------------# use strict; #use warnings; #use diagnostics; use LWP; use LWP::UserAgent; use Storable qw(lock_store lock_retrieve); use File::Spec; use FindBin; sub send_notify_mail($$); sub _get_file($); my $emacs_neeed_this = {}; my $work_dir = $FindBin::Bin; while(1){ foreach my $url(@url_list){ my $ua = LWP::UserAgent->new(); $ua->agent("Server-Control/0.1, powered by KFSW"); $ua->timeout(15); my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); my $file = _get_file($url); my $data; if(-f $file){ $data = lock_retrieve($file); }else{ $data = {}; $data->{'status'} = 'offline'; $data->{'off_notified'} = 'no'; $data->{'on_notified'} = 'no'; $data->{'time'} = time(); } if($res->is_success){ $data->{'status'} = 'online'; $data->{'time'} = time(); unless($data->{'on_notified'} eq 'yes'){ my $res = send_notify_mail($url,'online'); if($res){ $data->{'on_notified'} = 'yes'; $data->{'off_notified'} = 'no'; } } print "$url Still running \n"; }else{ unless($data->{'off_notified'} eq 'yes'){ my $res = send_notify_mail($url,'offline'); if($res){ $data->{'off_notified'} = 'yes'; $data->{'on_notified'} = 'no'; } } $data->{'status'} = 'offline'; $data->{'time'} = time(); print "$url NOT running \n" . $res->status_line . "\n"; } lock_store($data,$file); } sleep ($sleep_minutes * 60); } sub _get_file($){ my $url = shift; $url =~ s/[^a-z0-9]/_/g; my $file = File::Spec->catfile($base_dir,'memory',$url); return $file; } sub send_notify_mail($$){ my $url = shift; my $status = shift; my $mail = 'From: ' . $send_adress . "\n"; $mail .= 'To: ' . $notiy_adress_1 . "\n"; $mail .= 'Cc: ' . $notiy_adress_2 . "\n"; $mail .= 'Return-Path: ' . $send_adress . "\n"; $mail .= 'Reply-To: ' . $send_adress . "\n"; $mail .= 'Errors-To: ' . $send_adress . "\n"; $mail .= 'Subject: ' . $url . " is $status \n\n"; $mail .= 'Hello!' . "\n\n"; $mail .= 'Date ' . scalar(gmtime()) . "GMT \n"; $mail .= 'The HTTP-Server on ' . $url . " is " . $status . "\n\n"; open(MAIL,"|$mail_prog") or die("open (MAIL,\"|$mail_prog\") failed\n $! \n"); print MAIL "$mail\n"; close(MAIL) or die("close(MAIL) failed\n $! \n"); return 1; }