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 | 72 |
1 files changed, 25 insertions, 47 deletions
diff --git a/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 index 82674b9..dde5dba 100644 --- a/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 +++ b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 @@ -2,52 +2,30 @@ 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')); - +function traffic($iface, $alias=false) { + + if(!$alias) $alias = $iface; + + $rx = file_get_contents('/sys/class/net/'.$iface.'/statistics/rx_bytes'); + $tx = file_get_contents('/sys/class/net/'.$iface.'/statistics/tx_bytes'); + + $data = 'rx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$rx."\n"; + $data.= 'tx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$tx; + + sendflux($data); +} + +(traffic('{{ ansible_default_ipv4.interface }}', 'wan')); +{% if ansible_default_ipv4.interface != ansible_default_ipv6.interface %} +(traffic('{{ ansible_default_ipv6.interface }}', 'wan6')); +{% endif %} +{% for site in sites %} +(traffic('bat{{ site.name }}')); +(traffic('vpn{{ site.name }}')); +(traffic('wg{{ site.name }}')); +{% endfor %} +{% for uplink in groups['uplink'] %} +(traffic('bb{{ hostvars[uplink]['wireguard_bb_name'] }}')); +{% endfor %} ?> |