Last active
November 23, 2015 15:52
-
-
Save jniltinho/ff6a673e9f2f0a733d5a to your computer and use it in GitHub Desktop.
Send Mail plugin for Nagios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Script Send Mail Nagios (send_mail.py) | |
Copyrighted by Nilton OS <jniltinho at gmail.com> | |
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) | |
Version: 0.1 | |
Put this script in Nagios plugins folder | |
Default Nagios Ubuntu/Debian: /usr/lib/nagios/plugins | |
define command{ | |
command_name host-notify-by-email | |
command_line $USER1$/send_mail.py -N "HOST $NOTIFICATIONTYPE$" -H "$HOSTNAME$" -S "$HOSTSTATE$" -A "$HOSTADDRESS$" -I "$HOSTOUTPUT$" -D "$LONGDATETIME$" -E "$CONTACTEMAIL$" | |
} | |
define command{ | |
command_name service-notify-by-email | |
command_line $USER1$/send_mail.py -N "$NOTIFICATIONTYPE$" -H "$HOSTNAME$" -S "$SERVICESTATE$" -A "$HOSTADDRESS$" -I "$SERVICEDESC$ - $SERVICEOUTPUT$ - $SERVICECHECKCOMMAND$" -D "$LONGDATETIME$" -E "$CONTACTEMAIL$" | |
} | |
""" | |
import os, sys, optparse, socket | |
import commands, time | |
#from smtplib import SMTP_SSL as SMTP #secure SMTP protocol (port 465, uses SSL) | |
from smtplib import SMTP #standard SMTP protocol (port 25,587, no SSL) | |
from email.MIMEText import MIMEText | |
#--------------------------------------------------------------------------- | |
## Change these fields without | |
l_hostname = socket.gethostname() | |
log_file = '/tmp/send_mail_py.log' | |
srv_name = "Nagios MAIL ALERT" | |
SMTPserver = 'smtp.mydomain.com' | |
sender = 'nagios.alert@mydomain.com' | |
USERNAME = 'nagios.alert@mydomain.com' | |
PASSWORD = 'password_nagios_email' | |
#--------------------------------------------------------------------------- | |
verbose = False | |
message = '' | |
usage = "usage: host-notify-by-email\n %prog " | |
usage += '-N "HOST $NOTIFICATIONTYPE$" -H "$HOSTNAME$" -S "$HOSTSTATE$" -A "$HOSTADDRESS$" -I "$HOSTOUTPUT$" -D "$LONGDATETIME$" -E "$CONTACTEMAIL$"' | |
usage += "\n\n" | |
usage += "usage: service-notify-by-email\n %prog " | |
usage += '-N "$NOTIFICATIONTYPE$" -H "$HOSTNAME$" -S "$SERVICESTATE$" -A "$HOSTADDRESS$" -I "$SERVICEDESC$ - $SERVICEOUTPUT$ - $SERVICECHECKCOMMAND$" -D "$LONGDATETIME$" -E "$CONTACTEMAIL$"' | |
def log(mes): | |
global message | |
str = ("%s - %s\n") %(time.strftime('%b %d %H:%M:%S'), mes) | |
message += str | |
if verbose: str = str.rstrip("\n"); print str | |
def send_mail(receiver, Subject, mailbody): | |
global message | |
subject = Subject | |
receiver = receiver | |
text_subtype = 'plain' | |
try: | |
msg = MIMEText(mailbody, text_subtype) | |
msg['Subject']= subject | |
msg['From'] = ("%s <%s>" %(srv_name,sender)) | |
msg['To'] = receiver | |
receiver = receiver | |
conn = SMTP(SMTPserver,587,l_hostname) | |
conn.set_debuglevel(False) | |
conn.login(USERNAME, PASSWORD) | |
try: | |
conn.sendmail(sender, receiver, msg.as_string()) | |
finally: | |
conn.close() | |
except Exception, exc: | |
log( "mail failed; %s" % str(exc) ) # give a error message | |
def main(): | |
global verbose, message | |
parser = optparse.OptionParser(usage) | |
parser.add_option("-N", action="store", type="string", dest="N", help="NOTIFICATIONTYPE") | |
parser.add_option("-H", action="store", type="string", dest="H", help="HOSTNAME") | |
parser.add_option("-S", action="store", type="string", dest="S", help="SERVICESTATE") | |
parser.add_option("-A", action="store", type="string", dest="A", help="HOSTADDRESS") | |
parser.add_option("-I", action="store", type="string", dest="I", help="DESCRIPTION") | |
parser.add_option("-D", action="store", type="string", dest="D", help="LONGDATETIME") | |
parser.add_option("-E", action="store", type="string", dest="E", help="CONTACTEMAIL") | |
parser.add_option("--debug", action="store_true", dest="DEBUG", help="Enable Debug") | |
opt, args = parser.parse_args() | |
if (opt.DEBUG): verbose = True | |
mailbody = """\ | |
***** %s ***** | |
Notification Type: %s | |
Host: %s | |
State: %s | |
Address: %s | |
Date/Time: %s | |
Additional Info: %s | |
""" % (srv_name, opt.N, opt.H, opt.S, opt.A, opt.D, opt.I) | |
if (opt.E): | |
log("Sent Mail TO %s" % opt.E) | |
log(mailbody) | |
mailsubject = ("Nagios Alert:[%s] -- IS:[%s]") %(opt.H, opt.S) | |
send_mail(opt.E, mailsubject, mailbody) | |
salve_log = open(log_file,"a+"); salve_log.write(message); salve_log.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment