#! /usr/bin/perl -w
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# My Hawkeye object
my $Hawkeye;
my $OsInfo = ();
my $OS;
my @OsInfo = (
	      { ostype => "IRIX.*", osrev => ".*",
		cmd => "" },
	      { ostype => "HP-UX", osrev => ".*",
		cmd => "" },
	      { ostype => "SunOS", osrev => "5",
		cmd => "" },
	      { ostype => "SunOS", osrev => "4",
		cmd => "" },
	      { ostype => "Linux", osrev => ".*",
		cmd => "/bin/cat /proc/meminfo"}
	  );
my %mem_info = (
		mem_total => "",
		mem_free => "",
		mem_shared => "",
		swap_total => "",
		swap_free => "" );
main();

# Main
sub main
{
    $| = 1;
    Init();
    RunIt();
}

# Initialize
sub Init
{
    HawkeyeLib::DoConfig( );

    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 0 );

    # Learn about our O/S
    foreach my $OsNum ( 0 .. $#OsInfo )
    {
	my $OsType = $OsInfo[$OsNum]{ostype};
	my $OsRev = $OsInfo[$OsNum]{osrev};
	if (  ( $ENV{OS_TYPE} =~ /$OsType/ ) &&
	      ( $ENV{OS_REV}  =~ /$OsRev/ )  )
	{
	    $OS = $OsInfo[$OsNum];
	    last;
	}
    }

    # Did we find a match?
    if ( ! defined( $OS->{cmd} ) )
    {
	die "O/S Not defined\n";
    }
}

# ***********************************************
# Do the actual work
sub RunIt
{

    ###
    ### Monitors memory usage
    ###

    # Basic setup
    my %mem_info;
    my $mem_output = `$OS->{cmd}`;

    # Right now, all we support is Linux
    if ($OS->{"ostype"} eq "Linux") {
	if ($mem_output =~ m/^Mem: \s+
	    (\d+) \s+		# total
	    (\d+) \s+		# used
	    (\d+) \s+		# free
	    /mx) {
	    $mem_info{mem_total} = $1;
	    $mem_info{mem_used} = $2;
	    $mem_info{mem_free} = $3;
	    $mem_info{mem_perc_used} = sprintf( "%.2f", 100.0*$2/$1 );
	} else {
	    die "bad output from `$OS->{cmd}`\n";
	}
	if ($mem_output =~ m/^Swap: \s+
	    (\d+) \s+		# total
	    (\d+) \s+		# used
	    (\d+) \s+		# free
	    /mx) {
	    $mem_info{swap_total} = $1;
	    $mem_info{swap_used} = $2;
	    $mem_info{swap_free} = $3;
	    $mem_info{swap_perc_used} = sprintf( "%.2f", 100.0*$2/$1 );
	} else {
	    die "bad output from `$OS->{cmd}`\n";
	}
    } elsif ($OS->{"ostype"} eq "SunOS") {
	# Do nothing
    } elsif ($OS->{"ostype"} eq "HP-UX") {
	# Do nothing
    } elsif ($OS->{"ostype"} eq "IRIX.*") {
	# Do nothing
    }

    # Publish to Hawkeye
    foreach my $Key ( keys %mem_info )
    {
	# Pull out & clean up the value, and store it away
	my $Value = $mem_info{$Key};
	my $Name = $Key;
	if ( $Value =~ /^(\d+)\%$/ )
	{
	    $Value = $1;
	    $Name = "$Name (Percent)";
	    $Hawkeye->StoreNum( $Name, $Value );
	}
	elsif ( ( $Value =~ /^\d+$/ ) ||
		( $Value =~ /^\d+\.\d+$/ ) )
	{
	    $Hawkeye->StoreNum( $Name, $Value );
	}
	else
	{
	    $Hawkeye->Store( $Name, $Value );
	}
    }
    $Hawkeye->Publish( );
}
