?
前言
大家好,這里是浩道linux,主要給大家分享linux、python、網(wǎng)絡(luò)通信相關(guān)的IT知識(shí)平臺(tái)。
今天浩道跟大家分享硬核shell實(shí)戰(zhàn)干貨,利用10行shell腳本監(jiān)控你的linux操作系統(tǒng)!
監(jiān)控我們的環(huán)境對(duì)于服務(wù)器運(yùn)維來(lái)說(shuō)至關(guān)重要,尤其是在部署新的應(yīng)用程序時(shí)。如今,公司每天都使用開(kāi)源解決方案來(lái)監(jiān)控系統(tǒng)資源。但是,當(dāng)出于測(cè)試的目的來(lái)監(jiān)控一定時(shí)間時(shí),bash 腳本會(huì)派上用場(chǎng)。
在本教程中,我們將編寫(xiě)一個(gè) bash shell 腳本,它將輸出一個(gè)三列表,來(lái)顯示我們機(jī)器上的內(nèi)存、磁盤(pán)和 CPU 的百分比。
讓我們開(kāi)始吧!
該腳本基本上由三個(gè)主要部分組成:
1. 監(jiān)控內(nèi)存:
?
free?-m?|?awk?'NR==2{printf?"%.2f%% ",?$3*100/$2?}' 9.24%
?
free -m?是用于顯示已使用和空閑內(nèi)存的命令,輸出如下:
?
[root@localhost?tmp]#?free?-m ?????????????total???????used???????free?????shared????buffers?????cached Mem:???????????996?????????92????????904??????????0?????????11?????????31 -/+?buffers/cache:?????????49????????947 Swap:?????????1583??????????0???????1583
?
但是,我們需要從上面的輸出中獲取第二行的總內(nèi)存和已用內(nèi)存。我們可以使用 AWK 來(lái)從給定輸出中提取數(shù)據(jù)。
AWK?是一種用于文本處理和數(shù)據(jù)提取的編程語(yǔ)言。它是大多數(shù) UNIX 系統(tǒng)的標(biāo)準(zhǔn)功能。awk ‘NR==2’?從第二行提取數(shù)據(jù)。$3?和?$2?分別充當(dāng)已用量和總量。
監(jiān)控磁盤(pán)
?
df?-h?|?awk?'$NF=="/"{printf?"%s ",?$5}' 7%
?
第二個(gè)命令輸出使用的磁盤(pán)百分比。df -h?輸出與磁盤(pán)使用情況和分區(qū)相關(guān)的數(shù)據(jù)。
?
[root@localhost?tmp]#?df?-h Filesystem????????????????????Size??Used?Avail?Use%?Mounted?on /dev/mapper/VolGroup-lv_root???14G??814M???12G???7%?/ tmpfs?????????????????????????499M?????0??499M???0%?/dev/shm /dev/sda1?????????????????????485M???32M??428M???7%?/boot
?
awk $NF?輸出字段數(shù)。但是,df -h | awk '$NF=="/"?將轉(zhuǎn)到包含字符?/?的那一行。$5?將從該行中選擇第 5 個(gè)字段。這可確保該命令能夠提取正確的磁盤(pán)使用百分比(在我們的示例中為 %7)。
監(jiān)控 CPU
?
top?-bn1?|?grep?load?|?awk?'{printf?"%.2f%% ",?$(NF-2)}'
?
top -bn1?命令將只執(zhí)行一次?top?命令(n1?= 一次迭代),當(dāng)我們想在 bash 腳本中使用?top?或?qū)⑵鋽?shù)據(jù)輸出到文件時(shí)使用可以使用?-b?選項(xiàng)。
grep load?將輸出包含字符串?load?的行。$(NF-2)?將計(jì)算該行上的字段數(shù)并減 2。
?
[root@localhost?tmp]#?top?-bn1?|?grep?load top?-?1925?up??1:47,??1?user,??load?average:?0.00,?0.00,?0.00
?
在瀏覽完 bash 腳本的基本部分之后,我們需要將這些命令保存到變量?MEMORY、DISK?和?CPU?中:
?
MEMORY=$(free?-m?|?awk?'NR==2{printf?"%.2f%% ",?$3*100/$2?}') DISK=$(df?-h?|?awk?'$NF=="/"{printf?"%s ",?$5}') CPU=$(top?-bn1?|?grep?load?|?awk?'{printf?"%.2f%% ",?$(NF-2)}')
?
我們需要腳本運(yùn)行一段時(shí)間(比如,一個(gè)小時(shí))。為了做到這一點(diǎn),我們需要使用?while do?循環(huán),每次循環(huán)后延遲 x 秒(取決于您的測(cè)試):
?
end=$((SECONDS+3600)) while?[?$SECONDS?-lt?$end?];?do echo?"$MEMORY$DISK$CPU" sleep?5 done
?
為了在一定時(shí)間內(nèi)運(yùn)行一次循環(huán),我們可以定義一個(gè)變量?$end,它從 bash 腳本開(kāi)始的時(shí)間開(kāi)始計(jì)算當(dāng)前的秒數(shù),因此是?SECONDS,并在當(dāng)前秒數(shù)上加上一個(gè)數(shù)字。所以一個(gè)小時(shí)就是 3600 秒。
上述代碼片段的第二行指出,只要$SECONDS(當(dāng)前秒數(shù))小于?$SECONDS+3600,while?循環(huán)就會(huì)繼續(xù)執(zhí)行。因此,我們定義了循環(huán)的開(kāi)始時(shí)間和結(jié)束時(shí)間,以及將每個(gè)循環(huán)暫停 5 秒的睡眠時(shí)間。循環(huán)內(nèi)部是每 5 秒將那三個(gè)變量分配一次新值,以及將輸出三個(gè)資源使用情況的?echo "$MEMORY$DISK$CPU"。
完整代碼如下:
?
#!?/bin/bash printf?"Memory Disk CPU " end=$((SECONDS+3600)) while?[?$SECONDS?-lt?$end?];?do MEMORY=$(free?-m?|?awk?'NR==2{printf?"%.2f%% ",?$3*100/$2?}') DISK=$(df?-h?|?awk?'$NF=="/"{printf?"%s ",?$5}') CPU=$(top?-bn1?|?grep?load?|?awk?'{printf?"%.2f%% ",?$(NF-2)}') echo?"$MEMORY$DISK$CPU" sleep?5 done
?
上面的代碼將輸出以下內(nèi)容:
?
[root@localhost?tmp]#?./stats.sh Memory?Disk?CPU 9.34%?7%?0.00% 9.34%?7%?0.00% 9.34%?7%?0.00% 9.34%?7%?0.00% ^C[root@localhost?tmp]#
?
您始終可以將數(shù)據(jù)輸出到日志文件:
?
[root@localhost?tmp]#?./stats.sh?>>?log.txt
?
Stress 測(cè)試
由于機(jī)器上幾乎沒(méi)有任何負(fù)載,我們可以使用?stress?來(lái)使 CPU 和內(nèi)存負(fù)載一段時(shí)間。
使用以下命令安裝(在 CentOS 上):
?
[root@localhost?tmp]#?yum?install?stress Failed?to?set?locale,?defaulting?to?C Loaded?plugins:?fastestmirror Determining?fastest?mirrors epel/metalink???????????????????????????????????????????????????????????????|?4.2?kB?????00:00????? ?*?base:?ba.mirror.garr.it ?*?epel:?ftp.riken.jp ?*?extras:?centos.fastbull.org ?*?updates:?centos.fastbull.org base????????????????????????????????????????????????????????????????????????|?3.7?kB?????00:00????? epel????????????????????????????????????????????????????????????????????????|?4.3?kB?????00:00????? epel/primary_db?????????????????????????????????????????????????????????????|?5.9?MB?????00:37????? extras??????????????????????????????????????????????????????????????????????|?3.4?kB?????00:00????? extras/primary_db???????????????????????????????????????????????????????????|??37?kB?????00:00????? updates?????????????????????????????????????????????????????????????????????|?3.4?kB?????00:00????? updates/primary_db??????????????????????????????????????????????????????????|?5.2?MB?????00:30????? Setting?up?Install?Process Resolving?Dependencies -->?Running?transaction?check --->?Package?stress.x86_64?0:1.0.4-4.el6?will?be?installed -->?Finished?Dependency?Resolution Dependencies?Resolved =================================================================================================== ?Package???????????????Arch??????????????????Version?????????????????????Repository???????????Size =================================================================================================== Installing: ?stress????????????????x86_64????????????????1.0.4-4.el6?????????????????epel?????????????????36?k Transaction?Summary =================================================================================================== Install???????1?Package(s) Total?download?size:?36?k Installed?size:?89?k Is?this?ok?[y/N]:?y Downloading?Packages: stress-1.0.4-4.el6.x86_64.rpm???????????????????????????????????????????????|??36?kB?????00:01????? Running?rpm_check_debug Running?Transaction?Test Transaction?Test?Succeeded Running?Transaction ??Installing?:?stress-1.0.4-4.el6.x86_64???????????????????????????????????????????????????????1/1? ??Verifying??:?stress-1.0.4-4.el6.x86_64???????????????????????????????????????????????????????1/1? Installed: ??stress.x86_64?0:1.0.4-4.el6?????????????????????????????????????????????????????????????????????? Complete!
?
現(xiàn)在我們可以使用命令?stress?來(lái)負(fù)載我們的機(jī)器。例如,通過(guò)指定兩個(gè) CPU 密集型進(jìn)程、一個(gè) I/O 密集型進(jìn)程和一個(gè)內(nèi)存分配器進(jìn)程,對(duì)系統(tǒng)施加 4 個(gè)平均負(fù)載,如下所示。以下壓力測(cè)試將運(yùn)行 1 小時(shí)。
?
[root@localhost?tmp]#?stress?-c?2?-i?1?-m?1?--vm-bytes?128M?-t?3600s stress:?info:?[1574]?dispatching?hogs:?2?cpu,?1?io,?1?vm,?0?hdd stress:?info:?[1574]?successful?run?completed?in?3600s [root@localhost?tmp]#?./stats.sh? Memory??Disk??CPU 20.48%??7%??1.21%?? 20.48%??7%??1.02%?? 20.48%??7%??0.94%?? 21.89%??7%??1.18%?? 20.68%??7%??1.41%?? 22.09%??7%??1.62%?? 24.10%??7%??1.81%?? 24.90%??7%??1.98%?? 32.93%??7%??2.14%?? 30.32%??7%??2.29%?? 20.58%??7%??2.63%?? 27.91%??7%??2.82%?? 20.48%??7%??2.59%?? 20.48%??7%??2.38%?? 20.48%??7%??2.19%?? 20.48%??7%??2.02%?? 20.48%??7%??1.86%
?
審核編輯:湯梓紅
評(píng)論
查看更多