一、添加短信告警

一、调试python脚本,使之能正常运行。
1.上传python脚本到服务器
2.运行脚本,如果提示suds模块不存在则需要使用easyinstall 安装suds
/usr/local/bin/SendSMS.py 13328100000 'TestMessage1' ' '
easy_install suds
3.测试脚本
/usr/local/bin/SendSMS.py 13328100000 'TestMessage2' ' '
二、修改contact配置文件,增加电话号码属性。
cd /etc/nagios/objects/
vim contacts.cfg ,修改如下:

define contact{
  contact_name guicj ; Short name of user
  use generic-contact ; Inherit default values from generic-contact template (defined above)
  alias Nagios Admin ; Full name of user

  email [email protected] ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
  pager 18251100000 ;cellphone number
}

三、修改command配置文件,增加自定义命令。
vim commands.cfg ,增加如下内容
# 'notify-host-by-sms' command definition
define command{
  command_name notify-host-by-sms
  command_line /usr/local/bin/SendSMS.py $CONTACTPAGER$ "*Nagios*:Host "$HOSTALIAS$" is $HOSTSTATE$! Date/Time: $DATETIME$" ""
}

# 'notify-service-by-sms' command definition
define command{
  command_name notify-service-by-sms
  command_line usr/local/bin/SendSMS.py $CONTACTPAGER$ "*Nagios*Notification Type: $NOTIFICATIONTYPE $Service: $SERVICEDESC $Host: $HOSTALIAS$ Address: $HOSTADDRESS$ State: $SERVICESTATE$ Date/Time: $LONGDATETIME$ Additional Info: $SERVICEOUTPUT$" ""
}

四、修改用户模板,使之默认调用新增命令。
vim templates.cfg
define contact{
  name generic-contact ; The name of this contact template
  service_notification_period 24x7 ; service notifications can be sent anytime
  host_notification_period 24x7 ; host notifications can be sent anytime
  service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
  host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
  service_notification_commands notify-service-by-email,notify-service-by-sms ; send service notifications via email
  host_notification_commands notify-host-by-email,notify-host-by-sms ; send host notifications via email
  register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
五、重启nagios,进行测试。

二、添加email告警

1. 使用sendEmail来发送邮件

sendEmail工具非常好用。具体内容参见:

《如何使用sendEmail发送邮件》

2. 创建一个联系人

1

2

3

4

5

6

7

8

9

10

11

12

# vim /usr/local/nagios/etc/objects/contacts.cfg

define contact{

        contact_name                    mobei

        alias                           mobei

        service_notification_period     24x7

        host_notification_period        24x7

        service_notification_options    w,u,c

        host_notification_options       d,u

        service_notification_commands   notify-service-by-email

        host_notification_commands      notify-host-by-email

        email                           You email address

        }

3. 创建一个组

1

2

3

4

5

define contactgroup{

        contactgroup_name       ops

        alias                   ops

        members                 mobei

        }

4. 修改发送邮件命令

1

2

3

4

5

6

7

8

9

10

11

12

# vim /usr/local/nagios/etc/objects/commands.cfg

# 'notify-host-by-email' command definition

define command{

        command_name    notify-host-by-email

        command_line  /usr/local/bin/sendEmail -l /tmp/sendEmail.log -f jiank@126.com -t $CONTACTEMAIL$ -s smtp.126.com -u "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" -xu jiank@126.com -xp jiankong -m "Service: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$  State: $SERVICESTATE$\\nDate/Time: $SHORTDATETIME$\\nInfo:$SERVICEOUTPUT$"

        }

 

# 'notify-service-by-email' command definition

define command{

        command_name    notify-service-by-email

        command_line  /usr/local/bin/sendEmail -l /tmp/sendEmail.log -f jiank@126.com -t $CONTACTEMAIL$ -s smtp.126.com -u "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" -xu jiank@126.com -xp jiankong -m "Service: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$  State: $SERVICESTATE$\\nDate/Time: $SHORTDATETIME$\\nInfo:$SERVICEOUTPUT$"

        }

5. 指定监控项目。如:

1

2

3

4

5

6

7

define service {

    use                 generic-service

    hostgroup_name          Mongo Servers

    service_description     Mongo Mapped Memory Usage

    check_command           check_mongodb!10.0.0.160!12345!'ttlsa'!'www.ttlsa.com'!memory_mapped!20!28

    contact_groups          ops

}





原文链接:https://www.cnblogs.com/helloLinux/archive/2012/07/16/2593678.html

                 http://www.ttlsa.com/nagios/nagios-mail-notify/