X-Git-Url: http://git.rot13.org/?p=snmp-json.git;a=blobdiff_plain;f=printer-multi.pl;h=5d42f155ec9eb1a24f9694364cbecb2fe6a32ac0;hp=6e181564954181dfc824bf172733195d084f19c4;hb=9c6b66d58c7ae5a2061062f96c8e421ac78b14f3;hpb=757066a2262d1680d7c9181a7b9aa7eba468ec1f diff --git a/printer-multi.pl b/printer-multi.pl index 6e18156..5d42f15 100755 --- a/printer-multi.pl +++ b/printer-multi.pl @@ -5,10 +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 @@ -72,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; @@ -119,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;