pytyhon--sendEmail/nagios
!/usr/bin/python
-- coding:utf-8 --
import smtplib
import sys
from email.mime.text import MIMEText
import time
current_time=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time()))
addresser to show
mail_nagios = 'Nagios Server'
host define
mail_host = 'mail.myhexin.com'
user
mail_user = 'iquant@myhexin.com'
user's password
mail_pwd = '10jqka.com'
def send_email( content, mailto, get_sub ):
msg = MIMEText( content,_subtype='plain',_charset='gb2312')
msg['From'] = mail_nagios
msg['Subject'] =get_sub
msg['To'] = ",".join( mailto )
try:
s = smtplib.SMTP_SSL( mail_host, 465 )
s.login(mail_user, mail_pwd )
s.sendmail(mail_user, mailto , msg.as_string())
s.close()
except Exception as e:
print 'Exception: ', e
topic get
title=sys.argv[1]
cont="""
| Nagios Mail
| Message: %s
| SendTime: %s
""" %(title,current_time)
to_list = [
sys.argv[2],
]
send_email( cont, to_list, title)
commands.cfg
'notify-service-by-python' command definition
define command{
command_name notify-service-by-python
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Ty¡¤pe: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState$SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/python /usr/local/nagios/libexec/sendMailPy/MailSend.py "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ -- Additional Info : $SERVICEOUTPUT$ ** " $CONTACTEMAIL$
}
'notify-host-by-python' command definition
define command{
command_name notify-host-by-python
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/python /usr/local/nagios/libexec/sendMailPy/MailSend.py "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}