#!/usr/bin/perl
#    Big Sister network monitor
#    Copyright (C) 1998  Thomas Aeby
#
#    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.
#

#=============================================================================
#
$BigSister::common::Usage = "[-D level] [-r rule_file] [-t loop_time] [-b basedir] [-l] [-c config]";
#
#=============================================================================
@BigSister::common::options = ( "D:i", "r:s", "t:i", "m:s@", "b:s", "l", "c:s"  );

use lib "$ENV{BIGSISTER_CHROOT}/usr/share/bigsister/bin"; use lib "$ENV{BIGSISTER_CHROOT}/usr/share/bigsister/uxmon"; #inslib
use BigSister::common;
proginit();

use strict;

my $loop_delay = 60;
my $hostname = $BigSister::common::hostname;
my $rule_file = "uxmon-rules.pl";

my $logmode = $BigSister::common::opt_l;
my $dl = $BigSister::common::dl;
($rule_file=$BigSister::common::opt_r) if( $BigSister::common::opt_r );
($loop_delay=$BigSister::common::opt_t) if( $BigSister::common::opt_t );
my @MOD = @BigSister::common::opt_m;
(@MOD = ("$BigSister::common::fs{'uxmon'}")) unless @MOD;
unshift( @INC, @MOD );

require Monitor::uxmon;
require Monitor::Monitor;
use Schedule;
use Requester::Requester;

uxmon::init();

$uxmon::configfile = $BigSister::common::opt_c;
$BigSister::common::on_unix = Platforms::isunix();

&uxmon::rule_file( $rule_file );
my $file;
foreach $file( @MOD ) {
    &uxmon::module_dir( $file );
}

&uxmon::load_rules || exit 1;

if( $logmode ) {
    print "entering log mode ...\n" if( $dl );
    &uxmon::logmode();
    exit(0);
}

background() unless( $dl );



my $next_time = time();

$SIG{"TERM"} = $SIG{"QUIT"} = \&sigdie;
$SIG{"PIPE"} = "IGNORE";
$SIG{"TSTP"} = "IGNORE" unless($dl);

my( $hupped, $pid, $max, $rule_file_stat, $time, $current );
my $reconfigured = 1;

while( 1 ) {
    if( (defined &rules::reload) && &rules::reload ) {
    	&uxmon::reconfigure();
    	$reconfigured = 1;
    }
    $SIG{"HUP"} = sub { $hupped = 1 };
    print "next pass ...\n" if( $dl );
    $current = $next_time;
    $next_time += $loop_delay;
    if( $next_time < time-10*60 )  {
	while( $next_time < time ) { $next_time += $loop_delay; }
    }
#    &uxmon::load_module( "uxmon" ) && &uxmon::force_rule_reload;
    $max = 0;
    while( (($pid=Platform::bswait_nohang()) != -1) && ($max++<10) ) {
	print "child $pid terminated\n" if( $dl>2 );
    }
    if( $reconfigured ) {
	$current = $reconfigured = 0;
    }
    Requester::Requester::clear_cache();
    &uxmon::run_once( $current );
    eval {
	$SIG{"HUP"} = sub { $rule_file_stat = 0; $hupped = 1; die };
	print "sleeping for ".($next_time-time())."\n" if( $dl>4 );
	$time = ($next_time-time());
	Platform::poll_sleep($time) if( ($time>0) && ! $hupped );
	Platform::poll();
    };
    if( $hupped ) { $next_time = time; }
    $hupped = 0;
}



sub sigdie {
	print STDERR "dying on signal\n" if( $BigSister::common::dl );
    &uxmon::quit_gracefully;
	print STDERR "done resigning from server\n" if( $BigSister::common::dl>2 );
    exit 1;
}
