#!/usr/bin/perl -I/usr/lib/bs/bin -I/usr/lib/bs/uxmon
#    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.
#

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

use common;
proginit();

use strict;

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

my $logmode = $main::opt_l;
my $dl = $main::dl;
($rule_file=$main::opt_r) if( $main::opt_r );
($loop_delay=$main::opt_t) if( $main::opt_t );
my @MOD = @main::opt_m;
(@MOD = ("$main::root/uxmon")) unless @MOD;
unshift( @INC, @MOD );

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

uxmon::init();

$uxmon::configfile = $main::opt_c;
$main::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{"INT"} = $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;
    }
    &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());
	sleep($time) if( ($time>0) && ! $hupped );
    };
    if( $hupped ) { $next_time = time; }
    $hupped = 0;
}



sub sigdie {
    &uxmon::quit_gracefully;
    exit 1;
}
