#!/usr/local/bin/perl # Script de monitoring de machines # # pour être executé en batch, le service SCHEDULER doit être démarré sous un compte ayant des # accès d'administration. # #Level#dns-resolved-name#long description#Starting hour of alerting#stopping hour of alerting#type of control# # # # example : alarm below are all effective between 00h and 24h # 0#\\cphtount005\f$\:10#CPHTOUNT005 F:#00#24#DSK# Alarm if disk F on cphtount005 is < 10% # 0#fire02-int#BORDERWARE#00#24#PNG# Alarm if machine fire02-int doesn't respond to ping # 1#FW1#Firewall-1#00#24#PNG# Alarm if machine FW1 doesn't respond to ping # 2#cisco-ib#CISCO-IB#00#24#PNG# NB : if fire02-int doesn't respond, FW1 would not be # 1#ibsql#IB SQL#00#24#PNG# tested because it's level is greater an just below fire02-int # 1#cphnet#IB WEB#00#24#PNG# So if you have cascaded material, just the first # error cascaded material is causing the alarm # 1#cphnet.cph.be#cphnet.cph.be#00#24#WEB# Alarm if WEB site respond with Error or doesn't respond # 0#techtalk#maxibg32#00#24#PRO# Alarm if elapsed cpu time of NT PROCESS "maxibg32" # on machine "techtalk" doesn't change # # use Time::localtime; require LWP::UserAgent; require HTTP::Request; use Net::Ping; use Net::SMTP; use Win32::AdminMisc; $serveur_mail="mailserver"; # mail server support for SMTP $destinataire="name\@adress"; # adress to alarm $destIT2="name\@adress"; # Adress of alarm with SEMASCRIPT $destTEL="1111111111"; # Tel n° of SEMASCRIPT $machine="machine_name"; # machine name to prevent via "NET SEND" if SMTP server is down. $message="Le serveur mail ne répond plus"; open(FILE,"d:\\travail\\admin\\perl\\alarme_hosts.dat"); # file with machines to control $i=0; while() { $i++; ($hostlevel[$i],$hostadress[$i],$hostname[$i],$hoststarttime[$i],$hostendtime[$i],$hosttype[$i])=split/\#/; $hostdisabled[$i]=0; if ( $hosttype[$i] eq 'DSK' ) { ($hostadress[$i],$hostdskmini[$i])=split/:/, $hostadress[$i]; } # print "$hostlevel[$i] $hostname[$i] $hostadress[$i]\n"; } close(FILE); $nbelement=$i; $ua = LWP::UserAgent->new; while(1) { $failedlevel=0; ($nowsec,$nowmin,$nowhour,$nowmday,$nowmon,$nowyear,$nowwday,$nowyday,$nowisdst) = localtime(time()); $nowyear+=1900; if ( $smtp = Net::SMTP->new("$serveur_mail") ) { $smtp->quit; } else { $res=system("net send $machine $message"); } for($i=1;$i<=$nbelement;$i++) { # print "traitement de $hostname[$i] en cours...\n"; if ( $hostlevel[$i] <= $failedlevel and $hoststarttime[$i]<= $nowhour and $hostendtime[$i] >= $nowhour ) { if ( $hosttype[$i] eq 'PNG' ) { # test de l'heure undef @res; open PIPE, "ping -n 1 -w 5000 $hostadress[$i] |"; @res = ; close PIPE or die "bad pipe: $?, $!"; # # On teste la 3ème ligne pour voir si elle contient le mot ATTENTE # le mot attente apparait si c'est un systeme FRANCAIS et si le ping FAIL # if ( $res[3]=~/attente|timed|expir/ ) { $failedlevel=$hostlevel[$i]; $hostdisabled[$i]++; if ( $hostdisabled[$i] == 3 ) { $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: $hostname[$i] est inaccessible .\n"); $smtp->datasend("\nle Host $hostname[$i] est inaccessible $hostdisabled[$i] fois. \n"); $smtp->dataend(); $smtp->quit; } } else { if ( $hostdisabled[$i] >= 3) { $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: $hostname[$i] est retabli.\n"); $smtp->datasend("\nle Host $hostname[$i] est retabli apres $hostdisabled[$i] essais. \n"); $smtp->dataend(); $smtp->quit; } $failedlevel=$hostlevel[$i]+1; $hostdisabled[$i]=0; } # fin type PING } elsif ( $hosttype[$i] eq 'WEB' ) { # modele d'erreur retourné par WEB.PL # # D:\travail\admin\perl>perl web.pl # error 'ASP 0115' #

# Unexpected error #

# /index.asp #

# A trappable error occurred in an external object. The # script cannot continue running. # erreur 501 # D:\travail\admin\perl>perl web.pl # # An Error Occurred # #

An Error Occurred

# 501 Protocol scheme 'https' is not supported # # # erreur 501 # D:\travail\admin\perl> # $request = HTTP::Request->new(GET => "http://$hostadress[$i]/"); $response = $ua->request($request); if ( ( $response->is_error && $response->error_as_HTML!~/501 Protocol scheme 'https' is not supported/ && $response->error_as_HTML!~/501 Can't locate object method "new" via package "LWP::Protocol::https"/ ) || ($response->content=~/Unexpected error/) ) { $failedlevel=$hostlevel[$i]; $hostdisabled[$i]++; if ( $hostdisabled[$i] == 2 ) { # ($nowsec,$nowmin,$nowhour,$nowmday,$nowmon,$nowyear,$nowwday,$nowyday,$nowisdst) = localtime(time()); # j'utilise la commande NT car visiblement le localtime ne fonctionne pas # quand on est en batch ???? undef @res1; open PIPE, "date /t |"; @res1 = ; close PIPE or die "bad pipe: $?, $!"; undef @res2; open PIPE, "time /t |"; @res2 = ; close PIPE or die "bad pipe: $?, $!"; chop($res1[0]); $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destIT2); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destIT2\n"); $smtp->datasend("Subject: $destTEL\n"); $smtp->datasend("\n"); $smtp->datasend("le web $hostname[$i] est inaccessible le $res1[0] $res2[0]"); $smtp->datasend($response->error_as_HTML); $smtp->datasend("Contenu de la reponse =================================="); $smtp->datasend($response->content); $smtp->dataend(); $smtp->quit; } } else { if ( $hostdisabled[$i] >= 2) { $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: le WEB $hostname[$i] est retabli.\n"); $smtp->datasend("\nle WEB $hostname[$i] est retabli apres $hostdisabled[$i] essais. \n"); $smtp->dataend(); $smtp->quit; } $failedlevel=$hostlevel[$i]+1; $hostdisabled[$i]=0; } # Fin type WEB } elsif ( $hosttype[$i] eq 'DSK' ) { $pourcent=100; ($Total, $Free) = Win32::AdminMisc::GetDriveSpace($hostadress[$i]); if ($Total eq "" and $hostdisabled[$i] == 0) { $hostdisabled[$i]++; $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: le disque $hostname[$i] est injoignable...\n"); $smtp->datasend("\nDisque $hostname[$i]. $^E\n"); $smtp->dataend(); $smtp->quit; } $pourcent=($Free/$Total) * 100 if ($Total ne "" ); if ( $pourcent < $hostdskmini[$i] ) { $failedlevel=$hostlevel[$i]; $hostdisabled[$i]++; if ( $hostdisabled[$i] == 2 ) { for ($j=3 ; $j < length($Total) ; $j += 4 ) {substr($Total,-$j,0) = '.' ; } for ($j=3 ; $j < length($Free) ; $j += 4 ) {substr($Free,-$j,0) = '.' ; } $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: la place disque de $hostname[$i] est inferieure a $hostdskmini[$i] %...\n"); $smtp->datasend("\nDisque $hostname[$i] $Free ($pourcent %) libres sur $Total. \n"); $smtp->dataend(); $smtp->quit; } } # fin type DSK } elsif ( $hosttype[$i] eq 'PRO' ) { undef @res; open PIPE, "pslist \\\\$hostadress[$i] $hostname[$i] |"; @res = ; close PIPE or die "bad pipe: $?, $!"; @results=split /\s+/,$res[8]; $avant=$results[6]; undef @res; sleep(3); open PIPE, "pslist \\\\$hostadress[$i] $hostname[$i] |"; @res = ; close PIPE or die "bad pipe: $?, $!"; @results=split /\s+/,$res[8]; if ($avant eq $results[6] || $avant eq "") { $failedlevel=$hostlevel[$i]; $hostdisabled[$i]++; if ( $hostdisabled[$i] == 5 ) { $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: le process $hostname[$i] est bloque ou injoignable ? \n"); $smtp->datasend("\nLe process $hostname[$i] de la machine $hostadress[$i] est injoignable ou bloque au niveau CPU... \n"); $smtp->dataend(); $smtp->quit; } } else { if ( $hostdisabled[$i] >= 5) { $smtp = Net::SMTP->new($serveur_mail); $smtp->mail($ENV{USER}); $smtp->to($destinataire); $smtp->data(); $smtp->datasend("From: $destinataire\n"); $smtp->datasend("To: $destinataire\n"); $smtp->datasend("Subject: le process $hostname[$i] tourne a nouveau.\n"); $smtp->datasend("\nLe process $hostname[$i] de la machine $hostaddress[$i] tourne de nouveau apres $hostdisabled[$i] essais. \n"); $smtp->dataend(); $smtp->quit; } $failedlevel=$hostlevel[$i]+1; $hostdisabled[$i]=0; } } else { $failedlevel=$hostlevel[$i]+1; # $hostdisabled[$i]=0; } # fin failedlevel } # Fin scan de tous les hosts } sleep(28); #On recommence... }