commit 706e66edeb4dc5ca70044905170bd4fe7293c64f Author: CactusProjects <57372290+CactusProjects@users.noreply.github.com> Date: Sun Mar 1 18:48:23 2020 +0000 Add files via upload diff --git a/pihole_grafana.json b/pihole_grafana.json new file mode 100644 index 0000000..1f3ba91 --- /dev/null +++ b/pihole_grafana.json @@ -0,0 +1,447 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": "-- Grafana --", + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "gnetId": null, + "graphTooltip": 0, + "id": 5, + "links": [], + "panels": [ + { + "collapsed": false, + "datasource": null, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "id": 2, + "panels": [], + "title": "PiHole Stats", + "type": "row" + }, + { + "cacheTimeout": null, + "datasource": "dev_pihole", + "gridPos": { + "h": 6, + "w": 6, + "x": 0, + "y": 1 + }, + "hideTimeOverride": false, + "id": 4, + "links": [], + "options": { + "colorMode": "value", + "fieldOptions": { + "calcs": [ + "last" + ], + "defaults": { + "mappings": [], + "min": 0, + "nullValueMode": "connected", + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "title": "DNS Queries", + "unit": "none" + }, + "overrides": [], + "values": false + }, + "graphMode": "area", + "justifyMode": "auto", + "orientation": "horizontal" + }, + "pluginVersion": "6.6.2", + "targets": [ + { + "alias": "DNS Queries", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "hide": false, + "measurement": "piholestats.pihole", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "dns_queries_today" + ], + "type": "field" + }, + { + "params": [], + "type": "distinct" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=", + "value": "pihole" + } + ] + } + ], + "timeFrom": null, + "timeShift": null, + "title": "PiHole Stats", + "type": "stat" + }, + { + "cacheTimeout": null, + "datasource": "dev_pihole", + "gridPos": { + "h": 6, + "w": 6, + "x": 6, + "y": 1 + }, + "hideTimeOverride": false, + "id": 5, + "links": [], + "options": { + "colorMode": "value", + "fieldOptions": { + "calcs": [ + "last" + ], + "defaults": { + "mappings": [], + "nullValueMode": "connected", + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "title": "Ads Percentage", + "unit": "%" + }, + "overrides": [], + "values": false + }, + "graphMode": "area", + "justifyMode": "auto", + "orientation": "horizontal" + }, + "pluginVersion": "6.6.2", + "targets": [ + { + "alias": "Ads Blocked", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "hide": false, + "measurement": "piholestats.pihole", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "ads_percentage_today" + ], + "type": "field" + }, + { + "params": [], + "type": "distinct" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=", + "value": "pihole" + } + ] + } + ], + "timeFrom": null, + "timeShift": null, + "title": "PiHole Stats", + "type": "stat" + }, + { + "cacheTimeout": null, + "datasource": "dev_pihole", + "gridPos": { + "h": 6, + "w": 6, + "x": 12, + "y": 1 + }, + "hideTimeOverride": false, + "id": 6, + "links": [], + "options": { + "colorMode": "value", + "fieldOptions": { + "calcs": [ + "last" + ], + "defaults": { + "mappings": [], + "min": 0, + "nullValueMode": "connected", + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "title": "Ads Blocked", + "unit": "none" + }, + "overrides": [], + "values": false + }, + "graphMode": "area", + "justifyMode": "auto", + "orientation": "horizontal" + }, + "pluginVersion": "6.6.2", + "targets": [ + { + "alias": "Ads Blocked", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "hide": false, + "measurement": "piholestats.pihole", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "ads_blocked_today" + ], + "type": "field" + }, + { + "params": [], + "type": "distinct" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=", + "value": "pihole" + } + ] + } + ], + "timeFrom": null, + "timeShift": null, + "title": "PiHole Stats", + "type": "stat" + }, + { + "cacheTimeout": null, + "datasource": "dev_pihole", + "gridPos": { + "h": 6, + "w": 6, + "x": 18, + "y": 1 + }, + "hideTimeOverride": false, + "id": 7, + "links": [], + "options": { + "colorMode": "value", + "fieldOptions": { + "calcs": [ + "last" + ], + "defaults": { + "mappings": [], + "min": 0, + "nullValueMode": "connected", + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "title": "Domains Blocked", + "unit": "none" + }, + "overrides": [], + "values": false + }, + "graphMode": "area", + "justifyMode": "auto", + "orientation": "horizontal" + }, + "pluginVersion": "6.6.2", + "targets": [ + { + "alias": "Domains Blocked", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "hide": false, + "measurement": "piholestats.pihole", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "domains_blocked" + ], + "type": "field" + }, + { + "params": [], + "type": "distinct" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=", + "value": "pihole" + } + ] + } + ], + "timeFrom": null, + "timeShift": null, + "title": "PiHole Stats", + "type": "stat" + } + ], + "schemaVersion": 22, + "style": "dark", + "tags": [], + "templating": { + "list": [] + }, + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ] + }, + "timezone": "", + "title": "PiHole", + "uid": "oP8IN5mRk", + "version": 5 +} diff --git a/piholestats.py b/piholestats.py new file mode 100644 index 0000000..8165dea --- /dev/null +++ b/piholestats.py @@ -0,0 +1,53 @@ +#! /usr/bin/python + +# History: +# 2016: Script originally created by JON HAYWARD: https://fattylewis.com/Graphing-pi-hole-stats/ +# 2016 (December) Adapted to work with InfluxDB by /u/tollsjo +# 2016 (December) Updated by Cludch https://github.com/sco01/piholestatus +# 2020 (March) Updated by http://cactusprojects.com/pihole-logging-to-influxdb-&-grafana-dash + +import requests +import time +from influxdb import InfluxDBClient + +HOSTNAME = "pihole" # Pi-hole hostname to report in InfluxDB for each measurement +PIHOLE_API = "http://xxx.xxx.xxx.xxx/admin/api.php" # IP of PiHole +INFLUXDB_SERVER = "xxx.xxx.xxx.xxx" # IP or hostname to InfluxDB server +INFLUXDB_PORT = 8086 # Port on InfluxDB server +INFLUXDB_USERNAME = "" +INFLUXDB_PASSWORD = "" +INFLUXDB_DATABASE = "dev_pihole" +DELAY = 10 # seconds + +def send_msg(domains_blocked, dns_queries_today, ads_percentage_today, ads_blocked_today): + + json_body = [ + { + "measurement": "piholestats." + HOSTNAME.replace(".", "_"), + "tags": { + "host": HOSTNAME + }, + "fields": { + "domains_blocked": int(domains_blocked), + "dns_queries_today": int(dns_queries_today), + "ads_percentage_today": float(ads_percentage_today), + "ads_blocked_today": int(ads_blocked_today) + } + } + ] + + client = InfluxDBClient(INFLUXDB_SERVER, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) # InfluxDB host, InfluxDB port, Username, Password, database + # client.create_database(INFLUXDB_DATABASE) # Uncomment to create the database (expected to exist prior to feeding it data) + client.write_points(json_body) + +api = requests.get(PIHOLE_API) # URI to pihole server api +API_out = api.json() + +#print (API_out) # Print out full data, there are other parameters not sent to InfluxDB + +domains_blocked = (API_out['domains_being_blocked'])#.replace(',', '') +dns_queries_today = (API_out['dns_queries_today'])#.replace(',', '') +ads_percentage_today = (API_out['ads_percentage_today'])# +ads_blocked_today = (API_out['ads_blocked_today'])#.replace(',', '') + +send_msg(domains_blocked, dns_queries_today, ads_percentage_today, ads_blocked_today)