5 # pull info from SNMP enabled printers and dump JSON
8 use Data::Dump qw(dump);
10 my $dir = 'public/json/monitor/printers';
14 my ( $ip, $json ) = @_;
15 my $path = "$dir/$ip";
16 open(my $fh, '>', $path) || die "$path: $!";
17 print $fh encode_json $json;
19 warn "# $path ", -s $path, " bytes\n";
22 my $debug = $ENV{DEBUG} || 0;
24 my $community = 'public';
47 @printers = @ARGV if @ARGV;
49 # remove final .1 since we are using bulkwalk to get values!
51 info iso.3.6.1.2.1.1.1.0
52 hostname iso.3.6.1.2.1.43.5.1.1.16.1
53 serial iso.3.6.1.2.1.43.5.1.1.17.1
54 pages iso.3.6.1.2.1.43.10.2.1.4.1
55 @message iso.3.6.1.2.1.43.18.1.1.8
56 @consumable_name iso.3.6.1.2.1.43.11.1.1.6.1
57 @consumable_max iso.3.6.1.2.1.43.11.1.1.8.1
58 @consumable_curr iso.3.6.1.2.1.43.11.1.1.9.1
59 @tray_max iso.3.6.1.2.1.43.8.2.1.9.1
60 @tray_capacity iso.3.6.1.2.1.43.8.2.1.10.1
61 @tray_name iso.3.6.1.2.1.43.8.2.1.13.1
62 @tray_dim_x iso.3.6.1.2.1.43.8.2.1.4.1
63 @tray_dim_y iso.3.6.1.2.1.43.8.2.1.5.1
68 while ( my ($name,$oid) = each %vars ) {
69 $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
71 $oid2name->{$oid} = $name;
73 my @oids = sort { length $a <=> length $b } keys %$oid2name;
74 warn "# vars = ",dump(@vars) if $debug;
76 my $sm = SNMP::Multi->new(
78 Community => $community,
79 Requests => SNMP::Multi::VarReq->new(
80 hosts => [ @printers ],
85 ) or die $SNMP::Multi::error;
87 warn "# working on: ", join(' ', @printers),$/;
89 my $resp = $sm->execute() or die $sm->error();
93 foreach my $host ( $resp->hosts ) {
96 foreach my $result ( $host->results ) {
97 if ( $result->error ) {
98 warn "ERROR: $host ", $result->error;
102 warn "## result = ", dump($result) if $debug;
104 foreach my $v ( $result->varlists ) {
105 foreach my $i ( @$v ) {
106 my ( $oid, undef, $val, $fmt ) = @$i;
107 if ( my $name = $oid2name->{$oid} ) {
108 $status->{$name} = $val;
112 my $oid_part = substr($oid,0,length($_));
113 if ( $oid_part eq $_ ) {
114 $oid_base = $oid_part;
119 my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
120 if ( $name =~ s/^\@// ) {
121 push @{ $status->{$name} }, $val;
123 $status->{$name} = $val;
131 foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
132 my ( $prefix,$name ) = split(/_/,$group,2);
133 foreach my $i ( 0 .. $#{ $status->{$group} } ) {
134 $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
136 delete $status->{$group};
139 print "$host = ",dump($status);
140 save_json $host => $status;
141 $collected->{$host} = $status;