#!/usr/bin/perl
# ------------------------------------------------------------------------------
# File Name:            check_seti.pl
# Original Code:        Based on seti-lcd client - Tim Niemueller
# Nagios Adoption:      Thomas Nilsen - Norway
# Date:                 14/06/2003
# Version:              0.1
# Description:          Displays info on your setiathome client
# Email:                thomas.nilsen@doc-s.co.uk
# WWW:                  www.doc-s.co.uk
# ------------------------------------------------------------------------------
# Copyright (C) 2000-2001 by Tim Niemueller
# Credits go to Ethan Galstad for coding Nagios
# License GPL
# ------------------------------------------------------------------------------
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#  Created       31.08.2000
#  Last Changed  20.05.2001
#-------------------------------------------------------------------------------
#  Add the following to your checkcommands.cfg file
#
#  define command{
#        command_name    check_seti
#        command_line    $USER1$/check_seti.pl --dir $ARG1$ --info
#        }
#-------------------------------------------------------------------------------

use Getopt::Long;
use strict;
use vars qw($opt_V $opt_h $opt_t $opt_F $PROGNAME);
use lib '/usr/local/nagios/libexec/';
use utils qw($TIMEOUT %ERRORS &print_revision &support);

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

$PROGNAME="check_seti";

my $version="0.1";
my $quiet,
my $pdir;
my $pinfo;
my $ps = "/bin/ps";
my $grep = "/bin/grep";
my $state;
my $state = $ERRORS{'OK'};

GetOptions("quiet", \$quiet,
           "dir=s", \$pdir,
           "info", \$pinfo
          );

&usage if (! $pdir);

if (! -e "$pdir/state.sah") {
   print "Could not find SETI state file in given directory '$pdir'";
   $state=$ERRORS{'UNKNOWN'};
   exit $state;
} 
if (! -e $grep) {
   print "Could not find 'grep'. Please set the 'grep' variable in the script\n";
   $state=$ERRORS{'UNKNOWN'};
   exit $state;
}
if (! -e "$pdir/state.sah") {
   print "Could not find ps. Please set the 'ps' variable in the script\n";
   $state=$ERRORS{'UNKNOWN'};
   exit $state;
}  
  my %state=();
  my %info=();

  open(STATE, "$pdir/state.sah");
    while(<STATE>) {
      chomp;
      (my $key, my $value) = split(/=/, $_);
      $state{$key}=$value;
    }
  close(STATE);

  if ($pinfo) {
    open(INFO, "$pdir/user_info.sah");
      while(<INFO>) {
        chomp;
        (my $key, my $value) = split(/=/, $_);
        $info{$key}=$value;
      }
    close(INFO);
  }

  my $pscom = "$ps auxw | $grep setiathome | $grep -v grep | $grep -v xseti | $grep -v tkseti | $grep -v rsh | $grep -v ssh";
  my $pid = `$pscom`;

  my $hours = int($state{'cpu'} / 3600);
  my $mins = int( ($state{'cpu'} - ($hours * 3600)) / 60);
  my $secs = int($state{'cpu'} - ($hours * 3600) - (60 * $mins));
  my $time= sprintf("%0.2d:%0.2d:%0.2d", $hours, $mins, $secs);
  my $done=sprintf("%3.1f%%", $state{'prog'} * 100);

  # Set progress to ridiculous small number to prevent division
  # by zero if it is zero...
  $state{'prog'} = 0.0001 if (! $state{'prog'});
  my $estcputimetotal = ($state{'cpu'} * 100) / ($state{'prog'} * 100);
  my $cputimeleft = $estcputimetotal - $state{'cpu'};
  $hours = int($cputimeleft / 3600);
  $mins = int( ($cputimeleft - ($hours * 3600)) / 60);
  $secs = int($cputimeleft - ($hours * 3600) - (60 * $mins));
  my $left = sprintf("%02d:%02d:%02d", $hours, $mins, $secs);
  my $leftperc = sprintf("%3.1f%%", 100 - $state{'prog'} * 100);


  # print the results
  print "Done: $done $time - ";
  if ($pid) {
    print "Left: $leftperc $left<BR>";
  } else {
    print "{ SETI not running}\n";
    exit $ERRORS{'WARNING'};
  }

  if ($pinfo) {
    my $units = $info{'nresults'};
    $hours = int($info{'total_cpu'} / 3600);
    my $total = $hours;

    $info{'last_result_time'} =~ /\(\S{3} (\S{3}){1} \s?(\d{1,2}){1} \d{2}:\d{2}:\d{2} (\d{4}){1}\)/;
    my $last="$1 $2 $3";

    print "Completed Units: $units - CPU Time $total h<BR>";
    print "Last Completed: $last\n";
  }

exit $state;


# usage()
# Prints usage message
sub usage {
print <<EOM;

SETI\@LCDd $version
Copyleft (C) 2000 by Tim Niemueller - http://www.niemueller.net

Outputs SETI\@home stats to Nagios.

Usage:    $0 --dir <SETI data dir> [OPTIONS]

Options:  --info      Display additional info (workunits processed, total time)
          --dir       (Required) Path to seti files 
EOM
exit $ERRORS{'UNKNOWN'};
}


### END of seti-lcd.pl ###.
