#!/usr/pkg/bin/perl

#           ########  hostmon-client.sunos4 #######
#
## $Header: /home/vikas/src/nocol/perlnocol/hostmon-osclients/RCS/hostmon-client.sunos4,v 0.10 1998/07/31 18:48:43 vikas Exp $
#
# This file is appended by 'hostmon-client'
#
###
# All the following functions must print out the following:
#   		VARIABLE VALUE UNITS COMMENT
# For variables that are part of a varying maximum, normalize it and give
# the percentage instead (e.g. free memory, etc.)
###
###

### csh% uptime
###  12:09pm  up 16 days, 19:58,  5 users,  load average: 0.32, 0.21, 0.01
###
sub uptime {
    local ($cmd) = "uptime";
    local ($value) = -1 ;
    local ($uptime) = 0;    
    open (CMD, "$cmd |");
    while (<CMD>) {
	$debug &&  print STDERR "(debug)uptime: $_" ;
	chop; tr/\r\n//d;

	if ( /^.*up\s+(\d+)\s+day.*,\s*(\d+):(\d+),.*load\s*average\S*:\s+(\S+),\s+(\S+),\s+(\S+).*$/ ) {
	    $uptime = ($1 * 1440) + ($2 * 60) + $3;
	    printf "Uptime %d mins\n", $uptime;
	    printf "Load5 %d load-5min\n", int($5) ;
	    last ;
	}
	elsif ( /^.*up\s+(\d+):(\d+),.*load\s*average\S*:\s+(\S+),\s+(\S+),\s+(\S+).*$/ ) {
	    $uptime = ($1 * 60) + $2;
	    printf "Uptime %d mins\n", $uptime;
	    printf "Load5 %d load-5min\n", int($4) ;
	    last ;
	}
	elsif ( /^.*load\s*average\S*:\s+(\S+),\s+(\S+),\s+(\S+).*$/ ) {
	    printf "Load5 %d load-5min\n", int($2) ;
	    last ;
	}
	else { print STDERR "(debug)uptime() : command format not understood.\n"; }
    }	
    close (CMD);

#    print "Load 9999 load UnParseableInput\n";
}

### sunos4% pstat -T
# (the values are all 'used/total' and NOT 'used/free')
#	367/1017 files
#	298/693 inodes
#	97/266 processes
#	53924/154220 swap

sub pstat {

    open (CMD, "pstat -T |");
    while (<CMD>) {
	if ($debug) {print STDERR "(debug)pstat: $_" ; }
	
	chop; tr/\r\n//d;
	
	if ( /^\s*(\d+)\/(\d+)\s*(files).*$/ ) {
	    printf "FileTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\/(\d+)\s*(inodes).*$/ ) {
	    printf "InodeTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\/(\d+)\s*(proc).*$/ ) {
	    printf "ProcTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\/(\d+)\s*(swap).*$/ ) {
	    printf "SwapSpace %d MBytes\n", ($2 - $1) ;}  # amount free
	
	next;
    }	
    close (CMD);

}  # end &pstat()

### nfsstat
# Have to calculate deltas in each pass since the nfsstat command does not
# give us 'rate' over a time period.  
# STORE THE OLD VALUES IN EACH PASS AND CALC DELTA EACH TIME.
#
# Timeouts are not the same as retries!  Timeouts to reflect problems 
# communicating to the server (nfs server not responding), which in turn
# is indicated by successive retries on a NFS request.  The cause for the
# retry may be local socket blockage, network loss, or a busy server.
#
# sunos4-vikas> nfsstat -rc
#
# Client rpc:
# calls    badcalls retrans  badxid   timeout  wait     newcred  timers
# 7915078  52102    0        345      52073    0        0        7164     
#
sub nfsstat {
    local ($cmd) = "nfsstat";
    local ($dcalls, $dtimeo, $dretries, $tvalue, $rvalue);
    open (CMD, "$cmd -rc |");	# '-rc' for client related data only
    while (<CMD>) {
	chop; tr/\r\n//d;
				# rate =  timeout/calls
	if (/^\s*(\d+)\s+\d+\s+\d+\s+\d+\s+(\d+).*$/ )  {
	    $debug && print STDERR "(debug)nfsstat: $_\n" ;
	    
	    $dcalls = $1 - $onfscalls ; $onfscalls = $1 ;
	    $dtimeo = $2 - $otimeo ; $otimeo = $2 ;
	    last ;		# no more lines
	}
    }				# end (while)
    close (CMD);

    if ($dcalls == 0) { $tvalue = 0 ; $rvalue = 0;}
    else {
	$tvalue = int($dtimeo * 100 / $dcalls);   # calc timeout rate
	$rvalue = int($dretries * 100 / $dcalls); # calc retry rate
    }

    if ($debug) { print STDERR "(debug)nfsstat: $dtimeo/$dcalls\n" ;}
    print "NFStimeouts $tvalue %timeouts\n";

}	# end &nfsstat()

## df
# Extract space availability stats. Printing out available MBytes and
# percent full (together they make sense).
# Note that this command prints out a number of lines with same variable
# name (one for each disk). The hostmon server picks out the worst of
# these lines.
#
# sun41-vikas> df -t 4.2
#   Filesystem            kbytes    used   avail capacity  Mounted on
#   /dev/sd0a              24239   12492    9324    57%    /
#   /dev/sd0g             191919  149692   23036    87%    /usr
#   /dev/sd3c            1230152 1062490   44647    96%    /usr/local
#
#
sub df {

    open (CMD, "df -t 4.2 |");
    while (<CMD>) {
	chop; tr/\r\n//d;
	if (/^\s*Filesystem/)  { next; } # ignore header line
	$debug && print STDERR "(debug)df: $_\n" ;
	
	if (/^.*\s+(\d+)\s+(\d+)%\s+(\/\S*)\s*$/)
	{
	    printf "DFspace_avail %d MB $3\n", int($1/1000);
	    print "DFspace_\%used $2 \%full $3\n";
	}
    }
    close (CMD);

    open (CMD, "df -i -t 4.2 |");
    while (<CMD>) {
	chop; tr/\r\n//d;
	if (/^\s*Filesystem/)  { next; } # ignore header line
	
	$debug && print STDERR "(debug)df: $_\n" ;
	
	if (/^.*\s+(\d+)\s+(\d+)%\s+(\/\S*)\s*$/)
	{
	    print "DFinodes_avail $1 inodes $3\n";
	    print "DFinodes_\%used $2 \%inodes $3\n";
	}
    }
    close (CMD);

}	# end &df()

##
# sunos41> iostat -d 5 3
#          sd1           sd2           sd3 
#  bps tps msps  bps tps msps  bps tps msps 
#   22   3  0.0   64  10  0.0    7   1  0.0 
#    0   0  0.0    0   0  0.0    0   0  0.0 
#    0   0  0.0    0   0  0.0    0   0  0.0 
#
# sunos41>  iostat -D 5 3
#          dk0           dk1           dk2 
# rps wps util  rps wps util  rps wps util 
#   1   1  2.7    2   0  2.6    0   0  1.7 
#   0   0  0.0    0   0  0.0    0   0  0.0 
#
##
sub iostat {
    local ($cmd) = "iostat";
    local ($str, $diskno, $numdisks);
    local (@IOname);

    open (CMD, "$cmd -d 5 3 |");

    while (<CMD>) {
	chop; tr/\r\n//d;
	$str = $_ ;
	if (/\s+msps|tps\s+/) { next; } # ignore header line
	if (/^\s+[A-z]+/) {	# header line containing disk names...
	    if ($debug)  { print STDERR "(debug)iostat: $_\n"; }
	    $numdisks = 0;
	    while ( $str =~ /\s*(\w+)\s+/ ) {
		if ( $1 eq 'tty' ) { $str = $'; next; }   # BSDI lists 'tty'
		if ( $1 eq 'cpu' ) { $str = $'; last; }   # 'cpu' is not a disk 
		$str = $' ; 		   # remaining string
		$IOname[++$numdisks] = $1; # extract the name of disk
	    }			# end while()
	}

    }		# end: while(<CMD>)
    close (CMD);

    # Here we have the last line from the command above
    if ($debug)  { print STDERR "(debug)iostat: $str\n"; }
    $diskno = 0;
    while ($str =~ /\s*\d+\s+\d+\s+(\d+\.\d+)/)  # extract decimal msps
    {
	++$diskno; $str = $' ; # remaining string
	print "IOseek $1 msps $IOname[$diskno]\n"; # seek time, msecs/seek
    }

    # Now run with the -D option to extract utilization for SunOS-4.x
    open (CMD, "$cmd -D 5 4 |");
    while (<CMD>) {
	chop; tr/\r\n//d;
	$str = $_;
	if (/\s+util\s*/) { next; } # ignore header line
	if (/^\s+[A-z]+/) {	# header line containing disk names...
	    if ($debug)  { print STDERR "(debug)iostat: $_\n"; }
	    $numdisks = 0;
	    while ( $str =~ /\s*(\w+)\s+/ ) {
		$str = $' ; 		   # remaining string
		$IOname[++$numdisks] = $1; # extract the name of disk
	    }
	}
    }		# end: while()
    close (CMD);

    # Here we have the last line from the command above
    if ($debug)  { print STDERR "(debug)iostat: $str\n"; }
    $diskno =0;
    while ($str =~ /\s*\d+\s+\d+\s+(\d+\.\d+)/ )  # decimal utilization
    {
	++$diskno; $str = $' ; # remaining string
	print "IObw $1 %util $IOname[$diskno]\n";
    }
}	# end &iostat()


##        vmstat
# Extract paging, real-memory, cache hits, CPU load, Context switches.
# Note: Seems like the fields are fixed length, and sometimes there are
#       no spaces between the output fields.
# sunos> vmstat -S 5 2
# procs     memory              page               disk       faults     cpu
# r b w   avm   fre  si so  pi  po  fr  de  sr d0 d1 d2 d3  in  sy  cs us sy id
# 0 0 0     0 21372   0 14   1   0   0   0   0  1  0  0  0 149 382 127 29  3 68
# 0 0 0     0 21328   5  0   0   0   0   0   0  0  0  0  0 482 247  74  1  1 99
#
## 
sub vmstat {
    local ($i) = 0;
    local($dline);
    local ($cmd) = "vmstat";

    open (CMD, "$cmd -S 5 2 |");

    while (<CMD>) { $dline = $_; next; }

    # Here we have the last data line from the command above.
    if ($debug)  { print STDERR "(debug)vmstat: $dline"; }
    $_ = $dline ;
    chop; tr/\r\n//d;
	
    # if any CPU time is 100, vmstat doesnt have any spaces in it.
    s/100(\s+\d+){0,2}\s*$/ 100\1/;
    
    # I have also found vmstat to mess up the 'cs' field (no space if
    # it or the sorrounding fields are too long). Hence taking only
    # 3 digits of the context switches.
    ###  procs  #  avm  #  fre  #  si   #  so   #   pi  #  po   # disks  #      #    cs     #  us   #  sy   #  id   ######
    if (/^(\s+\d+){3}\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)(\s+\d+)+\s+\d*((\s|\d)\d\d)\s+(\d+)\s+(\d+)\s+(\d+)\s*$/)
    {
	printf "MemFree %d MB\n", int($3 / 1000); # convert KB to MB
	print "SwapIn $4 si\n";
	print "SwapOut $5 so\n";
	print "PageIn $6 pi\n";
	print "PageOut $7 po\n";
	print "CtxtSw $9 rate\n";
	print "CPUus $11 %user\n";
	print "CPUsy $12 %system\n";
	print "CPUidle $13 %idle\n";
    }
    close (CMD);

}	# end &vmstat()


##       netstat
# Extract collision and error rates. Luckily, on SunOS there is an option
# for printing out the delta values. Since there is no easy way to 
# terminate the command after displaying 5 lines, forcibly close the
# command stream.
# Ignore the first few lines, look at the 5th and then forcibly close.
# Since the command only displays one interface's data, we look at the
# 'totals' field (anchor regexp on the right).
#
#  sunos> netstat 5
#       input   (le0)     output           input  (Total)    output       
#  packets errs  packets errs  colls  packets errs  packets errs  colls  
#   <first line with total values, ignore>
#  1467    0     64      0     0      1467    0     64      0     0     
#  1584    0     96      0     1      1584    0     96      0     1     
#
#
# 
sub netstat {
    local ($i) = 0;
    local ($cmd) = "netstat";

    local($pid) = open (CMD, "$cmd 10 |");	# 10 second sampling
    while (<CMD>) {
	if (/^\s+[A-z]+/) { next; }	# ignore header lines if any
	if (++$i < 3)  { next ;} # ignore first 3 lines
	chop; tr/\r\n//d;
	$debug && print STDERR "(debug)netstat: $_\n" ;

	if (/^\s*\d+(\s+\d+)*\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$/)
	{
	    local ($ipkts, $opkts) = ($2, $4);

	    if ($ipkts == 0) { $ipkts = 1; } # avoid division by zero
	    if ($opkts == 0) { $opkts = 1; }
	    printf "NetIErr %d PktRate\n", int ($3 * 100 / $ipkts);
	    printf "NetOErr %d PktRate\n", int ($5 * 100 / $opkts);
	    printf "NetColl %d PktRate\n", int ($6 * 100 / $opkts);
	}
	kill ('TERM', $pid) ; last ;		# no more lines
    }	
    close (CMD);
}	# end &netstat()

# require this otherwise require &xxxx will issue an error saying true not
# returned by xxxxx module.
1;


###############      END OF hostmon-client.sunos4      ####################

