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:
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.
Before starting this lab you should have the following:
Any computer with an operating system
Python 3 installed
A WiFi or wired network to connect to the Internet
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
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.
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()) logMessage(sys.exc.info(), “ERROR”)
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:
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:
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):
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):
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.