案例1:經常使用系統監控命令 案例2:搭建nagios監控服務器 案例3:配置文件及插件使用
1 案例1:經常使用系統監控命令
1.1 問題php
本案例要求練習經常使用的系統監控命令完成如下任務:html
使用vmstat命令監控內存及磁盤I/O信息 使用iostat命令監控CPU處理器及磁盤的I/O信息 使用sar命令監控系統最近的CPU/內存等活動
1.2 步驟node
實現此案例須要按照以下步驟進行。ios
步驟一:使用vmstat命令監控內存及磁盤I/O信息web
1)報告內存、交換分區、CPU等活動信息,每2秒更新一次數據apache
[root@svr7 ~]# vmstat 2 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 1447960 1204 331260 0 0 422 9 59 153 1 1 98 0 0 0 0 0 1447944 1204 331260 0 0 0 0 9 12 0 0 100 0 0 0 0 0 1447944 1204 331260 0 0 0 0 9 12 0 .. ..
2)報告一次磁盤的I/O信息vim
[root@svr7 ~]# vmstat -d disk- ------------reads------------ ------------writes----------- -----IO------ total merged sectors ms total merged sectors ms cur sec vda 7863 12 576137 2560 1427 25 12085 6142 0 3 sr0 11 0 88 3 0 0 0 0 0 0 dm-0 6529 0 562281 2503 356 0 7989 6480 0 3 dm-1 141 0 2536 7 0 0 0 0 0 0
3)報告一次指定分區的I/O信息服務器
[root@svr7 ~]# vmstat -p /dev/vda1 vda1 reads read sectors writes requested writes 1010 8792 1025 4096
步驟二:使用iostat命令監控CPU處理器及磁盤的I/O信息socket
1)報告一次CPU和磁盤I/O信息tcp
[root@svr7 ~]# iostat Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn) 2017年05月03日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.77 0.00 0.71 0.05 0.00 98.47 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn vda 9.85 304.60 6.45 288160 6103 scd0 0.01 0.05 0.00 44 0 dm-0 7.30 297.28 4.29 281232 4055 dm-1 0.15 1.34 0.00 1268 0
2)報告CPU和磁盤I/O信息,每2秒鐘採樣一次
[root@svr7 ~]# iostat 2 Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn) 2017年05月03日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.66 0.00 0.61 0.05 0.00 98.68 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn vda 8.42 259.61 5.62 288168 6235 scd0 0.01 0.04 0.00 44 0 dm-0 6.25 253.37 3.77 281240 4187 dm-1 0.13 1.14 0.00 1268 0 avg-cpu: %user %nice %system %iowait %steal %idle 0.00 0.00 0.00 0.00 0.00 100.00 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn vda 0.00 0.00 0.00 0 0 scd0 0.00 0.00 0.00 0 0 dm-0 0.00 0.00 0.00 0 0 dm-1 0.00 0.00 0.00 0 0 .. ..
步驟三:使用sar命令監控系統最近的CPU/內存等活動
1)查看一次最近的系統活動完整(-A)報告
[root@svr7 ~]# sar -A Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn) 2017年05月03日 _x86_64_ (1 CPU) 11時20分01秒 CPU %usr %nice %sys %iowait %steal %irq %soft %guest %gnice %idle 11時30分01秒 all 0.17 0.00 0.11 0.01 0.00 0.00 0.00 0.00 0.00 99.71 11時30分01秒 0 0.17 0.00 0.11 0.01 0.00 0.00 0.00 0.00 0.00 99.71 平均時間: all 0.17 0.00 0.11 0.01 0.00 0.00 0.00 0.00 0.00 99.71 平均時間: 0 0.17 0.00 0.11 0.01 0.00 0.00 0.00 0.00 0.00 99.71 11時20分01秒 proc/s cswch/s 11時30分01秒 0.39 23.27 平均時間: 0.39 23.27 11時20分01秒 pswpin/s pswpout/s 11時30分01秒 0.00 0.00 平均時間: 0.00 0.00 11時20分01秒 pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff 11時30分01秒 6.30 0.35 165.40 0.03 133.94 0.00 0.00 0.00 0.00 平均時間: 6.30 0.35 165.40 0.03 133.94 0.00 0.00 0.00 0.00 11時20分01秒 tps rtps wtps bread/s bwrtn/s 11時30分01秒 0.38 0.27 0.11 12.59 0.70 平均時間: 0.38 0.27 0.11 12.59 0.70 11時20分01秒 frmpg/s bufpg/s campg/s 11時30分01秒 -4.90 0.00 1.37 平均時間: -4.90 0.00 1.37 11時20分01秒 kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 11時30分01秒 1444844 604264 29.49 1204 277020 1547884 37.33 249044 241260 0 平均時間: 1444844 604264 29.49 1204 277020 1547884 37.33 249044 241260 0 11時20分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 11時30分01秒 2097148 0 0.00 0 0.00 平均時間: 2097148 0 0.00 0 0.00 .. ..
2)報告CPU活動信息,以5秒爲間隔再採用3次
[root@svr7 ~]# sar -u 5 3 Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn) 2017年05月03日 _x86_64_ (1 CPU) 11時44分20秒 CPU %user %nice %system %iowait %steal %idle 11時44分25秒 all 0.00 0.00 0.00 0.00 0.00 100.00 11時44分30秒 all 0.00 0.00 0.00 0.00 0.00 100.00 11時44分35秒 all 0.00 0.00 0.00 0.00 0.00 100.00 平均時間: all 0.00 0.00 0.00 0.00 0.00 100.00
3)報告最近的磁盤I/O信息
[root@svr7 ~]# sar -b Linux 3.10.0-327.el7.x86_64 (svr7.tedu.cn) 2017年05月03日 _x86_64_ (1 CPU) .. .. 11時20分01秒 tps rtps wtps bread/s bwrtn/s 11時30分01秒 0.38 0.27 0.11 12.59 0.70 11時40分01秒 0.25 0.13 0.12 15.62 0.93 平均時間: 0.31 0.20 0.11 14.11 0.81
2 案例2:搭建nagios監控服務器
2.1 問題
本案例要求搭建一臺nagios監控服務器,確認Web界面可用:
安裝nagios、nagios-plugins軟件包 配置及訪問web界面,將管理用戶/密碼均設爲 nagiosadmin
2.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:安裝前的準備工做
1)安裝網站平臺環境、編譯工具
[root@svr7 ~]# yum -y install httpd php gd [root@svr7 ~]# yum -y install gcc glibc glibc-common
2)建立運行帳號
[root@svr7 ~]# groupadd nagcmd [root@svr7 ~]# useradd -G nagcmd nagios [root@svr7 ~]# gpasswd -a apache nagcmd
步驟二:編譯安裝nagios及nagios-plugins
1)安裝nagios
[root@svr7 ~]# tar xf ~/nagios-4.3.1.tar.gz -C /usr/src/ [root@svr7 ~]# cd /usr/src/nagios-4.3.1/ [root@svr7 nagios-4.3.1]# ./configure --with-command-group=nagcmd [root@svr7 nagios-4.3.1]# make all //編譯 [root@svr7 nagios-4.3.1]# make install //安裝程序 [root@svr7 nagios-4.3.1]# make install-init //安裝控制腳本 [root@svr7 nagios-4.3.1]# make install-config //安裝配置 [root@svr7 nagios-4.3.1]# make install-commandmode //調權限 [root@svr7 nagios-4.3.1]# make install-webconf //部署網站配置 [root@svr7 nagios-4.3.1]# chkconfig --add nagios //加載服務配置
2)安裝nagios-plugins
[root@svr7 ~]# tar xf ~/nagios-plugins-2.1.4.tar.gz -C /usr/src/ [root@svr7 ~]# cd /usr/src/nagios-plugins-2.1.4/ [root@svr7 nagios-plugins-2.1.4]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd [root@svr7 nagios-plugins-2.1.4]# make [root@svr7 nagios-plugins-2.1.4]# make install
步驟三:啓用及訪問Nagios平臺
1)爲Nagios平臺添加Web管理用戶
[root@svr7 ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //建Web管理用戶
2)啓用nagios、httpd服務
[root@svr7 ~]# systemctl restart nagios //啓用nagios [root@svr7 ~]# systemctl restart httpd //啓用Web
3)訪問nagios網頁界面
firefox http://服務器地址/nagios/,如圖-1所示。
圖-1
3 案例3:配置文件及插件使用
3.1 問題
本案例要求熟悉nagios的經常使用插件程序,完成如下任務:
使用插件 check_http 檢測本機的Web端口 使用插件 check_tcp 檢測本機的FTP服務端口 使用插件 check_disk 檢測本機根分區使用率:可用空間低於80%報 WARNING,可用空間低於60%報 CRITICAL
3.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:使用插件 check_http 檢測本機的Web端口
1)關閉本機的httpd服務,執行check_http檢查
[root@svr7 ~]# /usr/local/nagios/libexec/check_http -H localhost -p 80connect to address localhost and port 80: 拒絕鏈接 HTTP CRITICAL - Unable to open TCP socket
2)啓動本機的httpd服務,並創建默認首頁index.html
[root@svr7 ~]# vim /var/www/html/index.html Default site. [root@svr7 ~]# systemctl restart httpd
3)再次執行check_http檢查
[root@svr7 ~]# /usr/local/nagios/libexec/check_http -H localhost -p 80 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檢查
[root@svr7 ~]# /usr/local/nagios/libexec/check_ftp -H localhost -p 21 connect to address localhost and port 21: 拒絕鏈接
2)安裝及啓動vsftpd服務之後,再次執行check_ftp檢查
[root@svr7 ~]# yum -y install vsftpd [root@svr7 ~]# systemctl restart vsftpd [root@svr7 ~]# /usr/local/nagios/libexec/check_ftp -H localhost -p 21 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目錄所在分區的使用率
[root@svr7 ~]# df -hT /boot 文件系統 類型 容量 已用 可用 已用% 掛載點 /dev/vda1 xfs 497M 140M 358M 29% /boot
2)使用check_disk檢查
可用空間低於80%報 WARNING,可用空間低於60%報 CRITICAL
[root@svr7 ~]# /usr/local/nagios/libexec/check_disk -w 80% -c 60% -p /boot DISK WARNING - free space: /boot 357 MB (71% inode=99%);| /boot=139MB;99;198;0;496
3)查看實際使用率狀況,調低閾值再次check_disk測試
[root@svr7 ~]# /usr/local/nagios/libexec/check_disk -w 60% -c 40% -p /boot DISK OK - free space: /boot 357 MB (71% inode=99%);| /boot=139MB;297;198;0;496