Skip to content

Instantly share code, notes, and snippets.

@daveol
Created February 25, 2018 19:15
Show Gist options
  • Save daveol/22d3cefa8d5f4146749ff80e433057eb to your computer and use it in GitHub Desktop.
Save daveol/22d3cefa8d5f4146749ff80e433057eb to your computer and use it in GitHub Desktop.
Put lora statistics in graphite
#!/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