Created
September 25, 2011 07:40
-
-
Save koenbollen/1240350 to your computer and use it in GitHub Desktop.
Very simple system load monitor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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