最新消息:请随时分享你的乐趣!

Java 检控服务器上所有的JVM虚拟机状态

技术博客 磊子 416浏览 0评论

概述

使用java 对该服务器上面所有JVM进程进行监控,包括GC/Memery/Thread/等。

其中包括的协议有:

  • RMI远程协议 rmi://$pid@$ip
  • local 本地进程协议 local://$pid@localhost
  • file协议(未研究)

可以获取所有的虚拟机当前参数,获取参数以后,就可以做你想做的任何事情了。

参数以List形式展现。

代码

package com.ghw.test;

import sun.jvmstat.monitor.*;

import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by cocky on 2017/1/19.
 */
public class TestJvm {

    public static void main(String[] args) throws URISyntaxException, MonitorException {
//        String hostname = "localhost"; //如果是本地那么就是localhost    local://$pid@localhost
        String hostname = "bigdata1";  //如果是远程 jstatd 启动那么就是rmi协议 rmi://$pid@$ip
        HostIdentifier localhost = new HostIdentifier(hostname);
        MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(localhost);
        Set<Integer> pids = monitoredHost.activeVms();
        Iterator<Integer> iterator = pids.iterator();
        while (iterator.hasNext()) {
            int next = (int) iterator.next();
            VmIdentifier vmIdentifier = new VmIdentifier(String.format("rmi://%s@%s",next,hostname));
            //VmIdentifier vmIdentifier = new VmIdentifier(String.format("local://%s@localhost",next));
            MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmIdentifier);
            List<Monitor> monitors = monitoredVm.findByPattern(".*");
            System.out.println("======================================= "+next);
            for (int i = 0; i < monitors.size(); i++) {
                Monitor monitor = monitors.get(i);
                System.out.println(String.format("%s\t%s\t%s\t%s",monitor.getName(),monitor.getValue(),monitor.getBaseName(),monitor.getUnits()));
            }

        }


    }
}

打印结果

其中一个进程:

=======================================pid=1759
java.ci.totalTime   68145359    totalTime   Ticks
java.cls.loadedClasses  365205  loadedClasses   Events
java.cls.sharedLoadedClasses    0   sharedLoadedClasses Events
java.cls.sharedUnloadedClasses  0   sharedUnloadedClasses   Events
java.cls.unloadedClasses    337355  unloadedClasses Events
java.property.java.class.path   /usr/hdp/current/hbase-client/lib/hbase-protocol.jar:/usr/hdp/2.3.2.0-2950/spark/conf/:/usr/hdp/2.3.2.0-2950/spark/lib/spark-assembly-1.4.1.2.3.2.0-2950-hadoop2.7.1.2.3.2.0-2950.jar:/usr/hdp/2.3.2.0-2950/spark/lib/datanucleus-core-3.2.10.jar:/usr/hdp/2.3.2.0-2950/spark/lib/datanucleus-api-jdo-3.2.6.jar:/usr/hdp/2.3.2.0-2950/spark/lib/datanucleus-rdbms-3.2.9.jar:/usr/hdp/current/hadoop-client/conf/:/usr/hdp/current/hadoop-client/hadoop-azure.jar:/usr/hdp/current/hadoop-client/lib/azure-storage-2.2.0.jar path    String
java.property.java.endorsed.dirs    /usr/jdk64/jdk1.7.0_67/jre/lib/endorsed dirs    String
java.property.java.ext.dirs /usr/jdk64/jdk1.7.0_67/jre/lib/ext:/usr/java/packages/lib/ext   dirs    String
java.property.java.home /usr/jdk64/jdk1.7.0_67/jre  home    String
java.property.java.library.path /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib    path    String
java.property.java.version  1.7.0_67    version String
java.property.java.vm.info  mixed mode  info    String
java.property.java.vm.name  Java HotSpot(TM) 64-Bit Server VM   name    String
java.property.java.vm.specification.name    Java Virtual Machine Specification  name    String
java.property.java.vm.specification.vendor  Oracle Corporation  vendor  String
java.property.java.vm.specification.version 1.7 version String
java.property.java.vm.vendor    Oracle Corporation  vendor  String
java.property.java.vm.version   24.65-b04   version String
java.rt.vmArgs  -XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:SoftRefLRUPolicyMSPerMB=0 -Dhdp.version=2.3.2.0-2950 -Xms3g -Xmx3g -XX:PermSize=128M -XX:MaxPermSize=256M    vmArgs  String
java.rt.vmFlags     vmFlags String
java.threads.daemon 62  daemon  None
java.threads.live   65  live    None
java.threads.livePeak   198 livePeak    None
java.threads.started    124493  started Events
sun.ci.compilerThread.0.compiles    1826    compiles    Events
sun.ci.compilerThread.0.method      method  String
sun.ci.compilerThread.0.time    4955    time    Ticks
sun.ci.compilerThread.0.type    1   type    None
sun.ci.compilerThread.1.compiles    1859    compiles    Events
sun.ci.compilerThread.1.method      method  String
sun.ci.compilerThread.1.time    4854    time    Ticks
sun.ci.compilerThread.1.type    1   type    None
sun.ci.lastFailedMethod org/apache/spark/ContextCleaner$$anonfun$org$apache$spark$ContextCleaner$$keepCleaning$1 apply$mcV$sp   lastFailedMethod    String
sun.ci.lastFailedType   1   lastFailedType  None
sun.ci.lastInvalidatedMethod        lastInvalidatedMethod   String
sun.ci.lastInvalidatedType  0   lastInvalidatedType None
sun.ci.lastMethod   org/apache/hadoop/hdfs/protocol/proto/ClientNamenodeProtocolProtos$FsyncResponseProto <init>    lastMethod  String
sun.ci.lastSize 955 lastSize    Bytes
sun.ci.lastType 1   lastType    None
sun.ci.nmethodCodeSize  3879968 nmethodCodeSize Bytes
sun.ci.nmethodSize  8683264 nmethodSize Bytes
sun.ci.osrBytes 29952   osrBytes    Bytes
sun.ci.osrCompiles  51  osrCompiles Events
sun.ci.osrTime  2464655 osrTime Ticks
sun.ci.standardBytes    1007033 standardBytes   Bytes
sun.ci.standardCompiles 3633    standardCompiles    Events
sun.ci.standardTime 65680704    standardTime    Ticks
sun.ci.threads  2   threads Bytes
sun.ci.totalBailouts    1   totalBailouts   Events
sun.ci.totalCompiles    3684    totalCompiles   Events
sun.ci.totalInvalidates 0   totalInvalidates    Events
sun.classloader.findClassTime   10691917761 findClassTime   Ticks
sun.classloader.findClasses 10157   findClasses Ticks
sun.classloader.parentDelegationTime    1635462041  parentDelegationTime    Ticks
sun.cls.appClassBytes   531752395   appClassBytes   Bytes
sun.cls.appClassLoadCount   10948   appClassLoadCount   Events
sun.cls.appClassLoadTime    10223084    appClassLoadTime    Ticks
sun.cls.appClassLoadTime.self   4733885 self    Ticks
sun.cls.classInitTime   8246724 classInitTime   Ticks
sun.cls.classInitTime.self  2760773 self    Ticks
sun.cls.classLinkedTime 60830302    classLinkedTime Ticks
sun.cls.classLinkedTime.self    49816826    self    Ticks
sun.cls.classVerifyTime 9137459 classVerifyTime Ticks
sun.cls.classVerifyTime.self    3914218 self    Ticks
sun.cls.defineAppClassTime  84018189    defineAppClassTime  Ticks
sun.cls.defineAppClassTime.self 12232311    self    Ticks
sun.cls.defineAppClasses    363244  defineAppClasses    Ticks
sun.cls.initializedClasses  361688  initializedClasses  Events
sun.cls.isUnsyncloadClassSet    0   isUnsyncloadClassSet    Events
sun.cls.jniDefineClassNoLockCalls   34  jniDefineClassNoLockCalls   Events
sun.cls.jvmDefineClassNoLockCalls   363042  jvmDefineClassNoLockCalls   Events
sun.cls.jvmFindLoadedClassNoLockCalls   986567  jvmFindLoadedClassNoLockCalls   Events
sun.cls.linkedClasses   363060  linkedClasses   Events
sun.cls.loadInstanceClassFailRate   0   loadInstanceClassFailRate   Events
sun.cls.loadedBytes 617876824   loadedBytes Bytes
sun.cls.lookupSysClassTime  201126  lookupSysClassTime  Ticks
sun.cls.methodBytes 98670560    methodBytes Bytes
sun.cls.nonSystemLoaderLockContentionRate   0   nonSystemLoaderLockContentionRate   Events
sun.cls.parseClassTime  70076660    parseClassTime  Ticks
sun.cls.parseClassTime.self 66412952    self    Ticks
sun.cls.sharedClassLoadTime 27830   sharedClassLoadTime Ticks
sun.cls.sharedLoadedBytes   0   sharedLoadedBytes   Bytes
sun.cls.sharedUnloadedBytes 0   sharedUnloadedBytes Bytes
sun.cls.sysClassBytes   5784443 sysClassBytes   Bytes
sun.cls.sysClassLoadTime    911428  sysClassLoadTime    Ticks
sun.cls.systemLoaderLockContentionRate  0   systemLoaderLockContentionRate  Events
sun.cls.time    141791908   time    Ticks
sun.cls.unloadedBytes   563828376   unloadedBytes   Bytes
sun.cls.unsafeDefineClassCalls  353079  unsafeDefineClassCalls  Events
sun.cls.verifiedClasses 363047  verifiedClasses Events
sun.gc.cause    No GC   cause   String
sun.gc.collector.0.invocations  43559   invocations Events
sun.gc.collector.0.lastEntryTime    770133498444    lastEntryTime   Ticks
sun.gc.collector.0.lastExitTime 770133513774    lastExitTime    Ticks
sun.gc.collector.0.name PCopy   name    String
sun.gc.collector.0.time 839184827   time    Ticks
sun.gc.collector.1.invocations  46  invocations Events
sun.gc.collector.1.lastEntryTime    52559712010 lastEntryTime   Ticks
sun.gc.collector.1.lastExitTime 52559854276 lastExitTime    Ticks
sun.gc.collector.1.name CMS name    String
sun.gc.collector.1.time 4291957 time    Ticks
sun.gc.generation.0.agetable.bytes.00   0   00  Bytes
sun.gc.generation.0.agetable.bytes.01   8432    01  Bytes
sun.gc.generation.0.agetable.bytes.02   56752   02  Bytes
sun.gc.generation.0.agetable.bytes.03   0   03  Bytes
sun.gc.generation.0.agetable.bytes.04   35968   04  Bytes
sun.gc.generation.0.agetable.bytes.05   0   05  Bytes
sun.gc.generation.0.agetable.bytes.06   35192   06  Bytes
sun.gc.generation.0.agetable.bytes.07   0   07  Bytes
sun.gc.generation.0.agetable.bytes.08   56440   08  Bytes
sun.gc.generation.0.agetable.bytes.09   0   09  Bytes
sun.gc.generation.0.agetable.bytes.10   35224   10  Bytes
sun.gc.generation.0.agetable.bytes.11   0   11  Bytes
sun.gc.generation.0.agetable.bytes.12   35192   12  Bytes
sun.gc.generation.0.agetable.bytes.13   0   13  Bytes
sun.gc.generation.0.agetable.bytes.14   35192   14  Bytes
sun.gc.generation.0.agetable.bytes.15   0   15  Bytes
sun.gc.generation.0.agetable.size   16  size    None
sun.gc.generation.0.capacity    348913664   capacity    Bytes
sun.gc.generation.0.maxCapacity 348913664   maxCapacity Bytes
sun.gc.generation.0.minCapacity 348913664   minCapacity Bytes
sun.gc.generation.0.name    new name    String
sun.gc.generation.0.space.0.capacity    116391936   capacity    Bytes
sun.gc.generation.0.space.0.initCapacity    0   initCapacity    Bytes
sun.gc.generation.0.space.0.maxCapacity 116391936   maxCapacity Bytes
sun.gc.generation.0.space.0.name    eden    name    String
sun.gc.generation.0.space.0.used    75448872    used    Bytes
sun.gc.generation.0.space.1.capacity    116260864   capacity    Bytes
sun.gc.generation.0.space.1.initCapacity    0   initCapacity    Bytes
sun.gc.generation.0.space.1.maxCapacity 116260864   maxCapacity Bytes
sun.gc.generation.0.space.1.name    s0  name    String
sun.gc.generation.0.space.1.used    0   used    Bytes
sun.gc.generation.0.space.2.capacity    116260864   capacity    Bytes
sun.gc.generation.0.space.2.initCapacity    0   initCapacity    Bytes
sun.gc.generation.0.space.2.maxCapacity 116260864   maxCapacity Bytes
sun.gc.generation.0.space.2.name    s1  name    String
sun.gc.generation.0.space.2.used    444160  used    Bytes
sun.gc.generation.0.spaces  3   spaces  None
sun.gc.generation.0.threads 4   threads None
sun.gc.generation.1.capacity    2872311808  capacity    Bytes
sun.gc.generation.1.maxCapacity 2872311808  maxCapacity Bytes
sun.gc.generation.1.minCapacity 2872311808  minCapacity Bytes
sun.gc.generation.1.name    old name    String
sun.gc.generation.1.space.0.capacity    2872311808  capacity    Bytes
sun.gc.generation.1.space.0.initCapacity    2872311808  initCapacity    Bytes
sun.gc.generation.1.space.0.maxCapacity 2872311808  maxCapacity Bytes
sun.gc.generation.1.space.0.name    old name    String
sun.gc.generation.1.space.0.used    1619737448  used    Bytes
sun.gc.generation.1.spaces  1   spaces  None
sun.gc.generation.2.capacity    137637888   capacity    Bytes
sun.gc.generation.2.maxCapacity 268435456   maxCapacity Bytes
sun.gc.generation.2.minCapacity 134217728   minCapacity Bytes
sun.gc.generation.2.name    perm    name    String
sun.gc.generation.2.space.0.capacity    137637888   capacity    Bytes
sun.gc.generation.2.space.0.initCapacity    134217728   initCapacity    Bytes
sun.gc.generation.2.space.0.maxCapacity 268435456   maxCapacity Bytes
sun.gc.generation.2.space.0.name    perm    name    String
sun.gc.generation.2.space.0.used    126241696   used    Bytes
sun.gc.generation.2.spaces  1   spaces  None
sun.gc.lastCause    Allocation Failure  lastCause   String
sun.gc.policy.collectors    2   collectors  None
sun.gc.policy.desiredSurvivorSize   58130432    desiredSurvivorSize Bytes
sun.gc.policy.generations   3   generations None
sun.gc.policy.maxTenuringThreshold  15  maxTenuringThreshold    None
sun.gc.policy.name  ParNew:CMS  name    String
sun.gc.policy.tenuringThreshold 15  tenuringThreshold   None
sun.gc.tlab.alloc   14612370    alloc   Bytes
sun.gc.tlab.allocThreads    13  allocThreads    None
sun.gc.tlab.fastWaste   0   fastWaste   Bytes
sun.gc.tlab.fills   70  fills   None
sun.gc.tlab.gcWaste 2558    gcWaste Bytes
sun.gc.tlab.maxFastWaste    0   maxFastWaste    Bytes
sun.gc.tlab.maxFills    50  maxFills    None
sun.gc.tlab.maxGcWaste  256 maxGcWaste  Bytes
sun.gc.tlab.maxSlowAlloc    0   maxSlowAlloc    None
sun.gc.tlab.maxSlowWaste    155 maxSlowWaste    Bytes
sun.gc.tlab.slowAlloc   0   slowAlloc   None
sun.gc.tlab.slowWaste   184 slowWaste   Bytes
sun.os.hrt.frequency    1000000 frequency   Hertz
sun.os.hrt.ticks    770140738015    ticks   Ticks
sun.perfdata.majorVersion   2   majorVersion    None
sun.perfdata.minorVersion   0   minorVersion    None
sun.perfdata.overflow   0   overflow    Bytes
sun.perfdata.size   32768   size    Bytes
sun.perfdata.timestamp  252466  timestamp   Ticks
sun.perfdata.used   16264   used    Bytes
sun.property.sun.boot.class.path    /usr/jdk64/jdk1.7.0_67/jre/lib/resources.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/rt.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/sunrsasign.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/jsse.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/jce.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/charsets.jar:/usr/jdk64/jdk1.7.0_67/jre/lib/jfr.jar:/usr/jdk64/jdk1.7.0_67/jre/classes   path    String
sun.property.sun.boot.library.path  /usr/jdk64/jdk1.7.0_67/jre/lib/amd64    path    String
sun.rt._sync_ContendedLockAttempts  3997613 _sync_ContendedLockAttempts Events
sun.rt._sync_Deflations 1232600 _sync_Deflations    Events
sun.rt._sync_EmptyNotifications 0   _sync_EmptyNotifications    Events
sun.rt._sync_FailedSpins    0   _sync_FailedSpins   Events
sun.rt._sync_FutileWakeups  1117805 _sync_FutileWakeups Events
sun.rt._sync_Inflations 1232609 _sync_Inflations    Events
sun.rt._sync_MonExtant  16896   _sync_MonExtant Events
sun.rt._sync_MonInCirculation   0   _sync_MonInCirculation  Events
sun.rt._sync_MonScavenged   0   _sync_MonScavenged  Events
sun.rt._sync_Notifications  1833442 _sync_Notifications Events
sun.rt._sync_Parks  3617009 _sync_Parks Events
sun.rt._sync_PrivateA   0   _sync_PrivateA  Events
sun.rt._sync_PrivateB   0   _sync_PrivateB  Events
sun.rt._sync_SlowEnter  0   _sync_SlowEnter Events
sun.rt._sync_SlowExit   0   _sync_SlowExit  Events
sun.rt._sync_SlowNotify 0   _sync_SlowNotify    Events
sun.rt._sync_SlowNotifyAll  0   _sync_SlowNotifyAll Events
sun.rt._sync_SuccessfulSpins    0   _sync_SuccessfulSpins   Events
sun.rt.applicationTime  769010287560    applicationTime Ticks
sun.rt.createVmBeginTime    1484046928548   createVmBeginTime   None
sun.rt.createVmEndTime  1484046928782   createVmEndTime None
sun.rt.internalVersion  Java HotSpot(TM) 64-Bit Server VM (24.65-b04) for linux-amd64 JRE (1.7.0_67-b01), built on Jul 25 2014 09:20:21 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)  internalVersion String
sun.rt.interruptedBeforeIO  0   interruptedBeforeIO Events
sun.rt.interruptedDuringIO  0   interruptedDuringIO Events
sun.rt.javaCommand  org.apache.spark.deploy.SparkSubmit --master yarn-client --conf spark.driver.memory=3g --class com.ghw.dtm.streaming.service.SparkKafkaStreaming --num-executors 2 --executor-memory 3g --executor-cores 2 --files /usr/hdp/current/spark-client/conf/log4j.properties --jars /home/spark/dataprocess/lib/data-depend-jar-with-dependencies.jar,/home/spark/dataprocess/lib/base.jar /home/spark/dataprocess/lib/datastreaming.jar -spark_master yarn-client -ttl 0 -zookeeper_connect bigdata3:2181,bigdata1:2181,bigdata2:2181    javaCommand String
sun.rt.jvmCapabilities  1000000000000000000000000000000000000000000000000000000000000000    jvmCapabilities String
sun.rt.jvmVersion   406913028   jvmVersion  None
sun.rt.safepointSyncTime    66400657    safepointSyncTime   Ticks
sun.rt.safepointTime    1127006530  safepointTime   Ticks
sun.rt.safepoints   324295  safepoints  Events
sun.rt.threadInterruptSignaled  0   threadInterruptSignaled Events
sun.rt.vmInitDoneTime   1484046928722   vmInitDoneTime  None
sun.threads.vmOperationTime 878404930   vmOperationTime Ticks
sun.urlClassLoader.readClassBytesTime   2492758521  readClassBytesTime  Ticks
sun.zip.zipFile.openTime    107588723   openTime    Ticks
sun.zip.zipFiles    33  zipFiles    Ticks

转载请注明:印迹. » Java 检控服务器上所有的JVM虚拟机状态

发表我的评论
取消评论

表情