Nagios监控服务

Nagios监控服务

  1. 案例1:常用系统监控命令
  2. 案例2:搭建nagios监控服务器
  3. 案例3:配置文件及插件使用
  4. 案例4:监控远程主机的公有数据
  5. 案例5:监控远程主机的私有数据

1 案例1:常用系统监控命令

1.1 问题

本案例要求练习常用的系统监控命令完成以下任务:

  1. 使用vmstat命令监控内存及磁盘I/O信息
  2. 使用iostat命令监控CPU处理器及磁盘的I/O信息
  3. 使用sar命令监控系统最近的CPU/内存等活动

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用vmstat命令监控内存及磁盘I/O信息

1)报告内存、交换分区、CPU等活动信息,每2秒更新一次数据

  1. [root@svr7 ~]# vmstat 2
  2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  4. 2  0      0 1447960   1204 331260    0    0   422     9   59  153  1  1 98  0  0
  5. 0  0      0 1447944   1204 331260    0    0     0     0    9   12  0  0 100  0  0
  6. 0  0      0 1447944   1204 331260    0    0     0     0    9   12  0
  7. .. ..

2)报告一次磁盘的I/O信息

  1. [root@svr7 ~]# vmstat -d
  2. disk- ------------reads------------ ------------writes----------- -----IO------
  3. total merged sectors      ms  total merged sectors      ms    cur    sec
  4. vda     7863     12  576137    2560   1427     25   12085    6142      0      3
  5. sr0       11      0      88       3      0      0       0       0      0      0
  6. dm-0    6529      0  562281    2503    356      0    7989    6480      0      3
  7. dm-1     141      0    2536       7      0      0       0       0      0      0

3)报告一次指定分区的I/O信息

  1. [root@svr7 ~]# vmstat -p /dev/vda1
  2. vda1          reads   read sectors  writes    requested writes
  3. 1010       8792       1025       4096

步骤二:使用iostat命令监控CPU处理器及磁盘的I/O信息

1)报告一次CPU和磁盘I/O信息

  1. [root@svr7 ~]# iostat
  2. Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn)     20170503     _x86_64_    (1 CPU)
  3. avg-cpu:  %user   %nice %system %iowait  %steal   %idle
  4. 0.77    0.00    0.71    0.05    0.00   98.47
  5. Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
  6. vda               9.85       304.60         6.45     288160       6103
  7. scd0              0.01         0.05         0.00         44          0
  8. dm-0              7.30       297.28         4.29     281232       4055
  9. dm-1              0.15         1.34         0.00       1268          0

2)报告CPU和磁盘I/O信息,每2秒钟采样一次

  1. [root@svr7 ~]# iostat 2
  2. Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn)     20170503     _x86_64_    (1 CPU)
  3. avg-cpu:  %user   %nice %system %iowait  %steal   %idle
  4. 0.66    0.00    0.61    0.05    0.00   98.68
  5. Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
  6. vda               8.42       259.61         5.62     288168       6235
  7. scd0              0.01         0.04         0.00         44          0
  8. dm-0              6.25       253.37         3.77     281240       4187
  9. dm-1              0.13         1.14         0.00       1268          0
  10. avg-cpu:  %user   %nice %system %iowait  %steal   %idle
  11. 0.00    0.00    0.00    0.00    0.00  100.00
  12. Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
  13. vda               0.00         0.00         0.00          0          0
  14. scd0              0.00         0.00         0.00          0          0
  15. dm-0              0.00         0.00         0.00          0          0
  16. dm-1              0.00         0.00         0.00          0          0
  17. .. ..

步骤三:使用sar命令监控系统最近的CPU/内存等活动

1)查看一次最近的系统活动完整(-A)报告

  1. [root@svr7 ~]# sar -A
  2. Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn)     20170503     _x86_64_    (1 CPU)
  3. 112001
  4. CPU      %usr     %nice      %sys   %iowait    %steal      %irq     %soft    %guest    %gnice     %idle
  5. 113001     all      0.17      0.00      0.11      0.01      0.00      0.00      0.00      0.00      0.00     99.71
  6. 113001       0      0.17      0.00      0.11      0.01      0.00      0.00      0.00      0.00      0.00     99.71
  7. 平均时间:     all      0.17      0.00      0.11      0.01      0.00      0.00      0.00      0.00      0.00     99.71
  8. 平均时间:       0      0.17      0.00      0.11      0.01      0.00      0.00      0.00      0.00      0.00     99.71
  9. 112001    proc/s   cswch/s
  10. 113001      0.39     23.27
  11. 平均时间:      0.39     23.27
  12. 112001  pswpin/s pswpout/s
  13. 113001      0.00      0.00
  14. 平均时间:      0.00      0.00
  15. 112001  pgpgin/s pgpgout/s   fault/s  majflt/s  pgfree/s pgscank/s pgscand/s pgsteal/s    %vmeff
  16. 113001      6.30      0.35    165.40      0.03    133.94      0.00      0.00      0.00      0.00
  17. 平均时间:      6.30      0.35    165.40      0.03    133.94      0.00      0.00      0.00      0.00
  18. 112001       tps      rtps      wtps   bread/s   bwrtn/s
  19. 113001      0.38      0.27      0.11     12.59      0.70
  20. 平均时间:      0.38      0.27      0.11     12.59      0.70
  21. 112001   frmpg/s   bufpg/s   campg/s
  22. 113001     -4.90      0.00      1.37
  23. 平均时间:     -4.90      0.00      1.37
  24. 112001 kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
  25. 113001   1444844    604264     29.49      1204    277020   1547884     37.33    249044    241260         0
  26. 平均时间:   1444844    604264     29.49      1204    277020   1547884     37.33    249044    241260         0
  27. 112001 kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
  28. 113001   2097148         0      0.00         0      0.00
  29. 平均时间:   2097148         0      0.00         0      0.00
  30. .. ..

2)报告CPU活动信息,以5秒为间隔再采用3次

  1. [root@svr7 ~]# sar -u 5 3
  2. Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn)     20170503     _x86_64_    (1 CPU)
  3. 114420     CPU     %user     %nice   %system   %iowait    %steal     %idle
  4. 114425     all      0.00      0.00      0.00      0.00      0.00    100.00
  5. 114430     all      0.00      0.00      0.00      0.00      0.00    100.00
  6. 114435     all      0.00      0.00      0.00      0.00      0.00    100.00
  7. 平均时间:     all      0.00      0.00      0.00      0.00      0.00    100.00

3)报告最近的磁盘I/O信息

  1. [root@svr7 ~]# sar -b
  2. Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn)     20170503     _x86_64_    (1 CPU)
  3. .. ..
  4. 112001       tps      rtps      wtps   bread/s   bwrtn/s
  5. 113001      0.38      0.27      0.11     12.59      0.70
  6. 114001      0.25      0.13      0.12     15.62      0.93
  7. 平均时间:      0.31      0.20      0.11     14.11      0.81

 

 案例2:搭建nagios监控服务器

2.1 问题

本案例要求搭建一台nagios监控服务器,确认Web界面可用:

  1. 安装nagios、nagios-plugins软件包
  2. 配置及访问web界面,将管理用户/密码均设为 nagiosadmin

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装前的准备工作

1)安装网站平台环境、编译工具

  1. [root@svr7 ~]# yum  -y  install  httpd  php  gd
  2. [root@svr7 ~]# yum  -y  install  gcc  glibc  glibc-common

2)创建运行账号

  1. [root@svr7 ~]# groupadd  nagcmd
  2. [root@svr7 ~]# useradd  -G  nagcmd  nagios
  3. [root@svr7 ~]# gpasswd  -a  apache  nagcmd

步骤二:编译安装nagios及nagios-plugins

1)安装nagios

  1. [root@svr7 ~]# tar  xf  ~/nagios-4.3.1.tar.gz  -C  /usr/src/
  2. [root@svr7 ~]# cd  /usr/src/nagios-4.3.1/
  3. [root@svr7 nagios-4.3.1]# ./configure  --with-command-group=nagcmd
  4. [root@svr7 nagios-4.3.1]# make  all                      //编译
  5. [root@svr7 nagios-4.3.1]# make  install                 //安装程序
  6. [root@svr7 nagios-4.3.1]# make  install-init             //安装控制脚本
  7. [root@svr7 nagios-4.3.1]# make  install-config             //安装配置
  8. [root@svr7 nagios-4.3.1]# make  install-commandmode      //调权限
  9. [root@svr7 nagios-4.3.1]# make install-webconf             //部署网站配置
  10. [root@svr7 nagios-4.3.1]# chkconfig  --add  nagios          //加载服务配置

2)安装nagios-plugins

  1. [root@svr7 ~]# tar  xf  ~/nagios-plugins-2.1.4.tar.gz  -C  /usr/src/
  2. [root@svr7 ~]# cd  /usr/src/nagios-plugins-2.1.4/
  3. [root@svr7 nagios-plugins-2.1.4]# ./configure  --with-nagios-user=nagios  --with-nagios-group=nagcmd
  4. [root@svr7 nagios-plugins-2.1.4]# make
  5. [root@svr7 nagios-plugins-2.1.4]# make install

步骤三:启用及访问Nagios平台

1)为Nagios平台添加Web管理用户

  1. [root@svr7 ~]# htpasswd  -c  /usr/local/nagios/etc/htpasswd.users nagiosadmin                                                  //Web管理用户

2)启用nagios、httpd服务

  1. [root@svr7 ~]# systemctl  restart  nagios      //启用nagios
  2. [root@svr7 ~]# systemctl  restart  httpd          //启用Web

3)访问nagios网页界面

firefox http://服务器地址/nagios/,如图-1所示。

-1


 

案例3:配置文件及插件使用

3.1 问题

本案例要求熟悉nagios的常用插件程序,完成以下任务:

  1. 使用插件 check_http 检测本机的Web端口
  2. 使用插件 check_tcp 检测本机的FTP服务端口
  3. 使用插件 check_disk 检测本机根分区使用率:可用空间低于80%报 WARNING,可用空间低于60%报 CRITICAL

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用插件 check_http 检测本机的Web端口

1)关闭本机的httpd服务,执行check_http检查

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_http -H localhost -p 80connect to address localhost and port 80: 拒绝连接
  2. HTTP CRITICAL - Unable to open TCP socket

2)启动本机的httpd服务,并建立默认首页index.html

  1. [root@svr7 ~]# vim  /var/www/html/index.html
  2. Default site.
  3. [root@svr7 ~]# systemctl  restart  httpd

3)再次执行check_http检查

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_http  -H  localhost  -p  80
  2. HTTP OK: HTTP/1.1 200 OK - 302 bytes in 0.006 second response time |time=0.006236s;;;0.000000 size=302B;;;0

步骤二:使用插件check_tcp检测本机的FTP服务端口

1)未启动FTP服务时,执行check_ftp检查

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_ftp -H localhost -p 21
  2. connect to address localhost and port 21: 拒绝连接

2)安装及启动vsftpd服务以后,再次执行check_ftp检查

  1. [root@svr7 ~]# yum  -y  install  vsftpd
  2. [root@svr7 ~]# systemctl  restart  vsftpd
  3. [root@svr7 ~]# /usr/local/nagios/libexec/check_ftp -H localhost -p 21
  4. FTP OK - 0.002 second response time on localhost port 21 [220 (vsFTPd 3.0.2)]|time=0.002164s;;;0.000000;10.000000

步骤三:使用插件check_disk检测本机/boot分区使用率

1)查看/boot目录所在分区的使用率

  1. [root@svr7 ~]# df  -hT  /boot
  2. 文件系统       类型  容量  已用  可用 已用% 挂载点
  3. /dev/vda1      xfs   497M  140M  358M   29% /boot

2)使用check_disk检查

可用空间低于80%报 WARNING,可用空间低于60%报 CRITICAL

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_disk  -w  80%  -c  60%  -p  /boot
  2. DISK WARNING - free space: /boot 357 MB (71% inode=99%);| /boot=139MB;99;198;0;496

3)查看实际使用率情况,调低阈值再次check_disk测试

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_disk  -w  60%  -c  40%  -p  /boot
  2. DISK OK - free space: /boot 357 MB (71% inode=99%);| /boot=139MB;297;198;0;496

 

 案例4:监控远程主机的公有数据

4.1 问题

本案例要求在Nagios平台上添加对远程主机pc207的监控,主要包括下列服务:

  1. ping连通状态
  2. Web服务
  3. FTP服务
  4. SSH服务

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备被监控主机pc207

1)正确配置IP地址,确保与主机svr7可相互ping通

  1. [root@pc207 ~]# ping  192.168.4.7
  2. PING 192.168.4.7 (192.168.4.7) 56(84) bytes of data.
  3. 64 bytes from 192.168.4.7: icmp_seq=1 ttl=64 time=0.621 ms
  4. 64 bytes from 192.168.4.7: icmp_seq=2 ttl=64 time=0.427 ms
  5. .. ..

2)确保已启用httpd、vsftpd、sshd服务,并关闭防火墙服务firewalld

  1. [root@pc207 ~]# yum  -y  install  httpd  vsftpd
  2. .. ..
  3. [root@pc207 ~]# systemctl  restart  httpd  vsftpd  sshd
  4. [root@pc207 ~]# systemctl  disable  firewalld  --now

步骤二:配置监控服务器svr7

1)新建监控配置pc207.cfg,定义监控目标(主机、服务)

  1. [root@svr7 ~]# vim  /usr/local/nagios/etc/objects/pc207.cfg
  2. define host {
  3. use    linux-server
  4. host_name   pc207
  5. address  172.25.0.10
  6. }
  7. define service {
  8. use    local-service
  9. host_name   pc207
  10. service_description     PING
  11. check_command         check_ping!100.0,20%!500.0,60%
  12. }
  13. define service  {
  14. use    local-service
  15. host_name   pc207
  16. service_description          Web Server
  17. check_command               check_http
  18. }
  19. define service  {
  20. use    local-service
  21. host_name   pc207
  22. service_description           FTP Server
  23. check_command               check_ftp
  24. }
  25. define service  {
  26. use    local-service
  27. host_name   pc207
  28. service_description           SSH Server
  29. check_command               check_ssh
  30. }

2)调整nagios.cfg,启用pc207.cfg监控配置

  1. [root@svr7 ~]# vim /usr/local/nagios/etc/nagios.cfg
  2. .. ..
  3. cfg_file=/usr/local/nagios/etc/objects/pc207.cfg

3)重启nagios服务

  1. [root@svr7 ~]# systemctl  restart  nagios

步骤三:刷新Nagios管理界面,确认新增主机pc207的监控结果

1)确认Hosts下新增的主机,如图-2所示

 

-2

2)确认Services下新增的服务信息,如图-3所示

 

-3


 

案例5:监控远程主机的私有数据

5.1 问题

本案例要求在Nagios服务器上添加对远程主机pc207上私有数据的监控,完成以下监控项目:

  1. 运行的进程数
  2. 登录的用户数
  3. CPU负载
  4. 根分区使用量

5.2 方案

Linux主机的内存信息、磁盘I/O信息等私有数据默认不对外提供,若希望从Nagios监控平台获取远程主机的私有数据,需要在被监控主机上配置启用nrpe服务。

主要工作结构:Nagios监控端(check_nrpe插件) ==> 被监控端(nrpe服务),如图-4所示。

 

-4

5.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备被监控主机pc207

1)安装nagios-plugins插件

  1. [root@pc207 ~]# yum  -y  install  gcc  glibc  glibc-common
  2. [root@pc207 ~]# groupadd  nagcmd
  3. [root@pc207 ~]# useradd  -G  nagcmd  nagios
  4. [root@pc207 ~]# tar  xf  ~/nagios-plugins-2.1.4.tar.gz  -C  /usr/src/
  5. [root@pc207 ~]# cd  /usr/src/nagios-plugins-2.1.4/
  6. [root@pc207 nagios-plugins-2.1.4]# ./configure  --with-nagios-user=nagios  --with-nagios-group=nagcmd
  7. [root@pc207 nagios-plugins-2.1.4]# make
  8. [root@pc207 nagios-plugins-2.1.4]# make install

2)安装nrpe

  1. [root@pc207 ~]# yum  -y  install  openssl-devel          //安装SSL头文件
  2. [root@pc207 ~]# tar  xf  nrpe-3.1.0.tar.gz  -C  /usr/src/
  3. [root@pc207 ~]# cd  /usr/src/nrpe-3.1.0/
  4. [root@pc207 nrpe-3.1.0]# ./configure
  5. [root@pc207 nrpe-3.1.0]# make  all                      //编译所有
  6. [root@pc207 nrpe-3.1.0]# make  install                     //安装nrpe及插件
  7. [root@pc207 nrpe-3.1.0]# make  install-init              //安装服务
  8. [root@pc207 nrpe-3.1.0]# make  install-config              //安装配置文件

3)定义nrpe监控命令

  1. [root@pc207 ~]# vim  /usr/local/nagios/etc/nrpe.cfg
  2. .. ..
  3. allowed_hosts=172.25.0.0/24,127.0.0.1,::1              //允许哪些主机
  4. command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
  5. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
  6. command[check_rootdir]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p  /
  7. command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 -s Z
  8. command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

4)启用nrpe服务并测试

  1. [root@pc207 ~]# systemctl  restart  nrpe
  2. [root@pc207 ~]# systemctl  enable  nrpe
  3. .. ..
  4. [root@pc207 ~]# /usr/local/nagios/libexec/check_nrpe   -H  localhost
  5. NRPE v3.1.0-rc1              //测试连本机nrpe服务

步骤二:配置Nagios监控服务器

1)为Nagios安装check_nrpe插件

  1. [root@svr7 ~]# yum  -y  install  openssl-devel
  2. [root@svr7 ~]# tar  xf  nrpe-3.1.0.tar.gz  -C  /usr/src/
  3. [root@svr7 ~]# cd  /usr/src/nrpe-3.1.0/
  4. [root@svr7 nrpe-3.1.0]# ./configure
  5. [root@svr7 nrpe-3.1.0]# make  all                  //编译所有
  6. [root@svr7 nrpe-3.1.0]# make  install-plugin         //安装check_nrpe插件

2)测试连远程的nrpe服务,并添加远程nrpe监控指令

  1. [root@svr7 ~]# /usr/local/nagios/libexec/check_nrpe  -H  172.25.0.10
  2. NRPE v3.1.0-rc1                                  //测试连远程 的nrpe服务
  3. [root@svr7 ~]# vim  /usr/local/nagios/etc/objects/commands.cfg
  4. .. ..
  5. define command{
  6. command_name   check_nrpe
  7. command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  8. //-c 要求对方执行的命令
  9. }

3)修改监控目标配置pc207.cfg,添加对远程主机pc207私有数据的获取

  1. [root@svr7 ~]# vim  /usr/local/nagios/etc/objects/pc207.cfg
  2. .. ..
  3. define service {
  4. use    local-service
  5. host_name    pc207
  6. service_description    NRPE-Total Procs
  7. check_command    check_nrpe!check_total_procs
  8. }
  9. define service {
  10. use    local-service
  11. host_name    pc207
  12. service_description    NRPE-Users
  13. check_command    check_nrpe!check_users
  14. }

步骤三:刷新Nagios管理界面,确认主机pc207的私有数据监控结果

确认pc207主机及服务监控结果,如图-5所示。

                                                                                       

-5

 

原文地址:https://www.cnblogs.com/qingbai/p/11949829.html