Home / BVA Auctions / Monitoring SMS gateway with Zabbix

Monitoring SMS gateway with Zabbix

Our website is, partly, depending on the ability to send SMS messages. This for instance happens when you try to register with a public email address like gmail.com. In that case, you need to enter your mobile phone number. We will then send a code to it, to verify that you really exist. If you do not enter a code (or enter a wrong code), you will not be able to register.

So it is very important for us that this functionality keeps on working. Since we do not have our own SMS gateway but are using an external one (Messagebird), we are dependent on it. And if we are depending on something to keep our business running, we should monitor it. And of course you can do a simple check to see if the service is actually reachable, but that is in our case not enough. We are buying credits to send messages. If we run out of credits, the service still is reachable, but we cannot send any messages anymore. So we need to have monitoring on the number of credits and give an alert when it becomes too low.

In this post it is shown how to monitor this in Zabbix, which is our monitoring solution.

Creating the (external) script

How Zabbix works, is that you can define external scripts which will be called. Where these scripts should be located and how to configure this location is not part of this post and can be found on various other websites. All that you need to know is, that these scripts should return a value that can then be stored and used to put alerts on or create graphs from. The script can be written in any language as long as it is executable. In this case, a call to the Messagebird API is needed to get the current balance. This then gives an indication of how many SMS messages you can send. So the script that we are using is to determine this amount.

Looking through the API documentation, the call is quite straightforward. As an example they give:

curl -X GET https://rest.messagebird.com/balance -H 'Authorization: AccessKey test_gshuPaZoeEG6ovbc8M79w0QyM'

Of course the key has to be replaced by our own key. And since we do not like to have keys in our code, it can be passed as a parameter. In the end we just put it in a Perl script that looks like this:

#!/usr/bin/perl -w

use strict;
use warnings;

my $key = $ARGV[0];


sub getbalance {

   use JSON::PP;
   use LWP::UserAgent;
   use HTTP::Request::Common qw(GET);

   use constant BALANCE_URI => "https://rest.messagebird.com/balance";

   my $ua = LWP::UserAgent->new;

   my $request = GET BALANCE_URI;
   $request->header('Authorization', 'AccessKey ' . $key);

   my $response = $ua->request($request);

   my $json = decode_json($response->content);
   print $json->{'amount'};

To make clear what it does, we store it as ‘messagebird_credits_check.pl‘.

As can be seen the script is not really complex. A request is created, the ‘Authorization’ header is set, request is executed and the response is parsed. At the end, the current amount of credits is printed. And that is also the value that eventually will be stored in Zabbix.

Defining the template

First thing that needs to be done in Zabbix, is create a ‘Template’. This can be done by navigating to ‘Configuration -> Templates‘. In the top right corner you will find a button ‘Create template‘, which will open a form to create the template. Here you give the template a name, like for instance ‘Template_Messagebird‘. Another thing we can do in this screen, is define a macro. You can see this as a variable you can use when calling scripts. In order to define it, the ‘Macros‘ tab must be opened. Now we can define the name of the macro and its value. So for instance:

Macro: {$API_KEY}
Value: test_gshuPaZoeEG6ovbc8M79w0QyM

Note: The key used above is the example key taken from the Messagebird API documentation. Of course you should use your own key.

Defining an application

To make it easier to find everything later on, we define an application. Click on the ‘Applications‘ link of the template, followed by clicking the ‘Create application‘ button in the top right. All we need to enter here, is a name. For example:

Name: Messagebird

Defining the item

After the template has been created, a new item has to be added to it. Click on the ‘Items‘ link of the template, followed by clicking the ‘Create item‘ button in the top right corner. In the form change at least the following items:

Name: Credit check
Type: External check
Key: messagebird_credits_check.pl[{$API_KEY}]
Type of information: Numeric (float)
Units: credits
Update interval (in sec): 1800
Applications: Messagebird

The most interesting one here, is the value we use for the ‘Key‘. Here we enter the name of the (external) script, followed by the macro we defined earlier. This will be a parameter that will be passed to the script. Since the result of the script is a float, the ‘Type of information‘ is set accordingly.

Defining the trigger

Now that we have a way of getting the number of credits, we also want to be alerted when it is becoming critically low. So if the credits get below get below for instance 500, an alert should be shown, so that action can be taken.

To create a trigger, click on the ‘Triggers‘ link of the template, followed by clicking on the ‘Create trigger‘ button. In the form enter the following information (as an example):

Name: SMS Credits running low ({ITEM.VALUE} remaining)
Expression: {Template_Messagebird:messagebird_credits_check.pl[{$API_KEY}].last(0)}<500
Severity: Warning

Putting the amount of credits in a graph

Additionally you can also put the amount of credits in a graph. This way you can see if there is a sudden drop, or something strange happening. Also it is a nice way to see how long it will take before you run out of credits.

In order to create a graph, you need to go to the ‘Graphs‘ link of the template and click on the ‘Create graph‘ button in the top right corner. An example of how the form can be filled in is:

Name: Messagebird Credits
Items: Template_Messagebird: Credit check

Of course the template has to be linked to a host, but that is described on a lot of places on the internet.

Interested joining BVA Auctions?

Take a look to our vacancies

About Marcel van den Brink

Marcel van den Brink is a software engineer with 20+ years of experience in web development. He is passionate about the development process, CI, CD, metrics and getting the most out of a team.

Leave a Reply

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