summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Yann Wettengel <niyawe@niyawe.de>2017-03-20 20:56:39 +0100
committerNiklas Yann Wettengel <niyawe@niyawe.de>2017-03-20 20:56:39 +0100
commiteade06a930c16f5cb08edd454d9e41364edf8103 (patch)
tree51dde0c60cf6c6486269edaa574efad4ad4d90a1
parent892aba103d0b58a888813a42bbb0fb00e852adfd (diff)
install_monitoring: added ffmyk-influx
-rw-r--r--host_vars/fastd2
-rwxr-xr-xroles/install_monitoring/files/ffmyk-influx/daemon.sh8
-rw-r--r--roles/install_monitoring/files/ffmyk-influx/ffmyk-influx.service10
-rw-r--r--roles/install_monitoring/files/ffmyk-influx/php.ini49
-rw-r--r--roles/install_monitoring/tasks/main.yml47
-rw-r--r--roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j223
-rw-r--r--roles/install_monitoring/templates/ffmyk-influx/fastd.php.j235
-rw-r--r--roles/install_monitoring/templates/ffmyk-influx/func.php.j217
-rw-r--r--roles/install_monitoring/templates/ffmyk-influx/traffic.php.j253
9 files changed, 244 insertions, 0 deletions
diff --git a/host_vars/fastd b/host_vars/fastd
index c041d00..532d309 100644
--- a/host_vars/fastd
+++ b/host_vars/fastd
@@ -16,6 +16,8 @@ mullvad_key: |
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
+influx_user: <user>
+influx_password: <password>
munin_node_plugins:
- name: cpu
- name: df
diff --git a/roles/install_monitoring/files/ffmyk-influx/daemon.sh b/roles/install_monitoring/files/ffmyk-influx/daemon.sh
new file mode 100755
index 0000000..1cc5a8d
--- /dev/null
+++ b/roles/install_monitoring/files/ffmyk-influx/daemon.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+cd /opt/ffmyk-influx
+while : ;do
+ php -c ./php.ini -f dhcp.php
+ php -c ./php.ini -f traffic.php
+ php -c ./php.ini -f fastd.php
+ sleep 15
+done
diff --git a/roles/install_monitoring/files/ffmyk-influx/ffmyk-influx.service b/roles/install_monitoring/files/ffmyk-influx/ffmyk-influx.service
new file mode 100644
index 0000000..3949c94
--- /dev/null
+++ b/roles/install_monitoring/files/ffmyk-influx/ffmyk-influx.service
@@ -0,0 +1,10 @@
+[Unit]
+
+[Service]
+ExecStart=/opt/ffmyk-influx/daemon.sh
+Restart=always
+RestartSec=5
+
+[Install]
+WantedBy=multi-user.target
+
diff --git a/roles/install_monitoring/files/ffmyk-influx/php.ini b/roles/install_monitoring/files/ffmyk-influx/php.ini
new file mode 100644
index 0000000..988b1c2
--- /dev/null
+++ b/roles/install_monitoring/files/ffmyk-influx/php.ini
@@ -0,0 +1,49 @@
+[PHP]
+engine = On
+short_open_tag = Off
+precision = 14
+output_buffering = 4096
+zlib.output_compression = Off
+implicit_flush = Off
+unserialize_callback_func =
+serialize_precision = 17
+zend.enable_gc = On
+expose_php = On
+max_execution_time = 30
+max_input_time = 60
+memory_limit = 128M
+error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
+display_errors = Off
+display_startup_errors = Off
+log_errors = On
+log_errors_max_len = 1024
+ignore_repeated_errors = Off
+ignore_repeated_source = Off
+report_memleaks = On
+track_errors = Off
+html_errors = Off
+variables_order = "GPCS"
+request_order = "GP"
+register_argc_argv = Off
+auto_globals_jit = On
+post_max_size = 8M
+auto_prepend_file =
+auto_append_file =
+default_mimetype = "text/html"
+default_charset = "UTF-8"
+doc_root =
+user_dir =
+extension_dir = "/usr/lib/php/modules/"
+enable_dl = Off
+file_uploads = On
+upload_max_filesize = 2M
+max_file_uploads = 20
+allow_url_fopen = On
+allow_url_include = Off
+default_socket_timeout = 60
+extension=curl.so
+extension=sockets.so
+extension=zip.so
+[CLI Server]
+cli_server.color = On
+
diff --git a/roles/install_monitoring/tasks/main.yml b/roles/install_monitoring/tasks/main.yml
index 07260e2..eb6b4a3 100644
--- a/roles/install_monitoring/tasks/main.yml
+++ b/roles/install_monitoring/tasks/main.yml
@@ -75,6 +75,53 @@
cron_file: fastd-api
job: '/usr/local/bin/check_internet.sh'
+- name: create ffmyk-influx folder
+ file:
+ path: /opt/ffmyk-influx
+ state: directory
+
+- name: copy ffmyk-influx daemon.sh
+ copy:
+ src: ffmyk-influx/daemon.sh
+ dest: /opt/ffmyk-influx/daemon.sh
+ mode: 0755
+
+- name: copy ffmyk-influx php.ini
+ copy:
+ src: ffmyk-influx/php.ini
+ dest: /opt/ffmyk-influx/php.ini
+
+- name: copy ffmyk-influx systemd service
+ copy:
+ src: ffmyk-influx/ffmyk-influx.service
+ dest: /etc/systemd/system/ffmyk-influx.service
+
+- name: copy ffmyk-influx dhcp.php
+ template:
+ src: ffmyk-influx/dhcp.php.j2
+ dest: /opt/ffmyk-influx/dhcp.php
+
+- name: copy ffmyk-influx fastd.php
+ template:
+ src: ffmyk-influx/fastd.php.j2
+ dest: /opt/ffmyk-influx/fastd.php
+
+- name: copy ffmyk-influx func.php
+ template:
+ src: ffmyk-influx/func.php.j2
+ dest: /opt/ffmyk-influx/func.php
+
+- name: copy ffmyk-influx traffic.php
+ template:
+ src: ffmyk-influx/traffic.php.j2
+ dest: /opt/ffmyk-influx/traffic.php
+
+- name: start and enable ffmyk-influx.service
+ systemd:
+ name: ffmyk-influx.service
+ enabled: yes
+ state: started
+
- name: install munin
pacman:
name: munin-node
diff --git a/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2
new file mode 100644
index 0000000..66371ef
--- /dev/null
+++ b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2
@@ -0,0 +1,23 @@
+<?php
+
+require('func.php');
+
+ $data = file_get_contents('/var/lib/dhcp/dhcpd.leases');
+
+ preg_match_all('/lease ([\d\.]+) \{[^\}]+ends \d+ (\d{4}\/\d{2}\/\d{2} \d+:\d{2}:\d{2});[^\}]+}/s', $data, $match);
+
+ unset($data, $match[0]);
+
+ $dend = time()-120;
+
+ $clients = 0;
+
+ foreach($match[2] as $timeout) {
+ $end = strtotime($timeout.' UTC');
+ if($end > $dend) $clients++;
+ }
+
+ $data = 'clients,host={{ ansible_hostname }},type=backend value='.$clients;
+ sendflux($data);
+
+?>
diff --git a/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2
new file mode 100644
index 0000000..985d7d3
--- /dev/null
+++ b/roles/install_monitoring/templates/ffmyk-influx/fastd.php.j2
@@ -0,0 +1,35 @@
+<?php
+require('func.php');
+
+function fastdGetPeers($file) {
+ if (($sock = socket_create(AF_UNIX, SOCK_STREAM, NULL)) === false) {
+ echo "socket_create() fehlgeschlagen: Grund: " . socket_strerror(socket_last_error()) . "\n";
+ }
+
+ if (($result = socket_connect($sock, $file)) === false) {
+ echo "socket_connect() fehlgeschlagen.\nGrund: ($result) " . socket_strerror(socket_last_error($sock)) . "\n";
+ }
+
+ $json = "";
+ #stream_set_timeout($sock, 5);
+ while ($out = socket_read($sock, 2048)) {
+ $json .= $out;
+ }
+
+ $json = json_decode($json);
+
+ $peers = 0;
+ foreach($json->peers as $peer) {
+ if($peer->connection != NULL) $peers++;
+ }
+
+ return $peers;
+}
+
+$fastd_1280 = fastdGetPeers('/run/ffmyk.socket');
+
+$data = 'fastdclient,mtu=1280,host={{ ansible_hostname }},type=backend value='.$fastd_1280."\n";
+
+sendflux($data);
+
+?>
diff --git a/roles/install_monitoring/templates/ffmyk-influx/func.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/func.php.j2
new file mode 100644
index 0000000..fd2c248
--- /dev/null
+++ b/roles/install_monitoring/templates/ffmyk-influx/func.php.j2
@@ -0,0 +1,17 @@
+<?php
+function sendflux($data) {
+ $url = 'http://10.222.42.54:8086/write?db=freifunk';
+
+ $options = array(
+ 'http' => array(
+ 'header' => "Authorization: Basic " . base64_encode("{{ influx_user }}:{{ influx_password }}") ."\r\nContent-type: application/x-www-form-urlencoded\r\n",
+ 'method' => 'POST',
+ 'content' => $data,
+ ),
+ );
+
+ $context = stream_context_create($options);
+ $result = file_get_contents($url, false, $context);
+}
+
+?>
diff --git a/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2
new file mode 100644
index 0000000..82674b9
--- /dev/null
+++ b/roles/install_monitoring/templates/ffmyk-influx/traffic.php.j2
@@ -0,0 +1,53 @@
+<?php
+
+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'));
+
+
+?>