Hyperion LED’s & Nagios (Part 2)

Maybe you read Part 1, maybe you skipped it ūüôĀ

Either way Part 2 is going to cover creating a new effect for Nagios. I wont say it’s the best bit of python programming I’ve done (Yes I copied and changed another effect as a starting point). The main thing I wanted was an alert status (yes Star Trek does come into it).

There’s 4 files to this effect:

  1. alert.py – The actual python program
  2. alert-green.json – etting a green colour (yes it’s spelt COLOUR not color!!!!!)
  3. alert-yellow.json
  4. alert-red.json

My hyperion is installed to ‘/opt/hyperion’ so first thing is to go to the effects directory

cd /opt/hyperion/effects

Then create the alert.py file

nano alert.py

Paste the following into it and save the file

import hyperion
import time
import colorsys
import numpy as np
 
# Get the rotation time
colorfrom =     hyperion.args.get('colorfrom', (0,0,0))
colorto =       hyperion.args.get('colorto', (0,255,0))
speed   =       float(hyperion.args.get('speed', 0.05))
freq    =       float(hyperion.args.get('frequency', 2.5))
step    =       float(hyperion.args.get('step', 30.0))
ledCount =      hyperion.ledCount
 
# Setup the colors
colorfromnp     =       np.array(colorfrom)
colortonp       =       np.array(colorto)
 
#define the lerp calc
def lerp(a, b, t):
        x = a*(1 - t) + b*t
        x = np.around(x)
        x = x.tolist()
        return x
 
# Start the write data loop
while not hyperion.abort():
        for i in range(0,int(step)):
                n = i / step
                thiscolor = lerp(colorfromnp, colortonp, n)
                colorLedsData = ledCount * bytearray((int(thiscolor[0]), int(thiscolor[1]), int(thiscolor[2])))
                hyperion.setColor(colorLedsData)
                time.sleep(speed)
        time.sleep(speed*10)
        for i in range(1,int(step)):
                n = i / step
                thiscolor = lerp(colortonp, colorfromnp, n)
                colorLedsData = ledCount * bytearray((int(thiscolor[0]), int(thiscolor[1]), int(thiscolor[2])))
                hyperion.setColor(colorLedsData)
                time.sleep(speed)
        colorLedsData = ledCount * bytearray(colorfrom)
        hyperion.setColor(colorLedsData)
        time.sleep(freq)

I do use a numpy array, I can’t remember installing it but may have done.¬†You can test if you have it installed

python
import numpy as np

Then we create the alert-green.json

nano alert-green.json

And Paste

{
        "name" : "Green Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [0,255,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

alert-yellow.json

{
        "name" : "Yellow Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [255,255,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

alert-red.json

{
        "name" : "Red Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [255,0,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

Finally restart hyperion to pickup the new effects

service hyperion restart

That’s it, you should now be able to run these effects. From the terminal:

hyperion-remote -e "Red Alert"

Should start the led’s glowing Red. If not I’d start with ‘hyperion-remote -l’ to check the effects are listed, then move on to working out what’s missing (see above about numpy array).

A few notes on this effect:

  • Originally I programmed it forced to black, but it made sense to change this to a colorfrom value¬†for future (if you want it green and fading to red for example).
  • The speed is how quickly to progress through the fade.
  • The step is how many¬†colours it goes through to get from the colorfrom to the colorto
  • Working out the speed and the step together are important for a nice fade (not jerky).
  • The Freq in how long it stays on black before running the fade again.

This completes the hyperion side of the setup. I now have this effect installed and running on 3 PI’s and yes when there’s a problem they ALL go to yellow/red alert

See Part 3 for the nagios setup

2 thoughts on “Hyperion LED’s & Nagios (Part 2)”

Leave a Reply

Your email address will not be published. Required fields are marked *