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

Centos 配置nagios 监控服务器

[复制链接]

尚未签到

发表于 2019-1-15 08:32:39 | 显示全部楼层 |阅读模式
  实验环境如下
  Nagios监控服务器IP:192.168.11.5
  *** 监控服务器IP:192.168.11.6
  Nagios 配置如下:
  一 、安装Apache+php
  1 编译安装apache服务器。(在之前的博客中已经写过,这里就偷懒了。)
  2 编译安装php


  • yum install libxml2-devel
  • Cd  /usr/local/php-5.4.3
  • ./configure--prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs
  • make  && make install

  3 修改apache的配置文件


  • vi /usr/local/apache2/conf/httpd.conf
  •   
  •     DirectoryIndex index.html index.php   # 添加 index.php


  • #LoadModule rewrite_module modules/mod_rewrite.so
  • LoadModule php5_module        modules/libphp5.so #添加

  •    AddType application/x-compress .Z
  •     AddType application/x-gzip .gz .tgz
  • AddType application/x-httpd-php .php  # 添加

  4 测试php是否安装成功


  • cd /usr/local/apache2/htdocs/
  • vi test.php  


  5 使用游览器访问

  php到此安装成功
  二 、安装Nagios监控服务器
  安装nagios
  1 安装nagios


  • useradd -s /sbin/nologin nagios # 建立nagios用户
  • cd /usr/local/nagios-3.2.3/  
  • ./configure --prefix=/usr/local/nagios
  • make all
  • make install
  • make install-init #在/etc/rc.d/init.d/目录下创建启动脚本
  • make install-commandmode #配置目录权限
  • make install-config #安装nagios示例配置文件,这里安装路径为/usr/local/nagios/etc

  2 安装nagios 插件


  • cd /usr/local/nagios-plugins-1.4.15/
  • ./configure --prefix=/usr/local/nagios #这里插件安装的位置与nagios相同
  • make &&  make install  

  3 配置apache ,使用apache支持nagios


  • vi /usr/local/apache2/conf/httpd.conf  
  • 修改为
  • User nagios
  • Group nagios
  • 最后添加
  • #nagios
  • ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

  •         AuthType Basic
  •         Options ExecCGI
  •         AllowOverride None
  •         Order allow,deny
  •         Allow from all
  •         AuthName "Nagios Access"
  •         AuthUserFile /usr/local/nagios/etc/htpasswd
  •         Require valid-user

  •   
  • Alias /nagios "/usr/local/nagios/share"

  •         AuthType Basic
  •         Options None
  •         AllowOverride None
  •         Order allow,deny
  •         Allow from all
  •         AuthName "Nagios Access"
  •         AuthUserFile /usr/local/nagios/etc/htpasswd
  •         Require valid-user


  4 创建nagios访问的用户 用户名称为nagios


  • /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd naigos   
  • New password:   
  • Re-type new password:   
  • Adding password for user naigos  

  5 启动apache


  • /usr/local/apache2/bin/apachectl restart

  6 修改naigos的权限


  • chown nagios.nagios /usr/local/nagios
  • chown nagios.nagios -R /usr/local/nagios/libexec/

  7 使用游览器访问nagios服务器

  显示乱码,解决方法:
主要是apache没有开启cgi脚本的缘故
进入apache的主配置文件httpd.conf


  • vi /usr/local/apache2/conf/httpd.conf
  • #LoadModule cgid_module modules/mod_cgid.so
  • #LoadModule actions_module modules/mod_actions.so
  • 将上面2行的#去掉,重启apache就OK了


  8 配置nagios服务器


  • [root@localhost local]# cd /usr/local/nagios-3.2.3/
  • [root@localhost nagios-3.2.3]# cd /usr/local/nagios
  • [root@localhost nagios]# ls
  • bin  etc  include  libexec  sbin  share  var
  • [root@ac96548d etc]# vi /usr/local/nagios/etc/cgi.cfg
  • main_config_file=/usr/local/nagios/etc/nagios.cfg
  • physical_html_path=/usr/local/nagios/share
  • url_html_path=/nagios show_context_help=0
  • use_pending_states=1
  • use_authentication=1
  • use_ssl_authentication=0
  • default_user_name=nagios
  • authorized_for_system_information=nagiosadmin,nagios
  • #添加刚才使用htpasswd建立的用户naigos (使用nagios于nagiosadmin用户才能看到监控的主机)
  • authorized_for_configuration_information=nagiosadmin,nagios
  • authorized_for_system_commands=nagios
  • authorized_for_all_services=nagiosadmin,nagios
  • authorized_for_all_hosts=nagiosadmin,nagios
  • authorized_for_all_service_commands=nagiosadmin,nagios
  • authorized_for_all_host_commands=nagiosadmin,nagios
  • default_statusmap_layout=5 default_statuswrl_layout=4
  • ping_syntax=/bin/ping -n -U -c 5 $HOSTADDRESS$
  • refresh_rate=90 escape_html_tags=1
  • action_url_target=_blank notes_url_target=_blank lock_author_names=1

  • /usr/local/nagios/bin/nagios -v  /usr/local/nagios/etc/nagios.cfg 检测nagios的配置文件是否有语法错误
  • /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg   启动nagios监控服务器

  9 通过游览器可以看到nagios监控服务器本机的状态

  至此 nagios监控服务器安装成功
  三、配置被监控的linux 主机 (***主机)
  1 安装nagios-plugins和nrpe插件


  • Yum –y  install gcc
  • useradd   -s /sbin/nologin nagios
  • 安装nagios-plugins
  • tar xzvf nagios-plugins-1.4.15.tar.gz
  • cd nagios-plugins-1.4.15
  • ./configure –prefix=/usr/local/nagios
  • make
  • make install
  •   
  • 安装 nagios_nrpe_2.9
  • wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.9/nrpe-2.9.tar.gz
  • tar xzvf  nrpe-2.9.tar.gz
  • cd nrpe-2.9
  • ./configure --with-command-group=nagios  --prefix=/usr/local/nagios  
  • (如果报缺少ssl,请安装 yum  install  openssl-devel)
  • make all
  • make install-plugin
  • make install-daemon
  • make install-daemon-config


  2 修改nrpe的配置文件


  • vi /usr/local/nagios/etc/nrpe.cfg
  • 找到 allowed_hosts=127.0.0.1
  • 后面加nagios服务器的IP, 用“,”隔开,加了之后如下:
  • allowed_hosts=127.0.0.1,192.168.11.5
  •   


  3 配置nrpe的监控对象
  
  


  • #配置监控对象
  • 说明:由监控原理可知被监控端做监控,然后将数据传给监控服务器绘总,设置监控详细参数主要是设置被监控端的nrpe.cfg文件,可以看到里面监控对象
  • vi /usr/local/nagios/etc/nrpe.cfg,查找并修改如下一行
  • command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10 command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_sda5]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda5 command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 command[check_***]=/usr/local/nagios/libexec/check_*** -w 150 -c 200  
  • ............................................#自已添加..........................................................   
  • #command[check_http]=/usr/local/nagios/libexec/check_http -I 127.0.0.1 -w 10 -c 20
  • #command[check_ssh]=/usr/local/nagios/libexec/check_ssh -4 127.0.0.1



  注释 在/usr/local/nagios/libexec 这个目录下,都是监控脚本(当我们需要监控特定服务时,需要在此目录下创建监控脚本,然后
  在nrpe.cfg的配置文件中添加监控的命令)


  • [root@localhost libexec]# ls
  • check_apt       check_dummy         check_ircd      check_ntp       check_rpc      check_wave
  • check_breeze    check_file_age      check_load      check_ntp_peer  check_sensors  negate
  • check_by_ssh    check_flexlm        check_log       check_ntp_time  check_smtp     urlize
  • check_clamd     check_ftp           check_mailq     check_nwstat    check_ssh      utils.pm
  • check_cluster   check_http          check_mrtg      check_oracle    check_swap     utils.sh
  • check_dhcp      check_icmp          check_mrtgtraf  check_overcr    check_tcp
  • check_dig       check_ide_smart     check_nagios    check_ping      check_time
  • check_disk      check_ifoperstatus  check_nntp      check_pop       check_udp
  • check_disk_smb  check_ifstatus      check_nrpe      check_procs     check_ups
  • check_dns       check_imap          check_nt        check_real      check_user

  4 启动nrpe


  • 启动nrpe,并测试
  • /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  启动nrpe
  • echo '/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d &> /dev/null'
  • >> /etc/rc.local   设置开机自启动
  • netstat -atulnp | grep 'nrpe'
  • tcp        0      0 0.0.0.0:5666                0.0.0.0:*       LISTEN      3308/nrpe
  • /usr/local/nagios/libexec/check_nrpe -H localhost
  • NRPE v2.12

  四、配置监控服务器 (nagios服务器)
  1 添加需要监控的linux客户端


  • vi /usr/local/nagios/etc/nagios.cfg
  • cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
  • cfg_file=/usr/local/nagios/etc/objects/***.cfg # 添加此行

  2、在commands.cfg中添加nrpe插件


  • vi /usr/local/nagios/etc/objects/commands.cfg  
  • #check nrpe
  • define command{
  • command_name check_nrpe
  • command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  • }

  3、建立被监控服务器的配置文件(***.cfg)


  • vi /usr/local/nagios/etc/objects/***.cfg
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •         host_name               ***
  •         alias                   ***
  •         address                 192.168.11.6
  •         }
  • define hostgroup{
  •         hostgroup_name  *** ; The name of the hostgroup
  •         alias           Linux Servers ; Long name of the group
  •         members         *** Server
  •         }
  • define service{
  •         use                             local-service         ; Name of service template to use
  •          host_name                        ***
  •         service_description             PING
  •         check_command                   check_ping!100.0,20%!500.0,60%
  •         }
  • define service{
  •         use                             local-service         ; Name of service template to use
  •           host_name                     ***
  •         service_description             disk
  •         check_command                   check_nrpe!check_sda5     # 通过nrpe插件监控***服务器的硬盘
  •         }

  • define service{
  •         use                             local-service         ; Name of service template to use
  •          host_name                       ***
  •         service_description             SSH
  •         check_command                   check_ssh
  •         notifications_enabled           0
  •         }
  • define service{
  •         use                             local-service         ; Name of service template to use
  •          host_name                        ***
  •         service_description             ***
  •         check_command                   check_tcp!1723  #监控tcp的1723端口
  •         notifications_enabled           0
  •         }

  当我们使用相同的参数监控多台服务器时候,我们可以建立组的方式,这样不必建立多个配置文件,例如监控多台tomcat 服务器


  • vi /usr/local/nagios/etc/nagios.cfg
  • cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
  • cfg_file=/usr/local/nagios/etc/objects/***.cfg # 添加此行
  • cfg_file=/usr/local/nagios/etc/objects/tomcat.cfg # 添加此行
  当有多台服务器时候使用组的优点


  • vi /usr/local/nagios/etc/objects/tomcat.cfg
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat1
  •         alias                   web1
  •         address                192.168.11.7
  •         }
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat2
  •         alias                   web2
  •         address               192.168.11.8
  •         }
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat3
  •         alias                   web3
  •         address                 192.168.11.9
  •         }
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat4
  •         alias                   web4
  •         address               192.168.11.10
  •         }
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat5
  •         alias                   web5
  •         address                 192.168.11.11
  •         }
  • define host{
  •         use                     linux-server            ; Name of host template to use
  •                                                         ; This host definition will inherit all variables that are defined
  •                                                         ; in (or inherited by) the linux-server host template definition.
  •         host_name               Tomcat6
  •         alias                   web6
  •         address                 192.168.11.12
  •         }
  • define hostgroup{
  •         hostgroup_name  Tomcat ; The name of the hostgroup
  •         alias           Linux Servers ; Long name of the group
  •         members         Tomcat1,Tomcat2,Tomcat3,Tomcat4,Tomcat5,Tomcat6     ; Comma separated list of hosts that belong to this group
  •         }  #在组中添加hostname
  • define service{
  •         use                             local-service         ; Name of service template to use
  •         hostgroup_name                       Tomcat   #由  host_name 改成hostgroup—name
  •         service_description             PING
  •         check_command                   check_ping!100.0,20%!500.0,60%
  •         }

  • define service{
  •         use                             local-service         ; Name of service template to use
  •         hostgroup_name                       Tomcat
  •         service_description             SSH
  •         check_command                   check_ssh
  •         notifications_enabled           0
  •         }
  • define service{
  •         use                             local-service         ; Name of service template to use
  •         hostgroup_name                       Tomcat
  •         service_description             HTTP
  •         check_command                   check_http
  •         notifications_enabled           0
  •         }

  4、通过游览器访问nagios服务器





运维网声明 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-663394-1-1.html 上篇帖子: nagios监控远程windows服务器 下篇帖子: Nagios的安装配置与应用之三Nagios的配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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