top of page

Lab: Send Twilio SMS Messages from Loggly

The purpose of this lab is to allow Python developers to log application events into Loggly and configure Loggly to send a SMS text message using the Twilio service. This is done by leveraging Enzo Online so that developers can abstract the logging service, and so that Loggly can use Twilio for sending SMS messages.

This Lab uses the following technologies:

  • Python 3

  • Enzo Online

  • Twilio

  • Loggly

Configuring Enzo Online to handle both the Loggly calls from the Python application and the Twilio calls from Loggly offers certain advantages:

  • Service configuration settings are stored in Enzo Online securely; not in Python

  • Loggly can send SMS message and make phone calls with Twilio

  • HTTP calls to Enzo Online can be made asynchronously (see online documentation)

  • Changes to Loggly configuration settings are made in Enzo Online, which can be useful when multiple client applications need to send log information

NOTE: This lab does not require a Raspberry Pi; you can code using Python from any operating system for this lab.

Pre-Requisites

Before starting this lab you should have the following:

  • Hardware

  • Any computer with an operating system

  • Python 3 installed

  • A WiFi or wired network to connect to the Internet

  • Services

  • An Enzo Online account (https://portal.enzounified.com). You can create a free account

  • A Twilio account to send text messages (https://www.twilio.com). You can create a free account

  • A Loggly accoung to log application messages (https://www.loggly.com). You can create a free account

Overview

The lab uses Python code to send application events as a log message to Loggly. The message is sent through Enzo Online to abstract the Loggly endpoint; using Enzo Online also allows you to log asynchronously using HTTP commands if desired and to centralize Loggly configuration settings when using multiple devices/applications. Once the logs are saved in Loggly, the lab also helps you configure Loggly to send an SMS text message if a number of errors have been logged; Loggly will be configured to send an SMS message using Twilio through Enzo Online. This allows the logging service to send an SMS message when specific conditions are detected without changing the Python code. This lab could very easily be modified to make a phone call instead/in addition of sending an SMS message.

Configure Loggly

  • Signup for the Loggly service at https://www.loggly.com

  • As part of the signup process you will need to register an application name, a user Id and a password for your personal use; these values can be anything you want

  • Once registered, you will need to find your Customer Token; you can locate it under the Source Setup -> Customer Token tab:

Logging Application Data Loggly Configuration As mentioned previously you will need to locate your Customer Key so that you can configure Enzo Online (the LOGGLY_CUSTOMER_TOKEN variable).

You first need to create a new Loggly service in Enzo Online. Go to the Enzo portal (https://portal.enzounified.com) and create a new Loggly service with the following values:

  • Name: logglyconfig

  • Enzo Instance: <leave the default value>

  • customerToken: LOGGLY_CUSTOMER_TOKEN

  • URI: The primary Loggly URI (https://logs-01.loggly.com/inputs/)

Code Let’s first create a simple Python application that logs to the Loggly service ever few seconds.

Note: Python is sensitive to leading spaces/tabs. Make sure the indentation is exactly as shown in the code below.

Note: Information highlighted in blue means that it may need to be updated for the code to work.

import http.client, urllib.request, urllib.parse, urllib.error, base64, json, uuid from time import sleep import time import requests import sys

#enzo settings enzoconfig = ‘logglyconfig' enzourl="https://daas001.enzounified.com" #logging through enzo enzourl_log= enzourl + '/bsc/loggly/log' #logging through enzo enzoguid="YOUR_ENZO_AUTHTOKEN"

def logMessage(message, severity):

iotheaders={'authToken':enzoguid, '_config': enzoconfig, 'message':message, 'severity':severity} response=requests.post(enzourl_log, headers=iotheaders) # Main code starts here print('starting...') i=0

while True: try: #log an error every 5 times i=i+1 if i==5: i=0 j=1/0 #force an error else: print("Log data...") logMessage("Test message", "INFO") except: print("Error:", sys.exc_info()[0]) logMessage(sys.exc.info()[0], “ERROR”)

sleep(5)

Viewing Log Data When you run the above code, you are essentially saving a message to Loggly through Enzo Online. To verify that you are indeed saving data in Loggly, login to Loggly and search for recent logs.

You should see a result similar to this:

Alerting

Now that you are sending log data to Loggy, let’s configure Loggy to send an alert using the Twilio services. The simplest way to use Twilio is to send a SMS message.

Signup for Twilio To signup for Twilio, visit the Twilio website and sign up (https://www.twilio.com/try-twilio). Once you have signed up, you will need to retrieve your Account SID and Twilio AuthToken from the Dashboard. You will use this information later.

You will also need a Twilio phone number; in order to obtain a Twilio phone number, go to the Learn & Build / Build tab on the Twilio portal, and click on Get a number. As long as you have a trial account, you can obtain a free Twilio phone number to send SMS text messages from.

Twilio Configuration in Enzo Online In the Enzo Online portal, select the Twilio service and create a new configuration with the following information:

  • Name: twilioconfig

  • AccountSID: YOUR_TWILIO_SID

  • Auth Token: YOUR_TWILIO_AUTH_TOKEN

  • Caller Id: TWILIO_PHONE_NUMBER (add +1 in front of the area code for a US phone number; use your country code if not in the US)

  • Country Code: +1 [note: this is your country code]

Your Twilio configuration should look like this:

Setup A Loggly Alert We are ready to configure Loggly to send a SMS using Twilio; to do so we will configure an Alert in Loggly that calls Enzo Online’s Twilio configuration when an Error has been logged (NOTE: it may take a few minutes for the Alert to fire since Loggly triggers its alerts periodically).

We must first create and save a Search. Within the Loggly portal:

  • Click on the Search tab

  • Type this filter (case-sensitive): json.Severity:"ERROR"

  • Click on the star icon (next to Search), select Save this search as

  • Enter LoggedErrors when prompted

Next, click on the Alerts tab and click on Add New to create a new Alert, then provide the information below (leave unspecified values to their default settings):

  • Name: SendMessageOnError

  • Description: Send a SMS when an error is detected

  • Saved Search: LoggedErrors

  • Alert If: Count is > 1 within last 5 minutes

  • Send to an endpoint:

  • Click on Create new Endpoint

  • Select HTTP/S Endpoint

  • Enter a name for the endpoint (ex: EnzoEndpoint-Twillio)

  • Add a short description

  • Add the following URL (replace the ENZO_AUTHTOKEN and CELLPHONENUMBER with your values, and make sure there are no spaces): https://daas001.enzounified.com/bsc/twilio/sendsms?_config=twilioconfig&authToken=ENZO_AUTHTOKEN&message=Error+Received&phones=CELLPHONENUMBER

  • Select the POST method

  • Click on Submit

  • Check for condition every: 5 minutes

  • Click Save

At this point, you have created an Alert in Loggly that will call Twilio through Enzo Online. The configuration screen of the alert should look like this:

You can now observe the number alerts sent from Loggly by looking at the Alerts tab; a line graph shows the number of calls made to Enzo Online.

Monitoring Detailed Activity Through Enzo Online Portal

Last but not least, let’s use the Enzo Online portal to see the activity generated by Loggly’s calls into Twilio. To view the calls made to Enzo Online, use the Access Log tab. Select the Twilio service and click on Apply. You should see an output similar to the picture below.

And here we go! We were able to create an alert in Loggly that sends SMS messages with Twilio using Enzo Online when errors are detected.

bottom of page