#!/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.
#

#=============================================================================
#
$main::Usage	  = "[-D level] [-d display[:port]] [-t timeout] [-b basedir] [-f commandfile] [command [arg1] [...]]

Commands

join group group1 ...
leave group group1 ...
remove host.check [host.check] [...]
displayname group name ...
savelogs [tag]
archivelogs directory [days]
status host.check color text\n";
#
#=============================================================================
@main::options = ( "t:i", "f:s", "d:s" );
use common;
proginit();

use FileHandle;
use strict;
use parse;
use Monitor::bb;

my $dl = $main::dl;
my $commfile = $main::opt_f;
my $timeout = 10;
($timeout = $main::opt_t) if( $main::opt_t );
my $display = $main::opt_d;
$display = "bsdisplay" unless( $display );

my $bbconn;
my $bbdisp;
my $error = 0;

$ENV{PATH} = "$main::root/bin:".$ENV{PATH};

if( $#ARGV >= 0 ) {
    command( @ARGV );
}

if( $commfile ) {
    unless( open( CMD, "<$commfile" ) ) {
	print STDERR "$man::prog: cannot open command file $commfile\n";
	exit(1);
    }
    while( <CMD> ) {
	chomp;
	next if( /^#/ || ! $_ );
	while( $_ =~ /\\$/ ) {
	    s/\\$/\n/;
	    chomp( $_ .= <CMD> );
	}
	command( parse( $_ ) );
    }
}
exit( $error );


sub command {
    my( $cmd, @args ) = @_;
    my( %cmds ) = (
	"status" => \&status,
	"remove" => \&remove,
	"join" => \&join,
	"leave" => \&leave,
	"displayname" => \&displayname,
	"savelogs" => \&savelogs,
	"archivelogs" => \&archivelogs
	);

    unless( defined $cmds{$cmd} ) {
	print STDERR "$main::prog: unknown command: $cmd\n";
	return;
    }
    my $do = $cmds{$cmd};
    my $err = &$do( @args );
    if( $err ) {
	print STDERR "$main::prog: $cmd failed with: $err\n";
	$error = 1;
    }
}



sub status {
    my( $check, $color, $text ) = @_;

    unless( grep( $color eq $_, keys %main::status_codes ) ) {
	return "unknown status color: $color";
    }
    unless( $check =~ /\./ ) {
	return( "$check should be hostname.check" );
    }
    return send_cmd( $display, "status $check $color (".time.") ".(scalar localtime)." ".$text );
}



sub remove {
    my( @check ) = @_;

    foreach my $check (@check) {
	unless( $check =~ /\./ ) {
	    return( "$check should be hostname.check" );
	}
        my $error = send_cmd( $display, "remove $check" );
	return( $error ) if( $error );
    }
}


sub leave {
    my( $group, @groups ) = @_;
    return join_or_leave( "leave", $group, @groups );
}


sub join {
    my( $group, @groups ) = @_;
    return join_or_leave( "join", $group, @groups );
}


sub join_or_leave {
    my( $mode, $group, @groups ) = @_;

    return send_cmd( $display, "$mode $group ".join(" ",@groups) );
}


sub displayname {
    my( $group, @name ) = @_;

    return send_cmd( $display, "displayname $group ".join(" ",@name) );
}


sub savelogs {
    my( $tag ) = @_;
    return send_cmd( $display, "savelogs".($tag?" $tag":"") );
}



sub archivelogs {
    my( $dir, $days ) = @_;

    unless( -w $dir ) {
	print STDERR "Usage: archivelogs directory [days]\n";
	return "You did not pass me a writable directory";
    }
    unless( $days>0 ) {
	$days = 7;
    }
    my $now = time;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now);  
    print "bundling $days days: last boundary is ".($yday%$days)." day(s) back\n" if( $main::dl>2 );
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now-($yday%$days)*24*3600);  
    $year += 1900;
    my $tag = sprintf( "%04d%02d%02d", $year, $mon+1, $mday );
    print "current tag is $tag\n" if( $main::dl>1 );
    my $file = $dir."/".$display.".$tag";
    return if( -f $file );
    send_cmd( $display, "savelogs $tag" );
    send_cmd( $display, "sendlogs $tag" );
    open( OUT, ">$file.$$" );
    my $socket = $bbconn->socket();
    print "now retrieving logs tagged $tag\n" if( $main::dl );
    eval {
        $SIG{'ALRM'} = sub { die "timed out" };
	Platform::bsalarm(30);
		 
	while( <$socket> ) {
	    if( /^--END/ ) {
		Platform::bsalarm(0);
		rename( $file.".$$", $file );
		print "received end tag - transmission complete\n" if( $main::dl );
		last;
	    }
	    print OUT;
	}
    };
    Platform::bsalarm(0);
    close OUT;
    unlink( $file.".$$" );
    return();
}





sub send_cmd {
    my( $display, $text ) = @_;
    my( $port );

    unless( $display eq $bbdisp ) {
	if( $display =~ /:(.*)/ ) {
	    $display = $`;
	    $port = $1;
	}
	$bbconn = Monitor::bb->new( $display, $timeout, $port );
	$bbdisp = $display;
    }
    print "sending $text\n" if( $main::dl );
    $text =~ s/\n/\|\>/g;
    $bbconn->sysreport( $text );
    return;
}

