希望通过自动发现Docker中的JAVA应用程序的JVM指标。
#自定义监控项 通过python脚本获取宿主机上的所有java容器
UserParameter=java.services,/etc/zabbix/script/java_services.py
##返回结果为json格式的字符串,其中包含了几个java服务的容器名称。那么通过该py脚本可以配置自动发现规则。
#!/usr/bin/env python3
import json
import os
cmd=os.popen("""docker ps -a |grep Up|grep java|grep -Ei "java_risk|java_notify|java_pay"|awk '{print $12}'""")
services=[]
for port in cmd.readlines():
r=port.strip()
services+=[{'{#SERVICE}':r}]
print (json.dumps({'data':services},sort_keys=True,indent=4,separators=(',',':')))
UserParameter=Socket.Survivor0[*],/etc/zabbix/script/get_gcstat.sh $1 s1
UserParameter=Socket.Survivor1[*],/etc/zabbix/script/get_gcstat.sh $1 s2
UserParameter=Socket.Eden[*],/etc/zabbix/script/get_gcstat.sh $1 eden
UserParameter=Socket.Old[*],/etc/zabbix/script/get_gcstat.sh $1 old
UserParameter=Socket.Metaspace[*],/etc/zabbix/script/get_gcstat.sh $1 meta
UserParameter=Socket.CCS[*],/etc/zabbix/script/get_gcstat.sh $1 css
UserParameter=Socket.YGC[*],/etc/zabbix/script/get_gcstat.sh $1 ygc
UserParameter=Socket.YGCT[*],/etc/zabbix/script/get_gcstat.sh $1 ygct
UserParameter=Socket.FGC[*],/etc/zabbix/script/get_gcstat.sh $1 fgc
UserParameter=Socket.FGCT[*],/etc/zabbix/script/get_gcstat.sh $1 fgct
UserParameter=Socket.GCT[*],/etc/zabbix/script/get_gcstat.sh $1 gct
UserParameter=Socket.LGCC[*],/etc/zabbix/script/get_gcstat.sh $1 lgcc
UserParameter=Socket.GCC[*],/etc/zabbix/script/get_gcstat.sh $1 gcc
## get_gcstat.sh
#!/bin/bash
TargetWord='SocketPlatform.jar'
JavaBinDir='/usr/bin/'
Server_name=$1
S_file=/tmp/gcstat_${Server_name}.txt
#PID=$("${JavaBinDir}"jps -l |grep ${TargetWord} | awk -F' ' '{print $1}')
Get_status(){
local PID=$(docker exec -i ${Server_name} /bin/ps -ef|grep java |grep -v grep |awk '{print $2}')
local CMD="docker exec -i ${Server_name}"
${CMD} ${JavaBinDir}jstat -gccause $PID 1 1 > $S_file
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} ] && [ ! -z ${3} ] ;then
#echo "cat $S_file |tail -1|awk -F' ' '{print $'${1}',$'${2}',$'${3}'}'"
ret=$(cat $S_file |tail -1|awk -F' ' '{print $'${1}',$'${2}',$'${3}'}')
echo $ret
elif [ ! -z ${1} ]&& [ ! -z ${2} ]
then
ret=$(cat $S_file |tail -1|awk -F' ' '{print $'${1}',$'${2}'}')
echo $ret
elif [ ! -z ${1} ] && [ -z ${2} ]
then
ret=$(cat $S_file |tail -1|awk -F' ' '{print $'${1}'}')
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 $2 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
gcstat_colum 12 13 14
;;
gcc)
Get_status
#gcstat_colum 14 15
gcstat_colum 15 16
;;
*)
print_prompt
;;
esac
特别要注意的是,因LGCC和GCC监控项的结果不一定是浮点数,将它们的字符类型改为文本,其他监控项的字符类型为浮点数。