#!/usr/bin/perl
###############################################################################
#
# marslib.pl - sl 5/2006
#
# Description: Misc perl routines
#
#
# History:
#       Date       Who      Description
#       ----       ---      ---------------------------------------------------
#      5/2006      SL       Create
###############################################################################

sub load_cfgfile {#loads mars config - returns Cfg hash array
	my($cfg_file) = "$_[0]";
        my($debug) = "$_[1]";
        my($line,$attr,$val);
        my(%Cfg);
    open(CFG,$cfg_file) || warn "***Unable to open cfg_file: $cfg_file\n";
    while($line = <CFG>)
      {next if ($line =~ /^#/); #skip comments
       ($attr,$val) = split('=',$line,2);
       $attr =~ s/^(\s+)//; #elim leading spaces
       $attr =~ s/(\s+)$//; #elim trailing spaces
       $val  =~ s/^(\s+)//; #elim leading spaces
       $val  =~ s/(\s+)$//; #elim trailing spaces
       if ($debug) {print "Cfg{$attr} = $val<br>\n";}
       $Cfg{"$attr"} = "$val";
       } #end while reading CFG Line
    close(CFG);
    return %Cfg;
}
       


sub load_node_ping_data {#loads node ping data - returns PING hash array
	my($dat_file) = "$_[0]";
	my($line,$attr,$val);
	my(%PING);

    open(DAT,$dat_file) || warn "***Unable to open dat_file: $dat_file\n";
    while($line = <DAT>)
      {chomp($line);
       ($attr,$val) = split(':',$line,2); $val =~ s/^\s*//;
       $PING{$attr} = "$val";
       }
    close(DAT);
    
    return %PING;
}


sub load_node_snmp_data {#loads node snmp data - returns SNMP hash array
	my($dat_file) = "$_[0]";
	my($line,$attr,$val);
	my($i,$dummy);
	my(%SNMP);

    open(DAT,$dat_file) || warn "***Unable to open dat_file: $dat_file\n";
    while($line = <DAT>)
      {chomp($line);
       ($attr,$val) = split(':',$line,2); $attr =~ s/^\s*//; $val =~ s/^\s*//;
       $SNMP{$attr} = "$val";
       next if ($attr eq "node" || $attr eq "utime");

       #Parse individual interface stats to SNMP{$component.if$i.attr}
       my($snmp_up,$uptime,$nif,@interface_stat_list) = split(',',$val);
       $SNMP{"$attr.snmp_up"} = $snmp_up;
       ($dummy,$SNMP{"$attr.uptime"}) = split('=',$uptime);
       ($dummy,$SNMP{"$attr.nif"})   = split('=',$nif);
       for ($i=1; $i<=$SNMP{"$attr.nif"}; $i++)
	  {#up,desc,type,speed,admin,oper,InOctets,InUCastPkts,InErrors,
	   #OutOctets,OutUCastPkts,OutDiscards,OutErrors
           $interface_line = $interface_stat_list[$i-1]; 
	   $interface_line =~ s/Gauge32: //g;
	   $interface_line =~ s/Counter32: //g;
	   my($desc,$type,$speed,$admin,$oper,$InOctets,$InUCastPkts,$InErrors,
	      $OutOctets,$OutUCastPkts,$OutDiscards,$OutErrors,$ip) = split('\|',$interface_line);
	   ($dummy,$SNMP{"$attr.if$i.desc"}) = split('=',$desc);
	   $SNMP{"$attr.if$i.type"}         = $type;
	   $SNMP{"$attr.if$i.speed"}        = $speed;
	   $SNMP{"$attr.if$i.admin"}        = $admin;
	   $SNMP{"$attr.if$i.oper"}         = $oper;
	   $SNMP{"$attr.if$i.InOctets"}     = $InOctets;
	   $SNMP{"$attr.if$i.InUCastPkts"}  = $InUCastPkts;
	   $SNMP{"$attr.if$i.InErrors"}     = $InErrors;
	   $SNMP{"$attr.if$i.OutOctets"}    = $OutOctets;
	   $SNMP{"$attr.if$i.OutUCastPkts"} = $OutUCastPkts;
	   $SNMP{"$attr.if$i.OutDiscards"}  = $OutDiscards;
	   $SNMP{"$attr.if$i.OutErrors"}    = $OutErrors;
	   $SNMP{"$attr.if$i.ip"}           = $ip;
	   }
       }
    close(DAT);

    
    return %SNMP;
}

sub load_node_pwr_data {#loads node pwr data - returns PWR hash array
	my($dat_file) = "$_[0]";
	my($line,$attr,$val);
	my(@att_val,$pair);

    open(DAT,$dat_file) || warn "***Unable to open dat_file: $dat_file\n";
    while($line = <DAT>)
      {chomp($line);
       ($attr,$val) = split(':',$line,2); $val =~ s/^\s*//;
       $PWR{$attr} = "$val";
       }
    close(DAT);
    
    #parse results attribute into individual elements
    (@att_val) = split(',',$PWR{'results'});
    foreach $pair (@att_val)
      {($attr,$val) = split('=',$pair,2); $val =~ s/^\s*//;
       $PWR{$attr} = "$val";
       }

    return %PWR;
}


sub gmtimestamp {local($no_gmtlabel) = $_[0];
        local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
        # zero fill hour,min,sec and month
        if ($sec < 10) {$sec = "0$sec";}
        if ($min < 10) {$min = "0$min";}
        if ($hour < 10) {$hour = "0$hour";}
        $mon += 1; #from gmtime defined 0-11, make it 1-12
        if ($mon < 10) {$mon = "0$mon";}
        if ($mday < 10) {$mday = "0$mday";}
   	#make year 4-digits - Note: Year is years since 1900
   	$year += 1900; 
	if ($no_gmtlabel)
             {return "$year/$mon/$mday $hour:$min:$sec";}
        else {return "$year/$mon/$mday $hour:$min:$sec GMT";}
}


sub sec2hhmmss {#takes total seconds and returns d hh:mm:ss
	local($sec) = $_[0];
	local($dy,$hr,$mn,$sc);
	$dy = int($sec/86400);
	$hr = int(($sec - ($dy*86400))/3600);
	$mn = int(($sec - ($dy*86400) - ($hr*3600))/60);
	$sc = $sec - ($dy*86400) - ($hr*3600) - ($mn*60);
	if ($hr < 10) {$hr = "0$hr";}
	if ($mn < 10) {$mn = "0$mn";}
	if ($sc < 10) {$sc = "0$sc";}

	if ($dy == 0) {return "$hr:$mn:$sc";}
	         else {return "$dy $hr:$mn:$sc";}
}


1;
