Files @ 207c124454d0
Branch filter:

Location: FVDE/Scripts/Exit-Network-Stats.pl

virii
Edited file Exit-Network-Stats.pl via Kallithea
#!/usr/bin/env perl
# by virii - enn.lu

use strict;

use XML::LibXML;
use HTTP::Tiny;

use JSON::Parse qw(:all);
use List::UtilsBy qw( nsort_by );


my ($api, $api_request, $json, $nodes_in_numbers, $parser, $vnstat_xml, $query, $entry);
my ($rx, $tx, $rx_TiB, $tx_TiB, $rx_TiB_total, $tx_TiB_total, $argument);
my (@nodes_rand, @nodes_sorted);

$api         = HTTP::Tiny->new;
$api_request = $api->get('https://enn.lu/api/export/exit');
$json        = valid_json($api_request->{content});
$json        = parse_json($api_request->{content});


$nodes_in_numbers = scalar @$json;

push @nodes_rand, [$json->[$_]{ip}, $json->[$_]{name}, $json->[$_]{country}] for (0..$nodes_in_numbers -1);
@nodes_sorted = nsort_by { int $_->[0] } @nodes_rand;


format HEAD =
************************************************
    Country             RX              TX
************************************************
.
write HEAD;


for (0..$nodes_in_numbers -1) {
    unless ($nodes_sorted[$_][0] == $nodes_sorted[$_ +1][0]) {
            $api_request = $api->get('http://' . $nodes_sorted[$_][1] . '.enn.lu/vnstat.xml');
            $parser      = XML::LibXML->new();
            $vnstat_xml  = $parser->load_xml(string => $api_request->{content});

            $query = '/vnstat/interface/traffic/total';

            foreach $entry ($vnstat_xml->findnodes($query)) {
                    $rx = int $entry->findnodes('./rx');
                    $tx = int $entry->findnodes('./tx');
            }


        ($rx_TiB, $tx_TiB) = ($rx * (9.313 * 1.0E-10), $tx * (9.313 * 10E-10));
        ($rx_TiB, $tx_TiB) = (substr ($rx_TiB, 0, 4), substr ($tx_TiB, 0, 4));

        $rx_TiB_total += $rx_TiB;
        $tx_TiB_total += $tx_TiB;

        format_output($nodes_sorted[$_][2]);
    }
}

format_output('TOTAL');


sub format_output {
$argument = shift;

if ($argument eq 'TOTAL') {
    ($argument, $rx_TiB, $tx_TiB) = ("    TOTAL", $rx_TiB_total, $tx_TiB_total);
    print '*' x 48, "\n";
}

format = 
@<<<<<<<<<   @>>>>>>>>>>>>>   @>>>>>>>>>>>>>                                
$argument,      "$rx_TiB TiB",   "$tx_TiB TiB"                                  
.
write;

}