You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB
Django/Jinja

<?php
require('func.php');
function traffic($iface, $alias=false) {
if(!$alias) $alias = $iface;
/* ifconfig eth0 | grep bytes
RX bytes:700194759 (667.7 MiB) TX bytes:1090382719 (1.0 GiB)
*/
$data = shell_exec('ifconfig '.escapeshellarg($iface).' | grep bytes');
preg_match('/RX.+?bytes (\d+) /', $data, $match);
$rx = $match[1];
unset($match);
preg_match('/TX.+?bytes (\d+) /', $data, $match);
$tx = $match[1];
unset($match);
$file = '/opt/ffmyk-influx/traffic.'.base64_encode($iface).'.cache';
$out['rx'] = 0;
$out['tx'] = 0;
if(file_exists($file)) {
$cache = unserialize(file_get_contents($file));
$diff = time() - filemtime($file);
if($rx > $cache['rx']) $out['rx'] = ($rx - $cache['rx']) / $diff;
if($tx > $cache['tx']) $out['tx'] = ($tx - $cache['tx']) / $diff;
}
file_put_contents($file, serialize(array("rx" => $rx, "tx" => $tx)));
$data = 'rx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$out['rx']."\n";
$data.= 'tx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$out['tx'];
sendflux($data);
$out['if'] = $iface;
return $out;
}
(traffic('ens3', 'eth0'));
(traffic('mullvad'));
(traffic('bat0'));
(traffic('ffmyk-mesh-vpn', 'ffmyk-mesh-vpnd'));
?>