updated influx-scripts

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

@ -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

@ -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);
?>

@ -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);

@ -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(

@ -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 %}
?>

Loading…
Cancel
Save