#!/usr/local/bin/perl -w
# slreportd - load reporting deamon for perl Schedule::Load
# Note: adding -T taint checks will break the storable feature
# $Id: slreportd,v 1.10 2001/02/13 14:33:52 wsnyder Exp $
################ Introduction ################
#
# This program is Copyright 2000 by Wilson Snyder.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either the GNU General Public License or the
# Perl Artistic License, with the exception that it cannot be placed
# on a CD-ROM or similar media for commercial distribution without the
# prior approval of the author.
# 
# 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.
# 
# If you do not have a copy of the GNU General Public License write to
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
# MA 02139, USA.
######################################################################

require 5.004;
use lib 'blib/lib';	# testing
use English;
use Getopt::Long;
use Pod::Text;
use Schedule::Load::Reporter;

BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }	# Secure path

######################################################################
# configuration

######################################################################
# globals

######################################################################
# main

@Orig_ARGV = @ARGV;

my $Debug = 0;
my %server_params = ();
my $opt_quiet = 1;

Getopt::Long::config ("pass_through", "no_auto_abbrev");
$result = &GetOptions (
		       "help"		=> \&usage,
		       "debug"		=> \&debug,
		       "version"	=> \&version,
		       "quiet!"		=> \$opt_quiet,	# Debugging
		       "port=i"		=> sub {shift; $server_params{port} = shift;},
		       "dhost=s"	=> sub {shift; push @{$server_params{dhost}}, shift;},
		       "stored_filename=s"	=> sub {shift; $server_params{stored_filename} = shift;},
		       "<>"		=> \&parameter,
		       );

if (!$result) { &usage(); }

autoflush STDOUT 1;
autoflush STDERR 1;
if ($opt_quiet && !$Debug) {
    # Silence please (in case user didn't pipe when starting us)
    open(STDOUT, "/dev/null");
    open(STDERR, "/dev/null");
    open(STDIN, "/dev/null");
}

# Loop in case something kills us
while (1) {
    print "Starting server: $0\n" if $Debug;
    unless ($pid = fork) {
        Schedule::Load::Reporter->start (%server_params);
	exit(0);
    }
    waitpid($pid,0);
    warn "%Warning: Server aborted\n";
    sleep(1);
    kill 9, $pid;
    sleep(1);
    # Restart this program by re-execing; allows users to restart the deamon
    exec $0, @Orig_ARGV if -r $0;
}

exit (0);

#----------------------------------------------------------------------

sub parameter {
    my $param = shift;
    if ($param =~ /^([a-zA-Z_0-9]+)=(.*)$/) {
	$server_params{const}{$1} = $2;
    } else {
	die "%Error: Unknown parameter: $param\n";
    }
}

######################################################################

sub usage {
    print '$Id: slreportd,v 1.10 2001/02/13 14:33:52 wsnyder Exp $ ', "\n";
    $SIG{__WARN__} = sub{};	#pod2text isn't clean.
    pod2text($0);
    exit(1);
}

sub version {
    print "Version: $Schedule::Load::VERSION\n";
    print 'Id: $Id: slreportd,v 1.10 2001/02/13 14:33:52 wsnyder Exp $ ';
    print "\n";
    exit (1);
}

sub debug {
    $Debug = 1;
    $Schedule::Load::Reporter::Debug = 1;
}

######################################################################
__END__

=pod

=head1 NAME

slreportd - Distributed load reporter for perl Schedule::Load

=head1 SYNOPSIS

B<slreportd>
[ B<--help> ]
[ B<--port=>I<port> ]
[ B<--dhost=>I<host> ]
[ B<--version> ]

=head1 DESCRIPTION

slreportd will start a deamon to report machine loading for the
Schedule::Load package.

=head1 ARGUMENTS

=over 4

=item --help
Displays this message and program version and exits.

=item --port
Specifies the port number that slchoosed uses.

=item --dhost
Specifies the deamon host name that slchoosed uses.  May be specified
multiple times to specify backup hosts.  Defaults to SLCHOOSED_HOST
environment variable, which contains colon separated host names.

=item --version
Displays program version and exits.

=item {variable}={value}
Sets a arbitary constant variable to the specified value.  This variable
may be used so that a process requesting a machine can choose a machine
with specific properties.

=over 4 

=item load_limit={value}
Set a maximum number of jobs that the scheduler can run on this machine.

=back

=back

=head1 SEE ALSO

C<slchoosed>, C<Schedule::Load>, C<Schedule::Load::Server>, C<Schedule::Load::Reporter>, 

=head1 DISTRIBUTION

This package is distributed via CPAN.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=cut
######################################################################
