aredn/files/usr/local/bin/snrlog

164 lines
4.8 KiB
Plaintext
Raw Normal View History

2015-12-10 10:17:03 -07:00
#!/usr/bin/perl
=for commnet
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2015 Joe Ayers ae6xe@arrl.net and Darryl Quinn K5DLQ
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version.
=cut
#delay just after rssi_monitor has a chance to run noise floor calibration
sleep 5;
2015-12-10 10:17:03 -07:00
$MAXLINES=2880; # 2 days worth
$tmpdir="/tmp/snrlog";
$lastdat="/tmp/snr.dat";
2015-12-10 10:17:03 -07:00
$sigdir="/sys/kernel/debug/ieee80211/phy0/netdev\:wlan0/stations";
if ( -e $lastdat )
{
open(FILE, "<$lastdat") or die "Unable to read \"$lastdat\"";
while($line = <FILE>)
{
($mac,$last) = split /\|/, $line;
chomp ($lasttime{$mac} = $last);
}
close FILE ;
}
2015-12-10 10:17:03 -07:00
$nf = `iw wlan0 survey dump | grep -A 1 'in use' | tail -1`;
$nf =~ s/^[^-\d]*([-\d]+).*$/$1/;
chomp $nf;
2015-12-10 10:17:03 -07:00
if (! -e $tmpdir ) { system("mkdir $tmpdir > /dev/null"); }
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
2015-12-11 00:04:34 -07:00
$d = sprintf ("%02d/%02d %02d:%02d:%02d",$mon+1, $mday,$hour, $min, $sec);
chomp ($now=`cat /proc/uptime | cut -f1 -d" "`);
2015-12-10 10:17:03 -07:00
foreach $sigmac ( `ls $sigdir` )
2015-12-10 10:17:03 -07:00
{
chomp $sigmac;
$isFile = "";
$hnamef = "";
$ipsig = "";
$macfname = "";
$macfname = `ls $tmpdir/$sigmac*` ;
$macfname =~ /^.*\/([^-]*)-(.*)$/;
$isFile = $1;
$hnamef = $2;
chomp ($ipsig = `cat /proc/net/arp | grep $sigmac | cut -f1 -d\" "`);
2015-12-10 10:17:03 -07:00
$hname="";
foreach(`nslookup $ipsig`)
{
next unless ($hname) = /Address 1: $ipsig (\S+)/;
2015-12-10 10:17:03 -07:00
$hname =~ s/\..*$// ;
break;
}
if (($hnamef and $hnamef eq $hname ) or ($hname and ! $isFile ))
{
$sigdat = "$sigmac-$hname";
}
elsif ( $hnamef eq $ipsig and $hname )
{
system("mv $tmpdir/$sigmac-$ipsig $tmpdir/$sigmac-$hname > /dev/null");
$sigdat = "$sigmac-$hname";
}
elsif ($hname and ! $hnamef and $isFile )
{
system("mv $tmpdir/$sigmac- $tmpdir/$sigmac-$hname > /dev/null");
$sigdat = "$sigmac-$hname";
}
elsif (( $hnamef eq $ipsig) or ( $ipsig and ! $isFile ))
{
$sigdat = "$sigmac-$ipsig";
}
elsif ( $isFile and ! $hnamef and $ipsig )
{
system("mv $tmpdir/$sigmac- $tmpdir/$sigmac-$ipsig > /dev/null");
$sigdat = "$sigmac-$ipsig";
}
elsif ($hnamef and ! $hname )
{
$sigdat = "$sigmac-$hnamef";
}
elsif ( $hnamef and $hname and $hnamef ne $hname )
{
system("mv $tmpdir/$sigmac-$hnamef $tmpdir/$sigmac-$hname > /dev/null");
$sigdat = "$sigmac-$hname";
}
else
{
$sigdat = "$sigmac-";
}
2015-12-10 10:17:03 -07:00
chomp ($siglevel = `cat $sigdir/$sigmac/last_signal`) ;
if ($siglevel < -95 or $siglevel >= 0 ) { $siglevel = "-95"; }
2015-12-10 10:17:03 -07:00
open(my $fh, '>>', "$tmpdir/$sigdat") or die "Could not open file '$tmpdir/$sigdat' $!";
print $fh "$d,$siglevel,$nf\n";
close $fh;
chomp ($lcount=`wc -l < $tmpdir/$sigdat`);
if($lcount>$MAXLINES+10)
{
$rc=`tail -n$MAXLINES $tmpdir/$sigdat > $tmpdir/$sigdat.tmp`;
$rc=`mv $tmpdir/$sigdat.tmp $tmpdir/$sigdat`;
}
$neighbor{$sigmac}="1";
$lasttime{$sigmac}=$now;
2015-12-10 10:17:03 -07:00
}
# process neigbors that are no longer connected
foreach $sigmac (`ls $tmpdir`)
{
chomp $sigmac ;
$sigf = $sigmac;
$sigmac =~ s/-.*$//;
if ( $now - $lasttime{$sigmac} > 25000 )
{
unlink "$tmpdir/$sigf" ;
delete $lasttime{$sigmac};
}
elsif ( ! $neighbor{$sigmac} )
{
open(my $fh, '>>', "$tmpdir/$sigf") or die "Could not open file '$tmpdir/$sigf' $!";
print $fh "$d,-95,-95\n";
close $fh;
}
}
open($fh, ">$lastdat") or die "Unable to create \"$lastdat\" $!";
for (keys %lasttime) { print $fh "$_|$lasttime{$_}\n"; }
close $fh;