Check Spam Report

今天寫了一個script來協助daily check…

沒用到太高級的方法,只是把事情搞定而已….

python,真是一個好物啊…:D

#!/usr/bin/env python
#-*- coding:  utf-8 -*-
'''
===== Check Spam Report Script =====
Log:
2009/12/09 21:58 ver 1.0 Create Script
2009/12/10 13:30 ver 1.1 add localtime
2009/12/11 ver 1.5 add if spam reboot count is equal 0 send massage
2009/12/12 16:02 ver 2.0 The spam report state sent to system
2009/12/29 11:20 ver 2.1 improve send mail in python function
2010/02/02 11:00 ver 2.2 fix sendmail problem
====================================
'''
import smtplib
import sys
import time
import commands

__copyright__ = "Copyright 2009, Check Spam Report Script"
__create_date__ = '2009/12/09 21:58'
__version__ = '2.2'
__author__ = 'Allen Wang'
__email__ = 'allencswang@gmail.com'
__credits__ = "Allen Wang"
__license__ = "GPL"
__maintainer__ = "Allen Wang"
__status__ = "Released"  

def chklog(sent, chkcount, chktime):
    cmd = 'cat /var/log/maillog |grep "%s" |grep "%s" |wc -l' % (sent, chktime)
    localtime = time.strftime("%H:%M", time.localtime())
    logcount = commands.getoutput(cmd)
    if int(logcount) == 0:
        emsg(logcount,localtime)
    elif int(logcount) < int(chkcount):
        emsg(logcount,localtime)
    else:
        print 'Check time: %s' % (localtime)
        print 'Total sent %s spam report' % (logcount)
        send_state(logcount,localtime)

def emsg(logcount,localtime):
    print 'Check time: %s' % (localtime)
    print 'please check you spam server,because only sent %s spam mail report' % (logcount)
    localtime = time.strftime("%H:%M", time.localtime())
    send_boxsol(logcount,localtime)
    send_yageo(logcount,localtime)

def send_state(logcount,localtime):
    msg = 'check time:%s \nTotal sent %s spam report' % (localtime,logcount)
    subject = 'The Spam Report check state at %s, Total sent %s spam report' % (localtime,logcount)
    contacts = ['xxx@xxx.xxx']
    mail('xxx.xxx.xxx.xxx','xxx@xxx.xxx',contacts,subject,msg)

def send_boxsol(logcount,localtime):
    msg = 'The spam report have problem at %s,because only sent %s spam report to YAGEO users, please check... message from YAGEO Allen mobile:0920576298' % (localtime,logcount)
    subject = 'The Spam Report have problem, please check...'
    contacts =['xxx@xxx.xxx.xxx','xxx@xxx.xxx.xxx','xxx@xxx.xxx.xxx','xxx@xxx.xxx']
    mail('xxx.xxx.xxx.xxx','xxx@xxx.xxx',contacts,subject,msg)

def send_yageo(logcount,localtime):
    msg = 'The spam report have problem at %s,because only sent %s spam report to YAGEO users, please check...' % (localtime,logcount)
    subject = 'The Spam Report have problem, please check...'
    contacts = ["xxx@xxx.xxx.xxx","xxx@xxx.xxx.xxx","xxx@xxx.xxx","xxx@xxx.xxx"]
    mail('xxx.xxx.xxx.xxx','xxx@xxx.xxx',contacts,subject,msg)

def mail(serverURL=None, strFrom='', strTo='', subject='', text=''):
    #headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (strFrom, strTo, subject)
    smtp = smtplib.SMTP()
    smtp.connect(serverURL)
    #smtp.login('exampleuser', 'examplepass')
    #smtp.sendmail(strFrom, strTo, message)
    for x in strTo:
    	headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (strFrom, x, subject)
    	message = headers + text
	#print x
    	smtp.sendmail(strFrom, x, message)
    smtp.quit()

def main():
    chklog(sent,chkcount,chktime)

if __name__ == '__main__':
        sent = sys.argv[1]
        chkcount = sys.argv[2]
        chktime = sys.argv[3]
        main()

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>