Python obtains the zabbix Monitoring Chart
The leaders have a requirement to send an email to the server monitoring status on the same day. At the beginning, zabbix is opened every afternoon, which is so tiring every day, therefore, I wrote a script to automatically capture images and assemble them into html, and send them through scheduled mail to automate daily reports. "Zabbix mail monitoring images" (I have seen many scripts on the Internet, such as shell, perl, and python. I don't know much about them, so I wrote it myself ,)
The following is the implementation process of getting zabbix images through python scripts.
# _ * _ Coding: UTF-8 _ * _ importsysimpdatedatetimeimportcookielib, urllib2, urllibclassZabbixGraph (object): def _ init _ (self, url, name, password): self. url = urlself. name = nameself. password = password # generate cookiescookiejar = cookielib during initialization. cookieJar () urlOpener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cookiejar) values = {"name": self. name, 'Password': self. password, 'autologin': 1, "enter": 'signin'} data = urllib. urlencode (Values) request = urllib2.Request (url, data) try: urlOpener. open (request, timeout = 10) self. urlOpener = urlopenerw.turllib2.httperror, e: printedefGetGraph (self, url, values, image_dir): key = values. keys () if "graphid" notinkey: printu ", check whether graphid" sys. exit (1) # The following if is the default value if "period" notinkey: # The default value is one day. The unit is second values ["period"] = 86400if "stime" notinkey: # The default value is values ["stime"] = datetime from the current time. datetime. now (). strftime ('% Y % m % d % H % M % S ') If "width" notinkey: values ["width"] = 800if "height" notinkey: values ["height"] = 200 data = urllib. urlencode (values) request = urllib2.Request (url, data) url = self. urlOpener. open (request) image = url. read () imagename = "% s/%s.png" % (image_dir, values ["graphid"]) f = open (imagename, 'wb') f. write (image) # This url is used to obtain the image. Note that the pie chart URL is different from this URL. Please observe carefully! Gr_url = "http://zabbix.XXXX.com/chart2.php" # login URLindexURL = "http://zabbix.XXX.com/index.php" username = "xxxx" password = "xxxx" # The image storage directory image_dir = "/tmp" # image parameters, the dictionary must be at least graphid. Values = {"graphid": "582", "period": 86400, "stime": 20160101000000, "width": 800, "height ": 200} B = ZabbixGraph (indexURL, username, password) B. getGraph (gr_url, values, image_dir)
Through the image obtained above, html can be assembled to enable automatic daily report sending; free your hands.