#!/usr/local/bin/perl
#
# Report info on a list of 3com devices
#
# Jim Trocki
#
# $Id: 3cinfo,v 1.2 1999/04/15 13:26:46 trockij Exp $
#
# Copyright (C) 1998 Jim Trocki
#
#    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
#
use A3Com::Sys;
use Getopt::Std;

sub help;

$COMM = $opt{"c"} || "public";
$SWITCH_LIST = "/home/mis/net/switch-config/switch-list";
%COLORS = (
    superStack3900 => "f0f0f0",
    superStack9300 => "c0c0c0",
    coreBuilder3500 => "a0a0a0",
    "" => "808080",
);

$CGI = 0;
if ($ENV{"REQUEST_METHOD"} eq "GET") {
    $CGI = 1;
    if (!open (I, $SWITCH_LIST)) {
	print <<EOF;
Content-type: text/html\r
\r
<html>
<pre>
could not open: $!
</pre>
</html>
EOF
	exit (1);
    }

    while (<I>) {
    	next if (/^\s*$/ || /^\s*#/);
	chomp;
	push @HOSTS, $_;
    }

    close (I);

} else {
    getopts ("hrsc:", \%opt);

    help() if ($opt{"h"} || @ARGV == 0);

    @HOSTS = @ARGV;
}

@l = ();
foreach $h (@HOSTS) {
    ($r, $v) = A3Com::Sys::system_info (
	    host => $h,
	    community => $COMM,
    );

    if (!defined $r) {
    	$v = {
	    hostname => $h,
	    type => "?",
	    serial_number => "?",
	    id => "?",
	    software_revision => "?",
	    hardware_revision => "?",
	    name => "?",
	};
    } else {
	$v->{"name"} =~ s/\s+/_/g;
    }

    $v->{"hostname"} = $h;
    push (@l, $v);

}

if ($opt{"s"} || $CGI) {
    @l = sort {$a->{"type"} cmp $b->{"type"}} @l;
}

if (!$CGI) {
    foreach $v (@l) {
	if ($opt{"r"}) {
	    write;
	} else {
	    print join (" ",
		$v->{"hostname"},
		$v->{"type"},
		$v->{"serial_number"},
		$v->{"id"},
		$v->{"software_revision"},
		$v->{"hardware_revision"},
		$v->{"name"},
		), "\n";
	}
    }

} else {
    print <<EOF;
Content-Type: text/html\r
\r
<html>
<head>
    <title>
    	3Com switch inventory
    </title>
</head>
<body BGCOLOR="#e1e1e1">

<h1>
    3Com switch inventory
</h1>

All data is gathered in real-time, so this list
is up-to-date.
<p>

<table border=1>
    <th> Hostname
    <th> Type
    <th> Serial number
    <th> ID
    <th> Software rev
    <th> Hardware rev
    <th> Name

EOF

    foreach $v (@l) {
	my $c = $COLORS{$v->{"type"}};

	print <<EOF;
<TR>
    <TD bgcolor=$c> $v->{"hostname"}
    <TD bgcolor=$c> $v->{"type"}
    <TD bgcolor=$c> $v->{"serial_number"}
    <TD bgcolor=$c> $v->{"id"}
    <TD bgcolor=$c> $v->{"software_revision"}
    <TD bgcolor=$c> $v->{"hardware_revision"}
    <TD bgcolor=$c> $v->{"name"}

EOF
    }

    print <<EOF;
</table>
</body>
</html>
EOF

}

exit;


sub help {
    print <<EOF;
usage: 3cinfo [-c readcommunity] [-r] host [host...]

    -c readcommunity    SNMP read community
    -r                  fancy table output (default is script-friendly output)
    -s                  sort based on switch type

EOF
    exit;
}


format STDOUT_TOP =
host          type            serial    id     sw     hw     name
---------------------------------------------------------------------------------------------
.

format STDOUT =
@<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<< @<<<<< @<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$v->{"hostname"}, $v->{"type"}, $v->{"serial_number"}, $v->{"id"}, $v->{"software_revision"}, $v->{"hardware_revision"}, $v->{"name"}
.
