Created
February 25, 2018 19:15
-
-
Save daveol/22d3cefa8d5f4146749ff80e433057eb to your computer and use it in GitHub Desktop.
Put lora statistics in graphite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
package GTG; | |
use strict; | |
use warnings; | |
use Date::Manip; | |
use JSON::Parse 'parse_json'; | |
use Net::Graphite; | |
my $graphite = Net::Graphite->new( | |
host => '127.0.0.1', | |
port => 2003, | |
trace => 0, | |
proto => 'tcp', | |
timeout => 1, | |
fire_and_forget => 1, | |
return_connect_error => 0 | |
); | |
$graphite->connect; | |
my $port = 1700; | |
my $host = '*'; | |
use base qw(Net::Server::PreFork); | |
GTG->run( port => "$host:$port/udp"); | |
exit; | |
sub default_values { | |
return { | |
cidr_allow => '10.42.45.15/16' | |
}; | |
} | |
sub process_request { | |
my $self = shift; | |
my $prop = $self->{'server'}; | |
my $type = substr(unpack('H*', $prop->{'udp_data'}), 6, 2); | |
if('00' eq $type){ | |
my $data = parse_json(substr($prop->{'udp_data'}, 12)); | |
if ($data->{'rxpk'}){ | |
my @packets = @{$data->{'rxpk'}}; | |
while (my $packet = shift @packets){ | |
my $date = UnixDate( ParseDate($packet->{'time'}), "%s" ); | |
print "send\n" if $graphite->send( | |
path => "revspace.lora.$packet->{'chan'}.$packet->{'datr'}", | |
value => 1, | |
time => $date | |
); | |
} | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment