At work, we run PRTG for our network/systems monitoring. It's a great system but I wanted something a little simpler to indicate the current status of all of our systems at a glance.

I had a Raspberry Pi Model 3 and a Unicorn Hat from Pimoroni lying around from some previous experiments/hacking around that I'd been doing.

I wrote a simple python script that runs at startup to interrogate PRTG and change what the Unicorn Hat outputs:

  • If there are 1 or more sensors down, set all the LEDs to red
  • If we cannot communicate with PRTG, set all the LEDs to orange
  • If there are no sensors down, set random LED to green at a random location

The last point allows you to see that the system is still working, as every time it polls PRTG the green LED moves position.

Here it is when everything's okay:

 

[caption id="attachment_123" align="alignnone" width="1332"]Okay All good[/caption]

 

And when it's not:

 

[caption id="attachment_124" align="alignnone" width="1484"]Help Help![/caption]

 

Below is the python script I used to achieve the above. It is very simple; I do almost no python coding at all so any hints/tips are gratefully received! I'm afraid WordPress may have messed up the tabs, so I've added the file to GitHub and it can be found at the following address: https://github.com/yakcam/BlogScripts/blob/master/PRTGStatus.py

[code lang="python"]
#!/usr/bin/env python

from time import sleep
from random import randint
import unicornhat as unicorn
import requests

def checkPrtg():
try:
resp = requests.get('https://YOUR_PRTG_ADDRESS_HERE/api/getstatus.htm?id=0&username=YOUR_USERNAME&passhash=YOUR_PASSHASH')
if resp.status_code != 200:
# This means something went wrong.
return -1
result = resp.json()
if not result['Alarms']:
return 0
return result['Alarms']
except Exception:
return -1

def setWarning():
unicorn.brightness(1)
unicorn.set_all(253,106,2)
unicorn.show()

def setAlert():
unicorn.brightness(1)
unicorn.set_all(255,0,0)
unicorn.show()

def setOkay():
unicorn.off()
unicorn.brightness(0.5)
unicorn.set_pixel(randint(0, 7),randint(0, 7),0,255,0)
unicorn.show()

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(1)

while True:
alertCount = checkPrtg()
if alertCount == -1:
setWarning()
elif alertCount > 0:
setAlert()
else:
setOkay()
sleep(4.7)
[/code]