diff options
author | Niklas Yann Wettengel <niyawe@niyawe.de> | 2018-07-22 13:14:46 +0200 |
---|---|---|
committer | Niklas Yann Wettengel <niyawe@niyawe.de> | 2018-07-22 13:14:46 +0200 |
commit | da72f062bbfdabaafece6c466df222a23e70b7c6 (patch) | |
tree | 4252dae62479ecae43458a2e2e0d9d51b39dee25 /roles/install_monitoring | |
parent | 4bfc6f1e293c2022d5238c1c09db130d80eea94d (diff) |
updated influx-scripts
Diffstat (limited to 'roles/install_monitoring')
5 files changed, 95 insertions, 62 deletions
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 @@ <?php +date_default_timezone_set('UTC'); require('func.php'); - $data = shell_exec("grep -e 'lease [[:digit:]\.]\+ {' -e '\s\+ends' /var/lib/dhcp/dhcpd.leases"); +$dhcp_config = file_get_contents('/etc/dhcpd.conf'); - preg_match_all('/lease ([\d\.]+) \{\n\s+ends \d+ (\d{4}\/\d{2}\/\d{2} \d+:\d{2}:\d{2});/s', $data, $match); +$num_ranges = preg_match_all('/range[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)/', $dhcp_config, $ranges); - unset($data, $match[0]); +$lease_file_handle = fopen("/var/lib/dhcp/dhcpd.leases", "r"); - $dend = time()-120; +$activeleases = array(); - $clients = 0; +$lease = -1; +$start = -1; +$end = -1; - foreach($match[2] as $timeout) { - $end = strtotime($timeout.' UTC'); - if($end > $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 @@ <?php function sendflux($data) { - $url = 'http://10.222.42.54:8086/write?db=freifunk'; + $url = 'http://[2a03:2260:1016:302:c03:19ff:fe06:285]:8086/write?db=freifunk'; $options = array( 'http' => 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 %} ?> |