35ad6bc9d34382d29f30571055c4e573a0d9c91d
[snmp-json.git] / printer-multi.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use SNMP::Multi;
6 use Data::Dump qw(dump);
7
8 my $debug = $ENV{DEBUG} || 0; 
9
10 my $community = 'public';
11 my @printers = qw(
12 10.60.3.15
13 10.60.3.17
14
15 10.60.3.19
16 10.60.3.21
17
18 10.60.3.23
19 10.60.3.25
20
21 10.60.3.27
22 10.60.3.29
23
24 10.60.3.31
25 10.60.3.33
26
27 10.60.3.35
28 10.60.3.37
29 );
30
31 @printers = @ARGV if @ARGV;
32
33 # remove final .1 since we are using bulkwalk to get values!
34 my %vars = qw[
35 info                            iso.3.6.1.2.1.1.1.0
36 name                            iso.3.6.1.2.1.43.5.1.1.16.1
37 serial                          iso.3.6.1.2.1.43.5.1.1.17.1
38 pages                           iso.3.6.1.2.1.43.10.2.1.4.1
39 @message                        iso.3.6.1.2.1.43.18.1.1.8
40 @consumable_name        iso.3.6.1.2.1.43.11.1.1.6.1
41 @consumable_max         iso.3.6.1.2.1.43.11.1.1.8.1
42 @consumable_curr        iso.3.6.1.2.1.43.11.1.1.9.1
43 ];
44
45 my $oid2name;
46 my @vars;
47 while ( my ($name,$oid) = each %vars ) {
48         $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
49         push @vars, [ $oid ];
50         $oid2name->{$oid} = $name;
51 }
52 my @oids = sort { length $a <=> length $b } keys %$oid2name;
53 warn "# vars = ",dump(@vars) if $debug;
54
55 my $sm = SNMP::Multi->new(
56         Method    => 'bulkwalk',
57         Community => $community,
58         Requests  => SNMP::Multi::VarReq->new(
59                 hosts => [ @printers ],
60                 vars  => [ @vars ],
61     ),
62         Timeout     => 1,
63         Retries     => 0,
64 ) or die $SNMP::Multi::error;
65
66 warn "# working on: ", join(' ', @printers),$/;
67
68 my $resp = $sm->execute() or die $sm->error();
69
70 foreach my $host ( $resp->hosts ) {
71         my $status;
72
73         foreach my $result ( $host->results ) {
74                 if ( $result->error ) {
75                         warn "ERROR: $host ", $result->error;
76                         next;
77                 }
78
79                 warn "## result = ", dump($result) if $debug;
80
81                 foreach my $v ( $result->varlists ) {
82                         foreach my $i ( @$v ) {
83                                 my ( $oid, undef, $val, $fmt ) = @$i;
84                                 if ( my $name = $oid2name->{$oid} ) {
85                                         $status->{$name} = $val;
86                                 } else {
87                                         my $oid_base;
88                                         foreach ( @oids ) {
89                                                 my $oid_part = substr($oid,0,length($_));
90                                                 if ( $oid_part eq $_ ) {
91                                                         $oid_base = $oid_part;
92                                                         last;
93                                                 }
94                                         }
95
96                                         my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
97                                         if ( $name =~ s/^\@// ) {
98                                                 push @{ $status->{$name} }, $val;
99                                         } else {
100                                                 $status->{$name} = $val;
101                                         }
102                                 }
103                         }
104                 }
105
106         }
107
108         foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
109                 my ( $prefix,$name ) = split(/_/,$group,2);
110                 foreach my $i ( 0 .. $#{ $status->{$group} } ) {
111                         $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
112                 }
113                 delete $status->{$group};
114         }
115
116         print "$host = ",dump($status);
117 }
118