yum install msmtp -y

1 step

yum install msmtp -y

2 step

# cat ~/.msmtprc
# Set default values for all following accounts.
defaults
auth on
tls on
tls_starttls on
#tls_trust_file /etc/ssl/certs/ca-certificates.crt
tls_trust_file /etc/ssl/certs/ca-bundle.crt

# The account you will use
#account qqmail
account default
host smtp.exmail.qq.com
port 587
from f@f.com
user f@f.com
password 123456
logfile ~/.msmtp.log

3 step 

[root@vm172-31-0-25 ~]# cat /usr/local/src/monitor_mongod.sh
#!/bin/bash

# 获取系统 IP 地址
ip_address=$(hostname -I | awk '{print $1}')

# 目标邮件地址(多个地址用逗号分隔)
email="a@a.com,b@b.com"
# 发送邮件的标题
subject="Mongod Process Alert on $ip_address"
# 发送邮件的内容
message="Mongod process is not running on $ip_address"

# 创建一个临时文件用于存放邮件内容
temp_mail=$(mktemp)

# 检查 mongod 进程是否存在
if ! pgrep -x "mongos" > /dev/null
#if ! pgrep -x "aaaa" > /dev/null
then
# 如果进程不存在,准备邮件内容
{
echo "From: monitor@a.com"
echo "To: $email"
echo "Subject: $subject"
echo ""
echo "$message"
} > "$temp_mail"

# 发送邮件
while IFS=',' read -ra ADDR; do
for i in "${ADDR[@]}"; do
msmtp "$i" < "$temp_mail"
done
done <<< "$email"
fi

# 删除临时文件
rm -f "$temp_mail"

4 step

# crontab -l
*/10 * * * * /usr/local/src/monitor_mongod.sh 1>/dev/null 2>/dev/null

 

posted @ 2024-06-05 19:19  GaoYanbing  阅读(2)  评论(0编辑  收藏  举报