#!/usr/bin/perl
#
# prtdevs - print a few server details, such as disk and network device
#	instance names. Solaris 8+, Perl/Kstat.
#
# 15-Aug-2005, ver 0.90  (check http://www.brendangregg.com/k9toolkit.html)
#
# USAGE: prtdevs [-h]
#        prtdevs                 # device instance names
#        prtdevs -h              # print help
#
# SEE ALSO: prtconf, /etc/path_to_inst, prtdiag
#
# 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)
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 21-Apr-2005	Brendan Gregg	Created this.

use Sun::Solaris::Kstat;
my $Kstat = Sun::Solaris::Kstat->new();


### Process command line args
if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help") { &usage(); }

### Calculate Pagesize
$ENV{PATH} = "/usr/bin";
chomp($PAGESIZE = `pagesize`);
$PAGETOMB = $PAGESIZE / (1024 * 1024);


#
#  --- Main ---
#

### Fetch Values
@CPUs = fetch_cpu();
@Disks = fetch_disk();
@Nets = fetch_net();
$mem = fetch_mem();

### Print Report
print "cpu: @CPUs\n";
printf("mem: %.2f Mb\n",$mem);
print "disk: @Disks\n";
print "network: @Nets\n";

exit(0);


#
#  --- Subroutines ---
#

# fetch_cpu - fetch the instance names of the CPUs.
#
sub fetch_cpu {
	### Variable Decleration
	my $instance;
	my %Modules;
	my @CPUs = ();
 
	### Loop over all CPUs
	$Modules = $Kstat->{cpu};
	foreach $instance (keys(%$Modules)) {
		push(@CPUs,$instance);
	}
 
	### Return
	return (sort(@CPUs));
}

# fetch_mem - fetch memory size.
#
sub fetch_mem {
	### Variable Decleration
	my $ram_total;
 
	### RAM total
	$ram_total = 0;
	foreach $i (keys(%{$Kstat->{lgrp}})) {                   # instance
		foreach $c (keys(%{$Kstat->{lgrp}->{$i}})) {     # class
			$ram_total += 
			 $Kstat->{lgrp}->{$i}->{$c}->{"pages installed"};
		}
	}
	### Return
	return ($ram_total * $PAGETOMB);
}

# fetch_disk - fetch disk names.
#
sub fetch_disk {
 
	### Variable Decleration
	my ($module,$instance);
	my (%Modules,%Instances,%Names);
	my @Disks = ();
 
	### Loop over all Disks
	foreach $module (keys(%$Kstat)) {
 
	   $Modules = $Kstat->{$module};
	   foreach $instance (keys(%$Modules)) {
		$Instances = $Modules->{$instance};
		foreach $name (keys(%$Instances)) {
 
		   # Check that this is a whole disk,
		   next unless ($$Instances{$name}->{"class"} eq "disk");
		   next if $name =~ /,/;
		   push(@Disks,$name);
		}
	   }
	}
 
	### Return
	return (sort(@Disks));
}

# fetch_net - Fetch a list of network interfaces.
#
sub fetch_net {
 
	### Variable Decleration
	my ($module,$instance,$name);
	my (%Modules,%Instances,%Names);
	my @Nets = ();
 
	### Loop over all NICs
	foreach $module (keys(%$Kstat)) {
	   $Modules = $Kstat->{$module};
	   foreach $instance (keys(%$Modules)) {
		$Instances = $Modules->{$instance};
		foreach $name (keys(%$Instances)) {
		   $Names = $Instances->{$name};
  
		   # Check this is a network device.
		   # Matching on ifspeed has been more reliable than "class"
                   if (defined $$Names{ifspeed}) {
			push(@Nets,$name);
		   }
		}
	   }
	}
 
	### Return
	return (sort(@Nets));
}

# usage - print usage and exit.
#
sub usage {
        print STDERR <<END;
USAGE: prtdevs [-h] 
   eg, prtdevs               # print device instance names
       prtdevs               # print help
END
        exit 1;
}

