#
# Copyright (c) Xerox Corporation 1997, 1998. All rights reserved.
#
# License is granted to copy, to use, and to make and to use derivative
# works, provided that Xerox is acknowledged in all documentation
# pertaining to any such copy or derivative work. Xerox grants no other
# licenses expressed or implied. The Xerox trade name should not be used
# in any advertising without its written permission.
#
# XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
# MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE
# FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" without
# express or implied warranty of any kind.
#
# These notices must be retained in any copies of any part of this software.
#
#------------ route monitor control functions------------------
#----- exported variables:
#-----    $rtemontable = the routemonitor table output
#-----       used by update and archive
#-----    $rtemon = the name of the routemonitor executable
#-----       used by preamble
#-----    $rtemondir = the routemonitor output directory
#-----       used by preamble
#----- internal variables:
#-----    $rtemonpid = the name of the routemonitor pid file
#-----    $opt_d = the command line option for $rtemondir
#-----    $opt_E = the name of the routemonitor executable
#-----    $opt_e = the -e option to routemonitor
#-----    $opt_x = the -x option to routemonitor
#
#----- fatal errors:
#-----    start = unable to start the route monitor
#----- warnings:
#-----    nortemon = route monitor died, attempts to restart it


# initialize the route monitor
sub init_routemonitor {
    # determine the route monitor output directory
    if (defined($opt_d)) {
	$rtemondir = $opt_d;
    }
    $rtemonpid = "$rtemondir/route_monitor.pid";
    $rtemontable = "$rtemondir/route_table";
    # determine the route monitor executable name and options
    if (defined($opt_E)) {
	$rtemon = $opt_E;
    }
    if (defined($opt_e)) {
	$rtemon = "$rtemon -e $opt_e";
    }
    if (defined($opt_x)) {
	$rtemon = "$rtemon -e $opt_e -x";
    }
    $rtemon = "$rtemon -d $rtemondir";
    my($rtepid) = &check_routemonitor;
    if ($rtepid == -1) {
	# launch new route_monitor
	&start_routemonitor;
    } else {
	# reset the running routemontior
	kill USR1, $rtepid;
    }
}

# launch a new route monitor program
sub start_routemonitor {
    # run a new route monitor, exit html_maker on failure
    system("nohup $rtemon > /dev/null &");
    # give it a few seconds to initialize (probably not needed...)
    sleep 10;
    # check it started and get the new pid
    my($rtepid) = &check_routemonitor;
    if ($rtepid == -1 ) {
	&log_quit(start,$rtemon);
    }
}

sub check_routemonitor {
    # returns the route monitor pid if its alive else returns -1
    # get the routemonitor pid from its pid file
    open(RTEPID, "$rtemonpid") || return (-1);
    my($rtepid) = <RTEPID>;
    chop($rtepid);
    close(RTEPID);
    # check pid is really running
    if (kill(0,$rtepid) != 1) {
	return(-1);
    } else {
    	return($rtepid);
    }
}

sub signal_routemonitor{
    # tell the route monitor to dump its route table and reset its stats
    my($rtepid) = &check_routemonitor;
    if ($rtepid == -1) {
	# the route monitor has failed...
	&log_error(nortemon, none);
	# lets try starting over with a new route monitor
	&start_routemonitor;
	return(-1);
    } else {
	kill(USR1,$rtepid);
	# pause to make sure route table dump completes
	sleep(120);
	return(1);
    }
    
}

