1 |
#!/usr/bin/perl -w |
2 |
# |
3 |
# cricket module which creates configuration file (when called with |
4 |
# apropriate option), re-reads configuration (so that it can report |
5 |
# filesystems in same order always) |
6 |
# |
7 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
8 |
# http://www.rot13.org/~dpavlin/sysadm.html |
9 |
# |
10 |
# Usage: |
11 |
# |
12 |
# parse_wifi.pl "ssh -i ~cricket/.ssh/wifi target.host" /mount_point |
13 |
# ssh into remote host and get data for /mount_point |
14 |
# |
15 |
# parse_wifi.pl "ssh -i ~cricket/.ssh/wifi target.host" --config |
16 |
# dump configuration file to stdout |
17 |
# |
18 |
|
19 |
use strict; |
20 |
|
21 |
my $ssh = shift @ARGV || ""; |
22 |
my $dev = shift @ARGV; |
23 |
|
24 |
# do we have Cache::FileCache installed? |
25 |
|
26 |
my $USE_CACHE = 0; |
27 |
eval("use Cache::FileCache;"); |
28 |
if (! $@) { $USE_CACHE = 1; } |
29 |
|
30 |
my $wifi; |
31 |
my @labels; |
32 |
|
33 |
my $cache = new Cache::FileCache() if ($USE_CACHE); |
34 |
|
35 |
if ($USE_CACHE) { |
36 |
print STDERR " [using cache] "; |
37 |
$wifi = $cache->get( $ssh ); |
38 |
return if (defined $wifi); |
39 |
print STDERR " [cache miss] "; |
40 |
} |
41 |
eval { |
42 |
local $SIG{ALRM} = sub { die "ssh timeout\n"; }; |
43 |
alarm 10; # wait for ssh to connect and return first line |
44 |
open(DF,"$ssh cat /proc/net/wireless /proc/net/dev |") || die "$ssh wifi: $!"; |
45 |
}; |
46 |
while(<DF>) { |
47 |
chomp; |
48 |
s/\|//g; # remove | delimiters |
49 |
my @arr = split(/[\s:]+/,$_); |
50 |
shift @arr; # dump first element -- empty one |
51 |
|
52 |
# store labels |
53 |
if ($arr[0] eq "face") { |
54 |
shift @arr; |
55 |
push (@labels,@arr); |
56 |
} |
57 |
|
58 |
if ($#arr == 10) { # /proc/net/wireless |
59 |
if (! $dev && $arr[0]) { |
60 |
$dev = $arr[0]; |
61 |
} |
62 |
if ($dev && $arr[0] eq $dev) { |
63 |
shift @arr; |
64 |
push @{$wifi->{$dev}},@arr; |
65 |
} |
66 |
} elsif ($#arr == 16 && $arr[0] eq $dev) { # /proc/net/dev |
67 |
shift @arr; |
68 |
push @{$wifi->{$dev}},@arr; |
69 |
} |
70 |
} |
71 |
close(DF); |
72 |
alarm 0; # turn alarm off |
73 |
$cache->set( $ssh, $wifi, "1 min" ) if ($USE_CACHE); |
74 |
|
75 |
# dump |
76 |
for(my $i=0; $i <= ($#labels+1); $i++) { |
77 |
# print $labels[$i]," [$i] : "; |
78 |
my $val = $wifi->{$dev}[$i]; |
79 |
$val =~ s/\.$//g; # kill trailing dot |
80 |
print $val || 0; |
81 |
print "\n"; |
82 |
} |
83 |
|