Alarm your TheThingsNetwork gateway

There is actually no solution to get an alarm when a TTN gateway is shutting down. That said, all the information are accessible thanks to the different API. 

To monitor my deployed gateways, I’ve made a little script in python working with IFTTT. It connects to the The Things Network api, get the gateway status and call the IFTTT webhook in case of last gateway update too old. 

On IFTTT i’ve created an applet to generate a Push notification on phone to get the alarm.

The script is accessible in the post detail.

2020 update

The script above in the initial post content is not working anymore: the noc API has been deprecated. Please take a look to my post updating the way to monitor your gateway.

The alarming script

This script will execute the monitoring and create an alarm when the last update is > KEEPALIVE_TIMEOUT_S seconds. The gateway ID is obtained from the TheThingsNetwork console.

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

KEEPALIVE_TIMEOUT_S = 60
GATEWAY_ID = "Add_your_GatewayID_here"
IFTTT_WEBHOOK_KEY = "Put_your_WebHook_key_here"

resp = requests.get('http://noc.thethingsnetwork.org:8085/api/v2/gateways/'+GATEWAY_ID)
if resp.status_code != 200:
    # This means something went wrong.
    raise ApiError('GET gateways {}'.format(resp.status_code))

delta=long(time.time()) - long(resp.json()['time'][:-9])

if ( delta > KEEPALIVE_TIMEOUT_S ):
    requests.get("https://maker.ifttt.com/trigger/"+GATEWAY_ID+"/with/key/"+IFTTT_WEBHOOK_KEY)

You have to add this script in you /etc/crontab to run it on regular basis.

*/5    *  *  *  * root	/path/to/script/monitor.py

Then restart crond.

Create the IFTTT Applet

Create a new Applet

Search for the Webhook receipt

Set the name of the gateway as it has been set in GATEWAY_ID

Now we can define the action to execute

Search for notifications

Select the right type to Push notification and configure the notification message to be pushed.

Now the Applet is ready and will execute the push notification on webhook triggering.

Get the IFTTT webhook key

To get your webhook key, you need to reach the webhook setting page

Once in the page you will see an url like this one: 

URL https://maker.ifttt.com/use/xxxxXXXxxXXXXXX-XxX

The string in Red is the Webhook Key to be used.

4 thoughts on “Alarm your TheThingsNetwork gateway

  1. Hi, I am trying to get this to work, but am getting an error on this line ..

    raise ApiError(‘GET gateways {}’.format(resp.status_code))

    Traceback (most recent call last):
    File “check.py”, line 13, in
    raise ApiError(‘GET gateways {}’.format(resp.status_code))
    NameError: name ‘ApiError’ is not defined

    What have I missed?

  2. Pingback: Monitor a gateway connected to The Things Network - disk91.com - technology blogdisk91.com – technology blog

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.