Due to certain requirements, it is necessary to send a Daily mail to the relevant personnel, including the day before the Zabbix monitoring graphics, log on daily and manually sent words is very troublesome.
A simple repetition of the work to the machine to do the principle of writing a Python program to automatically get the graphics and send.
The general idea is:
Analog Login Zabbix---> Find the desired image and download it to local---> Send mail using python
First, impersonate the login Zabbix on the server, and download the required image to the local
#!/usr/bin/env python#-*-coding:utf-8-*-ImportHtmlparserImportUrlparseImportUrllibImportUrllib2ImportCookielibImportstringImportOS#log in to the main pageHosturl ='http://zabbix.example.com/screens.php?elementid=26&sid=fbc2974a32b8dc87' #fill in according to your actual address#post data receive and process pages (we want to send our constructed post data to this page)PostURL ='http://zabbix.example.com/index.php' #The URL to process the post request from the data packet#set up a cookie processor that is responsible for downloading cookies from the server to local and bringing local cookies when sending requestsCJ =Cookielib. Lwpcookiejar () Cookie_support=Urllib2. Httpcookieprocessor (CJ) Opener=Urllib2.build_opener (Cookie_support, Urllib2. HttpHandler) Urllib2.install_opener (opener)#Open the Login Main page (his purpose is to download the cookie from the page so that we have a cookie when we send the post data, otherwise the delivery is unsuccessful)h =Urllib2.urlopen (Hosturl)#to construct the header, the general header must contain at least two items. These two items are analyzed from the bag caught. headers = {'user-agent':'mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) gecko/20100101 firefox/14.0.1', 'Referer':'******'}#The post data is constructed, and he is also analyzed from the big bag. PostData = { 'name':'username',#User name 'Password':'passwd',#Password 'Autologin': 1, 'Enter':' Sign In'}#need to encode the post dataPostData =Urllib.urlencode (postdata)#use the request method provided by URLLIB2 to send the data we construct to the specified URL and complete the login processRequest =Urllib2. Request (PostURL, PostData, headers) Response=Urllib2.urlopen (Request) Text=Response.read ()defget_graph (host, Graphid, period, image_name): Path='/opt/daily_mail/image/' #Save the address of the picture #The construction of the address of the Zabbix pictureURL ="http://%s/chart2.php?graphid=%s&period=%s&width=800&height=100"%(host, Graphid, period) Img_req=Urllib2. Request (URL) PNG=Urllib2.urlopen (img_req). Read () file= path + Image_name +'. PNG'with open (file,'WB') as F:f.write (PNG) host='zabbix.example.com'//your Zabbix service domain name Get_graph (host,1237, 86400,'Cpu_load') get_graph (host,1239, 86400,'net_traffic') get_graph (host,918, 86400,'155_eth0')
Second, after downloading the image to the local, you need to use Python to send mail, there are many e-mail tutorials
#!/usr/bin/env python#-*-coding:utf-8-*-ImportSmtplib fromEmail.mime.multipartImportMimemultipart fromEmail.mime.textImportMimetext fromEmail.mime.imageImportMimeimageImportDataImporttimemailto_list=['[email protected]']#recipient listmailcc_list=['[email protected]']#cc Listmail_host="smtp.example.com:25" #Mail ServerMail_user="username" #User namemail_pass="passwd" #Passwordmail_postfix="example.com" #message suffixHTML="""<meta http-equiv= "Content-type" content= "text/html; charset=utf-8" ><p> server CPU User Time 24h<br ></p><p> server Net outging 24h<br></p><p> server eth0 24h egress bandwidth 24h<br></p>"""defSend_mail (To_list, Cc_list, sub):#Add Image defaddimg (SRC, imgid): FP= Open (src,'RB') Msgimage=Mimeimage (Fp.read ()) Fp.close () Msgimage.add_header ('Content-id', Imgid)returnmsgimage msg= Mimemultipart ('related') #HTML codeMsgtext = Mimetext (data.html,"HTML","Utf-8") Msg.attach (msgtext)#full file path, which is the location where ID is inserted in HTML based on IDMsg.attach (Addimg ("/opt/daily_mail/image/cpu_load.png","Cpu_load")) Msg.attach (Addimg ("/opt/daily_mail/image/net_traffic.png","net_traffic")) Msg.attach (Addimg ("/opt/daily_mail/image/155_eth0.png","155_eth0")) Me= Mail_user +"@"+Mail_postfix msg['Subject'] =Sub msg[' from'] =Me msg[' to'] =",". Join (to_list) msg['Cc'] =",". Join (cc_list) send_to= To_list +cc_listTry: Server=Smtplib. SMTP () server.connect (mail_host) server.login (Mail_user, Mail_pass) Server.sendmail (Me, send_to, Msg. As_string ()) Server.close ()returnTrueexceptException, E:PrintStr (e)returnFalseif __name__=='__main__': Title="Zabbix monitoring Data"+ Time.strftime ('%y%m%d', Time.localtime (Time.time ()))ifSend_mail (mailto_list, Mailcc_list, title):Print "sent successfully" Else: Print "Send failed"
It's that simple, the sample sent is not
Analog landing that block has reference to the online blog, too long do not remember the address, if the author found please contact me, I add a reference address.
Use Python on the server to automatically get the Zabbix shape and send mail