#!/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 will kill off your collector(s) so nisca_check
 can restart them (to fix the annoying stuck UDP sockets problem).
 Don't use this script unless you're using nisca_check, or the collectors
 will never get restarted.

 Move this to some directory like /usr/sbin, and run it from crontab
 with an entry like this, which would run it every day at 4 am:

0 4 * * * root /usr/sbin/nisca_kill 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".)

 This will only find collectors whose filenames contain "collect".
 And be sure you've set $location["local_pidfile"] and snmp_pidfile in
 your "nisca.conf" file... this script needs them.

 You will need to set one or two things here. If $kill_local isn't blank,
 the localhost collector will be affected by this script. Likewise for
 $kill_snmp. There's really no reason to kill the localhost collector, but
 it's in here too just for the sake of completeness.

 */
$kill_local="";
$kill_snmp="y";

$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_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_cmd=preg_replace('/\s.+$/i', '', $ps_cmd);
  $cmd="$ps_cmd ww $in";
  exec($cmd, $return, $retval);
  foreach ($return as $line) {
    if (preg_match('/collect/i', $line)) {
      return 1;
    }
  }
  return 0;
}

if ($kill_local != "") {
  if ($tmp=@file($location["local_pidfile"])) {
    $pid=trim($tmp[0]);
    if (check_pid($pid)==1) {
      $tmp=posix_kill($pid, 9);
    }
    unlink($location["local_pidfile"]);
  }
}

if ($kill_snmp != "") {
  if ($tmp=@file($location["snmp_pidfile"])) {
    $pid=trim($tmp[0]);
    if (check_pid($pid)==1) {
      $tmp=posix_kill($pid, 9);
    }
    unlink($location["snmp_pidfile"]);
  }
}
?>
