Shell程式設計---監控(本地主機)磁碟使用情況並郵件報警
阿新 • • 發佈:2018-11-13
要求:監控(本地主機)磁碟使用情況並郵件報警
分析:
- 任何一個分割槽使用到80%就傳送一個郵件幾個人通知提醒他們磁碟的使用情況;
- 任何一個分割槽使用到90%以上就在郵件主題給出警告(warning);
- Linux伺服器上傳送郵件程式我們使用sendmail。
解答:
第1步: 安裝並啟動sendmail服務並測試
[[email protected] ~]# yum install -y sendmail mailx
[[email protected] ~]# /bin/systemctl stop sendmail.service
[ [email protected] ~]# ps -ef |grep sendmail
[[email protected] ~]# netstat -lntup |grep 25
[[email protected] ~]# lsof -i:25
[[email protected] ~]# echo "This is test mail" | mail -s 'Test mail' [email protected]
第2步:測試檢視磁碟使用情況的命令
方法1:
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 40G 13G 26G 33% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs 1.8G 428K 1.8G 1% /run
tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
tmpfs 354M 0 354M 0% /run/user/0
tmpfs 354M 0 354M 0% /run/user/1004
[ [email protected] ~]# df -h|grep -o [0-9]*%
%
33%
0%
0%
1%
0%
0%
0%
[[email protected] ~]# df -h|grep -o [0-9]*%|grep -o '[0-9]\+'
33
0
0
1
0
0
0
方法2:
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/xvda1 41152832 12756544 26299188 33% /
devtmpfs 1931300 0 1931300 0% /dev
tmpfs 1809252 0 1809252 0% /dev/shm
tmpfs 1809252 456 1808796 1% /run
tmpfs 1809252 0 1809252 0% /sys/fs/cgroup
tmpfs 361852 0 361852 0% /run/user/0
tmpfs 361852 0 361852 0% /run/user/1004
[ [email protected] ~]# df |sed 1d
/dev/xvda1 41152832 12756476 26299256 33% /
devtmpfs 1931300 0 1931300 0% /dev
tmpfs 1809252 0 1809252 0% /dev/shm
tmpfs 1809252 456 1808796 1% /run
tmpfs 1809252 0 1809252 0% /sys/fs/cgroup
tmpfs 361852 0 361852 0% /run/user/0
tmpfs 361852 0 361852 0% /run/user/1004
[[email protected] ~]# df |sed 1d | awk '{print $1,$5}'
/dev/xvda1 33%
devtmpfs 0%
tmpfs 0%
tmpfs 1%
tmpfs 0%
tmpfs 0%
tmpfs 0%
[[email protected] ~]# df |sed 1d | awk '{print $1,$5}'|tr -d %[[email protected] ~]# df -TPh
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext3 40G 13G 26G 33% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 1.8G 456K 1.8G 1% /run
tmpfs tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
tmpfs tmpfs 354M 0 354M 0% /run/user/0
tmpfs tmpfs 354M 0 354M 0% /run/user/1004
[[email protected] ~]# df -TPh|awk '{print $6}'
Use%
33%
0%
0%
1%
0%
0%
0%
[[email protected] ~]# df -TPh|awk '{print +$6}'
0
33
0
0
1
0
0
0
方法3:
[[email protected] ~]# df -TPh
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext3 40G 13G 26G 33% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 1.8G 456K 1.8G 1% /run
tmpfs tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
tmpfs tmpfs 354M 0 354M 0% /run/user/0
tmpfs tmpfs 354M 0 354M 0% /run/user/1004
[[email protected] ~]# df -TPh|awk '{print $6}'
Use%
33%
0%
0%
1%
0%
0%
0%
[[email protected] ~]# df -TPh|awk '{print +$6}'
0
33
0
0
1
0
0
0
#注意$6前的+號,使用算術運算子進行操作時,運算元自動轉換為數值
方法4:
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext3 40G 13G 26G 33% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 1.8G 456K 1.8G 1% /run
tmpfs tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
tmpfs tmpfs 354M 0 354M 0% /run/user/0
tmpfs tmpfs 354M 0 354M 0% /run/user/1004
[[email protected] ~]# df -Th|awk -F '[ |%]+' 'NR>1 { print $1 " " $6}'
/dev/xvda1 33
devtmpfs 0
tmpfs 0
tmpfs 1
tmpfs 0
tmpfs 0
tmpfs 0
第3步:編寫監控指令碼
#!/bin/sh
source /etc/profile
#define variable
usedList=`df -h|grep -o [0-9]*%|grep -o '[0-9]\+'`
emailArray=('[email protected]' '[email protected]')
happenTime=`date "+%Y-%m-%d %H:%M:%S" `
for i in ${usedList}
do
if [ ${i} -ge 80 -a ${i} -le 90 ];then
for email in ${emailArray[*]}
do
echo "NOTICE: Disk space for your server, already used ${i}%, ${happenTime} " | mail -s 'Disk Space Notice' ${email}
done
elif [ ${i} -gt 90 ];then
for email in ${emailArray[*]}
do
echo "WARNING: Low disk space for your server, already used ${i}%, ${happenTime} " | mail -s 'Disk Space Warning' ${email}
done
fi
done