implement grouping of tabular results
[snmp-json.git] / printer-multi.pl
index 6e18156..5d42f15 100755 (executable)
@@ -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;