#!/usr/local/bin/perl
#
#   Test SNMP perl Module
#

%conversions = qw(INTEGER integer OCTETSTR varchar(254) COUNTER integer UINTEGER integer IPADDR varchar(254) OBJECTID varchar(254) GAGUE integer OPAQUE varchar(254) TICKS integer);

use SNMP;
use DBI;
init_mib;

#===========================================================================
#  Global defines
#===========================================================================

$hostname = 'localhost';          # Host that serves the mSQL Database
$dbname = 'snmp';                 # mySQL Database name
$doit = 1;

sub usage {
    print "$0 [-H host] [-u user] [-p password] [-v] [-h] [-n] [-d] <-m mibnode>\n";
    exit 0;
}

while ($#ARGV > -1) {
    $_ = shift @ARGV;
    usage if (/-h/);
    $hostname = shift if (/-H/);
    $mibnode = shift if (/-m/);
    $user = shift if (/-u/);
    $pass = shift if (/-p/);
    $verbose = 1 if (/-v/);
    $delete = 1 if (/-d/);
    $doit = 0 if (/-n/);
}

die "You must specify a mib node (-m node)" if (!defined($mibnode));

#===========================================================================
# Connect to the mSQL database with the appropriate driver
( $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $user, $pass))
    or die "\tConnect not ok: $DBI::errstr\n";

#===========================================================================
# Get host records from database and process

# set up mib info
my $mib = $SNMP::MIB{SNMP::translateObj($mibnode)};
my $children = $$mib{'children'};
die "$mib has no chlidren" if (ref($children) ne "ARRAY");

if ($delete) {
    $cmd = "drop table if exists $mib->{label}";
    print "cmd: $cmd\n" if ($verbose);
    $dbh->do($cmd)
	or die "\nnot ok: $DBI::errstr\n" if ($doit);
}

$cmd = "create table $mib->{label} (id integer auto_increment primary key, host varchar(16), oidindex varchar(254)";
foreach $j (@$children) {
    $cmd .= ", $j->{label} $conversions{$j->{type}}";
}
$cmd .= ", updated timestamp)";

print "cmd: $cmd\n" if ($verbose);
$dbh->do("$cmd")
    or die "\nnot ok: $DBI::errstr\n" if ($doit);
$dbh->disconnect();
