JVM内存监控shell脚本,该shell脚本主要用于监控各个进程的jvm内存使用情况,脚本名称:jvm-monitor.sh,具体脚本如下:
[root@loong /]#more jvm-monitor.sh
#!/bin/bash
export Java_PATH=/callcent/jdk1.6.0_25
export PATH=$PATH:$JAVA_PATH/bin:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/mit/bin:/usr/lib/mit/sbin
sysdate=date
for pid in jps -v |grep weblogic|grep -v AdminServer |awk '{print $1}'
do
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
java_name=$(ps -ef|grep weblogic|grep $pid|grep -v AdminServer)
name=echo ${java_name}|sed 's/^.*-Dweblogic.Name=//g'|awk '{print $1}'
#jvm内存监控
echo ##############################################
echo $sysdate
echo pid $pid
echo name $name
echo $addr
if [ "$pid" = "" ]
then
echo "the program is not exists."
exit 0
fi
heap=$(jmap -heap ${pid})
eden=echo ${heap}|sed 's/^.*Eden Space://g'|sed 's/From Space.*$//g'|awk '{print $13}'|sed 's/%.*$//g'|cut -b 1-6
from=echo ${heap}|sed 's/^.*From Space://g'|sed 's/To Space.*$//g'|awk '{print $13}'|sed 's/%.*$//g'|cut -b 1-6
tospc=echo ${heap}|sed 's/^.*To Space://g'|sed 's/tenured generation.*$//g'|awk '{print $13}'|sed 's/%.*$//g'|cut -b 1-6
oldge=echo ${heap}|sed 's/^.*PS Old Generation//g'|sed 's/Perm Generation.*$//g'|awk '{print $13}'|sed 's/%.*$//g'|cut -b 1-6
perm=echo ${heap}|sed 's/^.*PS Perm Generation//g'|awk '{print $13}'|sed 's/%.*$//g'|cut -b 1-6
echo "Eden Space: ${eden}%"
echo "From Space: ${from}%"
echo "To Space: ${tospc}%"
echo "Old Generation: ${oldge}%"
echo "Perm Generation: ${perm}%"
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}'
perm1=gawk -v x=$perm -v y=1000 'BEGIN{printf "%.0f\n",x*y}'
#取对应进程的jvm内存值
for name1 in $name
do
if [ ${eden1} -ge "99999" ]
then
jvm=$jvm+${name1}+":Eden Space:"+${eden}+"%超阀值 "
fi
if [ ${from1} -ge "99999" ]
then
jvm=$jvm+${name1}+":From Space:"+${from}+"%超阀值 "
fi
if [ ${tospc1} -ge "99999" ]
then
jvm=$jvm+${name1}+":To Space:"+${topspc}+"%超阀值 "
fi
if [ ${oldge1} -ge "99999" ]
then
jvm=$jvm+${name1}+":Old Generation:"+${oldge}+"%超阀值 "
fi
if [ ${perm1} -ge "99999" ]
then
jvm=$jvm+${name1}+":Perm Generation:"+${perm}+"%超阀值 "
fi
echo $jvm
done
#端口监控
if [ "$name" = "baobiao" ]
then
port='91400'
fi
if [ "$name" = "csr" ]
then
port='91200'
fi
if [ "$name" = "sso" ]
then
port=91500
fi
if [ "$name" = "gongdan" ]
then
port=91100
fi
if [ "$name" = "manager" ]
then
port=91300
fi
if [ "$name" = "cron" ]
then
port=91090
fi
net=netstat -an |grep $port|grep LISTEN|grep $addr
if [ "$net" = "" ]
then
p1=$p1+${port}+"端口异常"
fi
#echo $pid
#echo $port
#echo $name
done
#p2=$p1+"端口异常"
#url监控
http=curl -I -m 10 -o /dev/null -s -w %{http_code}"\n"
#echo $http
if [ "$http" != "200" ]
then
http1="统一登陆页面访问异常"
fi
echo $p1
echo $addr
echo $http1
#发送告警信息
if [ -n "$p1" -o -n "$http1" -o -n "$jvm" ]
then
curl -d "action=SendMessage&msg=ip:${addr} $jvm ${p1} ${http1};&usernames=user1;user2" :8090/TestService.ashx
fi
通过定时任务调用该脚本(该脚本每30秒执行一次):
* * * * * /monitor/script/monitor19.sh >> /monitor/log.txt
[root@loong /]#more /monitor/script/monitor19.sh
#!/bin/bash
export JAVA_PATH=/callcent/jdk1.6.0_25
export PATH=$PATH:$JAVA_PATH/bin:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/mit/bin:/usr/lib/mit/sbin
step=30 #间隔的秒数,不能大于60,表示每半分钟执行一次
for (( i = 0; i < 60; i=(i+step) )); do
/monitor/script/jvm-monitor.sh
sleep $step
done
exit 0
监控截图:
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx