linux上監控磁盤使用情況併發送郵件

1.設置外部郵箱

set [email protected]

set smtp=smtp.163.com

set [email protected]

set smtp-auth-password=密碼

set smtp-auth=login


2.編寫監控腳本

#!/bin/bash

# 預警值

use_ratio=20

df_info="/home/monitor/df_info.txt"

task_status_file="/home/monitor/task_status.txt"


# 獲取當前IP

ip=`ifconfig em1 | grep inet | grep netmask | awk '{print $2}'`


for value in `df -h | awk '{print $5}' | cut -f 1 -d %`

do

if [ $value -gt $use_ratio ]; then

df -h >> $df_info

if [ ! -f $task_status_file ];then

echo -e "服務器$ip磁盤使用超出預警值\n `cat $df_info` " | mail -s "服務器使用報警" 收件人郵箱地址;

echo "unfinish" > $task_status_file;

exit 0;

fi

fi

done


3.說明

查看使用率

df -h


獲取具體使用率的值

方式一:

df -P | grep /home | awk '{print $5}' | cut -f 1 -d %


方式二:

df -P | grep /home | awk '{print $5}' | sed 's/%//g'


獲取本機IP

方式一:

/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"


方式二:

ifconfig em1 | grep "inet" | grep netmask | awk '{print $2}'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章