e9553c5007df67d2a0b421665a770d54efba4393
[angular-mojolicious.git] / scripts / snmp-printer.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 # pull info from SNMP enabled printers and dump JSON
6 #
7 # dump all snmp data on printer with:
8 # snmpwalk -c public -v 1 10.60.3.15
9
10 use SNMP::Multi;
11 use Data::Dump qw(dump);
12
13 my $dir = 'public/json/monitor/printers';
14 die "output directory $dir: $!" unless -d $dir;
15
16 use JSON;
17 sub save_json {
18         my ( $ip, $json ) = @_;
19         my $path = "$dir/$ip";
20         open(my $fh, '>', $path) || die "$path: $!";
21         print $fh encode_json $json;
22         close($fh);
23         warn "# $path ", -s $path, " bytes\n";
24 }
25
26 sub iso_datetime {
27         my ($ss,$mm,$hh,$d,$m,$y) = localtime(time);
28         return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $y+1900, $m, $d, $hh, $mm, $ss;
29 }
30
31 my $log_path = join('.', $dir, (split(/T/,iso_datetime,2))[0], 'json');
32 open(my $log, '>>', $log_path) || die "$log_path: $!";
33
34 my $debug = $ENV{DEBUG} || 0; 
35
36 my $community = 'public';
37 my @printers = qw(
38 10.60.0.20
39
40 10.60.3.15
41 10.60.3.17
42
43 10.60.3.19
44 10.60.3.21
45
46 10.60.3.23
47 10.60.3.25
48
49 10.60.3.27
50 10.60.3.29
51
52 10.60.3.31
53 10.60.3.33
54
55 10.60.3.35
56 10.60.3.37
57 );
58
59 @printers = @ARGV if @ARGV;
60
61 # remove final .1 since we are using bulkwalk to get values!
62 my %vars = qw[
63 info                            iso.3.6.1.2.1.1.1.0
64 hostname                        iso.3.6.1.2.1.43.5.1.1.16.1
65 serial                          iso.3.6.1.2.1.43.5.1.1.17.1
66 pages                           iso.3.6.1.2.1.43.10.2.1.4.1
67 @message                        iso.3.6.1.2.1.43.18.1.1.8
68 @consumable_name        iso.3.6.1.2.1.43.11.1.1.6.1
69 @consumable_max         iso.3.6.1.2.1.43.11.1.1.8.1
70 @consumable_curr        iso.3.6.1.2.1.43.11.1.1.9.1
71 @tray_max                       iso.3.6.1.2.1.43.8.2.1.9.1
72 @tray_capacity          iso.3.6.1.2.1.43.8.2.1.10.1
73 @tray_name                      iso.3.6.1.2.1.43.8.2.1.13.1
74 @tray_dim_x                     iso.3.6.1.2.1.43.8.2.1.4.1
75 @tray_dim_y                     iso.3.6.1.2.1.43.8.2.1.5.1
76 ];
77
78 my $oid2name;
79 my @vars;
80 while ( my ($name,$oid) = each %vars ) {
81         $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
82         push @vars, [ $oid ];
83         $oid2name->{$oid} = $name;
84 }
85 my @oids = sort { length $a <=> length $b } keys %$oid2name;
86 warn "# vars = ",dump(@vars) if $debug;
87
88 my $sm = SNMP::Multi->new(
89         Method    => 'bulkwalk',
90         Community => $community,
91         Requests  => SNMP::Multi::VarReq->new(
92                 hosts => [ @printers ],
93                 vars  => [ @vars ],
94     ),
95         Timeout     => 1,
96         Retries     => 0,
97 ) or die $SNMP::Multi::error;
98
99 warn "# working on: ", join(' ', @printers),$/;
100
101 my $resp = $sm->execute() or die $sm->error();
102
103 my $collected;
104
105 foreach my $host ( $resp->hosts ) {
106         my $status;
107
108         foreach my $result ( $host->results ) {
109                 if ( $result->error ) {
110                         warn "ERROR: $host ", $result->error;
111                         next;
112                 }
113
114                 warn "## result = ", dump($result) if $debug;
115
116                 foreach my $v ( $result->varlists ) {
117                         foreach my $i ( @$v ) {
118                                 my ( $oid, undef, $val, $fmt ) = @$i;
119                                 if ( my $name = $oid2name->{$oid} ) {
120                                         $status->{$name} = $val;
121                                 } else {
122                                         my $oid_base;
123                                         foreach ( @oids ) {
124                                                 my $oid_part = substr($oid,0,length($_));
125                                                 if ( $oid_part eq $_ ) {
126                                                         $oid_base = $oid_part;
127                                                         last;
128                                                 }
129                                         }
130
131                                         my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
132                                         if ( $name =~ s/^\@// ) {
133                                                 push @{ $status->{$name} }, $val;
134                                         } else {
135                                                 $status->{$name} = $val;
136                                         }
137                                 }
138                         }
139                 }
140
141         }
142
143         foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
144                 my ( $prefix,$name ) = split(/_/,$group,2);
145                 foreach my $i ( 0 .. $#{ $status->{$group} } ) {
146                         $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
147                 }
148                 delete $status->{$group};
149         }
150
151         $status->{ip} = $host->hostname;
152         $status->{date} = iso_datetime;
153         print "$host = ",dump($status);
154         save_json $host => $status;
155         $collected->{$host} = $status;
156
157         print $log encode_json($status),"\n";
158 }
159
160 close($log);
161 warn "# log $log_path ", -s $log_path, " bytes\n";