JVM内存监控shell脚本

该shell脚本主要用于监控:

  • 1.各个进程的jvm内存使用情况;
  • 2.端口监控;
  • 3.访问的页面是否正常。

脚本名称: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

done

#url监控
http=`curl -I -m 10 -o /dev/null -s -w %{http_code}"\n"  http://www.callcent.kefu.com/sso/jsp/login.jsp`

if [ "$http" != "200" ]
then
http1="统一登陆页面http://www.xxxxxx.com/sso/jsp/login.jsp访问异常"
fi

#发送告警信息
if [ -n "$p1" -o -n "$http1" -o -n "$jvm" ]
then
curl -d "action=SendMessage&msg=ip:${addr}            $jvm             ${p1}               ${http1};&usernames=user1;user2" http://172.xx.xx.xxx: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

监控截图:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券