#!/usr/bin/php
<? // ( -*- php -*- this makes emacs users happy too? )
/*
 Network Interface Statistics Collection Agent v2.5
 by Brett Baugh

 This is the script that checks to make sure your collectors keep running.
 Move it to some directory like /usr/sbin, and run it from crontab
 with an entry like:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * root /usr/sbin/nisca_check 2>&1 > /dev/null

 That should all be on one line; if it wraps around, get a new editor.  :)
 (You only need the "root" in there if you're putting it in "/etc/crontab".)

 Why is this such a useful script?  If you run it from crontab, it means
 you don't have to put entries in your system init scripts; it'll run the
 collectors within 5 minutes of every reboot.  And it means maintenance-
 free NISCA collectors.  You start your system, and just know without
 having to worry about it that they'll always either work or tell you
 they didn't work.

 See Global Options in the administration section to configure this script.

 */
$inhibit_html=1; // tells functions.php to just DIE if this is being run from a browser.
require_once("/etc/nisca/nisca.conf");
require_once($location["functions"]);

function check_for($in) {
  global $ps_cmd;
  if (trim($ps_cmd)=="") {
    echo "\nYou don't have a \"ps\" command set up in the Global Options admin section.\n" .
      "I can't run without it. Please go set it up now in your NISCA Administration.\n\n";
    die;
  }
  $cmd="$ps_cmd | grep '$in' | grep -v grep";
  exec($cmd, $return, $retval);
  foreach ($return as $line) {
    if (preg_match('/[\s \/]+' . $in . '/', $line)) {
      return 1;
    }
  }
  return 0;
}

function check_pid($in) {
  global $ps_cmd;
  if (trim($ps_cmd)=="") {
    echo "\nYou don't have a \"ps\" command set up in the Global Options admin section.\n" .
      "I can't run without it. Please go set it up now in your NISCA Administration.\n\n";
    die;
  }
  $ps_cmd2=preg_replace('/\s.+$/i', '', $ps_cmd);
  $cmd="$ps_cmd2 ww $in";
  exec($cmd, $return, $retval);
  foreach ($return as $line) {
    if (preg_match('/collect/i', $line)) {
      return 1;
    }
  }
  return 0;
}

$collect_running = $snmp_running = 0;

if ($collect_cmd != "") {
  if ($tmp=@file($location["local_pidfile"])) {
    $pid=trim($tmp[0]);
    $collect_running=check_pid($pid);
  }
}

if ($snmp_cmd != "") {
  if ($tmp=@file($location["snmp_pidfile"])) {
    $pid=trim($tmp[0]);
    $snmp_running=check_pid($pid);
  }
}

$failure="Success";
$mailbody="";
$lenchk=strlen($mailbody);
if ($collect_cmd != "" && $collect_running == 0) {
  exec ($collect_cmd);
  usleep(200);
  if (check_for("collect") == 0) {
    $failure="Failure";
    $mailbody .= 
      "------------------------------------------------------------------------------\n" .
      "Could not start \"collect\". You sure it's set up right?\n\n" .
      "Commands used:\n--------------\n" .
      "\"ps\" command:\t\t$ps_cmd\n\"collect\" command:\t$collect_cmd\n" .
      "------------------------------------------------------------------------------\n\n";
  } else {
    $mailbody .=
      "------------------------------------------------------------------------------\n" .
      "Successfully restarted \"collect\" at " . date($tformat) .
      " on " . date($dformat) . ".\n" .
      "------------------------------------------------------------------------------\n\n";
  }
}
if ($snmp_cmd != "" && $snmp_running == 0) {
  exec ($snmp_cmd);
  usleep(300);
  if (check_for("snmp_collect") == 0) {
    $failure="Failure";
    $mailbody .=
      "------------------------------------------------------------------------------\n" .
      "Could not start \"snmp_collect\". You sure it's set up right?\n\n" .
      "Commands used:\n--------------\n" .
      "\"ps\" command:\t\t$ps_cmd\n\"snmp_collect\" command:\t$snmp_cmd\n" .
      "------------------------------------------------------------------------------\n\n";
  } else {
    $mailbody .=
      "------------------------------------------------------------------------------\n" .
      "Successfully restarted \"snmp_collect\" at " . date($tformat) .
      " on " . date($dformat) . ".\n" .
      "------------------------------------------------------------------------------\n\n";
  }
}

if ($mailto != "" && strlen($mailbody) != $lenchk) {
  $tmp=posix_uname();
  $host=$tmp["nodename"];
  $hdr="From: nisca_check@$host\n";
  mail($mailto, "NISCA Collector Restart $failure", $mailbody, $hdr);
}
?>
