#!/usr/bin/perl
##########################################################################
# CheckLink v1.1 (980307) - A must have for any kind of Internet Provider
# Author: Daniel Lafraia (lafraia@iron.com.br)
# Get other scripts on http://www.iron.com.br/~lafraia/scripts
#
# About:
# CheckLink, checks your Internet link! Wow! :) If you're a System Admin
# and you're tired of user's calls when you lose your Internet connection?
# Here is your solution! You should specify an IP to be checked, if
# it's not answering, CheckLink put a message on your index.html with
# a custom message (and sends an e-mail to you!). When your Internet
# conn. it good again, CheckLink removes the message from your index.html
# (and sends an e-mail to you!). Easy, simple and useful!
#
# (Technically) About:
# CheckLink was developed using Perl5, I guess you won't have troubles
# when installing it on any lower or grater versions. You should put a
# mark on your index.html (or any other file you want to be modified)
# called <CheckLink> (case sensitive). THIS MARK MUST BE IN ONE LINE, DO
# NOT PUT OTHER THINGS ON THE SAME LINE OF <CheckLink>.
#
# Versions:
# 1.0 - Unknown day of Feb'98 - First Version
# 1.1 - March 7th 1998	      - E-mail support added
#
# Installing:
# CheckLink must be on the crontab. Type crontab -e to edit it.
# I use the following thing here:
# 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/httpd/important/checklink.pl
# man crontab would also help you ;)
#
# Be sure to adjust your configuration.
# Questions? Suggestions? Code? Everything is welcome! E-Mail me!
##########################################################################

# Settings:
$admin = "lafraia\@iron.com.br";
$IPtocheck = "200.255.253.241";
$CustomMessageIfFails = "<hr width=95%><font color=red size=8>Internet Connection Lost!<br>Our backbone is trying to fix that!</font>";
$IndexPage = "/home/httpd/html/index.html";
$rm = "/bin/rm";
$mv = "/bin/mv";
$sendmail = "/usr/sbin/sendmail";
$ping = "/bin/ping";

# Uncomment it if your index page is a CGI and must be chmoded to 755
#$ItsCGI = 1;

# Script starts here
if (! -e $mv) { die "Cannot find $mv\n"; }
if (! -e $rm) { die "Cannot find $rm\n"; }
if (! -e $ping) { die "Cannot find $ping\n"; }
if (! -e $sendmail) { die "Cannot find $sendmail\n"; }
if (! -e $IndexPage) { die "Cannot find $IndexPage\n"; }
open (SOURCE, $IndexPage);
 @file=<SOURCE>; 
close (SOURCE);
foreach (@file) {
 if (substr($_,0,11) eq "<CheckLink>") { $fileok = 1; last; }
}
if (! $fileok) { die "Cannot find <CheckLink> on $IndexPage\n"; }
$version = "1.1";

$date = `/bin/date`;
chop($date);
@checkit = `ping -c 5 $IPtocheck`;

if ($#checkit < 5 && ! -e "/tmp/checklinkflag") {
 $motivo = "$CustomMessageIfFails";
 $do = 1;
 $dotouch = 1;
 $email = 1;
 $ok = 1;
}

if ($#checkit >= 6 && -e "/tmp/checklinkflag") {
 $motivo = "";
 $do = 1;
 $email = 1;
 $ok = 2;
 system("$rm /tmp/checklinkflag");
}

if ($do) {
  open (OUT, ">/tmp/checklink.$$");
  foreach (@file) {
   if (substr($_,0,11) eq "<CheckLink>") {
     print OUT "<CheckLink>$motivo\n";
     next;
   }
  print OUT $_;
  } 
close (OUT);
if ($dotouch) {
 open (TOUCH, ">/tmp/checklinkflag");
 close(TOUCH);
}

system "$mv $IndexPage ${IndexPage}.old";
system "$mv /tmp/checklink.$$ $IndexPage";
chmod 0755, "$IndexPage" if $ItsCGI;

if ($email) {
 if ($ok == 1) {
  $message = "Dear Admin,\n\n\tI'm here to report you that your Internet link is NOT WORKING.\n\tIt was detected on ${date}.\n";
  $message = "$message\tI'll keep checking it, when it estabilishes I'll send another mail.\n\nPing Status:\n------------\n@checkit-----\n\nCheckLink v$version - By Daniel Lafraia (lafraia\@iron.com.br)\n";
 } else {
  $message = "Dear Admin,\n\n\tI'm here to report you that your Internet link is now OK.\n\tIt was detected on ${date}.\n\tI'll stay turned if it stops again.\n\n\n-----\nCheckLink v$version - By Daniel Lafraia (lafraia\@iron.com.br)\n";
 }
 open (MAIL, "|$sendmail $admin") || die "CheckLink: Can't open Sendmail!";
 print MAIL "From: $admin\n";
 print MAIL "To: $admin\n";
 print MAIL "Subject: CheckLink Report\n\n";
 print MAIL $message;
 close (MAIL);
}
}
exit;
# EOF
