#!/usr/pkg/bin/perl

#           ########  hostmon-client.ultrix #######
#
## $Header: /home/vikas/src/nocol/perlnocol/hostmon-osclients/RCS/hostmon-client.ultrix,v 1.1 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
#	367/1017 files
#	298/693 inodes
#	97/266 processes
#	53924/154220 swap
#
# ultrix> pstat -T
#       371/ 609       files
#       222/ 444       gnodes
#        81/ 288       processes
#        36/  40/  76  active/reclaimable/total texts
#       255/1310       00k swap   # (the 00k indicates x100kB)

sub pstat {

    open (CMD, "pstat -T |");
    while (<CMD>) {
	if ($debug) {print STDERR "(debug)pstat: $_" ; }
	
	chop; tr/\r\n//d;
	
	if ( /^\s*(\d+)\s*\/\s*(\d+)\s*(files).*$/ ) {
	    printf "FileTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\s*\/\s*(\d+)\s*(gnodes).*$/ ) {
	    printf "InodeTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\s*\/\s*(\d+)\s*(proc).*$/ ) {
	    printf "ProcTable %d %%used\n", int($1 * 100 / $2);}
	elsif ( /^\s*(\d+)\s*\/\s*(\d+)\s+.*(swap).*$/ ) {
	    printf "SwapSpace %d MBytes\n", int( ($2 - $1) / 10);}
	
	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> nfsstat -rc
#
# Client rpc:
# calls    badcalls retrans  badxid   timeout  wait     newcred  timers
# 7915078  52102    0        345      52073    0        0        7164
#
# ultrix> nfsstat -rc
# Client rpc:
# calls      badcalls   retrans    badxid     timeout    wait       newcred
# 1232708    10         56         0          66         0          0          

sub nfsstat {
    local ($cmd) = "nfsstat";
    local ($dcalls, $dtimeo, $dretries, $tvalue, $rvalue);
    if ($OS_BSDI) { open (CMD, "$cmd |");}
    else { 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
#
# ultrix> df -l -i
# Filesystem   Total    kbytes   kbytes   %     inodes   inodes   %            
# node       kbytes   used     free     used  used     free     used Mounted on
# /dev/rz0a    32207   11226   17761    39%      799    3297    20%  /
# /dev/rz0g  1020302  860168   58104    94%    16864  194080     8%  /usr
# /dev/rz0d   984396  739510  146447    83%    16258  465022     3%  /home


sub df {

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

##
# Reports number of 512 byte blocks per second and number of transfers per sec
# for disks.
# ultrix> iostat [interval]5 [count]3
#   rz0      rz1        cpu   
# bps tps  bps tps  us ni sy id
#  40   6    0   0  24 60  6  9    <<<<< SINCE REBOOT, IGNORE
#  40   5    0   0  68 27  5  0
#  53   7    0   0  68 27  5  0

#
##
sub iostat {
    local ($cmd) = "iostat";
    local ($str, $diskno, $numdisks);
    local (@IOname);

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

    while (<CMD>) {
	chop; tr/\r\n//d;
	$str = $_ ;
	if (/^\s*bps\s+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+)/)  # extract bps & tps
    {
	if ($diskno == $numdisks) { last ;}
	++$diskno ; $str = $' ; # remaining string
	printf "IObps %d kbps $IOname[$diskno]\n", int($1 / 2);
    }

}	# 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.
#
# ultrix> vmstat -S 5 2
# procs      faults    cpu      memory              page             disk  
# r b w   in  sy  cs us sy id  avm  fre  si so  pi  po  fr  de  sr s0 
# 3 0 0  389 216  48 86  6  8  82k 6364  69103   1   0   0   0   0  0
# 3 1 0  595 174 117 84 16  0  82k 6660   0  0   0   0   0   0   0  0
## 
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;

    ## in Sun's the output of vmstat gets messed up if:
    #     -context switches are too large, should take only 3 digits.
    #     -any CPU time is 100, vmstat doesnt have any spaces in it.
    #s/100(\s+\d+){0,2}\s*$/ 100\1/;

    ##     procs         in      sy      cs      us      sy      id      avm     fre     si      so      pi     po       fr      de
    if (/^(\s+\d+){3}\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\S\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+/)
    {
	printf "MemFree %d MB\n", int($9 / 1000); # convert KB to MB for 'fre'
	print "SwapIn $10 si\n";
	print "SwapOut $11 so\n";
	print "PageIn $12 pi\n";
	print "PageOut $13 po\n";
	print "CtxtSw $4 rate\n";
	print "CPUus $5 %user\n";
	print "CPUsy $6 %system\n";
	print "CPUidle $7 %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.
#
#  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     
#
# ultrix> netstat 10
#    input   (ln0)     output            input  (Total)    output
# packets  errs  packets  errs colls  packets  errs  packets  errs colls 
#    <ignore first line>
#     349     0      275     0    16      349     0      275     0    16 
#     147     0      317     0    18      147     0      317     0    18 
#

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.ultrix  ###################
