1.實現基礎
linux下監控jvm的gc狀態的工具爲jdk自帶 jstat
這裏我的java進程名爲SocketPlatform.jar
[user1@localhost ~]$ jps -l
47856 sun.tools.jps.Jps
35902 /opt/app/SocketPlatform/SocketPlatform.jar
[user1@localhost ~]$ jstat -gccause 35902 1s 5
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
82.12 0.00 43.67 6.56 90.99 81.08 1492 1153.473 118 83.444 1236.916 Allocation Failure No GC
82.12 0.00 43.67 6.56 90.99 81.08 1492 1153.473 118 83.444 1236.916 Allocation Failure No GC
82.12 0.00 45.61 6.56 90.99 81.08 1492 1153.473 118 83.444 1236.916 Allocation Failure No GC
82.12 0.00 47.55 6.56 90.99 81.08 1492 1153.473 118 83.444 1236.916 Allocation Failure No GC
82.12 0.00 47.57 6.56 90.99 81.08 1492 1153.473 118 83.444 1236.916 Allocation Failure No GC
[user1@localhost ~]$
2.zabbix-agent代理端監控腳本爲
/root/shell/zabbix/get_socket_jstat_status.sh
#!/bin/bash
TargetWord='SocketPlatform.jar'
JavaBinDir='/opt/app/jdk1.8.0_162/bin/'
PID=$("${JavaBinDir}"jps -l |grep ${TargetWord} | awk -F' ' '{print $1}')
flag=0
for i in $PID
do
if [ ! -z $i ]
then
let flag+=1
fi
done
if [ $flag -ne 1 ]
then
echo "the number of $TargetWord is more than 1 !!!"
exit
fi
# get value from jstat
function gcstat_colum(){
if [ ! -z ${1} ] && [ -z ${2} ]
then
ret=`"${JavaBinDir}"jstat -gccause $PID 1 1 |tail -1|awk -F' ' '{print $'${1}'}'`
echo $ret
elif [ ! -z ${1} ]&& [ ! -z ${2} ]
then
ret=`"${JavaBinDir}"jstat -gccause $PID 1 1 |tail -1|awk -F' ' '{print $'${1}',$'${2}'}'`
echo $ret
else
echo 'function get wrong arguments !'
fi
}
# print prompt when script parameter is wrong
function print_prompt(){
echo ' please input correct parameter !'
echo '
s1 (Survivor0)
s2 (Survivor1)
eden(Eden)
old (Old)
meta(Metaspace)
css (CCS)
ygc (YGC)
ygct(YGCT)
fgc (FGC)
fgct(FGCT)
gct (GCT)
lgcc(LGCC)
gcc (GCC)
'
}
# transfer script's parameter to function gcstat_colum()
case $1 in
s1)
gcstat_colum 1
;;
s2)
gcstat_colum 2
;;
eden)
gcstat_colum 3
;;
old)
gcstat_colum 4
;;
meta)
gcstat_colum 5
;;
css)
gcstat_colum 6
;;
ygc)
gcstat_colum 7
;;
ygct)
gcstat_colum 8
;;
fgc)
gcstat_colum 9
;;
fgct)
gcstat_colum 10
;;
gct)
gcstat_colum 11
;;
lgcc)
gcstat_colum 12 13
;;
gcc)
gcstat_colum 14 15
;;
*)
print_prompt
;;
esac
其中需要需要修改的就是
TargetWord和JavaBinDir兩個內容,以實際情況爲準。
添加執行權限
chmod +x /root/shell/zabbix/get_jvm_gc_status.sh
3.zabbix-agent代理端的配置文件
/etc/zabbix/zabbix_agentd.d/userparameter_socket.conf
UserParameter=Socket.Survivor0,/root/shell/zabbix/get_socket_jstat_status.sh s1
UserParameter=Socket.Survivor1,/root/shell/zabbix/get_socket_jstat_status.sh s2
UserParameter=Socket.Eden,/root/shell/zabbix/get_socket_jstat_status.sh eden
UserParameter=Socket.Old,/root/shell/zabbix/get_socket_jstat_status.sh old
UserParameter=Socket.Metaspace,/root/shell/zabbix/get_socket_jstat_status.sh meta
UserParameter=Socket.CCS,/root/shell/zabbix/get_socket_jstat_status.sh css
UserParameter=Socket.YGC,/root/shell/zabbix/get_socket_jstat_status.sh ygc
UserParameter=Socket.YGCT,/root/shell/zabbix/get_socket_jstat_status.sh ygct
UserParameter=Socket.FGC,/root/shell/zabbix/get_socket_jstat_status.sh fgc
UserParameter=Socket.FGCT,/root/shell/zabbix/get_socket_jstat_status.sh fgct
UserParameter=Socket.GCT,/root/shell/zabbix/get_socket_jstat_status.sh gct
UserParameter=Socket.LGCC,/root/shell/zabbix/get_socket_jstat_status.sh lgcc
UserParameter=Socket.GCC,/root/shell/zabbix/get_socket_jstat_status.sh gcc
agent修改配置後需要重啓
service zabbix-agent restart
4.zabbix-server web端添加自定義item
現在創建一個模板
在新模板Templates Socke 下創建一個應用(Applications)名爲 JVM
在應用下創建item,比如 Socket.Eden
其他item都類似,添加完畢如下圖
注意,其中大部分數據類型都是float浮點數,GCC 和 LGCC爲 character。
其他triger和graphs就按自己的需求去定義了。
此模板可以導出,以備其他時候使用。