#!/usr/local/bin/perl -Tw
use vars qw( $VERSION $DBHOST $DBTYPE $DATABASE $DBUSER $DBPASS $DEBUG 
	     $DB $CLASS $ROOTCLASS );
$VERSION = 1.2;

###############################################################################
### Configuration + Private Data ##############################################
###############################################################################

## Load shared configurations and/or private data using 'do' commands, as
## seen below.  Note that several 'do's can be run if necessary.  

# do '/FULL/PATH/TO/CODE/TO/RUN';       

## This is the perl class that you will be using in this script.  

$CLASS   = "TCB::SysLoads";                     # Database class

## This is the root class of the above class.  Essentially a hack to let
## there be multiple modules using the same database.

$ROOTCLASS = "TCB::System";             	# Class of the database class

## Modify and uncomment this to use user code instead of just system-wide 
## modules.  Note that this path must be set up as a standard Perl tree;
## I'd personally recommend just installing things system-wide unless you're
## a developer.

# use lib '/PATH/TO/USER/CODE';
# use lib '/home/tskirvin/dev/mdtools/tcb-sysloads';

## Database Information
## You may want to set these with a common config file, using 'do FILE'.
## Also, defaults may already be set within the class; only set these if
## you want to override the defaults.

# $DBHOST   = "";               # System that hosts the database
# $DBTYPE   = "";               # The type of database that we're working on
# $DATABASE = "";               # Name of the database we're connecting to
# $DBUSER   = "";               # Username to connect to the database
# $DBPASS   = "";               # Password to connect to the database

do "/home/webserver/dbaccess/user.sysloads";	# Defines above variables

###############################################################################
### main() ####################################################################
###############################################################################

use DBIx::Frame;
use strict;

# Load the appropriate class module
{ local $@; eval "use $CLASS";  die "$@\n" if $@; }

$0 =~ s%.*/%%g;         # Lose the annoying path information
my ($machine, $load, $maximum, $date, @other) = @ARGV;

# Parse the user-input information into something useful.
$machine ||= "";
$machine =~ s/(\.ks\.uiuc\.edu)//g;	# Trim hostname of .ks.uiuc.edu
$maximum ||= $load;

error ("Usage: $0 machine load maxload date", 
       "  (date like '2001-12-30')") unless 
	($machine && defined $load && defined $maximum && 
	 $date=~ m/^\d{4}-\d\d-\d\d$/);

# Connect to the database
$DB = $ROOTCLASS->connect( $DATABASE, $DBUSER, $DBPASS, $DBHOST, $DBTYPE )
	or error("Couldn't connect to $DBHOST: $DBI::errstr");
my $error = $DBI::errstr;       # Avoid a warning, otherwise unnecessary

my $timestamp = $date;
if ($DB->select('SysLoad', { 'TimeStamp' => $timestamp, 
			     'Machine' => $machine } ) ) {
  error("Entry already exists for $machine on $timestamp, exiting");
} else {
  $DB->insert('SysLoad', { 'TimeStamp' => $timestamp, 'SysLoad' => $load, 
			   'Machine'   => $machine,   'MaxLoad' => $maximum } )
	|| error("$timestamp - $machine ($load/$maximum)", 
	         "Couldn't insert into database - $$DB{ERROR}");
}
$DB->disconnect;

exit(0);

###############################################################################
### Subroutines ###############################################################
###############################################################################

### error ( MESSAGE )
# Gives a warning of MESSAGE and exits.

sub error {
  foreach (@_) { warn "$_\n"; }
  exit(1);
}

###############################################################################
### Documentation #############################################################
###############################################################################

=head1 NAME

add-sysload - adds a system load to the sysload database

=head1 SYNOPSIS

  add-sysload cancun 1.25 2.5 2001-01-19 

See below for more information.

=head1 DESCRIPTION

add-sysload adds system load information to the TCB::SysLoads database.  
It takes the following arguments:

  add-sysload [machine] [avgload] [maxload] [date]
  
    machine	Machine we got system load information from
    avgload 	The average load over the day.
    maxload	The maximum load for the day.
    date	The date that this information was gathered.

This information is generally gathered using the sysload package, available 
at B<http://www.ks.uiuc.edu/Development/MDTools/sysload/>.

=head1 REQUIREMENTS

Perl 5.6.0 or better, MySQL, C<DBIx::Frame>, B<TCB::System>,
B<TCB::SysLoads>, the sysload package

=head1 SEE ALSO

B<sysload_graph>, B<sysload.cgi>, B<TCB::SysLoads>

=head1 AUTHOR

Written by Tim Skirvin <tskirvin@ks.uiuc.edu>.

=head1 HOMEPAGE

B<http://www.ks.uiuc.edu/Development/MDTools/tcb-sysloads>

=head1 LICENSE

This code is distributed under the University of Illinois Open Source
License.  See
C<http://www.ks.uiuc.edu/Development/MDTools/tcb-sysloads/license.html>
for
details.

=head1 COPYRIGHT

Copyright 2001-2004 by the University of Illinois Board of Trustees and
Tim Skirvin <tskirvin@ks.uiuc.edu>.

=cut

###############################################################################
### Version History ###########################################################
###############################################################################
# v1.1		Fri Jan 19 15:05:59 CST 2001
### Initial Version.  No version history was being stored at that point.
# v1.2		Fri May 14 08:32:48 CDT 2004 
### Updated for TCB::SysLoads.
