Skip to content

Instantly share code, notes, and snippets.

@ptone
Created January 7, 2010 22:26
Show Gist options
  • Save ptone/271651 to your computer and use it in GitHub Desktop.
Save ptone/271651 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2010-01-07.
preston@ptone.com
Copyright (c) 2010
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import with_statement
import sys
import os
from subprocess import Popen, call, STDOUT, PIPE
import time
# CONFIG:
SENDER = 'server@example.com'
SENDER_PASS = 'secret'
RECIPIENT_OK = 'tech@example.com'
ADDITIONAL_DEGRADED_RECIP = '555@txt.att.net'
SMTP_SERVER = "smtp"
def touch(fname, times = None):
with file(fname, 'a'):
os.utime(fname, times)
def sh(cmd):
return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0]
def send_alert(body):
from email.MIMEText import MIMEText
import smtplib,sys
ok_check = '/tmp/raidok'
msg = MIMEText(body)
mfrom = "phone@sanroque.net"
if 'degraded' in body.lower():
to = "%s,%s" % (RECIPIENT_OK,ADDITIONAL_DEGRADED_RECIP)
msg['Subject'] = "RAID DEGRADED"
else:
to = RECIPIENT_OK
msg['Subject'] = "RAID OK"
if os.path.exists(ok_check):
mtime = os.path.getmtime(ok_check)
now = time.time()
if now-mtime < (60*60*24):
sys.exit()
touch (ok_check)
msg['From'] = SENDER
msg['To'] = to
server = smtplib.SMTP(SMTP_SERVER,587)
# server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(SENDER,SENDER_PASS)
server.sendmail(SENDER,to.split(','),msg.as_string())
server.quit
def main():
raid_check = sh("raidutil list status")
if "degraded" in raid_check.lower():
send_alert('''RAID DEGRADED on %s
''' % sh("hostname"))
print "Alert Sent"
else:
send_alert('''RAID OK on %s
''' % sh("hostname"))
print "OK"
sys.exit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment