| 1 |
######################################################################### |
| 2 |
package AnyData::Storage::SNMP; |
| 3 |
######################################################################### |
| 4 |
|
| 5 |
## XXX: TODO: |
| 6 |
## scalar sets? |
| 7 |
## multi-hosts |
| 8 |
|
| 9 |
$AnyData::Storage::VERSION = '5.09052'; |
| 10 |
use strict; |
| 11 |
use warnings; |
| 12 |
|
| 13 |
use vars qw(@basecols); |
| 14 |
|
| 15 |
@AnyData::Storage::SNMP::basecols = qw(hostname iid); |
| 16 |
$AnyData::Storage::SNMP::iidptr = 1; # must match array column of above |
| 17 |
@AnyData::Storage::SNMP::basetypes = qw(OCTETSTR OBJECTID); |
| 18 |
$AnyData::Storage::SNMP::debug = 0; |
| 19 |
$AnyData::Storage::SNMP::debugre = undef; |
| 20 |
|
| 21 |
use Data::Dumper; |
| 22 |
use AnyData::Storage::File; |
| 23 |
use SNMP; |
| 24 |
SNMP::init_snmp("AnyData::SNMP"); |
| 25 |
|
| 26 |
sub new { |
| 27 |
# DEBUG("calling AnyData::Storage::SNMP new\n"); |
| 28 |
# DEBUG("new storage: ",Dumper(\@_),"\n"); |
| 29 |
my $class = shift; |
| 30 |
my $self = shift || {}; |
| 31 |
$self->{open_mode} = 'c'; |
| 32 |
return bless $self, $class; |
| 33 |
} |
| 34 |
|
| 35 |
sub open_table { |
| 36 |
DEBUG("calling AnyData::Storage::SNMP open_table\n"); |
| 37 |
my ($self, $parser, $table, $mode, $tname) = @_; |
| 38 |
$self->{'process_table'} = $tname; |
| 39 |
DEBUG("open_table: ",Dumper(\@_),"\n"); |
| 40 |
} |
| 41 |
|
| 42 |
sub get_col_names { |
| 43 |
DEBUG("calling AnyData::Storage::SNMP get_col_names\n"); |
| 44 |
my ($self, $parser, $tname) = @_; |
| 45 |
DEBUG("get_col_names\n",Dumper(\@_),"\n"); |
| 46 |
$tname = $self->{'process_table'} if (!$tname); |
| 47 |
|
| 48 |
# get cached previous results |
| 49 |
return $self->{col_names}{$tname} if (defined($self->{col_names}{$tname})); |
| 50 |
|
| 51 |
# table name |
| 52 |
$tname = $self->{'process_table'} if (!$tname); |
| 53 |
|
| 54 |
# mib node setup |
| 55 |
my $mib = $SNMP::MIB{$tname} || return warn "no such table $tname"; |
| 56 |
my $entry = $mib->{'children'}[0]; |
| 57 |
|
| 58 |
# base columns and types |
| 59 |
my @cols = @AnyData::Storage::SNMP::basecols; |
| 60 |
my @types = @AnyData::Storage::SNMP::basetypes; |
| 61 |
my %donecol; |
| 62 |
my $count = $#cols; |
| 63 |
|
| 64 |
foreach my $index (@{$entry->{'indexes'}}) { |
| 65 |
push @cols, $index; |
| 66 |
push @types, $SNMP::MIB{$index}{type}; |
| 67 |
$donecol{$index} = 1; |
| 68 |
$count++; |
| 69 |
if ($SNMP::MIB{$index}{parent}{label} eq $entry->{label}) { |
| 70 |
# this index is a member of this table |
| 71 |
$self->{columnmap}[$count] = $index; |
| 72 |
$self->{coloffset} += 1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
# search children list |
| 77 |
foreach my $child ( sort { $a->{'subID'} <=> $b->{'subID'} } @{$entry->{'children'}}) { |
| 78 |
push @{$self->{real_cols}}, $child->{'label'}; |
| 79 |
next if ($donecol{$child->{label}}); |
| 80 |
push @cols, $child->{'label'}; |
| 81 |
push @types, $child->{'type'}; |
| 82 |
$count++; |
| 83 |
$self->{columnmap}[$count] = $child->{'label'}; |
| 84 |
} |
| 85 |
|
| 86 |
# save for later. |
| 87 |
$parser->{col_names} = \@cols; |
| 88 |
$self->{col_types} = \@types; |
| 89 |
$self->{col_names} = \@cols; |
| 90 |
map { $self->{col_uc_map}{uc($_)} = $_ } @cols; |
| 91 |
return \@cols; |
| 92 |
} |
| 93 |
|
| 94 |
sub set_col_nums { |
| 95 |
DEBUG("calling AnyData::Storage::SNMP set_col_nums\n"); |
| 96 |
DEBUG("set_col_nums\n",Dumper(\@_),"\n"); |
| 97 |
my ($self, $tname) = @_; |
| 98 |
return $self->{col_nums} if (defined($self->{col_nums})); |
| 99 |
my $mib = $SNMP::MIB{$tname}; |
| 100 |
my $entry = $mib->{'children'}[0]; |
| 101 |
my (%cols, %mibnodes); |
| 102 |
my $cnt = -1; |
| 103 |
|
| 104 |
foreach my $i (@{$self->{col_names}}) { |
| 105 |
$cols{$i} = ++$cnt; |
| 106 |
} |
| 107 |
|
| 108 |
$self->{col_nums} = \%cols; |
| 109 |
return \%cols; |
| 110 |
} |
| 111 |
|
| 112 |
# not needed? |
| 113 |
sub get_file_handle { |
| 114 |
DEBUG("calling AnyData::Storage::SNMP get_file_handle\n"); |
| 115 |
DEBUG("get_file_handle\n",Dumper(\@_),"\n"); |
| 116 |
return shift; |
| 117 |
} |
| 118 |
|
| 119 |
# not needed? |
| 120 |
sub get_file_name { |
| 121 |
DEBUG("calling AnyData::Storage::SNMP get_file_name\n"); |
| 122 |
DEBUG("get_file_name\n",Dumper(\@_),"\n"); |
| 123 |
my $self = shift; |
| 124 |
return $self->{process_table} || $self->{table_name}; |
| 125 |
} |
| 126 |
|
| 127 |
sub truncate { |
| 128 |
DEBUG("calling AnyData::Storage::SNMP truncate\n"); |
| 129 |
my ($self) = @_; |
| 130 |
DEBUG("trunacte, ", Dumper(@_)); |
| 131 |
|
| 132 |
# We must know how to delete rows or else this is all pointless. |
| 133 |
# return $self->{col_nums}{$tname} if (defined($self->{col_nums}{$tname})); |
| 134 |
my $tablemib = $SNMP::MIB{$self->{process_table}}; |
| 135 |
my $entrymib = $tablemib->{'children'}[0]; |
| 136 |
my ($delcolumn, $delcolumnname, $delcolumnvalue); |
| 137 |
|
| 138 |
foreach my $child (@{$entrymib->{'children'}}) { |
| 139 |
if ($child->{'textualConvention'} eq 'RowStatus') { |
| 140 |
$delcolumn = $child->{subID}; |
| 141 |
$delcolumnname = $child->{label}; |
| 142 |
$delcolumnvalue = 6; # destroy |
| 143 |
last; |
| 144 |
} |
| 145 |
} |
| 146 |
if (!$delcolumn) { |
| 147 |
return warn "Can't (or don't know how to) delete from table $self->{process_table}. Failing.\n"; |
| 148 |
} |
| 149 |
|
| 150 |
# we should have a session, or else something is really wierd but... |
| 151 |
$self->{'sess'} = $self->make_session() if (!$self->{'sess'}); |
| 152 |
|
| 153 |
# for each key left in our cache, delete it |
| 154 |
foreach my $key (keys(%{$self->{'existtest'}})) { |
| 155 |
# xxx: fullyqualified oid better |
| 156 |
my $vblist = new SNMP::VarList([$delcolumnname, $key, |
| 157 |
$delcolumnvalue]); |
| 158 |
DEBUG("truncate $key: \n", Dumper($vblist)); |
| 159 |
$self->{'sess'}->set($vblist) || warn $self->{'sess'}->{ErrorStr}; |
| 160 |
} |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
sub make_session { |
| 165 |
DEBUG("calling AnyData::Storage::SNMP make_session\n"); |
| 166 |
my $self = shift; |
| 167 |
my @args = @_; |
| 168 |
my @sessions; |
| 169 |
foreach my $key (qw(SecName Version SecLevel AuthPass Community RemotePort Timeout Retries RetryNoSuch SecEngineId ContextEngineId Context AuthProto PrivProto PrivPass)) { |
| 170 |
push @args, $key, $self->{$key} if ($self->{$key}); |
| 171 |
} |
| 172 |
foreach my $host (split(/,\s*/,$self->{DestHost})) { |
| 173 |
push @sessions, new SNMP::Session(@args, 'DestHost' => $host); |
| 174 |
} |
| 175 |
return \@sessions; |
| 176 |
} |
| 177 |
|
| 178 |
sub file2str { |
| 179 |
DEBUG("calling AnyData::Storage::SNMP file2str\n"); |
| 180 |
my ($self, $parser, $uccols) = @_; |
| 181 |
my ($cols, @retcols); |
| 182 |
DEBUG("file2str\n",Dumper(\@_),"\n"); |
| 183 |
restart: |
| 184 |
if (!$self->{lastnode}) { |
| 185 |
# my @vbstuff = @{$parser->{'col_names'}}; |
| 186 |
# splice (@vbstuff,0,1+$#AnyData::Storage::SNMP::basecols); |
| 187 |
# map { $_ = [ $_ ] } @vbstuff; |
| 188 |
# $self->{lastnode} = new SNMP::VarList(@vbstuff); |
| 189 |
# splice (@$cols,0,1+$#AnyData::Storage::SNMP::basecols); |
| 190 |
map { push @$cols,$self->{col_uc_map}{$_} } @$uccols; |
| 191 |
if ($#$cols == -1) { |
| 192 |
$cols = $self->{'col_names'}; |
| 193 |
# remove base columns |
| 194 |
splice (@$cols,0,1+$#AnyData::Storage::SNMP::basecols); |
| 195 |
# remove not accessible columns |
| 196 |
foreach my $col (@$cols) { |
| 197 |
my $mib = $SNMP::MIB{$col}; |
| 198 |
push @retcols, $col if ($mib->{'access'} =~ /Read|Create/); |
| 199 |
} |
| 200 |
} else { |
| 201 |
@retcols = @$cols; |
| 202 |
# remove base columns |
| 203 |
foreach my $c (@AnyData::Storage::SNMP::basecols) { |
| 204 |
@retcols = grep(!/^$c$/, @retcols); |
| 205 |
} |
| 206 |
# remove non-accessible columns |
| 207 |
@retcols = grep {$SNMP::MIB{$_}{'access'} =~ /Read|Create/} @retcols; |
| 208 |
} |
| 209 |
map { $_ = [ $_ ] } @retcols; |
| 210 |
$self->{lastnode} = new SNMP::VarList(@retcols); |
| 211 |
} |
| 212 |
|
| 213 |
if (!$self->{'sess'}) { |
| 214 |
$self->{'sess'} = $self->make_session(); |
| 215 |
if ($#{$self->{'sess'}} == 0 && $self->{'silence_single_host'}) { |
| 216 |
$self->{'hostname'} = ''; |
| 217 |
} else { |
| 218 |
$self->{'hostname'} = $self->{'sess'}[0]{'DestHost'}; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
# perform SNMP operation |
| 223 |
my $lastnode = $self->{'lastnode'}[0][0]; |
| 224 |
my $result; |
| 225 |
$result = $self->{'sess'}[0]->getnext($self->{lastnode}); |
| 226 |
if (!defined($result)) { |
| 227 |
warn " getnext of $self->{lastnode}[0][0] returned undef\n"; |
| 228 |
} |
| 229 |
DEBUG(" result: ",Dumper($self->{lastnode}),"\n"); |
| 230 |
|
| 231 |
# XXX: check for holes! |
| 232 |
|
| 233 |
# need proper oid compare here for all nodes |
| 234 |
if ($self->{'lastnode'}[0][0] ne $lastnode) { |
| 235 |
if ($#{$self->{'sess'}} > 0) { |
| 236 |
shift @{$self->{'sess'}}; |
| 237 |
delete($self->{'lastnode'}); |
| 238 |
@$cols = (); |
| 239 |
$self->{'hostname'} = $self->{'sess'}[0]{'DestHost'} if($self->{'hostname'}); |
| 240 |
goto restart; |
| 241 |
} |
| 242 |
return undef; |
| 243 |
} |
| 244 |
|
| 245 |
# add in basecols information: |
| 246 |
my @ret = ($self->{'hostname'}, $self->{'lastnode'}[0][1]); |
| 247 |
DEBUG("Dump row results: ",Dumper($self->{'lastnode'}),"\n"); |
| 248 |
|
| 249 |
# build result array from result varbind contents |
| 250 |
map { $ret[$self->{'col_nums'}{$_->[0]}] = map_data($_); } @{$self->{'lastnode'}}; |
| 251 |
|
| 252 |
# store instance ID for later use if deletion is needed later. |
| 253 |
$self->{'existtest'}{$self->{'lastnode'}[0][1]} = 1; |
| 254 |
|
| 255 |
DEBUG("Dump row results2: ",Dumper(\@ret),"\n"); |
| 256 |
return \@ret; |
| 257 |
} |
| 258 |
|
| 259 |
sub map_data { |
| 260 |
if ($_->[3] eq "OBJECTID") { |
| 261 |
$_->[2] = pretty_print_oid(@_); |
| 262 |
} |
| 263 |
return $_->[2]; |
| 264 |
} |
| 265 |
|
| 266 |
sub pretty_print_oid { |
| 267 |
use NetSNMP::default_store qw(:all); |
| 268 |
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 269 |
NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS,0); |
| 270 |
my $new = SNMP::translateObj($_->[2], 0); |
| 271 |
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 272 |
NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS,1); |
| 273 |
my $new2 = SNMP::translateObj($_->[2], 0); |
| 274 |
if ($new) { |
| 275 |
$_->[2] = $new2 . "$new"; |
| 276 |
} else { |
| 277 |
$_->[2] = $_->[0] . $_->[1]; |
| 278 |
} |
| 279 |
return $_->[2]; |
| 280 |
} |
| 281 |
|
| 282 |
sub push_row { |
| 283 |
DEBUG("calling AnyData::Storage::SNMP push_row\n"); |
| 284 |
DEBUG("push_row: ",Dumper(\@_),"\n"); |
| 285 |
DEBUG("push_row\n"); |
| 286 |
my ($self, $values, $parser, $cols) = @_; |
| 287 |
my @callers = caller(3); |
| 288 |
my $mode = $callers[3]; |
| 289 |
if ($mode =~ /DELETE/) { |
| 290 |
DEBUG("not deleting $values->[$AnyData::Storage::SNMP::iidptr]\n"); |
| 291 |
delete $self->{'existtest'}{$values->[$AnyData::Storage::SNMP::iidptr]}; |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
my @origvars; |
| 296 |
if ($#$cols == -1) { |
| 297 |
# no column info passed in. Update everything (mode probably INSERTS). |
| 298 |
# @origvars = @{$self->{'col_names'}}}; |
| 299 |
# splice (@origvars,0,1+$#AnyData::Storage::SNMP::basecols); |
| 300 |
|
| 301 |
map { push @origvars, $_ if $SNMP::MIB{$_}{'access'} =~ /Write|Create/; } @{$self->{'real_cols'}} ; |
| 302 |
|
| 303 |
DEBUG("set cols: ", Dumper(\@origvars)); |
| 304 |
} else { |
| 305 |
# only update the columns in question. (mode probably UPDATE) |
| 306 |
map { push @origvars, $self->{col_uc_map}{$_} } @$cols; |
| 307 |
} |
| 308 |
|
| 309 |
my @vars; |
| 310 |
foreach my $var (@origvars) { |
| 311 |
my $access = $SNMP::MIB{$var}{'access'}; |
| 312 |
# not in this table, probably (hopefully) an index from another: |
| 313 |
next if ($SNMP::MIB{$var}{'parent'}{'parent'}{'label'} ne |
| 314 |
$self->{process_table}); |
| 315 |
DEBUG("$var -> $access\n"); |
| 316 |
if ($access =~ /(Write|Create)/) { |
| 317 |
push @vars, $var; |
| 318 |
} elsif ($mode eq 'insert') { |
| 319 |
DEBUG("XXX: error if not index\n"); |
| 320 |
} elsif ($mode eq 'update') { |
| 321 |
DEBUG("update to non-writable column attempted (SNMP error coming)\n"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
# generate index OID component if we don't have it. |
| 326 |
if ($values->[$AnyData::Storage::SNMP::iidptr] eq '') { |
| 327 |
$values->[$AnyData::Storage::SNMP::iidptr] = |
| 328 |
$self->make_iid($self->{process_table}, $values); |
| 329 |
} |
| 330 |
|
| 331 |
# add in values to varbind columns passed in from incoming parameters |
| 332 |
my @newvars; |
| 333 |
foreach my $v (@vars) { |
| 334 |
my $num = $self->{'col_nums'}{$v}; |
| 335 |
DEBUG("types: $v -> $num -> ", $self->{'col_types'}[$num], |
| 336 |
" -> val=", $values->[$num], "\n"); |
| 337 |
next if (!defined($values->[$num])); |
| 338 |
# build varbind: column-oid, instance-id, value type, value |
| 339 |
push @newvars, [$v, $values->[1], $values->[$num], |
| 340 |
$self->{'col_types'}[$num]]; |
| 341 |
}; |
| 342 |
|
| 343 |
# create the varbindlist |
| 344 |
# print STDERR Dumper(\@newvars); |
| 345 |
my $vblist = new SNMP::VarList(@newvars); |
| 346 |
|
| 347 |
# print STDERR Dumper($vblist); |
| 348 |
DEBUG("set: ", Dumper($vblist)); |
| 349 |
$self->{'sess'} = $self->make_session() if (!$self->{'sess'}); |
| 350 |
if (!$self->{'sess'}[0]) { |
| 351 |
warn "couldn't create SNMP session"; |
| 352 |
} elsif (!$self->{'sess'}[0]->set($vblist)) { |
| 353 |
my $err = "$self->{process_table}: " . $self->{'sess'}[0]->{ErrorStr}; |
| 354 |
if ($self->{'sess'}[0]->{ErrorInd}) { |
| 355 |
$err = $err . " (at varbind #" |
| 356 |
. $self->{'sess'}[0]->{ErrorInd} . " = " ; |
| 357 |
my $dump = Data::Dumper->new([$vblist->[$self->{'sess'}[0]->{ErrorInd} -1]]); |
| 358 |
$err .= $dump->Indent(0)->Terse(1)->Dump; |
| 359 |
} |
| 360 |
warn $err; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
sub seek { |
| 365 |
DEBUG("calling AnyData::Storage::SNMP seek\n"); |
| 366 |
my ($self, $parser) = @_; |
| 367 |
DEBUG("seek\n",Dumper(\@_),"\n"); |
| 368 |
} |
| 369 |
|
| 370 |
sub make_iid { |
| 371 |
DEBUG("calling AnyData::Storage::SNMP make_iid\n"); |
| 372 |
my ($self, $tname, $vals) = @_; |
| 373 |
|
| 374 |
# Get indexes |
| 375 |
my $mib = $SNMP::MIB{$tname}; |
| 376 |
my $entry = $mib->{'children'}[0]; |
| 377 |
my $indexes = $entry->{'indexes'}; |
| 378 |
my $iid; |
| 379 |
|
| 380 |
# XXX: implied |
| 381 |
|
| 382 |
# print STDERR "INDEXES: ", Dumper($vals),"\n"; |
| 383 |
foreach my $index (@$indexes) { |
| 384 |
warn "A null index value was found, which I doubt is correct." if (!defined($vals->[$self->{col_nums}{$index}])); |
| 385 |
my $val = $vals->[$self->{col_nums}{$index}]; |
| 386 |
my $type = $SNMP::MIB{$index}->{'type'}; |
| 387 |
DEBUG("index type: $index -> $type -> $val -> " . length($val) . "\n"); |
| 388 |
if ($type eq "OCTETSTR") { |
| 389 |
$iid .= "." . length($val) . "." . join(".", unpack("c*", $val)); |
| 390 |
} elsif ($type eq "OBJID") { |
| 391 |
$iid .= "." . (scalar grep(/\./,$val) + 1) . "." . $val; |
| 392 |
} else { |
| 393 |
# should be only an INTEGER? |
| 394 |
$iid .= "." . $val; |
| 395 |
} |
| 396 |
} |
| 397 |
DEBUG("made iid: $iid\n"); |
| 398 |
return $iid; |
| 399 |
} |
| 400 |
|
| 401 |
sub DEBUG { |
| 402 |
my @info = caller(1); |
| 403 |
if ($AnyData::Storage::SNMP::debug |
| 404 |
|| ($AnyData::Storage::SNMP::debugre && |
| 405 |
$_[0] =~ /$AnyData::Storage::SNMP::debugre/)) { |
| 406 |
DEBUGIT(\@info, @_); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
sub DEBUGIT { |
| 411 |
my $info; |
| 412 |
if (ref($_[0]) eq 'ARRAY') { |
| 413 |
$info = shift @_; |
| 414 |
} else { |
| 415 |
my @y; |
| 416 |
my $c=0; |
| 417 |
print STDERR "debug chain: "; |
| 418 |
for(@y = caller($c); $#y > -1; $c++, @y = caller($c)) { |
| 419 |
print STDERR " $c: $y[3]\n"; |
| 420 |
} |
| 421 |
my @x = caller(1); |
| 422 |
$info = \@x; |
| 423 |
} |
| 424 |
print STDERR "$info->[3]: "; |
| 425 |
print STDERR @_; |
| 426 |
} |
| 427 |
|
| 428 |
1; |