Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux 伺服器

五個實用的 Linux 監控腳本:流量、系統狀態、磁碟、CPU 與全方位採集

開源社群裡流傳的一些小腳本,寫法樸素但很有效。以下五個分別對應不同監控需求,可依環境調整後使用。

一、查看網卡即時流量

#!/bin/bash
#network
while : ; do
time='date +%m"-"%d" "%k":"%M'
day='date +%m"-"%d'
rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
sleep 2
rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
rx_result=$[(rx_after-rx_before)/256]
tx_result=$[(tx_after-tx_before)/256]
echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
sleep 2
done

原理是取兩次 ifconfig 的累計位元組數相減,除以 256 換算成 kbps。注意它依賴 ifconfig 的固定行號與欄位,不同版本輸出格式有差異時需自行調整,改用 /proc/net/dev 會更穩。

二、系統狀況採集

#!/bin/sh
#systemstat.sh
IP=192.168.1.227
top -n 2| grep "Cpu" >>./temp/cpu.txt
free -m | grep "Mem" >> ./temp/mem.txt
df -k | grep "sda1" >> ./temp/drive_sda1.txt
df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt
df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt
time=`date +%m"."%d" "%k":"%M`
connect=`netstat -na | grep "219.238.148.30:80" | wc -l`
echo "$time $connect" >> ./temp/connect_count.txt

把 CPU、記憶體、各掛載點與連線數逐次追加到檔案,後續就能畫成趨勢圖。top -n 2 取第二次是因為第一次的 CPU 數據是估算值。

三、磁碟空間告警

#!/bin/bash
#monitor available disk space
SPACE='df | sed -n '/ \/ $/p' | gawk '{print $5}' | sed 's/%//'
if [ $SPACE -ge 90 ]
then
   echo "Disk usage ${SPACE}% on `hostname`" | mail -s "警告" jbxue123@163.com
fi

使用率超過 90% 就發信通知。實務上要留意兩點:一是應該用 df -P 避免長裝置名導致換行錯位;二是最好加上「已告警」標記,否則每分鐘跑一次 crontab 會寄出大量重複信。

四、CPU 與記憶體使用情況

#!/bin/bash
#script to capture system statistics
OUTFILE=/home/xu/capstats.csv
DATE='date +%m/%d/%Y'
TIME='date +%k:%m:%s'
TIMEOUT='uptime'
VMOUT='vmstat 1 2'
USERS='echo $TIMEOUT | gawk '{print $4}'
LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//"
FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4}'
IDLE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}'
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE

輸出成 CSV,可直接丟進試算表或圖表工具。vmstat 1 2 取第二次抽樣,同樣是為了避開第一筆平均值的雜訊。

五、全方位監控

#!/bin/bash
# check_xu.sh
# 0 * * * * /home/check_xu.sh
DAT="`date +%Y%m%d`"
HOUR="`date +%H`"
DIR="/home/oslog/host_${DAT}/${HOUR}"
DELAY=60
COUNT=60

if ! test -d ${DIR}
then
    /bin/mkdir -p ${DIR}
fi

export TERM=linux
/usr/bin/top    -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log    2>&1 &
/usr/bin/sar    -u ${DELAY} ${COUNT}      > ${DIR}/cpu_${DAT}.log    2>&1 &
/usr/bin/vmstat ${DELAY} ${COUNT}         > ${DIR}/vmstat_${DAT}.log 2>&1 &
/usr/bin/iostat ${DELAY} ${COUNT}         > ${DIR}/iostat_${DAT}.log 2>&1 &
/usr/bin/sar    -n DEV ${DELAY} ${COUNT}  > ${DIR}/net_${DAT}.log    2>&1 &

掛進 crontab 每小時執行:

0 * * * * /home/check_xu.sh

就會在 /home/oslog/host_yyyymmdd/hh 下按小時生成 CPU、記憶體、網路與 IO 的統計資料。某個時段出問題時,直接翻對應的日誌,就能回溯當時主機的負載狀況——這比事後靠猜有效太多。

使用提醒

這些腳本大量使用 ifconfig、netstat 等已逐步被 ip、ss 取代的指令,在新型發行版上可能預設沒有安裝。上線前建議逐行驗證輸出,並為 /home/oslog 這類日誌目錄安排輪替,避免監控本身把磁碟寫滿。

Copyright © Linux教程網 All Rights Reserved