#!/usr/bin/perl -w

# Copyright 1999, 2000, 2001 (c) Thomas Erskine <thomas.erskine@sourceworks.com>
# See the COPYRIGHT file with the distribution.

# datapage-inventory - make an inventory page of what the various 
#		machines we monitor are.  Uses SNMP system.sysDescr and
#		unix uname -a results, for now.
# $Id: datapage-inventory.pl,v 1.6 2001/08/28 15:22:24 remstats Exp $

# - - -   Configuration   - - -

# What is this program called, for error-messages and file-names
$main::prog = 'datapage-inventory';
# Where is the default configuration dir
$main::config_dir = '/etc/remstats/config';

# - - -   Version History   - - -

(undef, $main::version) = split(' ', '$Revision: 1.6 $');

# - - -   Setup   - - -

use Getopt::Std;
use lib '.', '/usr/lib/remstats/lib', '/usr/lib/perl5/';
require "remstats.pl";

# Parse the command-line
# STRICT use vars qw( $opt_d $opt_h $opt_f );
getopts('d:f:h');

if (defined $main::opt_h) { &usage; } # no return
if (defined $main::opt_d) { $main::debug = $main::opt_d; } else { $main::debug = 0; }
if (defined $main::opt_f) { $main::config_dir = $main::opt_f; }

&read_config_dir($main::config_dir, 'general', 'html', 'oids', 'rrds', 
	'groups', 'host-templates', 'hosts');

my $pagefile = $main::config{'DATAPAGEDIR'} .'/inventory.page';

# - - -   Mainline   - - -

my $top = '# written by '. $main::prog .' version '. $main::version .' on '. 
	&timestamp ."\n\n";
my $bottom = "\nBEGIN-PAGE\ncontent-type: text/html\n\n" .
	"<DATAPAGE::HEADER Systems Inventory><hr>" .
	"<H1>Systems Inventory</H1>\n" .
	"\n<table border=1>\n" .
	"<tr>\n<th>Host</th>\n<th>Uptime</th>\n<th>Hardware</th>\n<th>Software</th>\n";

# Collect the hosts, in the config-specified order
my @hosts = ();
foreach my $group (@{$main::config{GROUPS}}) {
# In case there is a group with no hosts
	next unless (defined @{$main::config{GROUP}{$group}});
	push @hosts, @{$main::config{GROUP}{$group}};
}

my ($host, $ip, $fixed_host, $previous_group, $sw, $hw, $uptime);
$previous_group = '';
foreach $host (@hosts) {
	$ip = &get_ip( $host);
	next unless (defined $ip);
	($fixed_host = $host) =~ tr/A-Z./a-z_/;

	unless ($previous_group eq $main::config{HOST}{$host}{GROUP}) {
		$previous_group = $main::config{HOST}{$host}{GROUP};
		$bottom .= <<"EOD_NEWGROUP";
<tr>
	<td valign=top align=left colspan=3><b><font size=+2>$previous_group</font></b></td>
</tr>
EOD_NEWGROUP
	}

# We might have unix-status info
	if (defined $main::config{HOSTCOLLECTEDBY}{'unix-status'}{$host} or
			defined $main::config{HOSTCOLLECTEDBY}{'nt-status'}{$host}) {
		$top .= <<"EOD_UNIX_TOP";
status  hw_$fixed_host $host HARDWARE
status  sw_$fixed_host $host SOFTWARE
EOD_UNIX_TOP
		$sw = 'sw_'. $fixed_host;
		$hw = 'hw_'. $fixed_host;
		$uptime = 'uptime_'. $fixed_host;
	}

# We might have SNMP data for this host instead
	elsif (defined $main::config{HOSTCOLLECTEDBY}{'snmp'}{$host}) {
		$top .= <<"EOD_SNMP_TOP";
oid     sw_$fixed_host $host sysDescr
EOD_SNMP_TOP
		$sw = 'sw_'. $fixed_host;
		$hw = ' &nbsp; ';
		$uptime = 'uptime_'. $fixed_host;
	}

	else {
		undef $sw;
		undef $hw;
		undef $uptime;
		next;
	}

# Might not have either
	next unless (defined $sw or defined $hw or defined $uptime);
	$top .= <<"EOD_UPTIME";
status	uptime_$fixed_host $host UPTIME.html
EOD_UPTIME

# Now add them to the page;
	$bottom .= <<"EOD_HOST";
<tr>
	<td valign=top><b><a href="$main::config{HTMLURL}/$host/index.cgi">$host</a></b></td>
	<td valign=top><DATAPAGE::VAR $uptime></td>
	<td valign=top><DATAPAGE::VAR $hw></td>
	<td valign=top><DATAPAGE::VAR $sw></td>
</tr>
EOD_HOST
}

# show it
open (INVENTORY, ">$pagefile") or &abort("can't write $pagefile: $!");
print INVENTORY $top . $bottom . "</table>\n<DATAPAGE::FOOTER>\n";
close (INVENTORY);

exit 0;

#----------------------------------------------------------------- usage ---
sub usage {
	print STDERR <<"EOD_USAGE";
$main::prog version $main::version
usage: $main::prog [options] file ...
where options are:
	-d	enable debugging output
	-f fff use 'fff' for config-dir [$main::config_dir]
	-h	show this help
EOD_USAGE
	exit 0;
}

#----------------------------------------------------------------- debug ---
sub debug {
	my ($msg) = @_;

	if ($main::debug) { print STDERR "DEBUG: $msg\n"; }
0;
}

#------------------------------------------------------------------- abort ---
sub abort {
	my ($msg) = @_;
	print STDERR "ABORT: $msg\n";
	exit 1;
}

#----------------------------------------------- keep_strict_happy ---
sub keep_strict_happy {
	$main::opt_h = 0;
}
