檢查version,發送郵件
阿新 • • 發佈:2018-01-23
tdi pri mysql version fault xxxxx content email 中文
#-*-coding:utf-8 -*-
#!/usr/bin/python
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
import MySQLdb
import houtai_dbname
import paramiko
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
import smtplib
#第三方服務smtp
mail_host=‘smtp.yeah.net‘
mail_user=‘xxxxx‘
mail_pass=‘xxxxx‘
mail_postfix= ‘yeah.net‘
receivers=‘[email protected]‘
def get_version(checkver,plname):
stri=" "
checkver=checkver
print plname
global conn
defdb=houtai_dbname.check_plname(plname)
try:
cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
conn.select_db(defdb)
sql=‘SELECT ip,gameport,zoneName from serverinfo where pf=\‘%s\‘ and zoneid<8000 ORDER BY zoneid‘ % plname
cur.execute(sql)
result = cur.fetchall()
cur.close()
except MySQLdb.Error,e:
print "MySQL Error %d:%s" % (e.args[0],e.args[1])
#print e
for i in result:
ip=i["ip"]
port=i["gameport"]
zonename=i["zoneName"]
print "%s:%s" % (ip,port)
sshport=22
pkey_file =‘/root/.ssh/id_rsa‘
user=‘root‘
key=paramiko.RSAKey.from_private_key_file(pkey_file)
cmd="tail -n 1 /usr/jyserver/%s/server/bin/version" % port
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(ip,sshport,user,pkey=key,timeout=5)
stdin,stdout,stderr=s.exec_command(cmd)
cmd_result=stdout.read()
print cmd_result
if str(cmd_result) != checkver:
stri=stri+"%s %s %s %s not rigth \n " % (plname,zonename,ip,port)
print stri
return stri
def send_mail(mail_to,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">" #郵件發送方
message = MIMEText(content, ‘plain‘, ‘utf-8‘) #郵件內容
message["Accept-Language"]="zh-CN" #中文
message["Accept-Charset"]="ISO-8859-1,utf-8"
message[‘subject‘] = subject #郵件標題
message[‘from‘] = me #發送方
message[‘to‘] = receivers #接收方
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host) #連接smtp
smtpObj.login(mail_user,mail_pass) #登錄
smtpObj.sendmail(me,receivers, message.as_string()) #獲取參數發送郵件
smtpObj.close() #關閉
print "郵件發送成功"
except Exception,e:
print "ERROR,無法發送郵件"
print e
if __name__=="__main__":
alls=""
dic=houtai_dbname.houtaidic
for i in dic:
print i,dic[i]
plname=i
conn = MySQLdb.connect(host="%s" % dic[i],user=‘xxxxx‘,passwd=‘xxxxxxx‘,port=3306,charset=‘utf8‘)
alls=alls+get_version(str(sys.argv[1]).lstrip().rstrip(),plname)+ ","
#print alls
send_mail(‘[email protected]‘,‘platfrom version check‘,alls)
檢查version,發送郵件