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();
$lease = -1;
$start = -1;
$end = -1;
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;
}
}
}
}
$pools = array();
for($range = 0; $range < $num_ranges; $range++)
{
$clients = 0; $clients = 0;
foreach($match[2] as $timeout) { $range_start = ip2long($ranges[1][$range]);
$end = strtotime($timeout.' UTC'); $range_end = ip2long($ranges[2][$range]);
if($end > $dend) $clients++; foreach($activeleases as $lease)
{
if( $lease > $range_start && $lease < $range_end )
{
$clients++;
}
} }
$data = 'clients,host={{ ansible_hostname }},type=backend value='.$clients; $pools[$range_start] = $clients;
sendflux($data); }
$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];
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); sendflux($data);
}
$out['if'] = $iface;
(traffic('{{ ansible_default_ipv4.interface }}', 'wan'));
return $out; {% if ansible_default_ipv4.interface != ansible_default_ipv6.interface %}
} (traffic('{{ ansible_default_ipv6.interface }}', 'wan6'));
{% endif %}
(traffic('ens3', 'eth0')); {% for site in sites %}
(traffic('mullvad')); (traffic('bat{{ site.name }}'));
(traffic('bat0')); (traffic('vpn{{ site.name }}'));
(traffic('ffmyk-mesh-vpn', 'ffmyk-mesh-vpnd')); (traffic('wg{{ site.name }}'));
{% endfor %}
{% for uplink in groups['uplink'] %}
(traffic('bb{{ hostvars[uplink]['wireguard_bb_name'] }}'));
{% endfor %}
?> ?>

Loading…
Cancel
Save