PHP前端页面二次发或优化
2 篇帖子 分页: 1 / 1
帖子young.liu » 2014年 8月 18日 16:08 星期一
感谢田川@青岛提供思路和数据库结构,感谢板桥老大给予的支持,感谢群里弟兄们积极讨论,感谢各种TV,感谢这几天没开发的同事烦我,让我有时间东拼西凑出了这个脚本。
赶脚写的逻辑很差,主要是自己太笨,没编程思想。附上代码,希望各路大神多多指教
#!/usr/bin/env python

#this script used for zabbix——server get graph from screens and mail to admin
#you must know the screen id and zabbix_url,zabbix_login_name,login_password
#mysql user_name and password
#run this script erverday 0:00
import os,sys,smtplib,MySQLdb,random,time,datetime,email,mimetypes,ctypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.mime.multipart import MIMEMultipart
Period = 86400
Stime = os.popen('date +%Y%m%d%H%M%S').read().rstrip()
Width = 600
heigh = 50
hostname = os.popen('echo $HOSTNAME').read()
mysql_server = "localhost"
mysql_user = "zabbix"
mysql_pass = "zabbix_password"
db_name = "zabbix"
zabbix_url = "zabbix_url"
#example "http://192.168.1.1/"
zabbix_user = "admin"
zabbix_pass = "your_password"
cookie = "/tmp/cookie"
graph_dir = "/tmp/zabbix-graph/report/"
if not os.path.isdir(graph_dir):
os.makedirs(graph_dir)
mail_server = "mail_address"
mail_port = 25
mail_from = "mail_user@domain.com"
mail_password = "mail_password"
global Subject
Subject = "Daily Report"
body = "Linux_Server Report"

def _get_pictures(graph_dir):
pictures = []
for f in os.listdir(graph_dir):
pictures.append(f)
print pictures
return pictures

def _get_graph():
db = MySQLdb.connect(mysql_server,mysql_user,mysql_pass,db_name)
cursor = db.cursor()
#screenid
sql = "select resourceid from screens_items where screenid=48 order by resourceid desc;"
cursor.execute(sql)
results = cursor.fetchall()
db.close()
for id in results:
graphid = int(id[0])
os.popen("""curl -c %s -b %s -d "request=&name=%s&password=%s&autologin=1&enter=Sign+in" %s/index.php""" % (cookie,cookie,zabbix_user,zabbix_pass,zabbix_url))
os.popen("""curl -b %s -F "graphid=%d" -F "period=%d" -F "stime=%s" -F "width=%d" -F "heigh=%d" %s/chart2.php > %s%s.png""" % (cookie,graphid,Period,Stime,Width,heigh,zabbix_url,graph_dir,graphid))
image_name = '%s.png' % (graphid)
#return graphid

###### remove graph
def removeFileInFirstDir(targetDir):
for file in os.listdir(targetDir):
targetFile = os.path.join(targetDir, file)
if os.path.isfile(targetFile):
os.remove(targetFile)

def _content(Subject,body,mail_to):
list = _get_pictures(graph_dir)
msgRoot = MIMEMultipart('related')
msgRoot['From'] = "root@zabbix-monitor.domain.com"
msgRoot['To'] = mail_to
msgRoot['Subject'] = Subject
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(body)
msgAlternative.attach(msgText)
body += "<table>"
#print i
for i in list:
image_name = ('%s' % (i))
f = open('%s%s' % (graph_dir,i),'rb')
msgimg = MIMEImage(f.read())
msgimg.add_header('Content-ID', "<%s>" % image_name)
msgRoot.attach(msgimg)
body += "<tr><td><img src='cid:%s'></td></tr></table><br>" % str(image_name)
print image_name
f.close()
msgText = MIMEText(body, 'html')
msgAlternative.attach(msgText)
msgRoot.attach(msgAlternative)
return msgRoot

def send_mail(mail_server,mail_port,account,password,mail_from,mail_to,content):
mail=smtplib.SMTP(mail_server,mail_port)
# mail.ehlo()
# mail.starttls()
mail.ehlo()
mail.login(account, password)
mail.sendmail(mail_from,mail_to,content)
mail.quit()

def main(mail_to):
_get_graph()
msgRoot = _content(Subject,body,mail_to)
removeFileInFirstDir(graph_dir)
send_mail(mail_server,mail_port,mail_from,mail_password,mail_from,mail_to,msgRoot.as_string())

if __name__ == "__main__":
main(sys.argv[1])
帖子mxj333 » 2014年 9月 6日 21:25 星期六
楼主辛苦了,谢谢分享。
zabbix中文论坛:http://www.zabbix.net.cn 或者 www.zabbix.org.cn
2 篇帖子 分页: 1 / 1

登录

在线用户

正在浏览此版面的用户:没有注册用户 和 5 位游客