上篇最后提到了jstat,jstat可以查看统计JVM内存信息,那么结合Zabbix,就可以监控多实例的JVM内存了。
1、下面两个脚本部署在被监控主机:
vm.py
用于JVM实例PID查找,ps命令亦可以换成jdk自带的jps工具:
#!/usr/bin.python
#
import os
import json data = {}
tcp_list = []
port_list = []
command = "ps -ef | grep weblogic.Server | grep -v \"grep web\" | awk '{print $2}'"
lines = os.popen(command).readlines()
for line in lines:
port=line.strip('\n')
# port = line.split(':')[1]
port_list.append(port) for port in list(set(port_list)):
port_dict = {}
port_dict['{#PID}'] = port
tcp_list.append(port_dict) data['data'] = tcp_list
jsonStr = json.dumps(data, sort_keys=True, indent=4)
print jsonStr
getvm.sh
统计内存使用情况脚本,需修改JAVA_HOME环境变量:
#!/bin/bash
#
JAVA_HOME="/u01/Middleware/jdk1.6.0_45"
javapid=$2 #S0
function s0 {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $1}')
} #S1
function s1 {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $2}')
} #Eden
function eden {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $3}')
} #Old
function old {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $4}')
}
#Perm
function perm {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $5}')
}
#YGC
function ygc {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $6}')
}
#YGCT
function ygct {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $7}')
} #FGC
function fgc {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $8}')
}
#FGCT
function fgct {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $9}')
}
#GCT
function gct {
echo $(sudo -u weblogic ${JAVA_HOME}/bin/jstat -gccause $javapid | awk '{if(NR!=1) print $10}')
} $1 exit $?
2、增加被监控主机zabbix_agentd配置文件
/etc/zabbix/zabbix_agentd.d下添加配置文件,jvm.conf
根据脚本实际目录配置:
UserParameter=java.pid,/home/weblogic/zabbix/vm.py
UserParameter=java.memory[*],/home/weblogic/zabbix/getvm.sh $1 $2
3、 为zabbix运行脚本授权
zabbix ALL=(user) NOPASSWD: /u01/Middleware/jdk1.6.0_45/bin/jstat,/usr/bin/python
Defaults !requiretty
4、 zabbix server端测试
./zabbix_get -s 192.168.1.109 -p 10050 -k 'java.pid' { "data": [ { "{#PID}": "8817" }, { "{#PID}": "9041" }, { "{#PID}": "3929" }, { "{#PID}": "8620" } ] } ./zabbix_get -s 192.168.1.109 -p 10050 -k 'java.memory[old,8817]' 5.83
5、zabbix导入模板(在最后),监控即可,如果要自己配置自动发现,参考前面文章Zabbix添加自定义监控项(一)
xml模板:
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.2</version>
<date>2017-06-13T05:25:25Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>JVM Memory</template>
<name>JVM Memory</name>
<description/>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<items/>
<discovery_rules>
<discovery_rule>
<name>Get weblogic pid</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>java.pid</key>
<delay>60</delay>
<status>0</status>
<allowed_hosts/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<delay_flex/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<filter>
<evaltype>0</evaltype>
<formula/>
<conditions/>
</filter>
<lifetime>7</lifetime>
<description/>
<item_prototypes>
<item_prototype>
<name>JVM EDEN MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[eden,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>%</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>Heap上的 Eden space 区已使用空间的百分比</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM FGC MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[fgc,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>次</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>从应用程序启动到采样时发生 Full GC 的次数</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM FGCT MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[fgct,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>s</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>从应用程序启动到采样时 Full GC 所用的时间(单位秒)</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM GCT MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[gct,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>s</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>从应用程序启动到采样时用于垃圾回收的总时间(单位秒)</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM OLD MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[old,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>%</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>Heap上的 Old space 区已使用空间的百分比</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM PERM MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[perm,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>%</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>Perm space 区已使用空间的百分比</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM S0 MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[s0,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>%</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>s0区内存大小</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM S1 MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[s1,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>%</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>s1区内存大小</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM YGC MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[ygc,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>次</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>从应用程序启动到采样时发生 Young GC 的次数</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>JVM YGCT MEMORY USED ON PID {#PID}</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>java.memory[ygct,{#PID}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units>s</units>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>从应用程序启动到采样时 Young GC 所用的时间(单位秒)</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>JVM</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
</item_prototypes>
<trigger_prototypes/>
<graph_prototypes>
<graph_prototype>
<name>JVM GC FREQUENCY ON {#PID}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[fgc,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[ygc,{#PID}]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
<graph_prototype>
<name>JVM GC TIME ON {#PID}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>1</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[fgct,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>1</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[gct,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>1</drawtype>
<color>2774A4</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[ygct,{#PID}]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
<graph_prototype>
<name>JVM MEMORY USED PID {#PID}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>1</ymin_type_1>
<ymax_type_1>1</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[old,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[perm,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>0</drawtype>
<color>2774A4</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[eden,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>3</sortorder>
<drawtype>0</drawtype>
<color>A54F10</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[s0,{#PID}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>4</sortorder>
<drawtype>0</drawtype>
<color>FC6EA3</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>JVM Memory</host>
<key>java.memory[s1,{#PID}]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
</graph_prototypes>
<host_prototypes/>
</discovery_rule>
</discovery_rules>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
</zabbix_export>