发表于 2019-1-13 06:13:59

(十一)企业部分之nagios

  
  Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等。
  Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。
  

  nagios常用配置文件主要用以下6个:
  cgi.cfg(控制cgi访问的配置文件),nagios.cfg(Nagios主配置文件),resource.cfg(resource.cfg定义了一些变量,以便被其他文件引用,如$USER1$),objects(objects是一个目录,用于定义Nagios对象),servers(servers是自己创建的目录,Nagios可以加载该目录下所有配置文件,常用于定义各远程服务器监控信息,需要在nagios.cfg中配置)
  其中:
  objects目录下文件::
  commands.cfg   #命令定义配置文件,里面定义的命令可以被其他文件引用
  contacts.cfg   #联系人和联系人组配置文件
  localhost.cfg   #监控本地机器的配置文件
  printer.cfg   #监控打印机的一个事例配置文件(默认未启用)
  switch.cfg   #监控路由器的一个事例配置文件(默认未启用)
  templates.cfg   #模板配置文件,在此可以定义模板,在其他文件中引用
  timeperiods.cfg   #定义监控时间段的配置文件
  windows.cfg   #监控Windows的一个事例配置文件(默认未启用)
  

  Nagios所需要的运行条件是机器必须可以运行Linux(或是Unix变种)并且有C语言编译器。你必须正确地配置TCP/IP协议栈以使大多数的服务检测可以通过网络得以进行。你需要但并非必须正确地配置Nagios里的CGIs程序,而一旦你要使用CGI程序时,你必须要安装以下这些软件。
  

  1.nagios安装##可参考官网文档
  
  所需软件包:gd-devel-2.0.35-11.el6.x86_64.rpm nagios-plugins-2.1.1.tar.gz nagios-cn-3.2.3.tar.bz2
  
tar jxf nagios-cn-3.2.3.tar.bz2
yum install gd-devel-2.0.35-11.el6.x86_64.rpm -ycd nagios-cn-3.2.3
groupadd nagcmd
useradd -M -d /usr/local/nagios -G nagcmd nagios
usermod -G nagcmd apache./configure --with-command-group=nagcmd
make all    ##可以看到后面步骤
make install
make install-init
make install-config
make install-commandmode
make install-webconf  
/etc/httpd/conf.d/nagios.conf
cat /usr/local/nagios/etc/htpasswd.users    ##可看到用户名和密码
htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin    ##修改nagiosadmin的密码/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/httpd start
/etc/init.d/nagios start  
  

  http://172.25.45.1/nagios/
   http://s4.运维网.com/wyfs02/M01/83/5A/wKioL1dxE-jDuTOeAABQ1Hbcirk144.png
  

  用户名:nagiosadmin   密码:修改的密码
  http://s2.运维网.com/wyfs02/M02/83/5A/wKioL1dxFFqh23sgAAF0fipikLM949.png
  
  2.nagios-plugins安装
tar zxf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1./configure##可看到插件安装的信息,可根据缺少的插件进行安装。例如:mysql-devel,openssl-devel
make
make install  
cd /usr/local/nagios/libexec
chown nagios.nagios * -R/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reloadcd /usr/local/nagios/etc/
vim nagios.cfg  

  添加两个cfg_file(33行下面)
  cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
  cfg_file=/usr/local/nagios/etc/objects/services.cfg
  注释掉cfg_file=/usr/local/nagios/etc/objects/localhost.cfg(38行)
http://s3.运维网.com/wyfs02/M01/83/5B/wKiom1dxFEbj6bSTAACSLAAJ9lc973.png
  

cd objects/
cp -p localhost.cfg hosts.cfg
cp -p localhost.cfg services.cfg
vim hosts.cfg  将内容修改为以下:
  define host{
        use                     linux-server
        host_name               server1.example.com
        alias                   Manager
  #       parents               MainSwitch
        address               172.25.45.1
        icon_image            server.gif
        statusmap_image         server.gd2
        2d_coords               500,200
        3d_coords               500,200,100
        }
  
  define hostgroup{
        hostgroup_namelinux-servers ; The name of the hostgroup
        alias         Linux Servers ; Long name of the group
        members         *   ; Comma separated list of hosts that belong to this group
        }
  

http://s2.运维网.com/wyfs02/M01/83/5A/wKioL1dxFGihxZ9yAAC4aMOpiS8534.png
  

vim services.cfg  将166行之前全部删掉,再删除第8行开始的servicegroup,只留下一个servicegroup。将此servicegroup的members最后两个成员HomeRouter和PING删除
  :%s/田朝阳家用机/server1.example.com/g
http://s4.运维网.com/wyfs02/M02/83/5B/wKiom1dxFHXiors5AABFCrJNC_A184.png
  

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  

  http://172.25.45.1/nagios/
  http://s1.运维网.com/wyfs02/M01/83/5B/wKiom1dxFZ3xqMooAAAiZawauYU754.png
  http://s3.运维网.com/wyfs02/M02/83/5B/wKiom1dxFceBtQCYAAA5940f9xc017.png
cd /usr/local/nagios/libexec
./check_disk
./check_disk -w 20 -c 10
./check_disk -w 20 -c 10 -p /  
vim services.cfg  添加以下内容:
  max_check_attempts            2
http://s1.运维网.com/wyfs02/M00/83/5A/wKioL1dxFJ-DXW2zAAB1I1Ek4m4552.png
  

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  

  http://172.25.45.1/nagios/
  http://s1.运维网.com/wyfs02/M00/83/5B/wKiom1dxFf3ipGHGAAAfPMt8NGM204.png
  
  【server2】
yum install -y mysql-server
/etc/init.d/mysqld start
mysql_secure_installation  
mysql -p
mysql> create database nagdb;
mysql> grant select on nagdb.* to nagios@'172.25.45.1' identified by 'westos';
mysql> quit  

  【server1】
cd /usr/local/nagios/libexec
./check_mysql -H 172.25.45.2 -u nagios -p westos  http://s1.运维网.com/wyfs02/M01/83/5B/wKiom1dxFSziY8hyAABdiiCbxcw543.png
  

mysql -h 172.25.45.2 -u nagios -p
mysql> show databases;
mysql> quit  http://s3.运维网.com/wyfs02/M00/83/5B/wKiom1dxFP-gw6rnAAAcEQOSAZw347.png
  
  将serevr2的mysql服务关闭
http://s1.运维网.com/wyfs02/M02/83/5A/wKioL1dxFTfzHRtcAABBK9XDess287.png
  
cd /usr/local/nagios/etc/objects
vim commands.cfg  在212行后添上以下内容:
  # 'check_mysql' command definition
  define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$
        }
  

http://s3.运维网.com/wyfs02/M02/83/5B/wKiom1dxFVHT9-ETAAAwIt27xic851.png
  
vim services.cfg  在最后添上以下内容:
  #######################check_mysql
  
  define service{
        use                           local-service         
        host_name                     server2.example.com
        service_description             MYSQL
        check_command                   check_mysql!nagios!westos
        }
  

http://s1.运维网.com/wyfs02/M00/83/5A/wKioL1dxFWGw8wILAAAxeJGVNXM893.png
  
vim hosts.cfg  在上面host下再添加一个host,内容如下:
  define host{
        use                     linux-server
        host_name               server2.example.com
        alias                   Manager
  #       parents               MainSwitch
        address               172.25.45.2
        icon_image            server.gif
        statusmap_image         server.gd2
        2d_coords               400,100
        3d_coords               400,100,100
        }
  

http://s3.运维网.com/wyfs02/M01/83/5A/wKioL1dxFXSRz03-AABB94YSWv0979.png
  
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  
  

  http://172.25.45.1/nagios/
http://s3.运维网.com/wyfs02/M01/83/5A/wKioL1dxFjPhh3eyAAA9UvLuIrc928.png
  

http://s2.运维网.com/wyfs02/M00/83/5A/wKioL1dxFjTBDM8-AABV7r9n49U296.png
  
  ###添加连线
vim hosts.cfg  将alias的值改为server2
  将parents的注释取消,并将值改为server1.example.com
http://s4.运维网.com/wyfs02/M01/83/5B/wKiom1dxFl-gUbZ7AAA6gvdzO7U362.png
  
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  
  
  

  http://172.25.45.1/nagios/
http://s3.运维网.com/wyfs02/M00/83/5B/wKiom1dxFmyxR8MJAAA1VCpoSN0478.png
  

http://s4.运维网.com/wyfs02/M02/83/5A/wKioL1dxFm6jMfdVAAF2-QaKdHc904.png
  

  此时HTTP的告警状态是因为没有index.html页面,在/var/www/html下建立一个index.html文件就可以解决HTTP告警问题
  http://s4.运维网.com/wyfs02/M01/83/EF/wKioL1eAZouh6K86AABKfo-t9IY674.png
  

  3.安装nrpe
  【server2】
  所需软件包:nrpe-2.15.tar.gz nagios-plugins-2.1.1.tar.gz
  
tar zxf nrpe-2.15.tar.gz
tar zxf nagios-plugins-2.1.1.tar.gzuseradd -M -d /usr/local/nagios nagios
cd nagios-plugins-2.1.1
./configure  

  出现以下错误,是因为缺少软件,下载gcc可解决
  # ./configure
  checking for a BSD-compatible install... /usr/bin/install -c
  checking whether build environment is sane... yes
  checking for a thread-safe mkdir -p... /bin/mkdir -p
  checking for gawk... gawk
  checking whether make sets $(MAKE)... yes
  checking whether to disable maintainer-specific portions of Makefiles... yes
  checking build system type... x86_64-unknown-linux-gnu
  checking host system type... x86_64-unknown-linux-gnu
  checking for gcc... no
  checking for cc... no
  checking for cl.exe... no
  configure: error: in `/root/nagios-plugins-2.1.1':
  configure: error: no acceptable C compiler found in $PATH
  See `config.log' for more details.
  
yum install openssl-devel -y  ./configure##这里不需要--with-mysql,可以不安装
http://s5.运维网.com/wyfs02/M02/83/5A/wKioL1dxFwegdR8-AABmYhYypk4605.png
  

make
make installcd /usr/local/nagios/
chown nagios.nagios . -Ryum install -y xinetdcd /root/nrpe-2.15
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetdcd /etc/xinetd.d
vim nrpe  将15行的only_from改为172.25.45.1
  
vim /etc/services  添加以下内容:

  nrpe            5666/tcp
  
/etc/init.d/xinetd start
netstat -antlpehttp://s2.运维网.com/wyfs02/M01/83/5A/wKioL1dxFsajlKDBAAB_ckLj-CM856.png
  
vim /usr/local/nagios/etc/nrpe.cfg  修改221行,改为以下内容:
  command=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
  http://s1.运维网.com/wyfs02/M02/83/5B/wKiom1dxF0XzqefGAABt5X9GN4w498.png
  

/etc/init.d/xinetd restartcd /usr/local/nagios/libexec/
scp check_nrpe root@172.25.45.1:/usr/local/nagios/libexec/  
  
  【server1】
cd /usr/local/nagios/libexec/
chown nagios.nagios check_nrpe
./check_nrpe -H 172.25.45.2
./check_nrpe -H 172.25.45.2 -c check_diskhttp://s5.运维网.com/wyfs02/M00/83/5B/wKiom1dxF3CzURMwAAAxfYxVKPc736.png
  

  测试:
  在server2将/usr/local/nagios/etc/nrpe.cfg修改,将command -w 改为90%,并重启服务
  ./check_nrpe -H 172.25.45.2 -c check_disk
  将server2的/usr/local/nagios/etc/nrpe.cfg文件恢复上一步之前,并重启服务。
  
  
  【server1】
cd /usr/local/nagios/etc/objects
vim commands.cfg  在# 'check_mysql' command definition这一段下添加以下内容:
  # 'check_nrpe' command definition
  define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
  

http://s5.运维网.com/wyfs02/M01/83/5C/wKiom1dxF5DCb7N9AAAq1RsTLYg342.png
  
vim services.cfg  在最后添上以下内容:
  #######################check_nrpe
  define service{
        use                           local-service         ; Name of service template to use
        host_name                     server2.example.com
        service_description             根分区
        check_command                   check_nrpe!check_disk
        }
  
  define service{
        use                           local-service         ; Name of service template to use
        host_name                     server2.example.com
        service_description             登录用户数
        check_command                   check_nrpe!check_users
        }
  
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  

http://172.25.45.1/nagios/
  http://s1.运维网.com/wyfs02/M02/83/5A/wKioL1dxF7_QeMDdAAB_th-VMyA303.png
  
  4.虚拟机连网
ip addr add 172.25.254.101/24 dev eth0
ip addr showip route show
route -n
ping 172.25.254.45
route del default gw 172.25.45.250
route add default gw 172.25.254.45
route -nvim /etc/resolv.confhttp://s1.运维网.com/wyfs02/M00/83/5C/wKiom1dxF9Tihyj6AAAOK8cmEQ4636.png
  

ping 192.168.122.1
ping 172.25.254.45  http://s2.运维网.com/wyfs02/M01/83/5A/wKioL1dxF-bCi-8NAACbTDzM1e8281.png
  

  测试网络是否连通
dig baidu.com
ping baidu.com  http://s5.运维网.com/wyfs02/M00/84/8C/wKiom1eULMKy9PAHAACZdsNW_k0862.png
  

  已连网
  在家里用无限上网,也可以用NAT的方式进行虚拟机连网,网络配置选择DHCP分配网络
  

http://s4.运维网.com/wyfs02/M01/84/8C/wKiom1eUK96RCZZ2AAAaLTDoot0813.png
  

http://s5.运维网.com/wyfs02/M02/84/8B/wKioL1eUK96TH9x6AACV_GUIoi4082.png
  

  可以看到本地发送邮件的地址。
  登录邮箱,进行设置。在反垃圾处设置邮件地址白名单:nagios@server1.example.com
  http://s2.运维网.com/wyfs02/M01/84/8B/wKioL1eULCWRnw6YAABCQZMoGas576.png
  
  【server1】
yum install mail -ysu - nagios
mail wjl19940429@qq.comhttp://s2.运维网.com/wyfs02/M02/84/8C/wKiom1eULEzixR0OAAAZsU89bYY379.png
  

http://s2.运维网.com/wyfs02/M00/84/8B/wKioL1eULEygWl1lAAAQQvqTy6Q345.png
  

http://s5.运维网.com/wyfs02/M02/84/8B/wKioL1eULE2yF_GiAAAvyGaS1mg663.png
  
cd /usr/local/nagios/etc/objects
vim contacts.cfg  修改35行email的值为自己的邮箱
  http://s5.运维网.com/wyfs02/M00/84/8C/wKiom1eULIaDuuN7AAAiYl6RSCY431.png
  

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  
  【server2】
/etc/init.d/mysqld stop  
  【server1】
vim /usr/local/nagios/etc/objects/templates.cfg  修改186行max_check_attempts的值为3
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  为了尽快测试,将检测次数减少一些 http://s4.运维网.com/wyfs02/M00/84/8B/wKioL1eULPrSH_5gAAB-f331UM4864.png
  

http://s2.运维网.com/wyfs02/M02/84/8C/wKiom1eULYjS9Sz8AAAp7xeNcpU286.png
  

http://s2.运维网.com/wyfs02/M02/84/8C/wKioL1eULYmRRj5RAACUeL4t0Go905.png
  

  ##110云告警##
  所需软件包:alert-agent-4.1.3.1-linux-x64.tar.gz
  

su - nagios
-bash-4.1$ tar zxf alert-agent-4.1.3.1-linux-x64.tar.gz -C /usr/local/nagios/libexec/
-bash-4.1$ cd /usr/local/nagios/libexec/
-bash-4.1$ cd alert-agent/
-bash-4.1$ llhttp://s2.运维网.com/wyfs02/M01/84/8C/wKiom1eULfSjyQEmAABRFx-W0l8523.png
  
-bash-4.1$ cd plugin/nagios-plugin/
-bash-4.1$ cp nagios /usr/local/nagios/libexec/
-bash-4.1$ cp 110monitor.cfg /usr/local/nagios/etc/objects/  

  在110云告警页面选择配置,添加应用云告警
http://s1.运维网.com/wyfs02/M02/84/8C/wKiom1eULkqThMfHAADQ7ApoOwM263.png
  

http://s3.运维网.com/wyfs02/M00/84/8C/wKioL1eULnqSGa18AAAQNWfnSao835.png
  复制下key
  
cd /usr/local/nagios/etc/objects
vim 110monitor.cfg  将复制下的key粘贴在18行,paper后
http://s5.运维网.com/wyfs02/M02/84/8C/wKioL1eULqawzIdeAAArPHtsRxg784.png
  
vim contacts.cfg  在54行members后添加110monitor
http://s5.运维网.com/wyfs02/M00/84/8C/wKiom1eULhKwWE_uAAAejTNECM4316.png
  
cd ..
vim nagios.cfg  在35行之后添加cfg_file=/usr/local/nagios/etc/objects/110monitor.cfg
http://s1.运维网.com/wyfs02/M00/84/8C/wKioL1eULiHBjQsiAAB8Ofqdk1w581.png
  
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload  

  网页进行下一步,完成配置。
  选择通知策略,进行配置
  例如
  http://s1.运维网.com/wyfs02/M00/84/8C/wKioL1eULvbQjoQAAAAitk1NmEQ487.png
  
  【server2】
/etc/init.d/mysqld stop  http://s5.运维网.com/wyfs02/M01/84/8C/wKioL1eULyfS-vo9AAB7pP1foTE613.png
  【server1】
cd /usr/local/nagios/var
tail -f nagios.log  http://s5.运维网.com/wyfs02/M02/84/8C/wKiom1eULzezX-0HAAB5Ta4TTJQ384.png
/etc/init.d/nagios reload  

  缺云告警成功示例截图



页: [1]
查看完整版本: (十一)企业部分之nagios