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

my $netdev = "";

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

# My Hawkeye object
my $Hawkeye;

# Collection info
my $OS;
my @Fields = qw( Name MTU RxOk RxErr TxOk TxErr );
my @OsInfo =
(
 {
  ostype => "IRIX.*", osrev => ".*",
  cmd => "",
 },
 {
  ostype => "HP-UX", osrev => ".*",
  cmd => "",
 },
 {
  ostype => "SunOS", osrev => "5",
  cmd => "/usr/bin/netstat -i ",
  fields => { Name => "Name",
	      MTU => "Mtu",
	      RxOk => "Ipkts",
	      RxErr => "Ierrs",
	      TxOk => "Opkts",
	      TxErr => "Oerrs" },
 },
 {
  ostype => "SunOS", osrev => "4",
  cmd => "/usr/bin/netstat -i ",
  fields => { Name => "Name",
	      MTU => "Mtu",
	      RxOk => "Ipkts",
	      RxErr => "Ierrs",
	      TxOk => "Opkts",
	      TxErr => "Oerrs" },
 },
 {
  ostype => "Linux", osrev => ".*",
  cmd => "/bin/netstat -i ",
  fields => { Name => "Iface",
	      MTU => "MTU",
	      RxOk => "RX-OK",
	      RxErr => "RX-ERR",
	      TxOk => "TX-OK",
	      TxErr => "TX-ERR" },
 },
);
my %net_info = (
		#tx_ok => "",
		#tx_err => "",
		#rx_ok => "",
		#rx_err => "",
		perc_error => "" );

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
{

    # Do we support this o/s
    die "No support" if ( ! exists $OS->{fields} );

    # Run the command, parse out the fields line
    my @NetOutput = `$OS->{cmd}`;
    chomp @NetOutput;
    my $LabelLine = shift @NetOutput;
    $LabelLine = shift @NetOutput if ( length $LabelLine < 40 );
    my @Labels = split( /\s+/, $LabelLine );

    # Try to find the relevant fields, build the Offsets hash
    my %Offsets;
    foreach my $FieldName ( @Fields )
    {
	foreach my $Offset ( 0 .. $#Labels )
	{
	    if ( $Labels[$Offset] eq $OS->{fields}{$FieldName} )
	    {
		$Offsets{$FieldName} = $Offset;
		last;
	    }
	}
	die "$FieldName not found" if ( ! exists $Offsets{$FieldName} );
    }

    # Parse the real output
    foreach my $Line ( @NetOutput )
    {
	next if ( $Line eq "" );
	$Line =~ s/(\s+)0(\d+)/$1 0 $2/g;
	my @Values = split( /\s+/, $Line );
	die "Field mismatch: '$Line'" if ( $#Values != $#Labels );

	# Hash to store the values in..
	my %ValueHash;

	# Extract out the values
	foreach my $Name ( @Fields )
	{
	    my $Offset = $Offsets{$Name};
	    $ValueHash{$Name} = $Values[$Offset];
	}

	# Get my name
	my $Name = $ValueHash{Name};

	# Extract out easy things...
	my $RxOk = $Values[$Offsets{RxOk}];
	my $RxErr = $Values[$Offsets{RxErr}];
	my $TxOk = $Values[$Offsets{TxOk}];
	my $TxErr = $Values[$Offsets{TxErr}];
	$ValueHash{RxPercErr} = 100.0 * ( $RxErr / $RxOk );
	$ValueHash{TxPercErr} = 100.0 * ( $TxErr / $TxOk );
	$ValueHash{PercErr} = 100.0 * ( ( $RxErr + $TxErr ) / ( $RxOk + $TxOk ) );

	# Store it all away...
	foreach my $Key ( keys %ValueHash )
	{
	    # Pull out & clean up the value, and store it away
	    my $Name = $Name . "_" . $Key;
	    my $Value = $ValueHash{$Key};

	    if ( $Key =~ /perc/i )
	    {
		$Hawkeye->StoreNum( $Name, sprintf( "%.02f", $Value ) );
	    }
	    elsif ( $Value =~ /^\d+$/ )
	    {
		$Hawkeye->StoreNum( $Name, $Value );
	    }
	    else
	    {
		$Hawkeye->Store( $Name, $Value );
	    }
	}
	$Hawkeye->Store( "FIELDS", join( " ", keys %ValueHash ) );
	$Hawkeye->StoreIndex( $Name );
    }

    $Hawkeye->Publish( );
}
