Skip to content

Instantly share code, notes, and snippets.

@koenbollen
Created September 25, 2011 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenbollen/1240350 to your computer and use it in GitHub Desktop.
Save koenbollen/1240350 to your computer and use it in GitHub Desktop.
Very simple system load monitor.
#!/usr/bin/env python
# load.py
# Test the current load of this system and report through e-mail when it's
# above a given threshold.
#
# Koen Bollen <meneer@koenbollen.nl>
# GPLv2 2011
#
LOAD_THRESHOLD = 0.8 # normalized (load/cpus > LOAD_THRESHOLD)
TARGET = "Your Name <you@example.com>"
FROM = "load.py <system@example.com>"
SMTPSERVER = "smtp.example.com"
from email.mime.text import MIMEText
import multiprocessing
import os
import smtplib
def trigger_alarm(load=None):
if load is None:
load = getload()
host = os.uname()[1]
msg = MIMEText( """***Warning!***
The load on the system {host} is extremely high!
Current load: {load}
Take action!
Sincerely,
load.py
""".format(**locals()) )
msg['Subject'] = "[load.py] {load}@{host}".format(**locals())
msg['From'] = FROM
msg['To'] = TARGET
s = smtplib.SMTP( SMTPSERVER )
s.sendmail( FROM, [TARGET], msg.as_string() )
s.quit()
def getload( procloadavg="/proc/loadavg" ):
with open( procloadavg ) as fp:
return max( map(float, fp.readline().split()[:3]) )
def main():
cpus = multiprocessing.cpu_count()
load = getload()
if load/cpus > LOAD_THRESHOLD:
trigger_alarm(load)
if __name__ == "__main__":
main()
# vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=79:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment