diff options
Diffstat (limited to 'roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2')
-rw-r--r-- | roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 new file mode 100644 index 0000000..82674b9 --- /dev/null +++ b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 @@ -0,0 +1,53 @@ +<?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')); + + +?> |