5 # pull info from SNMP enabled printers and dump JSON
8 use Data::Dump qw(dump);
10 my $dir = 'public/json/monitor/printers';
11 die "output directory $dir: $!" unless -d $dir;
15 my ( $ip, $json ) = @_;
16 my $path = "$dir/$ip";
17 open(my $fh, '>', $path) || die "$path: $!";
18 print $fh encode_json $json;
20 warn "# $path ", -s $path, " bytes\n";
23 my $debug = $ENV{DEBUG} || 0;
25 my $community = 'public';
48 @printers = @ARGV if @ARGV;
50 # remove final .1 since we are using bulkwalk to get values!
52 info iso.3.6.1.2.1.1.1.0
53 hostname iso.3.6.1.2.1.43.5.1.1.16.1
54 serial iso.3.6.1.2.1.43.5.1.1.17.1
55 pages iso.3.6.1.2.1.43.10.2.1.4.1
56 @message iso.3.6.1.2.1.43.18.1.1.8
57 @consumable_name iso.3.6.1.2.1.43.11.1.1.6.1
58 @consumable_max iso.3.6.1.2.1.43.11.1.1.8.1
59 @consumable_curr iso.3.6.1.2.1.43.11.1.1.9.1
60 @tray_max iso.3.6.1.2.1.43.8.2.1.9.1
61 @tray_capacity iso.3.6.1.2.1.43.8.2.1.10.1
62 @tray_name iso.3.6.1.2.1.43.8.2.1.13.1
63 @tray_dim_x iso.3.6.1.2.1.43.8.2.1.4.1
64 @tray_dim_y iso.3.6.1.2.1.43.8.2.1.5.1
69 while ( my ($name,$oid) = each %vars ) {
70 $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
72 $oid2name->{$oid} = $name;
74 my @oids = sort { length $a <=> length $b } keys %$oid2name;
75 warn "# vars = ",dump(@vars) if $debug;
77 my $sm = SNMP::Multi->new(
79 Community => $community,
80 Requests => SNMP::Multi::VarReq->new(
81 hosts => [ @printers ],
86 ) or die $SNMP::Multi::error;
88 warn "# working on: ", join(' ', @printers),$/;
90 my $resp = $sm->execute() or die $sm->error();
94 foreach my $host ( $resp->hosts ) {
97 foreach my $result ( $host->results ) {
98 if ( $result->error ) {
99 warn "ERROR: $host ", $result->error;
103 warn "## result = ", dump($result) if $debug;
105 foreach my $v ( $result->varlists ) {
106 foreach my $i ( @$v ) {
107 my ( $oid, undef, $val, $fmt ) = @$i;
108 if ( my $name = $oid2name->{$oid} ) {
109 $status->{$name} = $val;
113 my $oid_part = substr($oid,0,length($_));
114 if ( $oid_part eq $_ ) {
115 $oid_base = $oid_part;
120 my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
121 if ( $name =~ s/^\@// ) {
122 push @{ $status->{$name} }, $val;
124 $status->{$name} = $val;
132 foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
133 my ( $prefix,$name ) = split(/_/,$group,2);
134 foreach my $i ( 0 .. $#{ $status->{$group} } ) {
135 $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
137 delete $status->{$group};
140 print "$host = ",dump($status);
141 save_json $host => $status;
142 $collected->{$host} = $status;