Nagios email alert script

Source: Internet
Author: User

I. python mail sending script
1. Many users send emails on the linux server directly using the mail command. Although the emails can be sent successfully, many emails will be processed by the Internet mailbox.

2. mutt and msmtp are related to the mail client in linux. They are similar to outlook and foxmail on windows.
3. Although mutt and msmtp can be installed, they can be replaced by python scripts, as shown below:
Ubuntu @ ubuntu:/etc/nagios3/scripts $ cat sendmail
#! /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, yangzi@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" (this is the smtp service domain name of 126)
# Smtp port
PORT = "25" (126 mail smtp PORT number)
# FROM mail user
USER = 'czar _ test' (you registered the USER Name of the 126 mailbox)
# FROM mail password
PASSWD = 'test123' (the password for your registered 126 mailbox)
# FROM EMAIL
FROM = "Czar_test@126.com" (your 126 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"


Then test the email sending
Ubuntu @ ubuntu:/etc/nagios3/scripts $/etc/nagios3/scripts/sendmail-t "yongkang_tian@126.com"-s "Nagios Test"-m "Hello Wrold"


2. Add code in the nagios Command File
Ubuntu @ ubuntu:/etc/nagios3 $ vim commands. cfg
Define command {
Command_name sort y-host-by-email
Command_line/etc/nagios3/scripts/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Host Alert: $ HOSTNAME $ is $ HOSTSTATE $ ** "-m" % B "-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/etc/nagios3/scripts/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Service Alert: $ HOSTALIAS $/$ SERVICEDESC $ is $ SERVICESTATE $ ** "-m" % B "-m" ****** Nagios ***** \ n \ nNotification Type: $ icationicationtype $ \ n \ nService: $ SERVICEDESC $ \ nHost: $ HOSTALIAS $ \ nAddress: $ HOSTADDRESS $ \ nState: $ SERVICESTATE $ \ n \ nDate/Time: $ LONGDATETIME $ \ n \ nAdditional Info: \ n $ SERVICEOUTPUT $ \ n"
}

Nagios details: click here
Nagios: click here

Network Monitor Nagios Overview

Nagios construction and Configuration

Build a Nagios monitoring platform in the Nginx Environment

Configure the basic Nagios System on RHEL5.3 (using Nagios-3.1.2)

CentOS 5.5 + Nginx + Nagios monitoring and control terminal installation and Configuration Guide

Install Nagios Core for Ubuntu 13.10 Server

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.