#!/usr/bin/perl

#
#  Author: Matt Judge
#    Date: 13/01/09
#    Desc: Utility to check the loader status and when it is paused/unpaused.
#

use strict;
use Storable;
use Getopt::Long;
use POSIX qw/getpgrp tcgetpgrp/;

#
# Files used to control the loader process (and which we need to check for)
#
use constant LOADER_KILL    => "/tmp/loaderkill";
use constant LOADER_PAUSE   => "/tmp/loaderpause";
use constant LOADER_PROCESS => "loader.py";
use constant LEEWAY         => 2;  # Seconds leeway for timed interval checks

#
# Who to email about status changes
#
use constant EMAIL_LIST     => qw(vincent.lizzi@taylorandfrancis.com
                                  jane.dawson@tandf.co.uk
                                  Nicholas.Everitt@tandf.co.uk
                                  mary.seddon@tandf.co.uk
                                  virginia.klaessen@informa.com
                                  marian.martin@informa.com
                                  robert.heywood@tandf.co.uk
                                  israr.nazir@tandf.co.uk
                                  james.bailey@informa.com
                                  matthew.judge@informa.com
                                  garrick.twinney@informa.com
                                  paula.wilkinson@tandf.co.uk
                                  mike.brace@tandf.co.uk
                                  oliver.walton@informa.com
                                  michael.mcnulty@informa.com
                                  ivan.sangiao@tfinforma.com
                                  gregg.warren@tandf.co.uk
                                 );

#
# Our state information (for storing information between checks)
#
use constant STATE_FILE    => "/root/.loader_statefile";
my %status = ();

#
# Get the current time(stamp)
#
my $now = time();

#
# Running at the command line?
#
my $cmdline = 0;
if (tcgetpgrp(fileno(*TTY)) == getpgrp())
{
  $cmdline = 1;
}

#
# Routine to send an email
#
sub send_email
{
  my $subj = shift;
  my $mesg = shift;

  return if $cmdline;

  my $boundary    = "_boundary_" . time();

  open OUT, "| /usr/sbin/sendmail -t";
  print OUT "From: loader_status\@isg-prod-loader\r\n";
  foreach my $address (EMAIL_LIST)
  {
    print OUT "To: $address\r\n";
  }
  print OUT "Subject: Loader Status Change: $subj\r\n";
  print OUT "MIME-Version: 1.0\r\n";
  print OUT "Content-Type: text/plain\r\n\r\n";
  print OUT $mesg;
  close OUT;
}

#
# Check if we have saved data stored in our state file
#
if ( -f STATE_FILE )
{
  %status = %{retrieve(STATE_FILE)};  
}

#
# Update the interval between this call and the last
#
if ( defined $status{'timestamp'} )
{
  $status{'interval'} = $now - $status{'timestamp'};
}
$status{'timestamp'} = $now;

#
# Check if the loader has been set to 'kill'
#
my $loader_kill = 0;
if ( -f LOADER_KILL )
{
  $loader_kill = 1;
}
if ( defined $status{'loader_kill'} && $status{'loader_kill'} != $loader_kill )
{
  my $mesg = "(No message for inclusion)";

  if ( $loader_kill )
  {
    # New loader killfile created
    if ( -s LOADER_KILL )
    {
      $mesg = "Message as follows:\n\n";

      open INP, LOADER_KILL;
      while(<INP>)
      {
        $mesg .= $_;
      }
      close INP;
    }
    send_email "A new killfile has been created", $mesg;
  }
  else
  {
    # Loader killfile has been removed
    send_email "The killfile has been removed", $mesg;
  }
}
$status{'loader_kill'} = $loader_kill;

#
# Check if the loaders has been set to pause
#
my $loader_pause = 0;
if ( -f LOADER_PAUSE )
{
  $loader_pause = 1;
}
if ( defined $status{'loader_pause'} && $status{'loader_pause'} != $loader_pause )
{
  my $mesg = "(No message for inclusion)";

  if ( $loader_pause )
  {
    # New loader pausefile created
    if ( -s LOADER_PAUSE )
    {
      $mesg = "Message as follows:\n\n";

      open INP, LOADER_PAUSE;
      while(<INP>)
      {
        $mesg .= $_;
      }
      close INP;
    }
    send_email "A new loader pausefile has been created", $mesg;
  }
  else
  {
    # Loader pausefile has been removed
    send_email "The pausefile has been removed", $mesg;
  }
}
$status{'loader_pause'} = $loader_pause;

#
# Check the actual process is running
#
my $loader_process = 0;
system("ps h -C ${\LOADER_PROCESS} >/dev/null 2>&1");
if ( ($? >> 8) == 0 )
{
  $loader_process = 1;
}
$status{'loader_process'} = $loader_process;

store \%status, STATE_FILE;

#
# Display information and any error messages
#
if ( $cmdline )
{
  printf "Killfile: %s, Pausefile: %s, Running: %s\n", ($loader_kill ? "TRUE" : "FALSE"), ($loader_pause ? "TRUE" : "FALSE"), ($loader_process ? "TRUE" : "FALSE");
}

