设为首页 收藏本站
查看: 991|回复: 0

nagios的详细配置和报警

[复制链接]

尚未签到

发表于 2019-1-13 09:07:45 | 显示全部楼层 |阅读模式
   注:本文有感谢程建提供

  一、需求

  nagios 更新速度是很快的,本公司采用最新的稳定版本nagios-4.0.8,插件采用是的nagios-plugins-2.0.3,新版本经过使用和测试,发现速度和报警的速度比以前快了许多,
  并且占用操作系统资源不多问题及时,界面干净,如下图:


    由于网上下载包不稳定,所以我整理后放到网盘中,所有的下载包全在360网盘地址中如下:
  http://yunpan.cn/cHjtN2f9Kz3FM  访问密码 f9af
  

  二、PHP环境的搭建

  本公司采用lamp的环境来做为nagios的基础环境,当然mysql是可以不用装的只要PHP环境就行了。
  1、apr的安装
  APR(Apache portable Run-time libraries,Apache可移植运行库)的目的如其名称一样,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。
在早期的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数。随着Apache的进一步开发,Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。这样,APR的开发就从Apache中独立出来,Apache仅仅是使用APR而已。
  一般情况下,APR开发包很容易理解为仅仅是一个开发包,不过事实上并不是。目前,完整的APR实际上包含了三个开发包:apr、apr-util以及apr-iconv,每一个开发包分别独立开发,并拥有自己的版本。
  现在新版本的http中apr集成到了一个httpd-2.4.9-deps.tar 类似于这样的包中,解压后的结果如下:

  集成后安装和管理方便多了,不用再担心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   apr和api-utils 安装好后,目录如下:
  


  

  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

  说明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 拷贝一份到下面的目录
  
  然后在apache的httpd.conf中添加
  AddType application/x-httpd-php .php
  然后重启apache 再访问如下:

  

  至此基础环境配置完成
  如果不知道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  如下图,先验证配置文件的正确性

  

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

  4、apache相关的配置
./htpasswd  -c /mnt/cellar/nagios/etc/htpasswd    letang // 添加web登陆用户letang  
  在apache的httpd.conf的最后面添加如下:
#setting for nagios
ScriptAlias /nagios/cgi-bin"/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

Alias /nagios"/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
  然后找到
  user apache
        group apache
  修改为
  user nagios
     group nagios
  最后重启apache 登陆验证如下:

  5、错误排除
  输入用户名和密码之后点击services出现下面的乱码情况如下:

  出现上面的原因是apache没有开启cgi脚本的缘故
进入apache的主配置文件httpd.conf
#LoadModule cgid_module modules/mod_cgid.so
#LoadModule actions_module modules/mod_actions.so  将上面2行的#去掉,重启apacheOK了,然后关掉浏览器重新登陆如下:

好像还是什么也没有?并且有错误提示
这是认证的问题,修改如下
use_authentication=1改为 use_authentication=0 然后重启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 然再观察如下:



当然这是本机localhost 的监控
  

7. windows 机器的监控
windows 机器的监控依靠的是

如果是通过snmp来监控的则不需要
本公司采用是的第一种通过安装插件来监控
安装如下:







  

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


  

已经启动起来了。
然后在nagios端定义要监控的项目
localhost.cfg是监控nagios本身的。为了方便管理,我们建了几个目录
database,fdfs,linux,widows 之类的目录

  

如下图所示是windows 目录下的监控项目
起名规则如下:用途+ip地址

  

下面以一台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,再观察如下:
windows 机器已经正常。

  

  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  安装成功后,目录如下:

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


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的,但经验证,不用安装,只要从客户机上面的
  


  如上图,把check_nrpe 拷贝到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这个目录是要自己创建的
  
  具体的配置如下:更具体可以的看网盘中的配置文件。
##########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上先验证一下如下:

  如果能返回结果,说明配置成功
  过几分钟可以看到结果:

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

  9. 短信报警和邮件报警
  这个功能最nagios最强大的功能,报警功能速度之快
  当然为了系统安全,建议安装sendmail服务
  yum -yinstall sendmail

  然后编辑/etc/mail.rc在最后面加入
set from=cheng7223@163.comsmtp=smtp.163.com
setsmtp-auth-user=cheng7223@163.com smtp-auth-password=*******
set smtp-auth=login  这里是定义用哪个邮箱来发送报警邮件
  最后定义收件人,也就接收人
  编辑

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    ;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-662597-1-1.html 上篇帖子: 运维黑暗料理 下篇帖子: 让nagios使用外部邮箱发送警告邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表