updated influx-scripts

master
Niklas Yann Wettengel 6 years ago
parent 4bfc6f1e29
commit da72f062bb

@ -2,8 +2,9 @@
- name: install vnstat - name: install vnstat
import_tasks: install_vnstat.yml import_tasks: install_vnstat.yml
#- name: install ffmyk-influx - name: install ffmyk-influx
# include: install_ffmyk-influx.yml include: install_ffmyk-influx.yml
when: "'fastd' in group_names"
- name: install munin - name: install munin
import_tasks: install_munin.yml import_tasks: install_munin.yml

@ -1,23 +1,73 @@
<?php <?php
date_default_timezone_set('UTC');
require('func.php'); 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) { while(($line = fgets($lease_file_handle)) !== false)
$end = strtotime($timeout.' UTC'); {
if($end > $dend) $clients++; 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; $pools = array();
sendflux($data);
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);
?> ?>

@ -26,9 +26,13 @@ function fastdGetPeers($file) {
return $peers; 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); sendflux($data);

@ -1,6 +1,6 @@
<?php <?php
function sendflux($data) { 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( $options = array(
'http' => array( 'http' => array(

@ -2,52 +2,30 @@
require('func.php'); require('func.php');
function traffic($iface, $alias=false) { function traffic($iface, $alias=false) {
if(!$alias) $alias = $iface; if(!$alias) $alias = $iface;
/* ifconfig eth0 | grep bytes $rx = file_get_contents('/sys/class/net/'.$iface.'/statistics/rx_bytes');
RX bytes:700194759 (667.7 MiB) TX bytes:1090382719 (1.0 GiB) $tx = file_get_contents('/sys/class/net/'.$iface.'/statistics/tx_bytes');
*/ $data = 'rx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$rx."\n";
$data = shell_exec('ifconfig '.escapeshellarg($iface).' | grep bytes'); $data.= 'tx,if='.$alias.',host={{ ansible_hostname }},type=backend value='.$tx;
preg_match('/RX.+?bytes (\d+) /', $data, $match);
$rx = $match[1]; sendflux($data);
unset($match); }
preg_match('/TX.+?bytes (\d+) /', $data, $match); (traffic('{{ ansible_default_ipv4.interface }}', 'wan'));
$tx = $match[1]; {% if ansible_default_ipv4.interface != ansible_default_ipv6.interface %}
unset($match); (traffic('{{ ansible_default_ipv6.interface }}', 'wan6'));
{% endif %}
$file = '/opt/ffmyk-influx/traffic.'.base64_encode($iface).'.cache'; {% for site in sites %}
(traffic('bat{{ site.name }}'));
$out['rx'] = 0; (traffic('vpn{{ site.name }}'));
$out['tx'] = 0; (traffic('wg{{ site.name }}'));
{% endfor %}
if(file_exists($file)) { {% for uplink in groups['uplink'] %}
$cache = unserialize(file_get_contents($file)); (traffic('bb{{ hostvars[uplink]['wireguard_bb_name'] }}'));
$diff = time() - filemtime($file); {% endfor %}
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'));
?> ?>

Loading…
Cancel
Save