代码如下:
#!/bin/bash
echo "--monitoring--"
for d in `df -P | grep /dev | awk '{print $5}' | cut -f 1 -d "%"`
do
if [ $d -gt 1 ]
then
echo "over!"
cat /test/a.txt | mail -s "mail subject" ******@163.com
else
echo "normal!"
fi
done
方法二:自建邮件服务器
1.下载文件
#wget https://files.cnblogs.com/files/sunziying/sendEmail-v1.56.tar.gz
2.安装
#yum install sendemail
3.命令参数详解
[root@li229-122 scripts]# sendEmail --help
sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net>
Synopsis: sendEmail -f ADDRESS [options]
Required:
-f ADDRESS from (sender) email address(发送人邮箱)
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE
Common:
-t ADDRESS [ADDR ...] to email address(es)(接收人邮箱)
-u SUBJECT message subject(主题)
-m MESSAGE message body(正文)
-s SERVER[:PORT] smtp mail relay, default is localhost:25(发件人邮箱的SMTP服务器)
Optional:
-a FILE [FILE ...] file attachment(s)(附件)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication(发件人邮箱的用户名)
-xp PASSWORD password for SMTP authentication(发件人邮箱的密码)
Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls=<auto|yes|no> -o fqdn=FQDN
Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more
4.实例:Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件。
例:
#!/bin/bash
partition_list=(`df -h | awk 'NF>3&&NR>1{sub(/%/,"",$(NF-1));print $NF,$(NF-1)}'`)
notification_email()
{
emailfrom='sunzy@163.com'
sendto='7589457343@qq.com'
emailsmtp='smtp.163.com'
title='Disk Space Alarm'
emailuser='sunzy@163.com'
emailpasswd='*****'
/usr/sbin/sendmail -f $emailfrom -t $sendto -s $emailsmtp -u $title -xu $emailuser -xp $emailpasswd
}
critical=80
crit_info=""
for (( i=0;i<${#partition_list[@]};i+=2 ))
do
if [ "${partition_list[((i+1))]}" -lt "$critical" ];then
echo "OK! ${partition_list[i]} used ${partition_list[((i+1))]}%"
else if [ "${partition_list[((i+1))]}" -gt "$critical" ];then
crit_info=$crit_info"Warning!!! ${partition_list[i]} used ${partition_list[((i+1))]}%\n"
echo -e $crit_info | notification_email
fi
done