-
Notifications
You must be signed in to change notification settings - Fork 123
/
monitor-jstat.sh
123 lines (102 loc) · 3.79 KB
/
monitor-jstat.sh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
## 定时监控本机器下所有java应用的 JVM信息
# 定时任务配置
# crontab -e
# 写入如下,5分钟执行一次检测
# */5 * * * * /home/admin/monitor-jstat.sh >> /data/logs/monitor/monitor.log 2>&1
#导入环境变量,需要查看当前的用户的环境变量,否则会出现jps ifconfig环境变量不可用错误提示
export PATH=$PATH:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/data/jdk1.8.0_181/bin:/home/admin/.local/bin:/home/admin/bin
# 老年代告警阈值
oldwarn="99000"
# 元空间(永久代)告警阈值
metawarn="99900"
# 手机号码,多个用逗号隔开
mobile=
# 短信发送url地址
sendsms=
jsonhead="Content-type:application/json"
## 当前日期
sysdate=`date`
## 当前服务器IP地址
addr=`ifconfig -a|grep -w inet|grep -v 127.0.0.1|grep -v 172.16.1.1|awk '{print $2}'|cut -d : -f 2`
for pid in `jps -v |grep -v Jps |grep -v logstash-watch-collection |awk '{print $1}'`
do
## 应用名称
name=`jps -v |grep -v Jps |grep -v logstash-watch-collection |grep $pid |awk '{print $2}'`
#jvm内存监控
#echo "##################开始Java应用:${name}内存监控############################"
#echo "监控日期: $sysdate"
#echo "进程PID: $pid"
#echo "进程名称: $name"
#echo "当前服务器IP: $addr"
# 堆栈信息
jstat=$(jstat -gcutil ${pid})
#echo -e "jstat: \n $jstat"
#S0=$(echo ${jstat}|sed -n '2p' |awk '{print $1}')
#S0=$(echo ${jstat}|awk '{print $12}')
#echo $S0
# 新生代
eden=$(echo ${jstat}|awk '{print $14}')
from=$(echo ${jstat}|awk '{print $12}')
tospc=$(echo ${jstat}|awk '{print $13}')
# 老年代
oldge=$(echo ${jstat}|awk '{print $15}')
# 永久代 JDK8已经废弃改用 元空间代替
meta=$(echo ${jstat}|awk '{print $16}')
#echo "Eden Space: ${eden}%"
#echo "From Space: ${from}%"
#echo "To Space: ${tospc}%"
#echo "Old Generation: ${oldge}%"
#echo "Meta Space: ${meta}%"
## 将读取到的各区的值*1000,以便做下一次的比较操作
#eden1=`gawk -v x=$eden -v y=1000 'BEGIN{printf "%.0f\n",x*y}'`
#from1=`gawk -v x=$from -v y=1000 'BEGIN{printf "%.0f\n",x*y}'`
#tospc1=`gawk -v x=$tospc -v y=1000 'BEGIN{printf "%.0f\n",x*y}'`
oldge1=`gawk -v x=$oldge -v y=1000 'BEGIN{printf "%.0f\n",x*y}'`
meta1=`gawk -v x=$meta -v y=1000 'BEGIN{printf "%.0f\n",x*y}'`
#echo "Eden*100 Space: ${eden1}"
#echo "From*100 Space: ${from1}"
#echo "To*100 Space: ${tospc1}"
#echo "Old*100 Generation: ${oldge1}"
#echo "Meta*1000 Space: ${meta1}"
#if [ ${eden1} -ge "99999" ]
#then
#jvm=$jvm+${name}+":Eden Space:"+${eden}+"%超阀值 "
#fi
#if [ ${from1} -ge "99999" ]
#then
#jvm=$jvm+${name}+":From Space:"+${from}+"%超阀值 "
#fi
#if [ ${tospc1} -ge "99999" ]
#then
#jvm=$jvm+${name}+":To Space:"+${topspc}+"%超阀值 "
#fi
# 仅监控 老年代和元空间,超过90以上报警
jvm=
if [ ${oldge1} -ge ${oldwarn} ]
then
jvm="${jvm}[老年代 ${oldge}%超阀值]"
fi
if [ ${meta1} -ge ${metawarn} ]
then
jvm="${jvm}[元空间(永久代) ${meta}%超阈值]"
fi
if [ ! -n "$jvm" ]
then
echo "应用:${name}-JVM信息正常"
else
jvm="应用${name}的JVM告警信息:${jvm}"
echo "监控日期: $sysdate"
echo "进程PID: $pid"
echo "进程名称: $name"
echo "当前服务器IP: $addr"
echo $jvm
#echo "JVM告警信息:${jvm}"
smsContent='{"mobile":"'$mobile'","content":"'$jvm'","userid":"0001","pwd":"123456"}'
#echo "JVM告警发送短信内容: ${smsContent}"
# -s 不输出请求信息
smsResult=$(curl -H ${jsonhead} -X POST -s -d "${smsContent}" ${sendsms})
#echo "短信返回结果:${smsResult}"
fi
#echo "##################结束Java应用:${name}内存监控############################"
done