真的太懶了…XDD,基本上當系統多到一定程度的時候,就因該進化到異常管理與通知….
人在怎麼勤快,一定會有懶散的時候…
這時候就是想想辦法讓電腦幫我們檢查個個系統的狀態與回報的時候,
前提:你已經裝好Nagios….:P
因Nagios…沒有report的功能…所以我就用python做了一個把網頁轉寄出去的功能…以應付老闆XDD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#!/usr/bin/env python #-*- coding: utf-8 -*- ''' ===== Check Spam Report Script ===== Log: 2009/12/20 10:42 ver 0.1 Create Script "The mail system daily check" 2009/12/20 15:40 ver 0.5 add css in the daily report 2010/01/04 14:00 ver 1.0 fix all html link in mail ==================================== ''' from subprocess import call from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import os import sys import urllib import smtplib import time import re __author__ = "AllenWang" __copyright__ = "Copyright 2009, SYSTEM CHECK FOR MAIL REPORT Project" __credits__ = "Allen Wang" __license__ = "GPL" __version__ = "1.0" __maintainer__ = "Allen Wang" __email__ = "allencswang@gmail.com" __status__ = "Released" def getstate(url, file): '''GET INFORMATION FROM NAGIOS WEB SITE''' #url = "http://nagios.XXXX.com/nagios/cgi-bin/status.cgi?hostgroup=dominoserver&style=detail" #file = "system_state.txt" wp = urllib.urlopen(url) content = wp.read() fp = open(file,"w") fp.write(content) fp.close() return True def replace(r_txt, r_url, data): '''FIX HTML LINK ''' #r_url = 'http://nagios.XXXX.com/nagios/cgi-bin/status.cgi' #r_txt = 'status.cgi' pattern = re.compile(r_txt, re.IGNORECASE) fp = open("tmp", "w") for line in file(data): fp.write(pattern.sub(r_url, line)) fp.close() os.rename("tmp",data) return True def msend_html(server, strFrom, strTo, subject, file): '''SEND MAIL''' #Define these once; use them twice! #strFrom = 'allen.wang@XXXX.com' #strTo = 'allen.wang@XXXX.com' #Create the root message and fill in the from, to, and subject headers msgRoot = MIMEMultipart('related') msgRoot['Subject'] = subject msgRoot['From'] = strFrom msgRoot['To'] = strTo msgRoot.preamble = 'This is a multi-part message in MIME format.' #Encapsulate the plain and HTML versions of the message body in an #'alternative' part, so message agents can decide which they want to display. msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) #We reference the image in the IMG SRC attribute by the ID we give it below fp = open(file, 'rb') msgText = MIMEText(fp.read(), 'html') msgAlternative.attach(msgText) fp.close() #This example assumes the image is in the current directory #fp = open('test.jpg', 'rb') #msgImage = MIMEImage(fp.read()) #fp.close() #Send the email (this example assumes SMTP authentication is required) smtp = smtplib.SMTP() smtp.connect(server) #smtp.login('exampleuser', 'examplepass') smtp.sendmail(strFrom, strTo, msgRoot.as_string()) smtp.quit() return True def main(): url = "http://nagios.XXXX.com/nagios/cgi-bin/status.cgi?hostgroup=dominoserver&style=detail" file = "system_state.txt" r_url = 'http://nagios.XXXX.com/nagios' r_txt = "/nagios" getstate(url, file) replace(r_txt, r_url, file) r_url = 'http://nagios.XXXX.com/nagios/cgi-bin/status.cgi' r_txt = 'status.cgi' replace(r_txt, r_url, file) r_url = 'http://nagios.XXXX.com/nagios/cgi-bin/extinfo.cgi' r_txt = 'extinfo.cgi' replace(r_txt, r_url, file) localtime = time.strftime("%H:%M", time.localtime()) subject = 'The mail system daily report at %s' % (localtime) msend_html('10.181.59.137', 'allen.wang@XXXX.com', 'allen.wang@XXXX.com', subject, file) os.remove(file) if __name__ == '__main__': main() |
python 讚啦…
搞定收工…:P