#!/usr/bin/perl -w
#
# uptime - Perl Kstat version of uptime. Solaris 8+.
#
# This program fetches similar statistics to the /usr/bin/uptime command,
# as a demonstation of the Perl Kstat module.
#
# 23-Mar-2006, ver 0.71
#
# USAGE:    uptime
#
# SEE ALSO: uptime
#
# COPYRIGHT: Copyright (c) 2006 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)
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 19-Mar-2006    Brendan Gregg    Created this.

use strict;
use Sun::Solaris::Kstat;

### Create Kstat object
my $Kstat = Sun::Solaris::Kstat->new();

### Fetch load averages
my $load1  = $Kstat->{unix}->{0}->{system_misc}->{avenrun_1min};
my $load5  = $Kstat->{unix}->{0}->{system_misc}->{avenrun_5min};
my $load15 = $Kstat->{unix}->{0}->{system_misc}->{avenrun_15min};

### Fetch boot time
my $boot = $Kstat->{unix}->{0}->{system_misc}->{boot_time};

### Processing
$load1  /= 256;
$load5  /= 256;
$load15 /= 256;
my $days = (time() - $boot) / (60 * 60 * 24);

### Print output
print scalar localtime();
printf ",  up %.2f days", $days ;
printf ",  load averages: %.2f, %.2f, %.2f\n", $load1, $load5, $load15;
