上一篇《Linux下Nagios的安裝與配置 及遇到的坑 》
Nagios 自帶的包里沒有直接檢查硬盤 I/O 的包: check_iostat,不過可以到官網上下載一個,下載頁面是:
http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_iostat--2D-I-2FO-statistics/details
下載完后直接上傳到監控端和被監控端的的:/usr/local/nagios/libexec/ 目錄。
給它執行權限:
chmod +x check_iostat
查看它的幫助:
[root@localhost libexec]# ./check_iostat -help
This plugin shows the I/O usage of the specified disk, using the iostat external program.
It prints three statistics: Transactions per second (tps), Kilobytes per second
read from the disk (KB_read/s) and and written to the disk (KB_written/s)
./check_iostat:
-d Device to be checked (without the full path, eg. sda)
-c ,, Sets the CRITICAL level for tps, KB_read/s and KB_written/s, respectively
-w ,, Sets the WARNING level for tps, KB_read/s and KB_written/s, respectively
可以看到,它是用來檢查硬盤上每秒數據寫入讀取的。
參數分別是:
- -d: 要檢查的設備名稱,不用寫全路徑
- -c: 當達到多少 KB/S 時就報 CRITICAL 級別的警
- -w: 當達到多少 KB/S 時就報 WARNING 級別的警
查看本機的硬盤信息:
[root@localhost libexec]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
128G 27G 95G 22% /
/dev/sda1 99M 13M 82M 14% /boot
tmpfs 4.0G 0 4.0G 0% /dev/shm
上面的信息是 sda1, 那么 -d 后就寫 sda
另外,還有可能不是 sda 的,如:
[root@li387-161 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda 79G 38G 40G 49% /
tmpfs 1009M 108K 1009M 1% /dev/shm
上面的情況,-d 后就寫 xvda
檢查是否能運行:
[root@localhost libexec]# ./check_iostat -d sda -w 1000 -c 2000
//輸出 OK - I/O stats tps=1.71 KB_read/s=2.77 KB_written/s=26.77 | 'tps'=1.71; 'KB_read/s'=2.77; 'KB_written/s'=26.77;
如果不能運行,報錯,先在本機安裝 sysstat:
[root@localhost libexec]# yum install sysstat
如果還報錯,那就根據報錯的信息一步步解決.
比如我這邊報過: bc: command not found ;
解決:
yum install bc
直到上面的 check_iostat 能正確執行,繼續配置
Nagios 配置
監控本地
在 commands.cfg 中添加 check_iostat
define command{
command_name check_iostat
command_line $USER1$/check_iostat -d $ARG1$ -w $ARG2$ -c $ARG3$
}
定義了 check_iostat 命令,且接收三個參數.
更改本地配置文件.假如叫: localhost.cfg
在里面定義一個服務:
define service{
use local-service ; Name of service template to use
host_name VOD-106 ;服務器名
service_description Disk I/O ; 描述,盡可能不要用中文
check_period 24x7 ; The service can be checked at any time of the day
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its final (hard) state
normal_check_interval 2 ; Check the service every 10 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every two minutes until a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group
notification_options w,u,c,r,f ; Send notifications about warning, unknown, critical, and recovery events
notification_interval 1 ;
notification_period 24x7 ; Notifications can be sent out at any time
check_command check_iostat!sda!1000!2000
}
check_iostat!sda!1000!2000
// 上面共有三個參數: sda, 1000, 2000 分別對應前面 commonds.cfg 中的三個參數.
重新加載配置文件:
service nagios reload
監控遠程:
在監控端,修改遠程服務器的配置文件.比如: hosts.cfg 文件對應主機的 services.cfg 文件內修改(例如在hosts.cfg,對應3臺主機,其中一臺名:test.local,那就需要在services.cfg中增加配置如下)
定義命令:
define service{
use generic-service ; Name of service template to use
host_name test.local ; 主機名
service_description Disk I/O
check_command check_nrpe!check_iostat
}
由于它是通過 check_nrpe 調用遠程服務器上的命令.我們要在遠程服務器上執行的命令就是這里 check_nrpe 命令的參數,即感嘆號后的那個: check_iostat
所以要確保被監控的機器上有 check_iostat 這個命令.安裝方式和前面一樣.
同時保證 check_nrpe 能順利調用遠程機器.可以通過命令嘗試:
[root@localhost libexec]# ./check_nrpe -H 111.111.44.111
NRPE v2.13
然后更改被監控機器上的 /usr/local/nagios/etc/nrpe.cfg
添加命令:
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -d sda -w 1000 -c 2000
重啟被監控端的服務:
service xinetd restart
至此從監控主機上可以看到遠程/本地磁盤讀寫信息
監控指定端口
修改被監控主機 /usr/local/nagios/etc/objects/commands.cfg 添加一個服務名
# check port 4000
define command{
command_name Port_80 ; 命令名,后期在監控主機中需要用到
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 4000 $ARG2$
}
在監控主機中 services.cfg 中增加監控服務
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use local-service ; Name of service template to use
host_name test.local ; 監控的主機名,需要和hosts.cfg對應
service_description 80 ; 描述
check_command Port_80 ; 命令,被監控主機定義的
is_volatile 0
check_period 24x7
max_check_attempts 2
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
}
重啟nagios
# service nagios restart
刷新頁面如下