Recently, in the practice of Python program, whim, want to use ZABBIX-API to achieve Zabbix by mail to send screen graphics report, in order to predict the occurrence of failure, directly on the source, I rookie, the program has rotten place, please master forgive.
#!/usr/bin/python# -*- coding: utf-8 -*-import osimport sysimport Timeimport shutilimport mysqldbimport smtplibimport requestsimport datetimeimport urllibimport urllib2import cookielibfrom email. Mimetext import mimetextfrom email. Mimeimage import mimeimagefrom email. mimemultipart import mimemultipart# based on zabbix 2.4.4zabbix_host = ' 127.0.0.1 ' username = ' admin ' password = ' Zabbix ' width = 400height = 156period = 86400dbhost = ' localhost ' dbuser = ' Zabbix ' dbpasswd = ' Zabbix ' dbname = ' Zabbix ' dbport = ' 3306 ' graph_path = '/tmp/zabbix_graph ' email_domain = ' 139.com ' email_username = ' 13434111885 ' email_password = ' [EMAIL protected] ' recipients = ["/home/*.jpeg"]graphid_list = []def&nbSp Sql_exec (query): try: _conn = MySQLdb.connect (host= ' localhost ', user= ' Zabbix ', passwd= ' Zabbix ', port=3306,connect_timeout=20) _conn.select_db (dbname) cur = _conn.cursor () _conn = cur.execute (query) if _Conn == 0: result = 0 Else: result = cur.fetchall () return result cur.close () _conn.close () except mysqldb.error,e: print "Mysql error:" ,edef Save_Graph ( Zabbix_host,username,password,width,height,period): for c in sql_exec (" Select resourceid from screens_items where screenid= ' "): for d in c: graphid_list.append (int (d)) for graphid in graphid_list: print graphid login_opt = urllib.urlencode ({ "name": username, "Password": password, "Autologin": 1, "Enter": " Sign in "}) &NBSP;&NBSP;&NBsp; get_graph_opt = urllib.urlencode ({ "Graphid": graphid, "Screenid": ' 16 ', ' 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.jpeg"%str (graphid) f = open (filename, "WB") f.write (data) F.close () def send_mail (screen_name, graphs, to_list): me = ' Zabbix <%[email protected]%s> ' % (email_username, email_domain) def _create _msg (): msg = mimemultipart (' related ') msg[' Subject '] = ' zabbix screen report: %s ' % screen_name msg[' From '] = me msg[' to '] = '; Join (to_list) msg.preamble = ' this is a multi-part Message in mime format. ' contents = "Finally, get the shape
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/F4/wKioL1ZyOWGQKacsAABzUF2HcZg656.png "style=" float: none; "Title=" DAT00001 (1). png "alt=" Wkiol1zyowgqkacsaabzuf2hczg656.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/77/F4/wKioL1ZyOWKzsYGuAACMMGyXBWs755.png "style=" float: none; "Title=" DAT00002 (1). png "alt=" Wkiol1zyowkzsyguaacmmgyxbws755.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/F4/wKioL1ZyOWPCyky8AABJfF0SuLc880.png "style=" float: none; "Title=" DAT00003 (1). png "alt=" Wkiol1zyowpcyky8aabjff0sulc880.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/F4/wKioL1ZyOWTQwgB6AABZ0jcvE5Y949.png "style=" float: none; "Title=" DAT00005 (1). png "alt=" Wkiol1zyowtqwgb6aabz0jcve5y949.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/F5/wKiom1ZyOVvR1_nTAABvXNiGfN0654.png "style=" float: none; "Title=" DAT00006 (1). png "alt=" Wkiom1zyovvr1_ntaabvxnigfn0654.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/77/F5/wKiom1ZyOVvA7b6uAABudx2k414849.png "style=" float: none; "Title=" DAT00007 (1). png "alt=" Wkiom1zyovva7b6uaabudx2k414849.png "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/77/F4/wKioL1ZyOWaTFsGlAABs8Bmx7sQ376.png "style=" float: none; "Title=" DAT00008 (1). png "alt=" Wkiol1zyowatfsglaabs8bmx7sq376.png "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/77/F4/wKioL1ZyOWeCTTVtAAB6AeP7qv0381.png "style=" float: none; "Title=" DAT00009 (1). png "alt=" Wkiol1zyowecttvtaab6aep7qv0381.png "/>
This article is from the "Microsoft" blog, so be sure to keep this source http://1238306.blog.51cto.com/1228306/1725589
Using Zabbix to send screen graphical reports via mail