Monitoring your server via twitter!

Manolis Kiagias is a FreeBSD addict. He runs the Greek FreeBSD community website. The FreeBSD operating system is an open source, free of charge UNIX based system from which Apple’s OSX derives!

Manolis has created a neat twitter command line python client, using the py-twitter extension in order to create a twitter user for the community server! It’s a great idea to keep track of your server’s change! Of course, you can read the system’s emails but, twitter is easier to use and much more laconic, which is a good thing.

Manolis shared with me the python twitter script:

#! /usr/bin/env python
#
# twitthis.py - Tweet a message (status update)
#
# Copyright (c) 2009 Manolis Kiagias
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
import sys
import twitter
#
# Enter your real twitter username
# and password in the variables below
#
myusername='myusername'
mypassword='mypassword'
#
# Note: It seems py-twitter can only handle en_US.ISO8859-1
#
count=len(sys.argv)
if count==1:
	print 'You must provide a message!'
	exit(1)
else:
	message=sys.argv[1]
	api=twitter.Api(username=myusername,password=mypassword)
	status=api.PostUpdate(message)
	exit(0)

The way the script works is simple. First you create an account at twitter.com, it’s fast and easy. Afterwards you just “follow” the new account. I used a closed twitter account, because I don’t want my server updates to be publicly available. You can see Manoli’s updates on MyServerStatus twitter account.

The script runs from command line as follows:

atma@devon:~ > python tweet \"Hello World\"

It takes just 1 argument, which in natural language parlance, can be a phrase enclosed in quotes “”. Manoli’s uses traditional cron demon to run the script.

However my home server, runs Apple’s (Tiger) OSX 10.4.11 . Which means that I can’t use cron. I need to use launchd. I chose to display the following informations: System IP address, it’s dynamic hence often changes, the system’s available GB and the Fail2ban last banned IP which I want to know for personal reasons and finally the uptime. Of course you can display other info such as, UPS status, etc.

First we need to install the components that will permit us to run the script. Manoli’s script needs python py-twitter. So using Macports we can install all the needed packages:

$ sudo macports install py25-twitter

NOTE: Under Macports 1.8.1 we have several versions of py-twitter. The one I used is the py25-twitter which is made for Python 2.5, which is the default Python version in my system, installed via macports. If you use Python-2.6 you should install py26-twitter instead.

Here is the script called “tweetall” that will execute Manoli’s script:

#!/bin/bash
 
#command line
cmd="/Users/atma/Progs/bin/tweet"
 
# aliases
ip=`links -dump http://www.whatismyip.org/|awk '{print $1}'`
ban=`echo $(grep Ban /var/log/fail2ban.log|tail -n 1|awk -F " " '{print $7}')`
uptime=`uptime`
gb=`df -h|grep disk0s3|awk -F "G" '{print $3}'`
 
# execute the commands
$cmd "My IP is: $ip |Last banned ip is: $ban |uptime: $up |Free Space is $gb GB"

The only thing we need to do now, is place the script to user’s $PATH or a custom made directory and then create our custom launchd script. Here is the launchd script that will launch the script once every hour:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>GroupName</key>
        <string>atma</string>
        <key>Label</key>
        <string>com.twitter.user</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/atma/Progs/bin/tweetall</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>3600</integer>
        <key>UserName</key>
        <string>atma</string>
</dict>
</plist>

This file is placed in the following directory: $HOME/Library/LaunchAgents/ and the name is com.twitter.user. This directory is custom, you could place the launchd script anywhere in your system, does not matter, it’s just for the sake of “standards” that I used this directory. The second thing you need to change is the “atma” strings in the launchd script. You should change this with your username under OSX. These are the user/groups permissions which the script will be executed, so you need to change the values.

Now launch the script as a user:

$ launchctl -w /Users/atma/Library/LaunchAgents/com.twitter.user

The rest will be handled by your system in automatic. To check that everything is okay just check your launchctl list:

$ launchctl list
com.twitter.user

And this is what you get:
m Monitoring your server via twitter!

Enjoy! :-)

Related posts:

  1. About Launchd and OSX issues. Is it really that good? A couple of weeks ago,  while I was trying to...
  2. ZFS For MacOSX Well, I can’t wait to try this one! The ZFS...
  3. Manage to launch OpenVPN at boot under MacOSX This took a lot of effort that’s why I want...
  4. Δεδομένα στο «σύννεφο» Με την έλευση του cloud computing έχουν έρθει στον μέσο...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply

 


Convalesco is using WP-Gravatar