Skip to content

Instantly share code, notes, and snippets.

@danmackinlay
Created August 26, 2009 04:05
Show Gist options
  • Save danmackinlay/175303 to your computer and use it in GitHub Desktop.
Save danmackinlay/175303 to your computer and use it in GitHub Desktop.
load-monitoring script that pushes notifications to prowl when something goes wrong
#!/usr/bin/env python
"""
load-monitoring script that pushes notifications to growl when something goes wrong
requires jacobb's prowlpy http://github.com/jacobb/prowlpy/tree/master - and a prowl
account (http://prowl.weks.net/)
this version, with a "panic" function, is untested.
"""
from __future__ import with_statement #python 2.5 compatibility
from time import sleep
import prowlpy
API_KEY = 'xxxxx'
EMAIL = "xxx@example.org"
APP_NAME = 'XXXX'
LOAD_NOTIFY_LIMIT = 3.0
LOAD_PANIC_LIMIT = 15.0
import os #could be inside the fn to save memory, but that's no good if the server is already overloaded
def panic():
os.system('killall -9 php-cgi')
os.system('/etc/init.d/apache2 restart')
def get_load():
with open('/proc/loadavg', 'r') as f:
return float(f.read().split(' ')[0])
p = prowlpy.Prowl(API_KEY)
load = 0.0
subject = None
message = None
priority = -2
while True:
try:
load = get_load()
if load > LOAD_NOTIFY_LIMIT:
if load > LOAD_PANIC_LIMIT:
(subject, message, priority) = (
'Server Overload Panic!',
"load average is %f. Running panic function" % load,
2
)
panic()
else:
subject, message, priority = 'Server Overloaded', "load average is %f" % load, 1
else:
subject, message, priority = None, None, -2
except IOError, e:
subject, message, priority = "Can't read server load", str(e), 2
if subject:
try:
p.add(APP_NAME, subject, message, priority)
except IOError:
import smtplib
s = smtplib.SMTP()
s.sendmail(EMAIL, EMAIL, """Subject: %s
%s""" % (subject, message))
sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment