个人邮箱信息
echo 'set from=123456@qq.com' >>/etc/mail.rc
echo 'set smtp=smtps://smtp.qq.com:465' >>/etc/mail.rc
echo 'set smtp-auth-user=123456@qq.com' >>/etc/mail.rc
echo 'set smtp-auth-password=453543453453' >>/etc/mail.rc #这里用的是授权码
echo 'set smtp-auth=login' >>/etc/mail.rc
[Shell脚本]Linux进程/端口监控邮件预警
#! /bin/bash
#监听特定服务,当服务挂掉之后发送邮件至监听邮箱内.
# 监控类型,1:服务进程,0:端口监听
monitor_type=0
# 服务进程信息 与 服务说明定义 / 服务端口监听信息 与 服务说明定义
declare -A serverInfo
#serverInfo["YDService"]="云盘服务"
#serverInfo["YD.S.1jf"]="测试服务"
serverInfo["80"]="云盘服务"
serverInfo["8080"]="测试服务"
# 邮件接收人信息,多个邮箱逗号分隔
receiving_mail=123456@qq.com,1234567@qq.com
# 正文内容
receiving_content="请及时排查问题!"
# 休眠时间,单位秒
sleepTime=60
# 获取当前服务器ip
serviceIp=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
# 服务器hostname
serviceHostName=$(hostname)
# 检查并更新mail服务
function update_mailx() {
mail=`whereis mailx | grep gz`
echo "${mail}"
if [ -z "${mail}" ]; then
yum install -y mailx
else
echo "mail is installed"
fi
}
# 检查并更新mail配置信息,设置我们发送邮件的邮箱配置
function set_mail_param() {
param_set=`cat /etc/mail.rc | grep "smtp-auth-user=123456@qq.com"`
if [ -z "${param_set}" ]; then
echo 'set from=123456@qq.com' >>/etc/mail.rc
echo 'set smtp=smtp.exmail.qq.com' >>/etc/mail.rc
echo 'set smtp-auth-user=123456@qq.com' >>/etc/mail.rc
echo 'set smtp-auth-password=654654654' >>/etc/mail.rc #这里用的是授权码
echo 'set smtp-auth=login' >>/etc/mail.rc
else
echo "mail profile already set!"
fi
}
#监听并发送邮件信息
function monitor() {
while [ true ]; do
for key in ${!serverInfo[*]};do
if [ ${monitor_type} -eq 0 ]; then
process_running=`netstat -antp |grep -w ${key} |grep -w LISTEN|wc -l`
else
process_running=`ps -ef |grep ${key}|grep -v grep|wc -l`
fi
if [ ${process_running} -le 0 ]; then
echo ${key}" process is not exist"
send_mail ${serverInfo[$key]} ${key}
#kill_process
else
echo ${key}" process is exist"
fi
sleep 5
done
sleep ${sleepTime}
done
}
#发送邮件
function send_mail() {
OLD_IFS="$IFS"
IFS=","
arr=(${receiving_mail})
IFS="$OLD_IFS"
for user_mail in ${arr[@]}
do
echo ${user_mail}
echo "服务器IP:${serviceIp};服务器:${serviceHostName};${receiving_content}" | mail -s "严重 ${1} ${2} 服务/端口异常" ${user_mail}
done
}
#删除当前进程
function kill_process() {
kill -9 $$
}
update_mailx
set_mail_param
monitor
[Shell脚本]Linux监控服务器磁盘空间邮件预警
#! /bin/bash
# 监听服务器磁盘,超过百分比进行预警
alert_percent=80
# 邮件接收人信息,多个邮箱逗号分隔
receiving_mail=1324567@qq.com,1455@soft.com
# 正文内容
receiving_content="\n"`df -h`"\n请及时排查问题!"
# 休眠时间,单位秒
sleepTime=60
# 获取当前服务器ip
serviceIp=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
# 服务器hostname
serviceHostName=$(hostname)
# 执行时间
run_time="["`date "+%y-%m-%d %H:%M:%S"`"]"
# 检查并更新mail服务
function update_mailx() {
mail=`whereis mailx | grep gz`
echo "$run_time${mail}"
if [ -z "${mail}" ]; then
yum install -y mailx
else
echo $run_time"mail is installed"
fi
}
# 检查并更新mail配置信息,设置我们发送邮件的邮箱配置
function set_mail_param() {
param_set=`cat /etc/mail.rc | grep "smtp-auth-user=admin@rbtsoft.com"`
if [ -z "${param_set}" ]; then
echo 'set from=123@453453.com' >>/etc/mail.rc
# 个人QQ邮箱修改为
# echo 'set smtp=smtps://smtp.qq.com:465' >>/etc/mail.rc
echo 'set smtp=smtp.exmail.qq.com' >>/etc/mail.rc
echo 'set smtp-auth-user=123@453453.com' >>/etc/mail.rc
echo 'set smtp-auth-password=123' >>/etc/mail.rc #这里用的是授权码
echo 'set smtp-auth=login' >>/etc/mail.rc
else
echo $run_time"mail profile already set!"
fi
}
#监听并发送邮件信息
function monitor() {
isSend=0
for d in `df -P | grep /dev | awk '{print $5}' | cut -f 1 -d "%"`
do
if [ $d -gt $alert_percent ]
then
isSend=1
fi
done
if [ $isSend -eq 1 ];then
echo $run_time'disk use monitor'
content=`df -h`
send_mail "磁盘空间使用率超过阈值"$alert_percent"%"
fi
}
#发送邮件
function send_mail() {
OLD_IFS="$IFS"
IFS=","
arr=(${receiving_mail})
IFS="$OLD_IFS"
for user_mail in ${arr[@]}
do
echo $run_time${user_mail}
echo -e "服务器IP:${serviceIp};服务器:${serviceHostName};${receiving_content}" | mail -s "严重 ${1} ${2} " ${user_mail}
done
}
#删除当前进程
function kill_process() {
kill -9 $$
}
update_mailx
set_mail_param
monitor
阿里云无法成功发送邮件问题
异常
[root@iZ2zeay5b02uru9p0lb63bZ ~]# could not connect: Network is unreachable
"/root/dead.letter" 22/940
. . . message not sent.
could not connect: Network is unreachable
"/root/dead.letter" 22/930
. . . message not sent.
could not connect: Connection timed out
"/root/dead.letter" 22/940
. . . message not sent.
解决方法
安装certutil
sudo apt install libnss3-tools
-or-
sudo yum install nss-tools
-or-
sudo pacman -S nss
-or-
sudo zypper install mozilla-nss-tools
[root@myserver ~]# mkdir -p /root/.certs/
[root@myserver ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq2.crt
depth=2 C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
verify return:1
depth=1 C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
verify return:1
depth=0 C = CN, ST = guangdong, L = shenzhen, O = Shenzhen Tencent Computer Systems Company Limited, CN = *.mail.qq.com
verify return:1
DONE
[root@myserver ~]#
[root@myserver ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq2.crt
[root@myserver ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq2.crt
[root@myserver ~]# certutil -L -d /root/.certs
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
GeoTrust SSL CA C,,
[root@myserver ~]# echo “邮件内容”|mail -s 标题 12345678@qq.com
[root@myserver ~]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.
[root@myserver .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq2.crt
Notice: Trust flag u is set automatically if the private key is present.
[root@myserver ~]# echo “邮件内容”|mail -s 标题 12345678@qq.com
异常二
[root@iZ2zeay5b02uru9p0lb63bZ ~]# Unexpected EOF on SMTP connection
"/root/dead.letter" 22/930
. . . message not sent.
解决方法
之前配置是
set smtp=smtp.qq.com:465
改为
set smtp=smtps://smtp.qq.com:465