Linux监控进程磁盘邮件预警
[toc]
# 个人邮箱信息
echo 'set from=806666800@qq.com' >>/etc/mail.rc
echo 'set smtp=smtps://smtp.qq.com:465' >>/etc/mail.rc
echo 'set smtp-auth-user=806666800@qq.com' >>/etc/mail.rc
echo 'set smtp-auth-password=jbguyqasdljwbaji' >>/etc/mail.rc #这里用的是授权码
echo 'set smtp-auth=login' >>/etc/mail.rc
1
2
3
4
5
2
3
4
5
# [Shell脚本]Linux进程/端口监控邮件预警
#! /bin/bash
#监听特定服务,当服务挂掉之后发送邮件至监听邮箱内.
# 监控类型,1:服务进程,0:端口监听
monitor_type=0
# 服务进程信息 与 服务说明定义 / 服务端口监听信息 与 服务说明定义
declare -A serverInfo
#serverInfo["YDService"]="云盘服务"
#serverInfo["YD.S.1jf"]="测试服务"
serverInfo["80"]="云盘服务"
serverInfo["8080"]="测试服务"
# 邮件接收人信息,多个邮箱逗号分隔
receiving_mail=745925668@qq.com,745925668@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=806666800@qq.com"`
if [ -z "${param_set}" ]; then
echo 'set from=806666800@qq.com' >>/etc/mail.rc
echo 'set smtp=smtp.exmail.qq.com' >>/etc/mail.rc
echo 'set smtp-auth-user=806666800@qq.com' >>/etc/mail.rc
echo 'set smtp-auth-password=j3i5wQigoE9R28Po' >>/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# [Shell脚本]Linux监控服务器磁盘空间邮件预警
#! /bin/bash
# 监听服务器磁盘,超过百分比进行预警
alert_percent=80
# 邮件接收人信息,多个邮箱逗号分隔
receiving_mail=745925668@qq.com,zhangsh@rbtsoft.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=admin@rbtsoft.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=admin@rbtsoft.com' >>/etc/mail.rc
echo 'set smtp-auth-password=j3i5wQigoE9R28Po' >>/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 阿里云无法成功发送邮件问题
# 异常
[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.
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 解决方法
安装certutil
sudo apt install libnss3-tools
-or-
sudo yum install nss-tools
-or-
sudo pacman -S nss
-or-
sudo zypper install mozilla-nss-tools
1
2
3
4
5
6
7
2
3
4
5
6
7
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 异常二
[root@iZ2zeay5b02uru9p0lb63bZ ~]# Unexpected EOF on SMTP connection
"/root/dead.letter" 22/930
. . . message not sent.
1
2
3
4
2
3
4
# 解决方法
之前配置是
set smtp=smtp.qq.com:465
1
改为
set smtp=smtps://smtp.qq.com:465
1
上次更新: 2023/10/08, 09:37:38