Tombuntu

Server Monitoring with Python and sSMTP

Earlier this week I wrote about using sSMTP as a simple way to send email from a system. The goal I had in mind was being notified when my web server is under a lot of load or goes down.

This week I wrote a script in Python, servermonitor2.py, to do just this. In case anyone else is interested in this, I’ll post it here:
Download servermonitor2.tar.gz

The script works in two parts. The main python script runs on your local computer. The simple PHP script runs on the web server, it just prints the output of free -m and uptime.

The python script periodically downloads this output and checks the free memory, swapped memory, and load average. If the download fails, or any of these checks are within defined warning zones, an email will be sent with sSMTP.

There’s an options section at the beginning of the python script for setting up the email addresses, the URL of the PHP script, the warning zones, and the time between checks.

Leave a comment, or email me (my address is here) if you have a bug, question, comment or suggestion. I’m new to programming, so I’d also like to hear any critiques on the code.

Archived Comments

Timmy Macdonald

On line 77, I think the Pythonic was of doing things is while True: , not while 1:
It’s a minor thing though :)

Abeisgreat

Awesome idea, I really love this script

tjansson

How about just using gkrellm? I am monitoring a few servers using gkrellmd through ssh tunnels. This way I have a quite detailed real time view of the servers.

As an example see this image of where I have 2 servers and my desktop monitored.
http://www.tjansson.dk/billeder/gkrellm-servers.png

Anonymous

The following error was occured:when I compiled

ALERT:
free ram warning
swap warning
load average warning

could not start sSMTP, email not sent
Traceback (most recent call last):
File “servermonitor2.py”, line 151, in
main_loop()
File “servermonitor2.py”, line 106, in main_loop
send_email(problemlist)
File “servermonitor2.py”, line 68, in send_email
ssmtp.communicate(message(recipient, sender, details))
UnboundLocalError: local variable ‘ssmtp’ referenced before assignment

Respond via email