[Tutor] System Monitoring

Sean Carolan scarolan at gmail.com
Wed Feb 9 16:18:56 CET 2011


> Hi,
> Im fairly new to programming in python, and have a question.
> Im looking to build a program that monitor's certain things on my Linux
> system. for instance disk space. What is the best way to monitor a Linux
> server without using to much resources?
> Should I execute shell commands and grab the output of them? Or should i use
> SNMP. Or is there a better way?
> Thanks in advance!
> de Haan

de Haan:

I'm guessing from the text of your message that this is a single,
stand-alone system.  Nagios and cacti are complete overkill for system
monitoring on a single machine.  Here are some other options that you
can try:

*  GUI monitoring - gkrellm is great for this.  You can run it and see
performance graphs and monitors right in a little app on your desktop.

*  Command line monitoring - it's pretty easy to cobble together a
python script that monitors things like load average, memory, disk
space, etc.  Hint:  you can get a *lot* of useful info from the /proc
directory, for example, /proc/meminfo, /proc/loadavg, etc.

Here's a quickie that I built for a client, it watches the 15 minute
load average.

#!/usr/bin/env python
'''
File: load_average_watcher.py
Author: Sean Carolan
Description: Watches 15 minute load average and alerts sysadmin if it
gets over a certain level.
'''

import smtplib
from email.MIMEText import MIMEText

threshhold = 3.0
sender = 'root at server.com'
recipient = 'sysadmin at example.com'

def sendMail():
	msg = MIMEText("Alert - system load is over "+str(threshhold))
	msg['Subject'] = 'System Load Alert'
	msg['From'] = sender
	msg['To'] = recipient
	s = smtplib.SMTP()
	s.connect()
	s.sendmail(sender, [recipient], msg.as_string())
	s.close()

for line in open('/proc/loadavg'):
    #If the load average is above threshhold, send alert
    loadav = float(line.split()[2])
    if loadav < threshhold:
	print '15 minute load average is '+str(loadav)+': OK!'
    else:
	print '15 minute load average is '+str(loadav)+': HIGH!'
        sendMail()


More information about the Tutor mailing list