Zabbix call external script when sending a message, the command line will be passed two parameters, the first parameter is to send to which mailbox address, the second parameter is the message information, in order to ensure that multiple parameters can be passed, so assume that there are multiple parameters passed in
#!/usr/bin/env python#Encoding:utf8##Zabbix Server Send mail script# fromEmailImportencoders fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, FormataddrImportSmtplibImportsysfrom_addr='[email protected]'Password='xxxxxxxx'Smtp_server='smtp.163.com'#Alert message recipient, first parameter passed inTO_ADDR = sys.argv[1]def_format_addr (s): Name, addr=parseaddr (s)returnformataddr (Header (name,'Utf-8'). Encode (), Addr.encode ('Utf-8')ifIsinstance (addr, Unicode)Elseaddr))defzabbixinfo (): Res=""itemList=SYS.ARGV forIndexinchRange (2, Len (itemList)):#The first parameter is the recipient address, so the following parameters are stitched as the message contentres = res +Itemlist[index]returnResdefsendmsg (): HeaderInfo= u"zabbixserver Alarm Information"Frominfo= u"Zabbix Monitoring"Bodyinfo=zabbixinfo () msg= Mimetext (Bodyinfo,'Plain','Utf-8') msg[' from'] = _format_addr (Frominfo +"<%s>"%from_addr) msg[' to'] = _format_addr (Frominfo +"<%s>"%to_addr) msg['Subject'] = Header (HeaderInfo,'Utf-8'). Encode ()returnmsg.as_string ()defMain (): Server= Smtplib. SMTP (Smtp_server, 25) #server.set_debuglevel (1)server.login (from_addr, password) server.sendmail (from_addr, [to_addr], sendmsg ()) Server.quit ()if __name__=="__main__": Main ()
The password of the 163 mailbox in the script is not the login password, it is the special password of POP3 way
The following is the log picture, the use of Zabbix version older, ensure stable operation, fewer bugs
Zabbix calling external script to send mail: Python scripting