turn find-owner into root daemon so we can open control files
[safeq] / snmp-supply.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6
7 my $ip = shift @ARGV || '10.60.3.35';
8 my $sep = $ENV{SEP} || "\t";
9
10 my $stat = { _take => 1 };
11
12 my $cache = "/dev/shm/$ip.snmp";
13 my $snmp;
14 if ( -e $cache ) {
15         open($snmp, '<', $cache);
16         warn "# cache $cache";
17 } else {
18         my $cmd = "snmpwalk -v1 -cpublic $ip | tee /dev/shm/$ip.snmp";
19         warn "# $cmd";
20         open($snmp, '-|', $cmd);
21 }
22
23 while(<$snmp>) {
24         chomp;
25
26         if ( m/Supplies/ ) {
27                 $stat->{_take} = 1;
28         } else {
29                 $stat->{_take} = 0;
30         }
31
32         if ( $stat->{_take} && m/::([^\.]+)\.(.+) = (\w+): (.+)/ ) {
33                 my ($name,$id,$type,$val) = ( $1,$2,$3,$4 );
34                 $stat->{$id}->{$name} = $val;
35                 $stat->{_ids}->{$id}++;
36
37                 if ( $stat->{_order}->[-1] ne $name ) {
38                         push @{ $stat->{_order} }, $name;
39                 }
40         }
41
42 }
43
44 warn "# stat = ",dump($stat);
45
46 my @order = @{ $stat->{_order} };
47 warn "# order = ",dump( \@order );
48
49 my @ids = keys %{ $stat->{_ids} };
50
51 warn "# ids = ",dump( \@ids );
52
53 print join($sep,'IP', 'ID', @order),"\n";
54 foreach my $id ( @ids ) {
55         print join($sep, $ip, $id, map { $stat->{$id}->{$_} } @order),"\n";
56 }
57