2019-05-20 15:15:54 4367瀏覽
今天千鋒扣丁學(xué)堂Linux培訓(xùn)老師給大家分享一篇關(guān)于解析Linux Shell腳本來監(jiān)控磁盤使用情況并發(fā)送郵件的詳細(xì)介紹,目前在市場(chǎng)上有很多用來監(jiān)控Linux系統(tǒng)的監(jiān)控工具,當(dāng)系統(tǒng)到達(dá)閥值后它將發(fā)送一封郵件。它監(jiān)控所有的東西例如CPU利用率、內(nèi)存利用率、交換空間利用率、磁盤空間利用率等等。然而,它更適合小環(huán)境和大環(huán)境。
# sh /opt/script/disk-usage-alert-old.sh /dev/mapper/vg_2g-lv_root test-script.sh: line 7: [: /dev/mapper/vg_2g-lv_root: integer expression expected / 9.8G
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_2g-lv_root 10G 6.7G 3.4G 67% / tmpfs 7.8G 0 7.8G 0% /dev/shm /dev/sda1 976M 95M 830M 11% /boot /dev/mapper/vg_2g-lv_home 5.0G 4.3G 784M 85% /home /dev/mapper/vg_2g-lv_tmp 4.8G 14M 4.6G 1% /tmp
# vi /opt/script/disk-usage-alert.sh #!/bin/sh df -Ph | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5,$1 }' | while read output; do echo $output used=$(echo $output | awk '{print $1}' | sed s/%//g) partition=$(echo $output | awk '{print $2}') if [ $used -ge 60 ]; then echo "The partition \"$partition\" on $(hostname) has used $used% at $(date)" | mail -s "Disk Space Alert: $used% Used On $(hostname)" [email protected] fi done
The partition "/dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Mon Apr 29 06:16:14 IST 2019 The partition "/dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Mon Apr 29 06:16:14 IST 2019
# crontab -e */10 * * * * /bin/bash /opt/script/disk-usage-alert.sh
# vi /opt/script/disk-usage-alert-1.sh #!/bin/sh df -Ph | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5,$1 }' | while read output; do max=60% echo $output used=$(echo $output | awk '{print $1}') partition=$(echo $output | awk '{print $2}') if [ ${used%?} -ge ${max%?} ]; then echo "The partition \"$partition\" on $(hostname) has used $used at $(date)" | mail -s "Disk Space Alert: $used Used On $(hostname)" [email protected] fi done
The partition "/dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Mon Apr 29 06:16:14 IST 2019 The partition "/dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Mon Apr 29 06:16:14 IST 2019
# crontab -e */10 * * * * /bin/bash /opt/script/disk-usage-alert-1.sh
*/10 * * * * df -Ph | sed s/%//g | awk '{ if($5 > 60) print $0;}' | mail -s "Disk Space Alert On $(hostname)" [email protected]
Filesystem Size Used Avail Use Mounted on /dev/mapper/vg_2g-lv_root 10G 6.7G 3.4G 67 / /dev/mapper/vg_2g-lv_home 5.0G 4.3G 784M 85 /home
# vi /opt/script/disk-usage-alert-2.sh #!/bin/bash used=$(df -Ph | grep '/dev/mapper/vg_2g-lv_dbs' | awk {'print $5'}) max=80% if [ ${used%?} -ge ${max%?} ]; then echo "The Mount Point "/DB" on $(hostname) has used $used at $(date)" | mail -s "Disk space alert on $(hostname): $used used" [email protected] fi
The partition /dev/mapper/vg_2g-lv_dbs on 2g.CentOS6 has used 82% at Mon Apr 29 06:16:14 IST 2019
# crontab -e */10 * * * * /bin/bash /opt/script/disk-usage-alert-2.sh
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】
查看更多關(guān)于“Linux培訓(xùn)資訊”的相關(guān)文章>>