利用python和jstat命令采集jvm数据
注:用法运行python jvcheck.py 变量如EC或E等
#coding=utf-8
#author=yangzai
# 用jstat检查jvm信息然后用zabbix监控
import commands
import sys
jvmvars=sys.argv[1]
(status, PID) = commands.getstatusoutput("sudo -u tomcat jps|grep Bootstrap$|grep -v Jps$|awk '{print $1}'") # PID
def change_gc_ke(cmd):
(status,gcresult_tmp)=commands.getstatusoutput(cmd)
gcresult_tmp = gcresult_tmp.split('\n')
gcresult_key = gcresult_tmp[0].split() # key
gcresult_values = gcresult_tmp[1].split() # values
gcresult = dict(zip(gcresult_key, gcresult_values))
return gcresult
####gcutil
if jvmvars in ('S0','S1','E','O','M','CCS','YGC','YGCT','FGC','FGCT','GCT'):
cmd_gcutil = "sudo -u tomcat jstat -gcutil " + PID
gcutil = change_gc_ke(cmd_gcutil)
print gcutil.get(jvmvars)
####gccapacity
if jvmvars in ('NGCMN', 'NGCMX', 'NGC', 'S0C', 'S1C', 'OGCMX', 'OGCMN', 'OGC', 'MCMX', 'MCMN', 'MC'):
cmd_gccapacity = "sudo -u tomcat jstat -gccapacity " + PID
gccapacity = change_gc_ke(cmd_gccapacity)
print gccapacity.get(jvmvars)
####gcnew
if jvmvars in ('EU', 'EC', 'S0U', 'S1U'):
cmd_gcnew = "sudo -u tomcat jstat -gcnew " + PID
gcnew = change_gc_ke(cmd_gcnew)
print gcnew.get(jvmvars)
####gccode
if jvmvars in ('OU', 'MU'):
cmd_gcold = "sudo -u tomcat jstat -gcold " + PID
gcold = change_gc_ke(cmd_gcold)
print gcold.get(jvmvars)
####gcclass
if jvmvars in ('Loaded', 'Bytes', 'Unloaded'):
cmd_class = "sudo -u tomcat jstat -class " + PID + "|awk '{print $1,$2,$3}'"
gcclass = change_gc_ke(cmd_class)
print gcclass.get(jvmvars)
时间: 2024-09-09 18:57:36