JVM内存监控shell脚本

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" http://www.callcent.kefu.com/sso/jsp/login.jsp

#echo $http

if [ "$http" != "200" ]

then

http1="统一登陆页面http://www.xxxxxx.com/sso/jsp/login.jsp访问异常"

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" 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

监控截图:

JVM内存监控shell脚本

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-09/154438.htm

原文 

https://www.linuxidc.com/Linux/2018-09/154438.htm

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » JVM内存监控shell脚本

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址