Toss nagios send mail for several days, finally finished, my system environment is ubuntu12.04, install Postfix service, first paste the script, as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
"From: Change to your email address"
Import Datetime,smtplib,string,sys
From email. Header Import Header
From email. Mimetext Import Mimetext
From email. Mimemultipart Import Mimemultipart
def send_mail (TO_ADDR):
If sys.argv[1] = = "Host":
Text=string.join ((
"******** host Problem ********\n\n",
"Notification type:%s\n\n"%sys.argv[2],
"Host Name:%s\n\n"%sys.argv[3],
"IP address:%s\n\n"%sys.argv[4],
"Machine Status:%s\n\n"%sys.argv[5],
"Date:%s\n\n"%sys.argv[6],
"Additional information:%s\n\n"%sys.argv[7]), "\ r \ n")
Else
Text=string.join ((
"******** process/Service problem ********\n\n",
"Notification type:%s\n\n"%sys.argv[1],
"Service Name:%s\n\n"%sys.argv[2],
"Host Name:%s\n\n"%sys.argv[3],
"IP address:%s\n\n"%sys.argv[4],
"Machine Status:%s\n\n"%sys.argv[5],
"Date:%s\n\n"%sys.argv[6],
"Additional information:%s\n\n"%sys.argv[7]), "\ r \ n")
msg = Mimemultipart ()
Msg[' to '] = to_addr
Msg[' from '] = ' [email protected] '
msg[' Subject ' = Header (' Detect SSH Port results (' + str (datetime.date.today ()) + ') ', ' utf-8 ')
Text_msg = Mimetext (text, ' Plain ', ' utf-8 ')
Msg.attach (TEXT_MSG)
Server = Smtplib. SMTP (' smtp.sina.com ')
Server.login (' [email protected] ', ' xxxxxx ')
Server.sendmail (msg[' from '],msg[' to '],msg.as_string ())
Server.close ()
if __name__ = = ' __main__ ':
addr_list=[' [email protected] ', ' [email protected] ', ' [email protected] '
For I in range (len (addr_list)):
Send_mail (Addr_list[i])
Note:
1. Use the Mimemultipart method to create a MIME object that can contain attachments;
2, using the Mimetext method to create the content of the message object;
3, use Smtplib. SMTP method to implement the mail system login and send mail;
4. The contents of the message are obtained from the macro variable parameter one by one of Nagios.
This article is from the "Snail's Home" blog, be sure to keep this source http://winters.blog.51cto.com/5617866/1590188
python-e-Mail script