X-Git-Url: http://git.rot13.org/?p=snmp-json.git;a=blobdiff_plain;f=printer-multi.pl;h=5d42f155ec9eb1a24f9694364cbecb2fe6a32ac0;hp=a0f7d3bc90b2b017a00afc2712e1062a4b6946cf;hb=9c6b66d58c7ae5a2061062f96c8e421ac78b14f3;hpb=4b29d24ccc45c435ed1b9bab1897570e9104f93e diff --git a/printer-multi.pl b/printer-multi.pl index a0f7d3b..5d42f15 100755 --- a/printer-multi.pl +++ b/printer-multi.pl @@ -5,8 +5,22 @@ use strict; use SNMP::Multi; use Data::Dump qw(dump); +use JSON; +sub save_json { + my ( $path, $json ) = @_; + $path = "json/$path"; + open(my $fh, '>', $path); + print $fh encode_json $json; + close($fh); + warn "# $path ", -s $path, " bytes\n"; +} + +my $debug = $ENV{DEBUG} || 0; + my $community = 'public'; my @printers = qw( +10.60.0.20 + 10.60.3.15 10.60.3.17 @@ -28,27 +42,39 @@ my @printers = qw( @printers = @ARGV if @ARGV; +# remove final .1 since we are using bulkwalk to get values! my %vars = qw[ -model .1.3.6.1.2.1.25.3.2.1.3.1 -serial .1.3.6.1.2.1.43.5.1.1.17 -pages .1.3.6.1.2.1.43.10.2.1.4.1.1 -@message .1.3.6.1.2.1.43.18.1.1.8 -@message .1.3.6.1.2.1.43.16 -@consumable_name .1.3.6.1.2.1.43.11.1.1.6.1 -@consumable_max .1.3.6.1.2.1.43.11.1.1.8.1 -@consumable_curr .1.3.6.1.2.1.43.11.1.1.9.1 +info iso.3.6.1.2.1.1.1.0 +name iso.3.6.1.2.1.43.5.1.1.16.1 +serial iso.3.6.1.2.1.43.5.1.1.17.1 +pages iso.3.6.1.2.1.43.10.2.1.4.1 +@message iso.3.6.1.2.1.43.18.1.1.8 +@consumable_name iso.3.6.1.2.1.43.11.1.1.6.1 +@consumable_max iso.3.6.1.2.1.43.11.1.1.8.1 +@consumable_curr iso.3.6.1.2.1.43.11.1.1.9.1 +@tray_max iso.3.6.1.2.1.43.8.2.1.9.1 +@tray_capacity iso.3.6.1.2.1.43.8.2.1.10.1 +@tray_name iso.3.6.1.2.1.43.8.2.1.13.1 +@tray_dim_x iso.3.6.1.2.1.43.8.2.1.4.1 +@tray_dim_y iso.3.6.1.2.1.43.8.2.1.5.1 ]; -my @oids = sort { length $a <=> length $b } values %vars; my $oid2name; -$oid2name->{ $vars{$_} } = $_ foreach keys %vars; +my @vars; +while ( my ($name,$oid) = each %vars ) { + $oid =~ s/\.[0-1]$// if $name !~ /^\@/; + push @vars, [ $oid ]; + $oid2name->{$oid} = $name; +} +my @oids = sort { length $a <=> length $b } keys %$oid2name; +warn "# vars = ",dump(@vars) if $debug; my $sm = SNMP::Multi->new( Method => 'bulkwalk', Community => $community, Requests => SNMP::Multi::VarReq->new( hosts => [ @printers ], - vars => [ map { [ $_ ] } values %vars ], + vars => [ @vars ], ), Timeout => 1, Retries => 0, @@ -58,6 +84,8 @@ warn "# working on: ", join(' ', @printers),$/; my $resp = $sm->execute() or die $sm->error(); +my $collected; + foreach my $host ( $resp->hosts ) { my $status; @@ -67,10 +95,11 @@ foreach my $host ( $resp->hosts ) { next; } + warn "## result = ", dump($result) if $debug; + foreach my $v ( $result->varlists ) { foreach my $i ( @$v ) { my ( $oid, undef, $val, $fmt ) = @$i; - $oid =~ s/^iso/.1/; if ( my $name = $oid2name->{$oid} ) { $status->{$name} = $val; } else { @@ -83,7 +112,7 @@ foreach my $host ( $resp->hosts ) { } } - my $name = $oid2name->{$oid_base} || die "no name for $oid_base in ",dump( $oid2name ); + my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name ); if ( $name =~ s/^\@// ) { push @{ $status->{$name} }, $val; } else { @@ -104,5 +133,16 @@ foreach my $host ( $resp->hosts ) { } print "$host = ",dump($status); + save_json $host => $status; + $collected->{$host} = $status; +} + +# generate json +my $json; +foreach my $ip ( sort keys %$collected ) { + my $status = $collected->{$ip}; + $status->{ip} = $ip; + push @$json, $status; } +save_json 'printers' => $json;