#!/usr/bin/perl
#
# osstat - Print OS version statistics. Perl, Unix/Linux.
#
# This tool provides an easy way to determine if your server is upgraded, 
#  installed or rebooted while you are using it.
#
# 30-Jun-2005, ver 1.00  
#
# USAGE: osstat [-h] | [interval [count]]
#        osstat                 # print a 1 second sample
#        osstat -h              # print help
#        osstat 5               # print continually, every 5 seconds
#        osstat 1 5             # print 5 times, every 1 second
#
# FIELDS:
#        OS_Ver         # Operating System Version
#        u/s            # upgrades per second
#        r/s            # reboots per second
#        i/s            # initial installs per second
#        b/s            # BFUs per second
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# 05-May-2005	Brendan Gregg	Created this.
# 30-Jun-2005	   "      "	Added BFU/sec (thanks Keith Wesolows)


#
#  Process command line args
#
if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help" || $ARGV[0] eq "0") { &usage(); }
$sleep = $ARGV[0];
$loop = $ARGV[1];
if ($sleep eq "") {
	$sleep = 1; $loop = 0; 
} elsif ($loop eq "") {
	$loop = 2**32;
}
$PAGESIZE = 20;				# max lines per header
$lines = $PAGESIZE;			# counter for lines printed
$| = 1;
$ENV{PATH} = "/usr/bin:/usr/sbin";
$kernel = "/kernel/genunix";

#
#  Main
#
while (1) {
	### Print header
	if ($lines++ >= $PAGESIZE) {
		$lines = 0;
		printf("%8s %-20s %3s %3s %3s %3s\n",
		 "TIME","OS_Ver","u/s","r/s","i/s","b/s");
	}

	### Fetch new values
	$ups = 0; $rps = 0; $ips = 0; $bps;
	$os_old = $os;
	$ver_old = $ver;
	$time_old = $time;
	$build_old = $build;
	$time = time();
	chomp($uname = `uname -rs`);
	@Stat = stat($kernel);
	$build = $Stat[9];	# mtime
	($os,$ver) = split(' ',$uname);
	if ($count) {
		# check for version change,
		if ($ver ne $ver_old) {
			$ups = 1 / $sleep;
		} 
		# check for install change,
		if ($os ne $os_old) {
			$ips = 1 / $sleep;
		}
		# check for reboots (time jump),
		if (($time - $time_old) > 8 * $sleep) {
			$rps = 1 / $sleep;
		}
		# check for BFUs (builds),
		if ($build != $build_old) {
			$bps = 1 / $sleep;
		}
	}
	
	### Print report
	@Time = localtime();
	printf("%02d:%02d:%02d %-20s %3.1f %3.1f %3.1f %3.1f\n",
	 $Time[2],$Time[1],$Time[0],"${os}_${ver}",$ups,$rps,$ips,$bps);

	### Check for end
	last if $count++ == $loop;

	### Interval
	sleep ($sleep);
}


# usage - print usage and exit.
#
sub usage {
        print STDERR <<END;
USAGE: osstat [-h] | [interval [count]]
   eg, osstat               # print a 1 second sample
       osstat 5             # print continually every 5 seconds
       osstat 1 5           # print 5 times, every 1 second
END
        exit 1;
}

