5 # pull info from SNMP enabled printers and dump JSON
8 use Data::Dump qw(dump);
15 my ( $path, $json ) = @_;
16 mkpath "$dir/$path" unless -d "$dir/$path";
17 $path = "$dir/$path/snmp-printer.json";
18 open(my $fh, '>', $path) || die "$path: $!";
19 print $fh encode_json $json;
21 warn "# $path ", -s $path, " bytes\n";
24 my $debug = $ENV{DEBUG} || 0;
26 my $community = 'public';
27 my @printers = map { s{^conf/ip/([0-9\.]+)/.+$}{$1}; $_ } glob 'conf/ip/*/snmp-printer*';
29 @printers = @ARGV if @ARGV;
31 # remove final .1 since we are using bulkwalk to get values!
33 info iso.3.6.1.2.1.1.1.0
34 name iso.3.6.1.2.1.43.5.1.1.16.1
35 serial iso.3.6.1.2.1.43.5.1.1.17.1
36 pages iso.3.6.1.2.1.43.10.2.1.4.1
37 @message iso.3.6.1.2.1.43.18.1.1.8
38 @consumable_name iso.3.6.1.2.1.43.11.1.1.6.1
39 @consumable_max iso.3.6.1.2.1.43.11.1.1.8.1
40 @consumable_curr iso.3.6.1.2.1.43.11.1.1.9.1
41 @tray_max iso.3.6.1.2.1.43.8.2.1.9.1
42 @tray_capacity iso.3.6.1.2.1.43.8.2.1.10.1
43 @tray_name iso.3.6.1.2.1.43.8.2.1.13.1
44 @tray_dim_x iso.3.6.1.2.1.43.8.2.1.4.1
45 @tray_dim_y iso.3.6.1.2.1.43.8.2.1.5.1
50 while ( my ($name,$oid) = each %vars ) {
51 $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
53 $oid2name->{$oid} = $name;
55 my @oids = sort { length $a <=> length $b } keys %$oid2name;
56 warn "# vars = ",dump(@vars) if $debug;
58 my $sm = SNMP::Multi->new(
60 Community => $community,
61 Requests => SNMP::Multi::VarReq->new(
62 hosts => [ @printers ],
67 ) or die $SNMP::Multi::error;
69 warn "# working on: ", join(' ', @printers),$/;
71 my $resp = $sm->execute() or die $sm->error();
75 foreach my $host ( $resp->hosts ) {
78 foreach my $result ( $host->results ) {
79 if ( $result->error ) {
80 warn "ERROR: $host ", $result->error;
84 warn "## result = ", dump($result) if $debug;
86 foreach my $v ( $result->varlists ) {
87 foreach my $i ( @$v ) {
88 my ( $oid, undef, $val, $fmt ) = @$i;
89 if ( my $name = $oid2name->{$oid} ) {
90 $status->{$name} = $val;
94 my $oid_part = substr($oid,0,length($_));
95 if ( $oid_part eq $_ ) {
96 $oid_base = $oid_part;
101 my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
102 if ( $name =~ s/^\@// ) {
103 push @{ $status->{$name} }, $val;
105 $status->{$name} = $val;
113 foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
114 my ( $prefix,$name ) = split(/_/,$group,2);
115 foreach my $i ( 0 .. $#{ $status->{$group} } ) {
116 $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
118 delete $status->{$group};
121 print "$host = ",dump($status);
122 save_json $host => $status;
123 $collected->{$host} = $status;