From da72f062bbfdabaafece6c466df222a23e70b7c6 Mon Sep 17 00:00:00 2001 From: Niklas Yann Wettengel Date: Sun, 22 Jul 2018 13:14:46 +0200 Subject: updated influx-scripts --- roles/install_monitoring/tasks/main.yml | 5 +- .../templates/ffmyk-influx/dhcp.php.j2 | 70 ++++++++++++++++++--- .../templates/ffmyk-influx/fastd.php.j2 | 8 ++- .../templates/ffmyk-influx/func.php.j2 | 2 +- .../templates/ffmyk-influx/traffic.php.j2 | 72 ++++++++-------------- 5 files changed, 95 insertions(+), 62 deletions(-) (limited to 'roles') diff --git a/roles/install_monitoring/tasks/main.yml b/roles/install_monitoring/tasks/main.yml index 407c261..afb4db9 100644 --- a/roles/install_monitoring/tasks/main.yml +++ b/roles/install_monitoring/tasks/main.yml @@ -2,8 +2,9 @@ - name: install vnstat import_tasks: install_vnstat.yml -#- name: install ffmyk-influx -# include: install_ffmyk-influx.yml +- name: install ffmyk-influx + include: install_ffmyk-influx.yml + when: "'fastd' in group_names" - name: install munin import_tasks: install_munin.yml diff --git a/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 index 7b81328..4d3773b 100644 --- a/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 +++ b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 @@ -1,23 +1,73 @@ $dend) $clients++; +while(($line = fgets($lease_file_handle)) !== false) +{ + if(preg_match('/lease ([\d]+\.[\d]+\.[\d]+\.[\d]+)/', $line, $match)) + { + $lease = ip2long($match[1]); + continue; + } + elseif(preg_match('/starts \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/', $line, $match)) + { + $start = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); + continue; + } + elseif(preg_match('/ends \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/', $line, $match)) + { + $end = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); + if($lease > 0 && $start > 0 && $end > 0) + { + if( $start < time() && $end > time() ) + { + $activeleases[$lease] = $lease; + $lease = -1; + $start = -1; + $end = -1; + } } + } +} - $data = 'clients,host={{ ansible_hostname }},type=backend value='.$clients; - sendflux($data); +$pools = array(); +for($range = 0; $range < $num_ranges; $range++) +{ + $clients = 0; + + $range_start = ip2long($ranges[1][$range]); + $range_end = ip2long($ranges[2][$range]); + foreach($activeleases as $lease) + { + if( $lease > $range_start && $lease < $range_end ) + { + $clients++; + } + } + + $pools[$range_start] = $clients; +} + +$data = ""; + +foreach($pools as $range => $clients) +{ + $data .= 'clients,host={{ ansible_hostname }},pool='.long2ip($range).',type=backend value='.$clients."\n"; +} + +sendflux($data); ?> diff --git a/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2 index 985d7d3..0cd05c3 100644 --- a/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2 +++ b/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2 @@ -26,9 +26,13 @@ function fastdGetPeers($file) { return $peers; } -$fastd_1280 = fastdGetPeers('/run/ffmyk.socket'); +$data = ""; -$data = 'fastdclient,mtu=1280,host={{ ansible_hostname }},type=backend value='.$fastd_1280."\n"; +{% for site in sites %} +$fastd_{{ site.name }} = fastdGetPeers('/run/ff{{ site.name }}1.socket'); +$data .= 'fastdclient,mtu=1280,host={{ ansible_hostname }},site={{ site.name }},type=backend value='.$fastd_{{ site.name }}."\n"; + +{% endfor %} sendflux($data); diff --git a/roles/install_monitoring/templates/ffmyk-influx/func.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/func.php.j2 index fd2c248..b7b3609 100644 --- a/roles/install_monitoring/templates/ffmyk-influx/func.php.j2 +++ b/roles/install_monitoring/templates/ffmyk-influx/func.php.j2 @@ -1,6 +1,6 @@ array( 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 %} ?> -- cgit v1.2.3-54-g00ecf