#!/usr/bin/perl

use Mail::Sendmail;
use strict;

my $from_email = 'Server Status <youremailhere';
my $to_email = 'Your Name Here <youremailhere>';
my $server = 'server.someplace.domain';
my $db_user = 'webuser';
my $timeout_cmd = '/usr/local/bin/timeout';
my $timeout = 10;
my $tempfile = "/tmp/$$.stderr";

# ----------------------------------------------------------------------------

Verify_Timeout_Command();

my $message;

{
  my $ping_status = Check_Ping();
  $message .= $ping_status;

  my $db_status = Check_DB();
  $message .= $db_status;

  my $cgi_status = Check_CGI();
  $message .= $cgi_status;
}

Send_Email($from_email, $to_email, $message) 
  if $message ne '';

exit;

# ----------------------------------------------------------------------------

sub Verify_Timeout_Command
{
  system("$timeout_cmd -t 0 echo -n '' 2>/dev/null") == 0
    or die<<'EOF';
You need to install the timeout script, available from
http://www.oase-shareware.org/shell/scripts/quickies/timeout
EOF
}

# ----------------------------------------------------------------------------

sub Check_Ping
{
  my $stderr;

  my $status = `ping -c 2 -w $timeout $server 2>$tempfile`;

  if (-s $tempfile)
  {
    open ERR,$tempfile;
    $stderr = join '', <ERR>;
    close ERR;
  }

  my $message;
  if ($status !~ /0\% packet loss/)
  {
    $message .= "The server is not responding to ping.\n\n";
    $message .= "Here is the status returned by ping:\n$status\n\n"
      if $status ne '';
    $message .= "Here is a message printed to STDERR by ping:\n$stderr\n\n"
      if $stderr ne '';
  }

  unlink $tempfile;

  return $message;
}

# ----------------------------------------------------------------------------

sub Check_DB
{
  my $stderr;

  # Sigh. mysqladmin -t doesn't seem to work. Use the timeout script from
  # http://www.oase-shareware.org/shell/scripts/quickies/timeout
  my $status = `$timeout_cmd -t $timeout mysqladmin -h $server -u $db_user status 2>$tempfile`;

  if (-s $tempfile)
  {
    open ERR,$tempfile;
    $stderr = join '', <ERR>;
    close ERR;
  }

  my $message;
  if ($status !~ /Uptime/)
  {
    $message .= "The database is not responding.\n\n";
    $message .= "It appears that the mysqladmin command timed out.\n\n"
      if $status eq '' && $stderr eq '';
    $message .= "Here is the status returned by mysqladmin:\n$status\n\n"
      if $status ne '';
    $message .= "Here is a message printed to STDERR by mysqladmin:\n$stderr\n\n"
      if $stderr ne '';
  }

  unlink $tempfile;

  return $message;
}

# ----------------------------------------------------------------------------

sub Check_CGI
{
  my $stderr;

  my $status = `lynx -source 'http://handlers.newsclipper.com/cgi-bin/checkversion?tag=date&ncversion=1.18&debug=0'`;

  if (-s $tempfile)
  {
    open ERR,$tempfile;
    $stderr = join '', <ERR>;
    close ERR;
  }

  my $message;
  if ($status !~ /\d\.\d/)
  {
    $message .= "checkversion for handler date is not working.\n\n";
    $message .= "Here is the status returned by the fetch:\n$status\n\n"
      if $status ne '';
    $message .= "Here is a message printed to STDERR by lynx:\n$stderr\n\n"
      if $stderr ne '';
  }

  unlink $tempfile;

  return $message;
}

# ----------------------------------------------------------------------------

sub Send_Email
{
  my $from_email = shift;
  my $to_email = shift;
  my $message = shift;

  my %mail = ( To      => $to_email,
               From    => $from_email,
               Message => $message,
               Subject => "Server is having problems!",
             );

  sendmail(%mail) or die $Mail::Sendmail::error;
}

# ----------------------------------------------------------------------------

END
{
  unlink $tempfile;
}
