chvvkumar / Monitoring

Monitor ESXi, Synology, Docker, PiHole and Raspberry Pi and Windows using Grafana, InfluxDB and Telegraf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

piholestats.py - invalid print syntax

SirToffski opened this issue · comments

The following error is encountered when executing piholestats.py

$ sudo -u telegraf /bin/piholestats.py
  File "/bin/piholestats.py", line 13
    print json.dumps(rstats)
             ^
SyntaxError: invalid syntax

The error is resolved when changing line 13 of the script to print(json.dumps(rstats)). So the script will look like:

#!/usr/bin/python
import json
import requests

# Note: Change localhost to the correct IP if needed.
r = requests.get('http://192.168.1.156/admin/api.php')
rstats = json.loads(r.text)

#stats = {}
#for x in rstats:
#    stats[x] = float(rstats[x].replace(',', ''))

print(json.dumps(rstats))

This works as expected:

$ sudo -u telegraf /bin/piholestats.py
{"domains_being_blocked": 1386484, "dns_queries_today": 16309, "ads_blocked_today": 5648, "ads_percentage_today": 34.631184, "unique_domains": 1333, "queries_forwarded": 9223, "queries_cached": 1438, "clients_ever_seen": 15, "unique_clients": 14, "dns_queries_all_types": 16309, "reply_NODATA": 4, "reply_NXDOMAIN": 2, "reply_CNAME": 289, "reply_IP": 663, "privacy_level": 0, "status": "enabled", "gravity_last_updated": {"file_exists": true, "absolute": 1561951929, "relative": {"days": "6", "hours": "13", "minutes": "37"}}}

I believe this is due to how Python 3 handles print statements (i.e. as a function call).
Using 2.4, the original print statement works perfectly. I have updated the script with your suggestion in this commit:

1c74015

Thanks for bringing up the issue!