summaryrefslogtreecommitdiff
path: root/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2
blob: 82674b9176a514b63a756035508ec147df1836bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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'));


?>