Zabbix calling external script to send mail: Python scripting

Source: Internet
Author: User
Tags sendmsg

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.