Nagios告警和监控主机安装介绍(三)

Nagios邮件告警

配置sendEmail

解压缩tar –zxvf sendEmail-v1.56.tar.gz

cd sendEmail-v1.56

将可执行程序复制cp sendEmail /usr/local/bin

然后给确认确实它具有执行权限

 

sendEmail使用案例:

/usr/local/bin/sendEmail -f asia_radius@163.com -t wangjian10@asiainfo.com -s smtp.163.com -u "test" -xu asia_radius -xp asia@radius -m test

解释:

-f 表示发送者的邮箱

-t 表示接收者的邮箱

-s 表示SMTP服务器的域名或者ip

-u 表示邮件的主题

-xu 表示SMTP验证的用户名

-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)

-m 表示邮件的内容

如果你不带-m参数的话,就会提示你自行输入

输入完成后使用CTRL-D来结束

当然我们也可以将一个文件的内容作为邮件的正文发出去的

那么就可以使用:

cat 文件名 | /usr/local/bin/sendEmail -f asia_radius@163.com -t wangjian10@asiainfo.com -s smtp.163.com -u "test" -xu asia_radius -xp asia@radius

 

nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中关于发邮件的命令的定义,我们现在来修改notify-host-by-email和notify-service-by-email命令,修改后结果:

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

define command{

        command_name    notify-host-by-email

        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HO

STADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f asia_radius@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NO

TIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu asia_radius -xp asia@radius

        }

 

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

define command{

        command_name    notify-service-by-email

        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddre

ss: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f asia_radius@163

.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service  Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu asia_radius -xp asia@rad

ius

        }

 

Nagios邮件配置项说明:

notification_timeout 默认发邮件的时间

notification_interval 通告间隔

check_interval  检测间隔

retry_interval   重试间隔

 

Nagios短信告警

短信告警前提条件:

1、存在短信下发平台

2、开发短信接口脚本

Python开发的短信接口脚本

import pycurl
import StringIO
import urllib
import certifi
import logging  
import logging.config
LOG_FILENAME = '/usr/local/nagios-sms/logger.conf'
logging.config.fileConfig(LOG_FILENAME)  
logger = logging.getLogger("test")

script, iphone, date = argv
logger.info('nagios input iphone: %s, date: %s' % (iphone, date))
# 初始化Pycurl类 def initCurl(): c = pycurl.Curl() c.setopt(pycurl.CONNECTTIMEOUT, 10) c.setopt(pycurl.TIMEOUT, 15) c.setopt(pycurl.FOLLOWLOCATION, 0) c.setopt(pycurl.MAXREDIRS, 5) c.setopt(pycurl.SSL_VERIFYPEER, 0) c.setopt(pycurl.SSL_VERIFYHOST, 0) c.setopt(pycurl.CAINFO, certifi.where()) return c # 封装了通用Http Get方法 def GetData(curl, url): head = ['Accept:*/*', 'User-Agent:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)'] buf = StringIO.StringIO() curl.setopt(pycurl.WRITEFUNCTION, buf.write) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.HTTPHEADER, head) curl.perform() the_page = buf.getvalue() buf.close() return the_page def sendSMS(phone, content): c = initCurl() paras = urllib.urlencode({'sysName': '', 'phoneNumbers': phone, 'content': content}) try: result = GetData(c, 'https://IP:PORT/Send?'+paras) except Exception as err: logger.error(err)

if __name__ == '__main__':

sendSMS(iphone,date)

 

用法:./sms.py 手机号 “告警内容”

 

Nagios配置部分

修改commands.cfg配置文件,用于调用短信接口脚本

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

define command{

        command_name    host-notify-by-sms

        command_line    /usr/local/nagios-sms/sms.py $CONTACTPAGER$ "*** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ IP: $HOSTADDRESS$ is $HOSTSTATE$ ***"

        }

 

 

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

define command{

        command_name    service-notify-by-sms

        command_line    /usr/local/nagios-sms/sms.py $CONTACTPAGER$ "*** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$ IP: $HOSTADDRESS$ / $SERVICEDESC$ is $S

ERVICESTATE$ ***"

        }

 

修改contacts.cfg配置文件,加入告警人的手机号

 

define contact{

        contact_name                    test

        use                             generic-contact

        alias                           Nagios Admin

        email                           test@163.com

        pager                           13100000000

        }

 

修改templates.cfg 配置文件,加入commands.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,service-notify-by-sms   ; send service notifications via email

        host_notification_commands      notify-host-by-email,host-notify-by-sms ; send host notifications via email

        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

        }

 

Nagios 远程linux主机监控(通过NRPE)

NRPE 总共由两部分组成:

check_nrpe 插件,位于监控主机上

NRPE daemon,运行在远程的Linux主机上(通常就是被监控机)

 

当Nagios 需要监控某个远程Linux 主机的服务或者资源情况时:

  • Nagios 会运行check_nrpe 这个插件,告诉它要检查什么;
  • check_nrpe 插件会连接到远程的NRPE daemon,所用的方式是SSL;
  • NRPE daemon 会运行相应的Nagios 插件来执行检查;
  • NRPE daemon 将检查的结果返回给check_nrpe 插件,插件将其递交给nagios做处理。

注意:NRPE daemon 需要Nagios 插件安装在远程的Linux主机上,否则,daemon不能做任何的监控。

1、必须创建nagios用户和用户组

创建用户:useradd nagios

设置用户密码:passwd nagios

创建用户组:groupadd nagios

给已有的用户增加用户组:usermod -G nagios nagios

 

2、安装nagios插件

切换root帐号

tar zxvf nagios-plugins-2.0.3.tar.gz

cd nagios-plugins-2.0.3

./configure --prefix=/home/nagios

make

make install

这一步完成后会在/home/nagios/下生成三个目录include、libexec和share。

修改目录权限

chown nagios.nagios /home/nagios

chown -R nagios.nagios /home/nagios/libexec

 

3、安装NRPE

tar -zxvf nrpe-2.15.tar.gz

cd nrpe-2.15

./configure --prefix=/home/nagios

make all

接下来安装NPRE插件,daemon和示例配置文件

安装check_nrpe 这个插件

make install-plugin

监控机需要安装check_nrpe 这个插件,被监控机并不需要,我们在这里安装它只是为了测试目的。

安装deamon

make install-daemon

安装配置文件

make install-daemon-config

现在再查看nagios 目录就会发现有5个目录了

 

安装xinted 脚本

make install-xinetd

编辑内容/etc/xinetd.d/nrpe,在only_from 后增加监控主机的IP地址

编辑/etc/services 文件,增加NRPE服务

重启xinted 服务

service xinetd restart

查看NRPE 是否已经启动

netstat -an|grep 5666

测试NRPE是否则正常工作,使用上面在被监控机上安装的check_nrpe 这个插件测试NRPE 是否工作正常

/home/nagios/libexec/check_nrpe -H 127.0.0.1

查看check_nrpe 命令用法

/home/nagios/libexec/check_nrpe -h

可以看到用法是:

check_nrpe –H 被监控的主机 -c 要执行的监控命令

注意:-c 后面接的监控命令必须是nrpe.cfg 文件中定义的。也就是NRPE daemon只运行nrpe.cfg中所定义的命令。

 

查看NRPE的监控命令

cd /home/nagios/etc

cat nrpe.cfg |grep -v "^#"|grep -v "^$"

 

log_facility=daemon

pid_file=/var/run/nrpe.pid

server_port=5666

nrpe_user=nagios

nrpe_group=nagios

allowed_hosts=127.0.0.1

 

dont_blame_nrpe=0

allow_bash_command_substitution=0

debug=0

command_timeout=60

connection_timeout=300

command[check_users]=/home/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/home/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_hda1]=/home/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1

command[check_zombie_procs]=/home/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/home/nagios/libexec/check_procs -w 150 -c 200

红色部分是命令名,也就是check_nrpe 的-c 参数可以接的内容,等号 “=” 后面是实际执行的插件程序(这与commands.cfg 中定义命令的形式十分相似,只不过是写在了一行)。也就是说check_users 就是等号后面/home/nagios/libexec/check_users -w 5 -c 10 的简称。

各条命令具体的含义见插件用法(执行“插件程序名 –h”)。

 

监控主机(Nagios-Server)上安装nrpe

tar -zxvf nrpe-2.15.tar.gz

cd nrpe-2.15

./configure --prefix=/home/nagios
make all
make install-plugin
只运行这一步就行了,因为只需要check_nrpe插件。
在Nagios-Linux 上我们已经装好了nrpe,现在我们测试一下监控机使用check_nrpe 与被监控机运行的nrpe daemon之间的通信。
/home/nagios/libexec/check_nrpe -H IP地址

看到已经正确返回了NRPE的版本信息,说明一切正常。

 

在commands.cfg中增加对check_nrpe的定义

# 'check_nrpe' command definition define command{         command_name    check_nrpe         # 定义命令名称为check_nrpe,在services.cfg中要使用这个名称.         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$       
这是定义实际运行的插件程序,这个命令行的书写要完全按照check_nrpe这个命令的用法,不知道用法的就用check_nrpe –h查看,-c 后面带的$ARG1$ 参数是传给nrpe daemon 执行的检测命令,之前说过了它必须是nrpe.cfg 中所定义的         }

services.cfg 中定义对Nagios-Linux 主机的监控

define service{ 

        use                     local-service

        host_name             SV55-HSS22

        service_description     Partition_ROOT

        check_command        check_nrpe!check_disk

        }

 

利用NSClient++监控远程Windows上的“本地信息”

在Nagios的libexec下有check_nt这个插件,它就是用来检查windows机器的服务的。其功能类似于check_nrpe。不过还需要搭配另外一个软件NSClient++,它则类似于NRPE

 

可以看到NSClient与nrpe最大的区别就是:

  • 被监控机上安装有nrpe,并且还有插件,最终的监控是由这些插件来进行的。当监控主机将监控请求发给nrpe后,nrpe调用插件来完成监控。
  • NSClient++则不同,被监控机上只安装NSClient,没有任何的插件。当监控主机将监控请求发给NSClient++后,NSClient直接完成监控,所有的监控是由NSClient完成的。

 

这也说明了NSClient++的一个很大的问题:不灵活、没有可扩展性。它只能完成自己本身包含的监控操作,不能由一些插件来扩展。好在NSClient++已经做的不错了,基本上可以完全满足我们的监控需求。

 

安装NSClient++

http://www.nsclient.org/nscp/downloads 下载zip包

 

Nagios 远程Solaris 10主机监控(通过NRPE)

前置条件:

加载ISO光盘的步骤:

lofiadm -a /home/aiobs6/wangjian/sol-10-u11-ga-sparc-dvd.iso /dev/lofi/1

注:用 lofiadm 创建一个附属的块设备给sol-10-u11-ga-sparc-dvd.iso

mount -F hsfs -o ro /dev/lofi/1 /mnt

卸载ISO镜像文件:

umount /mnt

注:将文件系统先卸载掉

lofiadm -d /dev/lofi/1

注:删除lofiadm创建的设备

查看lofiadm设备信息:

Lofiadm

 

Gcc安装

准备gcc-3.4.6-sol10-sparc-local.gz安装包

解压gunzip gcc-3.4.6-sol10-sparc-local.gz

安装pkgadd -d gcc-3.4.6-sol10-sparc-local

 

ln -s /home/aiobs6/bes/lib/apache2/libssl.so.0.9.7 /usr/lib/libssl.so.0.9.7

ln -s /home/aiobs6/bes/lib/apache2/libcrypto.so.0.9.7 /usr/lib/libcrypto.so.0.9.7

 

 

1、必须创建nagios用户和用户组

创建用户组:groupadd nagios

创建用户:useradd -c "nagios system user" -d /home/nagios -m nagios

设置用户密码:passwd nagios

chown -R nagios:nagios /home/nagios

 

2、安装nagios插件

nagios环境变量配置/etc/profile文件,增加

LD_LIBRARY_PATH=/oracle/product/10.2.0/lib:/opt/oracle/product/10.2.0/lib32:/usr/local/ssl/lib:/usr/lib

export LD_LIBRARY_PATH

 

gzip -d  nagios-plugins-2.0.3.tar.gz

tar -xvf nagios-plugins-2.0.3.tar

cd nagios-plugins-2.0.3

./configure --prefix=/home/nagios --with-snmpgetnext-command=/usr/sfw/bin --with-openssl=/usr/local/ssl

编译中10.11.244.84测试环境一些库文件是64位,编译错误,需使用32位

使用32位文件

unlink /usr/local/lib/libstdc++.so.6

unlink /usr/local/lib/libgcc_s.so.1

ln -s /usr/local/lib/libgcc_s.so /usr/local/lib/libgcc_s.so.1

ln -s /usr/local/lib/libstdc++.so.6.0.3 /usr/local/lib/libstdc++.so.6

还原环境

unlink /usr/local/lib/libstdc++.so.6

unlink /usr/local/lib/libgcc_s.so.1

ln -s /usr/local/lib/libgcc_s.so.1.20110106_lm /usr/local/lib/libgcc_s.so.1

ln -s /usr/local/lib/libstdc++.so.6.20110106_lm /usr/local/lib/libstdc++.so.6

 

make

make install

这一步完成后会在/home/nagios/下生成三个目录include、libexec和share。

 

3、安装NRPE

gzip -d nrpe-2.15.tar.gz

tar -xvf nrpe-2.15.tar

cd nrpe-2.15

./configure --prefix=/home/nagios

gmake clean

gmake all

gmake install

gmake install-daemon-config

/etc/services下添加

在/etc/inet/inetd.conf添加以下内容

nrpe stream tcp nowait nagios /usr/sfw/sbin/tcpd /home/nagios/bin/nrpe -c /home/nagios/etc/nrpe.cfg -i

使用inetconv将nrpe导入到SMF中

检查导入结果

inetconv -e

检查nrpe是否为online

svcs svc:/network/nrpe/tcp:default

检查nrpe端口状态

netstat -a | grep nrpe

检查安装缺省参数

inetadm -l svc:/network/nrpe/tcp:default

如果tcp_wrappers=FALSE,则更改为tcp_wrappers=TRUE

inetadm -m svc:/network/nrpe/tcp:default tcp_wrappers=TRUE

 

修改/home/nagios/etc/nrpe.cfg文件,加入naigos服务端主机地址

allowed_hosts=127.0.0.1

改成

allowed_hosts=127.0.0.1,10.11.209.173

 

检查安装是否正确(返回版本值表示正常)

/home/nagios/libexec/check_nrpe -H 10.11.244.84

 

 

svcs -a查看所有服务的状态

svcs -a |grep nrpe查看nrpe服务的状态

svcadm restart svc:/network/nrpe/tcp:default 重启nrpe服务

 



 

posted @ 2016-05-27 12:00  shhnwangjian  阅读(845)  评论(0编辑  收藏  举报