#!/usr/bin/perl
# (C) 2000 Geir Inge Jensen
#
# Rewritten to be more robust on several platforms by Olav Kolbu
# 2003/03/13 - Hacked mercilessly by Mart Brooks for LON4

$|=1;

@sets = ([0xfb1e6, 0, 40, -138, 2518],
         [0xfa237, 0, 40, -138, 1813],
         [0xfa237, 0, 40, -140, 1813],
         [0xfaf76, 0, 40, -138, 2534],
         [0xfb286, 0, 40, -138, 2502],
         [0xfb436, 0, 40, -138, 2486],
         [0xf9fd7, 0, 40, -138, 2117],
         [0xfb286, 0, 40, -138, 2518],
	 [0xfa227, 0, 40, -140, 1813],
	 [0xfa227, 0, 40, -138, 1813]);

@patterns = ('^Dell', '[:print:]', '^\w+$',
             '^\d{2}/\d{2}/\d{4}$', '[:print:]');

@header = ('','','','','','') if (-t);


chomp($hostname = `hostname`);
$hostname =~ s/^([^\.]+)\..*$/\1\t/ unless (-t);
$nl = (-t) ? "\n" : ", ";
$/ = "\0";


open(MEM, "</dev/mem") || die "Unable to open /dev/mem: $!";
foreach $set (@sets) {
	$res = $header[0] . $hostname;
	$bad = $i = 0; seek(MEM, 0, 0);
	foreach $offset (@$set) {
		seek(MEM, $offset, 1);
		#printf "\n0x%.8x: " , tell(MEM); $nl = "";
		($data = <MEM>) =~ s/[ \t\0\r\n]*$//g;
		#print "GOT $data\n";
		if ( $data !~ $patterns[$i] ) { 
#		    printf STDERR "FAILED! %s '%s' did not match '%s' ".
#                                 "on this machine (0x%.8x)\n",
#		  		  $header, ($data =~ s/[^[:print:]]//g), 
#				  $patterns[$i], $offset;
                    $bad = 1; 
                    last; 
		}
		next unless ($i++ || (-t));
		$data =~ s/ROM BIOS PLUS // unless (-t);
		#$res .= $nl . $header[$i] . $data;
		$res.="$data,";
	}
        last if not $bad;
}
close(MEM);

my $host=`hostname`;
my @hostparts=split/\./,$host;

my @lines=split/,/,$res; 

print "$hostparts[0],";
print "$lines[-3],";

my @procs=`cat /proc/cpuinfo | grep processor`;

print scalar @procs.",";

my $mem=`cat /proc/meminfo | grep MemTotal:`;
my @tmpmem=split/ /,$mem;
$mem=sprintf("%.1f",$tmpmem[-2]/1024/1024);

print "$mem";

my $partlist=`cat /proc/partitions | grep sd[a-z] | grep -v sd[a-z][0-9]`;
my %disks=();

$disks{18}=0;
$disks{36}=0;

my @parts=split/\n/,$partlist;

foreach my $part (@parts){
	$_=$part; s/\t/\ /g; while (/\ \ /){s/\ \ /\:/g;} s/\ /\:/g; while (/\:\:/){s/\:\:/\:/g;} $part=$_;
	my @pinfo=split/:/,$part;
	my $dsize=sprintf("%2f",$pinfo[3]/1024/1024);
	if ($dsize<=20){$dsize=18;} else {$dsize=36};
	$disks{$dsize}++;
}

foreach my $size (keys %disks){
	#print "Number of ".$size."gb disks: ".$disks{$size}."\n";
	print ",$disks{$size}";
}

print "\n";
my $up=`uptime`;
print "$up\n";

