我是靠谱客的博主 复杂羽毛,最近开发中收集的这篇文章主要介绍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 "%.0fn",x*y}'
from1=gawk -v x=$from -v y=1000 'BEGIN{printf "%.0fn",x*y}'
tospc1=gawk -v x=$tospc -v y=1000 'BEGIN{printf "%.0fn",x*y}'
oldge1=gawk -v x=$oldge -v y=1000 'BEGIN{printf "%.0fn",x*y}'
perm1=gawk -v x=$perm -v y=1000 'BEGIN{printf "%.0fn",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脚本

转载于:https://blog.51cto.com/3241766/2091452

最后

以上就是复杂羽毛为你收集整理的JVM内存监控shell脚本的全部内容,希望文章能够帮你解决JVM内存监控shell脚本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部