At Cerner Engineering, we love to celebrate Pi Day. This day is not only a fun time to enjoy eating pie and reflecting on mathematical properties, but we also share big announcements internally for our developers conference, DevCon.
For this post, we thought it would be fun to share a simple example of how you can hook your existing monitoring system to a physical alarm system powered by a Raspberry Pi. This alarm will be a red spinning light, the universal symbol of “something is going wrong.” We will build a program that will integrate with New Relic to determine if there are issues in our environment. If there are any issues, it will trigger the alarm and your monitoring system will come alive!
- Jumper wires – Wires that we will use to trigger the switch from your GPIO pins.
- Alarm light that is AC powered – There are several different types of fun alarm or party lights you could use. The important characteristic that we are seeking is an AC powered light, as we will use this to control its state. Many lights you will find are battery powered, which will not work for this example.
- PowerSwitch Tail or alternative – I have had this setup for quite some time, so I still have a PowerSwitch Tail, but looks like they may not be available by all providers. You can get other alternatives out there, where you can control an AC power switch safely through GPIO interactions.
- Raspberry Pi – I have an older model, but anything with a powered ethernet connection works well (ex. Raspberry Pi 3 Model B+), as we will be making remote HTTPS calls. If this is your first time getting a Raspberry Pi, there are great kits that include your power adapter and SD card (as you will need that too).
- RJ45 ethernet cable (optional) – We won’t leverage WiFi for this example, and simply use a ethernet cable for maintaining a network connection for the Raspberry Pi. You don’t need this if you already know how you plan to connect your Raspberry Pi to your existing network.
Once you have this setup, we are going to use the Raspberry Pi to control your AC output by communicating to the PowerSwitch Tail through our GPIO pins. A Go program will control the logic of flexing the alarm on or off by polling a monitoring system: New Relic. New Relic is a real-time monitoring platform that gives you powerful insights about the applications you are operating. One of the features of New Relic, is that you can build alerts about different indicators of your application (ex. high memory utilization of a service). These can be rolled up to an “incident” concept, when you have a violation on an alert condition. For this physical alarm, it made sense to pair it with this concept that we use from New Relic. Therefore, if you build something that would trigger human engagement with your alerts (like an incident), this alarm can generically pick these up, without you having to manage anything else.
Here is the code snippet of what we will implement. It is a Go program, which will interact with the GPIO pins using go-rpio. It will essentially run in a loop, and poll New Relic’s Alert API every minute. To ensure we aren’t running the alarm in the after-hours, we will also flex when this can trigger (ex. Mon – Friday, 9 – 5 PM).
First, we will build something that can invoke the New Relic Alerts API. This will offer a single function (
hasOpenIncidents) that will dictate if there are any open incidents when checking with New Relic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
We will then manage the GPIO pin state in a simple loop which will check to see if there are any open incidents. If so, it will set the pin to High, which will trigger the light switch. Otherwise it will set it to low. We will also include a handler for setting the pin to low when we terminate the application (ex. via a SIGTERM). Example of managing the state:
1 2 3 4 5 6 7 8
When you put it all together, the full picture of code looks like this (
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
For my example, I have this in a
alarm.go file within my
nr-pi-alarm directory. I then issue the following command:
This will produce a
nr-pi-alarm you can then transfer to your Raspberry Pi for execution. One example of doing this is with
Wiring it up
For this example, we are using the physical pins #12 and #14 (GPIO18 and GND). We will use our jumper wires to then hook this to the AC switch:
You will then hook your alarm light to the AC switch (PowerSwitch Tail). If the alarm light has its own on/off switch, turn it to on, as we don’t want this manual switch to block what our Raspberry Pi is going to control based on the flow of power.
After you have transferred the build to your pi, you can then configure the alarm to use your New Relic account. This is achieved by creating a
config.yml file in your
alarm directory which currently hosts the
nr-pi-alarm program (set to
600 for file permissions):
After you have configured it, simply invoke this to run the alarm:
1 2 3
Happy Pi Day
We hope you are having a great Pi Day and maybe this example will give you other ideas of what you can build to bring your systems alive. 😀