Python gets Zabbix data graph concurrent Mail
#! /usr/bin/env python# coding=utf-8# andy_fimport time, os,datetimeimport urllibimport urllib2import cookielibimport mysqldbimport smtplibfrom email.mime.multipart import mimemultipart # Import Mimemultipart class from email.mime.text import mimetext # Import Mimetext class From email.mime.image import MIMEImage # import Mimeimage class screens = ["nginx_flow", "mysql"]now_date = Time.strftime ("%y-%m-%d") save_graph_path = "./weeks/%s" % time.strftime ("%Y-%m-%d") if not os.path.exists (save_graph_path): os.makedirs (save_graph_path) zabbix_host = "zbx. xxxx.com "username = " Admin "password = " Xxxx&zabbix "width = 750height = 200# graph time period, speriod = 604800# zabbix dbdbhost = "192.168.22.22" DBPORT&NBsp;= 3317dbuser = "xxxx_read" dbpasswd = "xxxx_read" dbname = "zabbix" def mysql_query (sql): try: conn = mysqldb.connect (host=dbhost, user=dbuser, passwd=dbpasswd, port=dbport, connect_ Timeout=20) conn.select_db (dbname) cur = conn.cursor () count = cur.execute (sql) if count == 0: result = 0 else: Result = cur.fetchall () Return result &nbSp; cur.close () conn.close () except MySQLdb.Error, e: print " Mysql error: ", edef get_graph (zabbix_host, username, password, screen, Width, height, period, save_graph_path): for i in mysql_ Query ("select screenid from screens where name= '%s '" % (screen)): for screenid in i: graphid_list = [] for c in mysql_query ("select resourceid from screens_items where screenid= '%s ' % (int (screenid))): for d in c: graphid_list.append (int (D)) for graphid in graphid_list: login_opt = urllib.urlencode ({ "name": username, "password": password, " Autologin ": 1, "enter": "sign in"}) get_graph_opt = urllib.urlencode ({ "graphid": graphid, " Screenid ": screenid, "width": width, "height": height, "period" : period}) cj = cookIelib. Cookiejar () Opener = urllib2.build_opener (urllib2. Httpcookieprocessor (cj)) login_url = r "http://%s/index.php" % zabbix_host save_graph_url = r "http://%s/ chart2.php " % zabbix_host opener.open (login_url, login_opt). Read () data = opener.open (save_graph_url, get_graph_opt). Read () filename = "%s/%s_%s.png" % (save_graph_path, screenId, graphid) f = open (filename, "wb") f.write (data) f.close () Class sendhtmlmail (object): def __init__ (self): self. host = "smtp.exmail.qq.com" self. Subject = u "weekly official website traffic Data report" self. to = [' [email protected] ', ' [email protected] '] self. from = "[email protected]" def addimg (self,src, imgid): fp = open (src, ' RB ') &NBSP;&NBSP;&NBSP;&NBsp; msgimage = mimeimage (fp.read ()) fp.close () msgimage.add_header (' Content-ID ', Imgid) return msgImage def Send (self): msg = mimemultipart (' related ') msgtext = mimetext ("" " <table width= " border=" 0 " cellspacing=" 0 " cellpadding=" 4 "> <tr bgcolor= "#CECFAD" height= " " > <td colspan= "8" style= "text-align: center;font-size: large" > Net loan home weekly flow report (responsible Person: Gaogd) </td> </tr> <tr bgcolor= "#EFEBDE" height= " " duokan-code-cn> <td>< /td> </tr> <tr bgcolor= "#EFEBDE" height= "duokan-code-cn> " <td></td> </tr> <tr bgcolor= "#EFEBDE" height= "duokan-code-cn> " <td></td> </tr> <tr bgcolor= "#CECFAD" height= " duokan-code-cn> " &Nbsp; <td colspan=2> Official Traffic Report <a href= "http://zbx. xxxx.com "> More >></a></td> </tr > </table> "" ", " HTML ", "utf-8") msg.attach (msgtext) msg.attach (self.addimg ("./weeks/%s/36_1904.png"%now_date, "no")) msg.attach (self.addimg ("./weeks/%s/36_1917.png"%now_date, "www")) msg.attach (self.addimg ("./weeks/%s/36_1928.png"%now_date, "bbs")) msg[' Subject '] = self. Subject msg[' from '] = self. from msg[' to '] = ', '. join '. To) print msg[' to '] try: server = Smtplib. SMTP () # Create an SMTP () object Server.connect (self. host, ) # Connect to the SMTP host via the Connect method server.starttls () # Start secure transfer Mode server.login ("[email protected]", "[email protected]*ax") # Email account Login Check Server.sendmail (msg[' from '], self. To, msg.as_string ()) # mail delivery server.quit () # Disconnect SMTP connection print "mail sent successfully! " except Exception, e: print "failed:" + str (e) if __name__ == ' __ main__ ': print ' start:: ', datetime.datetime.now () now_ Date = datetime.datetime.now (). strftime ("%y-%m-%d") for screen in screens: get_graph (zabbix_host, username, password, screen, width, height, period,&nbsP;save_graph_path) send_obj = sendhtmlmail () send_obj. Send () print ' stop:: ', datetime.datetime.now ()
This article is from the "struggle bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1882765
Python gets Zabbix data graph concurrent Mail