Make your server tweet alerts

Today I’m going to show you how to make your server tweet alerts. This might be the first of a long series of admin tools!

The aim of this very first example is to create a script that, periodically, is being run on your server to check if there are updates available. If it is the case, then the script must send an alert.

Challenge accepted, b*tch!

Challenge accepted, b*tch!

Technical choices.

To achieve this goal, here’s what we are going to use:

  • Python, to write the script. No particular skills to have here: I’m a beginner in Python, and believe me, anyone can do this!
  • Twitter, to receive the alerts. The reason why I chose twitter is because it’s universal (can be accessed from OSX, iOS, Windows, Android and so on), almost instantaneous and very easy to use and configure.

 

What you need

To complete this setup you will need:

  • a server running a Linux distribution. I use an Ubuntu web server, but I guess any well-fitted server will do.
  • a twitter account, to receive the notifications.
  • on your server, you will need Python. We’re going to use 2.7 branch.

 

So let’s get started.

 

Install PTT, Python Twitter Tools

PTT is at the very core of everything. It’s a nice framework written by Mike Verdone that allows you to tweet from the command line, which is nice!

Huehuehuehue!

Huehuehuehue!

To install it, we first need to install EZSetup tool:

Then, download and install PTT:

Now that PTT is installed, we must set it up and allow it to tweet on a certain account. To do so, simply type:

to configure PTT, scroll with your keyboard arrows onto the fields, input what you are asked for.

If everything went fine, let’s give it a try. Use this command with the email address that’s associated to your twitter account to send your first tweet from command line:

If you did everything the right way, a tweet should appear on your timeline!

 

Writing the script

Create in your home a directory that will contain all your scripts:

Then, create and edit a new .py file. Let’s name it “update_checker.py”:

Now, input this. Don’t worry, I’ll explain everything in a minute.

Now, let’s explain the whole thingy. If you don’t need explanations, fast forward to the next step.

These are the imports that are necessary to use, respectively, subprocess.Popen and time.strftime functions.

This line creates a subprocess (“subprocess.popen”) to launch “apt-check”. I use absolute paths to avoid $PATH issues, we will see that soon. “shell=True” parameter tells that the subprocess must be executed within a command line. Finally, “stdout=subprocess.PIPE” and “stderr=subprocess.PIPE” tells subprocess.popen that we want the result to be retrieved. That’s exactly what we do next:

Here we retrieve stderr and transform it into a string.

If you run apt-check as-is from command line, it will output something like “2;5”. This means there are 2 updates and 5 security updates available for your system. Try this for a more user friendly version:

Next, we split the “X;Y” string into two integers:

“str.split(“;”)” splits the string on the “;” character and returns an array of results. the first line retrieves the first result, the second the second. now that we have the informations, if one of these two numbers is different from zero we must tweet it.

“cur_date = time.strftime(“%x”)” and “cur_time = time.strftime(“%X”)” respectively retrieve current date and time. The reason I do this is that Twitter won’t allow you to post two times the same content. To bypass this limitation I timestamp every tweet.

The next line composes the tweet itself.

The final line is almost the same as the previous subprocess. It launches the “twitter” command line tool with the message to be posted. Don’t forget to change the “-e” parameter with the email associated to the twitter account you wanna post to.

 

Create a planified job using cron

We got a nice script that tweets when an update is available. This is nice, but if that script could execute, let’s say every day at noon, that would be perfect!

To do so, we are going to use cron jobs. Cron is a service that runs continuously and executes tasks from a config file.

For example, this command:

can be explained like this:

  • 00 : at the top of the hour
  • 12 : during the 12th hour of the day
  • * : every day
  • * : every month
  • * : every day of the week
  • python /home/me/scripts/update_check.py : command to execute.

You can also, for example, execute the very same thing every quarter, all day long, with:

Now simply type

,place your command at the end of the file, save and voilĂ !

 

That’s it!

Now you have an automated task that makes your server tweet alerts when an update is available. The next step is to create a dedicated twitter account for your server and follow it. This way you can receive alerts right on you phone, as I do:

The result!

The result!

 

Now it’s your turn! This is just an idea of what you can do with this system. But let’s think a bit about what you can monitor this way: SSH enforcing, disk space, SMART status, low RAM alerts…

Have ideas? Leave a comment!