From 96f066189e832e26b611bc233bc53974c517e021 Mon Sep 17 00:00:00 2001 From: crcro Date: Tue, 6 Jun 2017 01:00:09 +0300 Subject: [PATCH] feature: Added pi-hole application support (#6782) * added data to webui * added last step on docs * fix scrutinizer issue * fix blank lines --- doc/Extensions/Applications.md | 19 ++++++ html/includes/functions.inc.php | 3 + .../graphs/application/pihole_queries.inc.php | 58 ++++++++++++++++++ .../graphs/application/pihole_stats.inc.php | 61 +++++++++++++++++++ html/pages/apps.inc.php | 5 ++ html/pages/device/apps/pi-hole.inc.php | 41 +++++++++++++ includes/polling/applications/pi-hole.inc.php | 61 +++++++++++++++++++ 7 files changed, 248 insertions(+) create mode 100644 html/includes/graphs/application/pihole_queries.inc.php create mode 100644 html/includes/graphs/application/pihole_stats.inc.php create mode 100644 html/pages/device/apps/pi-hole.inc.php create mode 100644 includes/polling/applications/pi-hole.inc.php diff --git a/doc/Extensions/Applications.md b/doc/Extensions/Applications.md index 49c588ed71..40c0b1482a 100644 --- a/doc/Extensions/Applications.md +++ b/doc/Extensions/Applications.md @@ -25,6 +25,7 @@ Different applications support a variety of ways collect data: by direct connect 1. [Open Grid Scheduler](#opengridscheduler) - SNMP extend 1. [OS Updates](#os-updates) - SNMP extend 1. [PHP-FPM](#php-fpm) - SNMP extend +1. [Pi-hole](#pi-hole) - SNMP extend 1. [Postfix](#postfix) - SNMP extend 1. [Postgres](#postgres) - SNMP extend 1. [PowerDNS](#powerdns) - Agent @@ -501,6 +502,24 @@ extend phpfpmsp /etc/snmp/phpfpm-sp It is worth noting that this only monitors a single pool. If you want to monitor multiple pools, this won't do it. +### Pi-hole +#### SNMP Extend + +1: Copy the shell script, pi-hole, to the desired host (the host must be added to LibreNMS devices) (wget https://github.com/librenms/librenms-agent/raw/master/snmp/pi-hole -O /etc/snmp/pi-hole) + +2: Make the script executable (chmod +x /etc/snmp/pi-hole) + +3: Edit your snmpd.conf file and add: +``` +extend pi-hole /etc/snmp/pi-hole +``` + +4: To get all data you must get your API auth token from Pi-hole server and change the API_AUTH_KEY entry inside the snmp script. + +5: Restard snmpd. + +6: On the device page in Librenms, edit your host and check the `Pi-hole` under the Applications tab or wait for it to be auto-discovered. + ### Postfix #### SNMP Extend diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index 13634e7ed0..a233c60d64 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -121,6 +121,9 @@ function nicecase($item) case 'sdfsinfo': return 'SDFS info'; + case 'pi-hole': + return 'Pi-hole'; + default: return ucfirst($item); } diff --git a/html/includes/graphs/application/pihole_queries.inc.php b/html/includes/graphs/application/pihole_queries.inc.php new file mode 100644 index 0000000000..92b2a251b7 --- /dev/null +++ b/html/includes/graphs/application/pihole_queries.inc.php @@ -0,0 +1,58 @@ +. +* +* @package LibreNMS +* @subpackage pi-hole +* @link http://librenms.org +* @copyright 2017 crcro +* @author Cercel Valentin +*/ + +require 'includes/graphs/common.inc.php'; + +$scale_min = 0; +$colours = 'mixed'; +$unit_text = 'Queries'; +$unitlen = 6; +$bigdescrlen = 25; +$smalldescrlen = 25; +$dostack = 0; +$printtotal = 0; +$addarea = 1; +$transparency = 33; +$rrd_filename = rrd_name($device['hostname'], array('app', $app['app_type'], $app['app_id'])); + +$array = array( + 'query_a' => array('descr' => 'A Type','colour' => '000000',), + 'query_aaaa' => array('descr' => 'AAAA Type','colour' => '657C5E',), + 'query_ptr' => array('descr' => 'PTR Type','colour' => 'F44842',), + 'query_srv' => array('descr' => 'SRV Type','colour' => '912925',), +); + +$i = 0; + +if (rrdtool_check_rrd_exists($rrd_filename)) { + foreach ($array as $ds => $var) { + $rrd_list[$i]['filename'] = $rrd_filename; + $rrd_list[$i]['descr'] = $var['descr']; + $rrd_list[$i]['ds'] = $ds; + $rrd_list[$i]['colour'] = $var['colour']; + $i++; + } +} else { + echo "file missing: $rrd_filename"; +} + +require 'includes/graphs/generic_v3_multiline_float.inc.php'; diff --git a/html/includes/graphs/application/pihole_stats.inc.php b/html/includes/graphs/application/pihole_stats.inc.php new file mode 100644 index 0000000000..e8d8602a21 --- /dev/null +++ b/html/includes/graphs/application/pihole_stats.inc.php @@ -0,0 +1,61 @@ +. +* +* @package LibreNMS +* @subpackage pi-hole +* @link http://librenms.org +* @copyright 2017 crcro +* @author Cercel Valentin +*/ + +require 'includes/graphs/common.inc.php'; + +$scale_min = 0; +$colours = 'mixed'; +$unit_text = 'Stats'; +$unitlen = 6; +$bigdescrlen = 25; +$smalldescrlen = 25; +$dostack = 0; +$printtotal = 0; +$addarea = 1; +$transparency = 33; +$rrd_filename = rrd_name($device['hostname'], array('app', $app['app_type'], $app['app_id'])); + +$array = array( + 'domains_blocked' => array('descr' => 'Blocked domains','colour' => '000000',), + 'dns_query' => array('descr' => 'Today DNS Queries','colour' => '657C5E',), + 'ads_blocked' => array('descr' => 'ADs blocked','colour' => 'F44842',), + 'ads_percentage' => array('descr' => 'ADs blocked %','colour' => '912925',), + 'unique_domains' => array('descr' => 'Unique domains','colour' => 'F44842',), + 'queries_forwarded' => array('descr' => 'Queries forwarded','colour' => '1F35A5',), + 'queries_cached' => array('descr' => 'Queries cached','colour' => '137C21',), +); + +$i = 0; + +if (rrdtool_check_rrd_exists($rrd_filename)) { + foreach ($array as $ds => $var) { + $rrd_list[$i]['filename'] = $rrd_filename; + $rrd_list[$i]['descr'] = $var['descr']; + $rrd_list[$i]['ds'] = $ds; + $rrd_list[$i]['colour'] = $var['colour']; + $i++; + } +} else { + echo "file missing: $rrd_filename"; +} + +require 'includes/graphs/generic_v3_multiline_float.inc.php'; diff --git a/html/pages/apps.inc.php b/html/pages/apps.inc.php index 1e2d96ee27..376d8b844d 100644 --- a/html/pages/apps.inc.php +++ b/html/pages/apps.inc.php @@ -271,6 +271,11 @@ $graphs['sdfsinfo'] = array( 'rates', ); +$graphs['pi-hole'] = array( + 'stats', + 'queries', +); + print_optionbar_start(); echo "Apps » "; diff --git a/html/pages/device/apps/pi-hole.inc.php b/html/pages/device/apps/pi-hole.inc.php new file mode 100644 index 0000000000..fb57e64847 --- /dev/null +++ b/html/pages/device/apps/pi-hole.inc.php @@ -0,0 +1,41 @@ + +*/ + +global $config; + +$graphs = array( + 'pihole_stats' => 'Stats', + 'pihole_queries' => 'Queries', +); + +foreach ($graphs as $key => $text) { + $graph_type = $key; + $graph_array['height'] = '100'; + $graph_array['width'] = '215'; + $graph_array['to'] = $config['time']['now']; + $graph_array['id'] = $app['app_id']; + $graph_array['type'] = 'application_'.$key; + + echo '
+
+

'.$text.'

+
+
+
'; + include 'includes/print-graphrow.inc.php'; + echo '
'; + echo '
'; + echo '
'; +} diff --git a/includes/polling/applications/pi-hole.inc.php b/includes/polling/applications/pi-hole.inc.php new file mode 100644 index 0000000000..093efcb43b --- /dev/null +++ b/includes/polling/applications/pi-hole.inc.php @@ -0,0 +1,61 @@ + +*/ + +use LibreNMS\RRD\RrdDefinition; + +$name = 'pi-hole'; +$app_id = $app['app_id']; +$options = '-O qv'; +$oid = '.1.3.6.1.4.1.8072.1.3.2.4.1.2.7.112.105.45.104.111.108.101'; + +$pihole = snmp_walk($device, $oid, $options); + +if ($pihole) { + update_application($app, $pihole); + list($domains_blocked, $dns_query, $ads_blocked, $ads_percentage, $unique_domains, $queries_forwarded, $queries_cached, $query_a, $query_aaaa, $query_ptr, $query_srv) = explode("\n", $pihole); + + $rrd_name = array('app', $name, $app_id); + $rrd_def = RrdDefinition::make() + ->addDataset('domains_blocked', 'GAUGE', 0) + ->addDataset('dns_query', 'GAUGE', 0) + ->addDataset('ads_blocked', 'GAUGE', 0) + ->addDataset('ads_percentage', 'GAUGE', 0) + ->addDataset('unique_domains', 'GAUGE', 0) + ->addDataset('queries_forwarded', 'GAUGE', 0) + ->addDataset('queries_cached', 'GAUGE', 0) + ->addDataset('query_a', 'GAUGE', 0) + ->addDataset('query_aaaa', 'GAUGE', 0) + ->addDataset('query_ptr', 'GAUGE', 0) + ->addDataset('query_srv', 'GAUGE', 0); + + $fields = array( + 'domains_blocked' => $domains_blocked, + 'dns_query' => $dns_query, + 'ads_blocked' => $ads_blocked, + 'ads_percentage' => $ads_percentage, + 'unique_domains' => $unique_domains, + 'queries_forwarded' => $queries_forwarded, + 'queries_cached' => $queries_cached, + 'query_a' => $query_a, + 'query_aaaa' => $query_aaaa, + 'query_ptr' => $query_ptr, + 'query_srv' => $query_srv, + ); + + $tags = array('name' => $name, 'app_id' => $app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name); + data_update($device, 'app', $tags, $fields); +} + +unset($pihole);