nagios的详细配置和报警

发布时间:2020-08-02 05:17:51 作者:zouqingyun
来源:网络 阅读:8326

 注:本文有感谢程建提供

一、需求

   nagios 更新速度是很快的,本公司采用最新的稳定版本nagios-4.0.8,插件采用是的nagios-plugins-2.0.3,新版本经过使用和测试,发现速度和报警的速度比以前快了许多,

并且占用操作系统资源不多问题及时,界面干净,如下图:

nagios的详细配置和报警

  由于网上下载包不稳定,所以我整理后放到网盘中,所有的下载包全在360网盘地址中如下:

http://yunpan.cn/cHjtN2f9Kz3FM  访问密码 f9af


二、PHP环境的搭建

   本公司采用lamp的环境来做为nagios的基础环境,当然mysql是可以不用装的只要PHP环境就行了。

1、apr的安装

  APR(Apache portable Run-time librariesApache可移植运行库)的目的如其名称一样,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。
在早期的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数。随着Apache的进一步开发,Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。这样,APR的开发就从Apache中独立出来,Apache仅仅是使用APR而已。

    一般情况下,APR开发包很容易理解为仅仅是一个开发包,不过事实上并不是。目前,完整的APR实际上包含了三个开发包:aprapr-util以及apr-iconv,每一个开发包分别独立开发,并拥有自己的版本。

 现在新版本的httpapr集成到了一个httpd-2.4.9-deps.tar 类似于这样的包中,解压后的结果如下:

nagios的详细配置和报警

 集成后安装和管理方便多了,不用再担心apache版本和apr的对应该关系了。

安装如下:

./configure--prefix=/mnt/cellar/httpd/apr     //这个目录一定要规划好,方便管理
make
make install

2、.apr-utils安装

  ./configure --prefix=/mnt/cellar/httpd/apr-util --with-apr=/mnt/cellar/httpd/apr
   // 这里要指定apr的安装目录才行
 make
  makeinstall

 aprapi-utils 安装好后,目录如下:


nagios的详细配置和报警


3、PCRE的安装

./configure  --prefix=/mnt/cellar/httpd/pcre
make
make install

如果报以下错误:

configure: error: You need a C++ compiler for C++ support

则需要安装yum install -y gcc gcc-c++


4、apache的安装

apache的版本是2.4.9

./configure--prefix=/mnt/cellar/httpd/apache --with-apr=/mnt/cellar/httpd/apr--with-apr-util=/mnt/cellar/httpd/apr-util/bin/apu-1-config--with-pcre=/mnt/cellar/httpd/pcre/ --enable-so --enable-rewrite
// 要指定前面安装的几个目录 (apr,apr-utils,pcre)

make
make install

apache 的启动如下

[root@kaifabin]# ./apachectl start

nagios的详细配置和报警

说明apache 运行正常

由于是源码包安装apache 所以启动和关闭apache比较麻烦,所以写了一个apache的启动脚本如下:

#!/bin/bash
#Startup script for the Apache2.0.X Web Server
# chkconfig:- 85 15
# Sourcefunction library.
./etc/rc.d/init.d/functions
if [ -f/etc/sysconfig/httpd ]; then
./etc/sysconfig/httpd
fi
INITLOG_ARGS=""
apachectl=/mnt/cellar/httpd/apache/bin/apachectl
httpd=${HTTPD-/mnt/cellar/httpd/apache/bin/httpd}
prog=httpd
RETVAL=0
start(){
echo -n$"Starting $prog: "
daemon$httpd $OPTIONS
RETVAL=$?
echo
[$RETVAL = 0 ] && touch /var/lock/subsys/httpd
return$RETVAL
}
stop() {
echo -n$"Stopping $prog: "
killproc$httpd
RETVAL=$?
echo
[$RETVAL = 0 ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid
}
reload(){
echo -n$"Reloading $prog: "
killproc$httpd -HUP
RETVAL=$?
echo
}
case"$1" in
start)
start
 ;;
stop)
stop
 ;;
status)
status$httpd
RETVAL=$?
 ;;
restart)
stop
start
 ;;
condrestart)
if [ -f/var/run/httpd.pid ] ; then
stop
start
fi
 ;;
reload)
reload
 ;;
graceful|help|configtest|fullstatus)
$apachectl$@
RETVAL=$?
 ;;
*)
echo$"Usage: $prog
{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit$RETVAL

以上脚本如果需要,只需要修改红色的部份即可(apache的安装目录)


5、PHP安装

 5.1 PHP版本是php-5.6.4

./configure  --prefix=/mnt/cellar/httpd/php--with-libdir=/usr/lib64 --with-config-file-path=/mnt/cellar/httpd/php/etc --with-config-file-scan-dir=/mnt/cellar/httpd/php/etc/php.d --with-apxs2=/mnt/cellar/httpd/apache/bin/apxs
make
make install

PHP安装的时候make的时候会报错 undefinedreference to `libiconv'

解决方法如下:

Makefile 大概 77 行左右的地方:
EXTRA_LIBS =
.. -lcrypt
在最後加上 -liconv,例如:
EXTRA_LIBS =
.. -lcrypt -liconv

验证有效!

 5.2 PHP 环境配置如下:

   把php.ini 拷贝一份到下面的目录

nagios的详细配置和报警

然后在apachehttpd.conf中添加

AddType application/x-httpd-php .php

然后重启apache 再访问如下:

nagios的详细配置和报警


至此基础环境配置完成

如果不知道configure时候的参数可以用以下命令来查看:

查看nginx编译参数:/usr/local/nginx/sbin/nginx-V
查看apache编译参数:cat/usr/local/apache2/build/config.nice
查看mysql编译参数:cat /usr/local/mysql/bin/mysqlbug |grep CONFIGURE_LINE
查看php编译参数:/usr/local/php/bin/php -i | grepconfigure


三、nagios 的安装

1、创建用户

 useradd  -M  -s/sbin/nologin nagios   // 不允许登陆,不要家目录

2、nagios 主程序的安装

 cd nagios-4.0.8

./configure   --prefix=/mnt/cellar/nagios   --with-command-group=nagios  --with-nagios-group=nagios
makeall 
makeinstall
makeinstall-init  // 生成init启动脚本
makeinstall-config  // 生成一些模块配置文件
makeinstall-commandmode  // 设置相应的权限
makeinstall-webconf

  安装完成

cp -R contrib/eventhandlers/   /mnt/cellar/nagios/libexec/
chown-R nagios.nagios eventhandlers/  //拷贝事件处理到nagios的安装目录下的libexec目录下  这一步很重要,牵扯到以后会不会邮件和短信报警的状态转变和发送。


3、启动nagios

./nagios -v/mnt/cellar/nagios/etc/nagios.cfg  如下图,先验证配置文件的正确性

nagios的详细配置和报警


下面启动,如下图,启动正确

nagios的详细配置和报警


4、apache相关的配置

./htpasswd  -c /mnt/cellar/nagios/etc/htpasswd    letang // 添加web登陆用户letang

apachehttpd.conf的最后面添加如下:

#setting for nagios
ScriptAlias /nagios/cgi-bin"/mnt/cellar/nagios/sbin"
<Directory"/mnt/cellar/nagios/sbin">
    AuthType Basic
    Options ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthUserFile /mnt/cellar/nagios/etc/htpasswd
    Require valid-user
</Directory>
Alias /nagios"/mnt/cellar/nagios/share"
<Directory "/mnt/cellar/nagios/share">
    AuthType Basic
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "nagios Access"
    AuthUserFile /mnt/cellar/nagios/etc/htpasswd
    Require valid-user
</Directory>

然后找到 

      user apache

      group apache

      修改为

   user nagios

   group nagios

最后重启apache 登陆验证如下:

nagios的详细配置和报警

5、错误排除

 输入用户名和密码之后点击services出现下面的乱码情况如下:

nagios的详细配置和报警

出现上面的原因是apache没有开启cgi脚本的缘故
进入apache的主配置文件httpd.conf

#LoadModule cgid_module modules/mod_cgid.so
#LoadModule actions_module modules/mod_actions.so

将上面2行的#去掉,重启apacheOK了,然后关掉浏览器重新登陆如下:

nagios的详细配置和报警

好像还是什么也没有?并且有错误提示

 

这是认证的问题,修改如下

 

use_authentication=1改为 use_authentication=0 然后重启nagios即可

 

nagios的详细配置和报警

然后看到如下说明有监控项目了但有红色提示:

 

nagios的详细配置和报警

nagios的详细配置和报警


再看上面的错,很明显,提示找到命令,这是因为没有安装插件的原因。

 


6、nagios 插件的安装

 

 nagios 之所以强大,就是因为功能强大的插件而已

 

tar
-zxvf nagios-plugins-2.0.3.tar.gz
cd
nagios-plugins-2.
cd
nagios-plugins-2.0.3
./configure  --prefix=/mnt/cellar/nagios/  --with-nagios-user=nagios
--with-nagios-group=nagios --with-command-user=nagios --with-command-group=nagios
make


安装完成后这个/mnt/cellar/nagios/libexec 下会有许多如下的命令

 

nagios的详细配置和报警

然后重启一下nagios 然再观察如下:

 

nagios的详细配置和报警


当然这是本机localhost 的监控

 


7. windows 机器的监控

 

windows 机器的监控依靠的是

 

nagios的详细配置和报警

如果是通过snmp来监控的则不需要

 

本公司采用是的第一种通过安装插件来监控

 

安装如下:

 


nagios的详细配置和报警

nagios的详细配置和报警

nagios的详细配置和报警

nagios的详细配置和报警

nagios的详细配置和报警


这里allowed host 是客户端的IP是服务器nagios的IP 密码要为空,不然很麻烦,椭圆里面就这样选择就行了

 

nagios的详细配置和报警

nagios的详细配置和报警


已经启动起来了。

 

然后在nagios端定义要监控的项目

 

localhost.cfg是监控nagios本身的。为了方便管理,我们建了几个目录

 

database,fdfs,linux,widows 之类的目录

 

nagios的详细配置和报警


如下图所示是windows 目录下的监控项目

 

起名规则如下:用途+ip地址

 

nagios的详细配置和报警


下面以一台windows 机器app_server_10.129.102.53.cfg为类

 

#define hostgroup{                                                                                                
#       
hostgroup_name 
windows-servers                                             
#       
alias           Windows
Servers                                                   
#       
}
 
 
 
define host{
       use             windows-server
       host_name       app-server
       alias          
testapp-server 
       address         10.129.102.53
 }
 
 
########## nsclient++ version
define service{
       use                    
generic-service
       host_name               app-server
       service_description     NSClient++
version
       check_command          
check_nt!CLIENTVERSION
       }
######### uptime
define service{
       use                    
generic-service
       host_name               app-server
       service_description     uptime
       check_command          
check_nt!UPTIME
       }
 
 
######### ping
 
define service{
       use                    
generic-service
       host_name               app-server
       service_description     ping
       check_command          
check_ping!100.0,20%!500.0,60%
       }
 
#########IIS
define service{
       use                     generic-service
       host_name               app-server
       service_description     IIS
       check_command          
check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
       }
 
########## cpu load
 
define service{
       use                     generic-service
       host_name               app-server
       service_description     cpu_load
       check_command          
check_nt!CPULOAD!-l 5,80,90
       }
########## memory
 
 
define service{
       use                    
generic-service
       host_name               app-server
       service_description     memory
       check_command          
check_nt!MEMUSE!-w 80 -c 90
       }
########## C:\ space
 
define service{                                                                 
                                 
       use                    
generic-service                                                              
       host_name              
app-server                                                                    
 
       service_description    
C:\space                                                                 
       check_command          
check_nt!USEDDISKSPACE!-l c -w 80 -c 90  
}
 
########## D:\ space
 
define service{
       use                     generic-service
       host_name               app-server
       service_description     D:\space
       check_command          
check_nt!USEDDISKSPACE!-l d -w 80 -c 90  
}
 
##########
 
 
define service{
       use                    
generic-service
       host_name               app-server
       service_description    
Explorer.exe
       check_command          
check_nt!PROCSTATE!-d SHOWALL -l explorer.exe
       }

定义好之后,在nagios.cfg中定义要监控主机的文件位置如下第二张图所示。

 

nagios的详细配置和报警

nagios的详细配置和报警

然后重启nagios,再观察如下:

 

windows 机器已经正常。

 

nagios的详细配置和报警


8、监控linux 机器

      在客户机上操作

 

      同样添加用户如下:

 

useradd -M -s /sbin/nologin nagios

安装nagios-plugins 插件

 

cd nagios-plugins-2.0.3

/configure --prefix=/mnt/cellar/nagios-plus --with-nagios-user=nagios--with-nagios-group=nagios
make all
make install

安装nrpe 服务

cd nrpe-2.15

 

./configure--prefix=/mnt/cellar/nrpe --with-nrpe-user=nagios --with-nrpe-group=nagios  --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config

安装成功后,目录如下:

nagios的详细配置和报警

然后进入到etc下面修改nrpe.cfg配置文件


nagios的详细配置和报警

log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666      //端口号
nrpe_user=nagios      //用户,就是刚才创建的
nrpe_group=nagios     // 组
allowed_hosts=127.0.0.1,192.168.1.127 // 后面要加上nagios的IP
 
dont_blame_nrpe=0
allow_bash_command_substitution=0
debug=0
command_timeout=60
connection_timeout=300
command[check_users]=/mnt/cellar/nagios-plus/libexec/check_users -w 5-c 10

// 这里要注意路径,这个路径是nagios-plus安装后生成的libexec下面的文件,当然也可以copy到你想要的目录下面,-w-c表示WARNING, CRITICAL,即报警状态,具体的理论可以到官网上面查看

ommand[check_load]=/mnt/cellar/nagios-plus/libexec/check_load -w15,10,5 -c 30,25,20

command[check_hda1]=/mnt/cellar/nrpe/libexec/check_disk -w 20% -c 10%-p /dev/hda1

command[check_zombie_procs]=/mnt/cellar/nrpe/libexec/check_procs -w 5-c 10 -s Z

command[check_total_procs]=/mnt/cellar/nrpe/libexec/check_procs -w 150-c 200


客户端当然也要开启动nrpe服务器才行,当然为了方便,这边还是写了一个启动脚本,放到/etc/init.d下即可,别忘记给执行权限就行(chmod+x nrpe), 也可以通过xinit 来启动nrpe 当然我认为独立启动更方便管理

 

#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/mnt/cellar/nrpe/bin/nrpe
NRPECONF=/mnt/cellar/nrpe/etc/nrpe.cfg
case "$1" in
        start)
                echo -n"Staring NRPE daemon...."
                $NRPE -c$NRPECONF -d
                echo "done.."
                ;;
        stop)
                echo -n"Stopping NRPE daemon...."
                pkill -u nagiosnrpe
                echo"done.."
                ;;
        restart)
                $0  stop
                sleep 1
                $0 start
                ;;
        *)
                echo "Usage:$0 start|stop|restart"
        esac
exit 0

最后就是如果防火墙开了的别忘记放行5666端口了。

 

客户端配置好后,还要在服务端配置相关的文件才行

下面是在nagios的配置

 

由于是通过nrpe来监控linux客户端的,所以在nagios上原则上也是要安装nrpe的,但经验证,不用安装,只要从客户机上面的


nagios的详细配置和报警

如上图,把check_nrpe 拷贝到nagios的下面路径即可。

nagios的详细配置和报警


nagios的主要的配置文件全在下面的目录下:

nagios的详细配置和报警

由于nrpe只是一个插件,nagios识别不了这个命令,所以要在commands.cfg中定义一下这个命令,编辑command.cfg 在最下面添加如下,这也就是为何要拷贝check_nrpe命令到nagios端的原因

############
# 'check_nrpe ' command definition
define command{
       command_name check_nrpe
       command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
       }

然后在下面路径定义要监控的项目,linux这个目录是要自己创建的

nagios的详细配置和报警

 

具体的配置如下:更具体可以的看网盘中的配置文件。

##########define hostname and ipaddress
define host{
       use                    linux-server           
       host_name              linux1-server
       alias                    linux1-server
       address                192.168.1.109
       }
 
 
########## ping
define service{
       use                    generic-service
       host_name              linux1-server
       service_description        ping
       check_command          check_ping!100.0,20%!500.0,60%
 
}
 
########## check_users
 
define service{
       use                    generic-service
       host_name               linux1-server
       service_description     login_user
       check_command          check_nrpe!check_users
 
}
 
########## check_cpu_load
 
define service{
       use                    generic-service
       host_name              linux1-server
       service_description     CPU_load
       check_command          check_nrpe!check_load
}


最后当然还是要配置nagios.cfg文件,要让nagios知道到哪个路径下去找要监控的项目

nagios的详细配置和报警

最后重启一下nagios服务即可,如果配置有错,是不能启动成功的

当然可以在nagios上先验证一下如下:

nagios的详细配置和报警

如果能返回结果,说明配置成功

过几分钟可以看到结果:

nagios的详细配置和报警

至此windowslinux的监控基本的就这样了,复杂的一些的,要通过脚本来监控了,具体的可以看网盘


9. 短信报警和邮件报警

   这个功能最nagios最强大的功能,报警功能速度之快

当然为了系统安全,建议安装sendmail服务

yum -yinstall sendmail

nagios的详细配置和报警

然后编辑/etc/mail.rc在最后面加入

set from=cheng7223@163.comsmtp=smtp.163.com
 
setsmtp-auth-user=cheng7223@163.com smtp-auth-password=*******
 
set smtp-auth=login

这里是定义用哪个邮箱来发送报警邮件

最后定义收件人,也就接收人

编辑

nagios的详细配置和报警

definecontact{
        contact_name                    nagiosadmin             ; Short name of user
        use                            generic-contact         ; Inheritdefault values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user
 
        email                           chengjian@114995.com    ; <<***** CHANGE THIS TO YOUR EMAILADDRESS ******
        }

如上红色的就是接收人的邮箱,如果是多个人,要用逗号隔开才行

配置完以后,一定要重启sendmailnagios服务器

下面把客户机的nrpe服务给停掉来验证邮件的功能

nagios的详细配置和报警

已经显示红色,说明有问题了,再查看邮箱,速度挺快的


nagios的详细配置和报警

这是有问题的报警。再验证一下,恢复后的报警

nagios的详细配置和报警

至此已经OK。至于通过图像显示的功能,这里不做介绍,因为zabbix 在图像显示这一块已经足够。


推荐阅读:
  1. Nagios设置报警间隔
  2. nagios邮件报警配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nagios详细配置与报警 nagios下windows监控 详细配置

上一篇:1.sql数据处理问题

下一篇:初级网络工程师面试题60例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》