The python script is used to send emails to nagios
Previously, the company's nagios email sending script was written using reverse CT, but there has always been a drawback that the nagios email text cannot be wrapped and can only be displayed on one line. It seems very difficult to send an alert each time. I always wanted to change it. This line feed problem was solved by using the python script this time.
To put it bluntly, use the script:
#! /Usr/bin/python
Import smtplib
Import string
Import sys
Import getopt
Def usage ():
Print "" sendmail is a send mail Plugins
Usage:
Sendmail [-h | -- help] [-t | -- to] [-s | -- subject] [-m | -- message]
Options:
-- Help |-h)
Print sendmail help.
-- To |-t)
Sets sendmail to email.
-- Subject |-s)
Sets the mail subject.
-- Message |-m)
Sets the mail body
Example:
Only one to email user
./Sendmail-t 'Eric @ nginxs.com '-s 'Hello eric'-m' hello eric, this is sendmail test!
Failed to email user
./Sendmail-t 'Eric @ nginxs.com, zhangsan@nginxs.com '-S' hello eric'-m' hello eric, this is sendmail test! """
Sys. exit (3)
Try:
Options, args = getopt. getopt (sys. argv [1:], "ht: s: m:", ["help", "to =", "subject =", "message ="])
Counter t getopt. GetoptError:
Usage ()
For name, value in options:
If name in ("-h", "-- help "):
Usage ()
If name in ("-t", "-- "):
# Accept message user
TO = value
TO = TO. split (",")
If name in ("-s", "-- title "):
SUBJECT = value
If name in ("-m", "-- message "):
MESSAGE = value
MESSAGE = MESSAGE. split ('\ n ')
MESSAGE = '\ n'. join (MESSAGE)
# Smtp HOST
HOST = "smtp.126.com" # change it to your post office smtp host address
# Smtp port
PORT = "25" # change to the smtp port of your post office
# FROM mail user
USER = 'Eric '# change to your email USER name
# FROM mail password
PASSWD = '000000' # change your email password
# FROM EMAIL
FROM = "test@163.com" # change to your email address
Try:
BODY = string. join ((
"From: % s" % FROM,
"To: % s" %,
"Subject: % s" % SUBJECT,
"",
MESSAGE), "\ r \ n ")
Smtp = smtplib. SMTP ()
Smtp. connect (HOST, PORT)
Smtp. login (USER, PASSWD)
Smtp. sendmail (FROM, TO, BODY)
Smtp. quit ()
Except t:
Print "unknown error"
Print "please look help"
Print "./sendmail-h"
Usage:
Send only to one user:
Nagios $>./sendmail-t 'test @ 163.com '-s 'Hello'-m' hello, this is sendmail test!
Send to multiple users:
./Sendmail-t 'test@163.com, test02@163.com '-S' hello'-m' hello, this is sendmail test!
Applied to nagios for alarm:
[Root @ master ~] # Vim/etc/nagios/objects/commands. cfg
Define command {
Command_name sort y-host-by-email
Command_line $ USER1 $/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Host Alert: $ HOSTNAME $ is $ HOSTSTATE $ ** "-m" ****** Nagios ***** \ n \ nNotification Type: $ icationicationtype $ \ nHost: $ HOSTNAME $ \ nState: $ HOSTSTATE $ \ nAddress: $ HOSTADDRESS $ \ nInfo: $ HOSTOUTPUT $ \ n \ nDate/Time: $ LONGDATETIME $ \ n"
}
Define command {
Command_name sort y-service-by-email
Command_line $ USER1 $/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Service Alert: $ HOSTALIAS $/$ SERVICEDESC $ is $ SERVICESTATE $ ** "-m" ****** Nagios ***** \ n \ nNotification Type: $ icationicationtype $ \ n \ nService: $ SERVICEDESC $ \ nHost: $ HOSTALIAS $ \ nAddress: $ HOSTADDRESS $ \ nState: $ SERVICESTATE $ \ n \ nDate/Time: $ LONGDATETIME $ \ n \ nAdditional Info: \ n $ SERVICEOUTPUT $"
}
[Root @ master ~] #/Etc/init. d/nagios reload
In this way, you can.
This article from the "dream into reality" blog, please be sure to keep this source http://zhhmj.blog.51cto.com/1666742/990830