Monitor a gateway connected to The Things Network

I’ve published a post in January 2019 on a solution to alarm you TTN gateway. It was based on noc API but these API have been deprecated. It means this solution is not working anymore.

So I’m investigating some new solution: the more relevant is the use of TTN cli solution.

Let’s seen what can we do with this.

Install TTN CLI

The TTN CLI is a client you download from the CLI Quick Start page it exists for all the different platforms. On Mac Os X you need to unzip and switch is to executable (chmod +x) to execute it.

You need to go to TTN account website, eventually login, and follow the link under your photo to access your access code

This will allow you to access a temporary code available for 5 minutes. This code can be use to login TTN CLI and then access the console information with your account rights. This code allows to identify your CLI but there is no time limitation for using the CLI once it has been authenticated.

I don’t know how we can later disconnect a CLI. This could be an interesting function. We could also investigate on creating different account to limit the access rights of the CLI session to only monitoring the gateway and no be able to disconnect them. I’ll investigate this later.

So now we can connect our CLI client with this generated ID.

# ./ttnctl-darwin-amd64 user login XXXXh-1y5dN5_XXXXqpuuOKBKi2J38XXX-0zJxxxxyw

Access a gateway status

# ./ttnctl-darwin-amd64 gateways status eui-58a0cbfffeXXXXXX

  INFO Discovering Router...                   
  INFO Connecting with Router...               
  INFO Connected to Router                     
  INFO Received status         GatewayID=eui-58a0cbfffeXXXXXX

           Last seen: 2020-03-28 10:58:27.578055432 +0100 CET
           Timestamp: 0
       Reported time: 2020-03-28 08:04:32.958090307 +0100 CET
      Frequency Plan: EU_863_870
              Bridge: gs.v3.
            Location: not available
                 Rtt: not available
                  Rx: (in: 0; ok: 0)
                  Tx: (in: 0; ok: 0)

Here we can see the Last seen information. This information is the one we are interesting in for the monitoring and alarming of our The Things Network gateway.

Script that stuff

I’ve made a first script running in background and checking the last seen parameter. When that one is higher than 30 minutes, I’m considering the gateway as down and reporting it in the console. I’ll later automate with IfTTT for alarming.

while [ 1 -eq 1 ] ; do
  d=`./ttnctl-darwin-amd64 gateways status eui-58a0cbfffeXXXXXX | grep "Last seen" | tr -s " " | cut -d " " -f 4,5,7 | sed "s/^\(.*\)\.\(.*\) \(.*\)$/\1 \3/"`
  t=`date -j -f "%Y-%m-%d %H:%M:%S %Z" ${d} +"%s"`
  now=`date "+%s"`
  delta=`echo "$now - $t" | bc `
  echo -n "last delta : $delta s \r"
  if [ $delta -gt 1800 ] ; then
    echo -n ">>> CONTACT LOST at $d now is "
  sleep 300

The more complicated things was to convert the date in String format to an epoc timestamp. Please note that this script has been made to run under Mac Os X where the date options are a bit different compared to Linux. So you may have to port it if under Linux. This script make a request on status every 5 minutes and report an error when the last seen time is higher than 30 minutes.

Upgrade the script for getting alert automatically

I’ve made a more advanced script available on TheThingsNetwork Gateway Monitoring github project. This script is a bash script you can launch on regular basis and firing an alarm over IFTTT on gateway state change.

The Readme file of the project will help you to get the required information on how to use it and configure it.

4 thoughts on “Monitor a gateway connected to The Things Network

  1. Pingback: Alarm your TheThingsNetwork gateway - - technology – technology blog

  2. Pingback: Low-cost outdoor solar powered LoRaWan Gateway - - technology – 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.