Linux監控磁盤空間並發送郵件


1.安裝mailx工具,mailx是一個小型的郵件發送程序

yum install mailx

 

2.編輯配置文件

vim /etc/mail.rc   #添加如下內容

 

情況一:25端口開放情況

set from=6247***@qq.com                 #對方收到郵件時顯示的發件人
set smtp=smtp.qq.com       #指定第三方發郵件的smtp服務器地址,如:smtp.qq.com
set smtp-auth-user=6247***@qq.com     #第三方發郵件的用戶名
set smtp-auth-password=*****  #用戶名對應的密碼,QQ郵箱可以使用授權碼
set smtp-auth=login                           #SMTP的認證方式,默認是login

 

情況二:25端口不開放

set from=62475***@qq.com                 
set smtp=smtp.qq.com       
set smtp-auth-user=6247***@qq.com     
set smtp-auth-password=*****  
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs   #制定的存放QQ郵箱SSL證書的位置。

 

保存上述的編輯退出。

因為需要QQ郵箱的SSL證書,所以我們還需要手動的獲取QQ郵箱的證書保存到本地指定的目錄里以備調用和驗證,具體命令如下:

mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -L -d /root/.certs

 

還需要進入郵箱SSL證書存放目錄/root/.certs里執行如下命令:

certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

 

返回如下提示即可:

**Notice: Trust flag u is set automatically if the private key is present.**

 

這是為了信任證書的標記操作。

3.測試發送郵件

echo "測試郵件" | mail -s "測試" 6247***@qq.com

4.准備Shell腳本

#!/bin/sh
email="6247***@qq.com"     #接收郵件的郵箱
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 90 ]; then            #預警界限,使用的百分比
  echo "$(hostname) 上的分區:\"$partition\" 已使用 $used%  $(date)" | mail -s "磁盤空間警報: $(hostname) 已使用 $used% " $email      #echo后邊為正文,mail -s后邊為主題
  fi
done

 

在windows下編輯的shell,到linux報錯,執行

sed -i 's/\r$//' shell.sh

 

5.准備定時任務

[app@DZWL-APP-SVR /home/app]$  crontab -e
00 10 * * * /bin/sh /home/app/backup/disk.sh

 

使用定時任務定時執行腳本以達到監控磁盤空間預警發送郵件的功能。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM