通过JMX获取weblogic的监控指标

通过JMX获取weblogic的监控数据,包括JDBC,SESSION,SERVERLET,JVM等信息。主要用到weblogic自己的t3协议,所以要用到weblogic的jar包:wlfullclient.jar和wlclient.jar。这两个jar包怎么获取我专门在另外一篇文章中讲。下面贴一些获取监控指标的代码,做个备份只用吧。

1、对JDBC的监控,只取了最重要的监控指标:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package  test;
 
import  java.io.IOException;
import  java.net.MalformedURLException;
import  java.util.Hashtable;
 
import  javax.management.MBeanServerConnection;
import  javax.management.MalformedObjectNameException;
import  javax.management.ObjectName;
import  javax.management.remote.JMXConnector;
import  javax.management.remote.JMXConnectorFactory;
import  javax.management.remote.JMXServiceURL;
import  javax.naming.Context;
 
public  class  PrintJdbc {
     private  static  MBeanServerConnection connection;
     private  static  JMXConnector connector;
     private  static  final  ObjectName service;
     
 
        static  {
           try  {
              service =  new  ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean" );
           } catch  (MalformedObjectNameException e) {
              throw  new  AssertionError(e.getMessage());
           }
        }
      
        public  static  void  initConnection(String hostname, String portString,
           String username, String password)  throws  IOException,
           MalformedURLException {
           String protocol =  "t3" ;
           Integer portInteger = Integer.valueOf(portString);
           int  port = portInteger.intValue();
           String jndiroot =  "/jndi/" ;
           String mserver =  "weblogic.management.mbeanservers.runtime" ;
           JMXServiceURL serviceURL =  new  JMXServiceURL(protocol, hostname,
              port, jndiroot + mserver);
           Hashtable h =  new  Hashtable();
           h.put(Context.SECURITY_PRINCIPAL, username);
           h.put(Context.SECURITY_CREDENTIALS, password);
           h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
              "weblogic.management.remote" );
           connector = JMXConnectorFactory.connect(serviceURL, h);
           connection = connector.getMBeanServerConnection();
        }
     
        public  static  ObjectName getServerRuntimes()  throws  Exception {
             return  (ObjectName)connection.getAttribute(service,
                   "ServerRuntime" );
        }
        
       
        public   ObjectName getJDBCServer()  throws  Exception {
            ObjectName dc = getServerRuntimes();
            ObjectName jdbcService = (ObjectName)connection.getAttribute(dc,
                    "JDBCServiceRuntime" );
            return  jdbcService;
        }
        
         public  ObjectName[] getJDBCDataSourceRuntime()  throws  Exception   {
             ObjectName[] jdbcDataSourceRTMB = (ObjectName[]) connection.getAttribute(getJDBCServer(),  "JDBCDataSourceRuntimeMBeans" );
             return  jdbcDataSourceRTMB;
         }
         
         
        public  void  printJdbc()  throws  Exception {
            ObjectName[] objectList = getJDBCDataSourceRuntime();
            if (objectList !=  null  && objectList.length >  0 ){
                for (ObjectName obj : objectList){
                    int  connectionsTotalCount = ( int ) connection.getAttribute(obj,  "ConnectionsTotalCount" );
                    int  activeConnectionsCurrentCount = ( int )connection.getAttribute(obj,  "ActiveConnectionsCurrentCount" );
                    int  activeConnectionsAverageCount = ( int )connection.getAttribute(obj, "ActiveConnectionsAverageCount" );
                    int  failuresToReconnectCount = ( int )connection.getAttribute(obj,  "FailuresToReconnectCount" );
                    String name = (String)connection.getAttribute(obj,  "Name" );
                    int  waitingForConnectionCurrentCount = ( int )connection.getAttribute(obj,  "WaitingForConnectionCurrentCount" );
                    long  waitingForConnectionFailureTotal = ( long )connection.getAttribute(obj,  "WaitingForConnectionFailureTotal" );
                    int  waitSecondsHighCount = ( int )connection.getAttribute(obj,  "WaitSecondsHighCount" );
                    int  connectionDelayTime = ( int )connection.getAttribute(obj,  "ConnectionDelayTime" );
                    int  activeConnectionsHighCount = ( int )connection.getAttribute(obj,  "ActiveConnectionsHighCount" );
                    int  currCapacity = ( int )connection.getAttribute(obj,  "CurrCapacity" );
                    System.out.println( "currCapacity=="  + currCapacity);
                    System.out.println( "activeConnectionsHighCount=="  + activeConnectionsHighCount);
                    System.out.println( "connectionDelayTime=="  + connectionDelayTime);
                    System.out.println( "connectionsTotalCount=="  + connectionsTotalCount);
                    System.out.println( "activeConnectionsCurrentCount=="  + activeConnectionsCurrentCount);
                    System.out.println( "activeConnectionsAverageCount=="  + activeConnectionsAverageCount);
                    System.out.println( "failuresToReconnectCount=="  + failuresToReconnectCount);
                    System.out.println( "name=="  + name);
                    System.out.println( "waitingForConnectionCurrentCount=="  +waitingForConnectionCurrentCount );
                    System.out.println( "waitingForConnectionFailureTotal=="  + waitingForConnectionFailureTotal);
                    System.out.println( "waitSecondsHighCount=="  + waitSecondsHighCount);
                   
                 
                }
            }
        }
        public  static  void  main(String[] args)  throws  Exception {
          
               String hostname =  "10.111.131.50" ;
               String portString =  "7001" ;
               String username =  "weblogic" ;
               String password =  "weblogic123" ;
               PrintJdbc s =  new  PrintJdbc();
               initConnection(hostname, portString, username, password);
               s.printJdbc();
               connector.close();
            }
        
        
}

  2、有关thread的监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package  test;
 
import  java.io.IOException;
import  java.net.MalformedURLException;
import  java.util.Hashtable;
 
import  javax.management.MBeanServerConnection;
import  javax.management.MalformedObjectNameException;
import  javax.management.ObjectName;
import  javax.management.remote.JMXConnector;
import  javax.management.remote.JMXConnectorFactory;
import  javax.management.remote.JMXServiceURL;
import  javax.naming.Context;
 
public  class  PrintThread {
 
     private  static  MBeanServerConnection connection;
     private  static  JMXConnector connector;
     private  static  final  ObjectName service;
     
 
        static  {
           try  {
              service =  new  ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean" );
           } catch  (MalformedObjectNameException e) {
              throw  new  AssertionError(e.getMessage());
           }
        }
      
        public  static  void  initConnection(String hostname, String portString,
           String username, String password)  throws  IOException,
           MalformedURLException {
           String protocol =  "t3" ;
           Integer portInteger = Integer.valueOf(portString);
           int  port = portInteger.intValue();
           String jndiroot =  "/jndi/" ;
           String mserver =  "weblogic.management.mbeanservers.runtime" ;
           JMXServiceURL serviceURL =  new  JMXServiceURL(protocol, hostname,
              port, jndiroot + mserver);
           Hashtable h =  new  Hashtable();
           h.put(Context.SECURITY_PRINCIPAL, username);
           h.put(Context.SECURITY_CREDENTIALS, password);
           h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
              "weblogic.management.remote" );
           connector = JMXConnectorFactory.connect(serviceURL, h);
           connection = connector.getMBeanServerConnection();
        }
     
        public  static  ObjectName getServerRuntimes()  throws  Exception {
             return  (ObjectName)connection.getAttribute(service,
                   "ServerRuntime" );
        }
        
        public  void  printInfo()  throws  Exception{
            ObjectName objThreadPool =  null ;
            ObjectName serverRT = getServerRuntimes();
            
            objThreadPool = (ObjectName) connection.getAttribute(serverRT,  "ThreadPoolRuntime" );
            double  throughput  = Double.parseDouble(String.valueOf(connection.getAttribute(
                    objThreadPool, "Throughput" )));
            System.out.println( "throughput:"  + throughput);
            
            int  executeThreadTotalCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool,  "ExecuteThreadTotalCount" )));
            System.out.println( "executeThreadTotalCount:"  + executeThreadTotalCount);
          
         
            int  executeThreadIdleCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool,  "ExecuteThreadIdleCount" )));
            System.out.println( "executeThreadIdleCount:"  + executeThreadIdleCount);
           
            int  StandbyThreadCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool,  "StandbyThreadCount" )));
            System.out.println( "StandbyThreadCount:"  + StandbyThreadCount);
            
            
            long  timestamp = System.currentTimeMillis() /  1000 ;
             String metricJson =  "" ;
             String jsonFormat =  "{\"name\": \"weblogic_threadpool_metric_demo\", "  +
                     "\"command\":\"weblogic_threadpool_metric\","  +
                     "\"type\": \"metric\","  +
                     "\"handlers\": [\"influxdb\"]," +
                     "\"output\": \"%s %.5f %d\\n%s %d %d\\n%s %d %d\\n\","  +
                     "\"status\": 0}" ;
             metricJson = String.format(jsonFormat,
                       ".weblogic.threadpool.throughput" , ( double )throughput, timestamp,
                       ".weblogic.threadpool.executeThreadTotalCount" , ( int )executeThreadTotalCount, timestamp,
                       ".weblogic.threadpool.executeThreadIdleCount" , ( int )executeThreadIdleCount, timestamp);
             System.out.println( "metricJson=="  + metricJson);
             
             
             
        }
        
        
     public  static  void  main(String args[])  throws  Exception {
          String hostname =  "10.111.131.50" ;
           String portString =  "7001" ;
           String username =  "weblogic" ;
           String password =  "weblogic123" ;
           PrintThread s =  new  PrintThread();
           initConnection(hostname, portString, username, password);
           s.printInfo();
           connector.close();
     }
}

  3、有关session的监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package  test;
 
import  java.io.IOException;
import  java.net.MalformedURLException;
import  java.util.Hashtable;
 
import  javax.management.MBeanServerConnection;
import  javax.management.MalformedObjectNameException;
import  javax.management.ObjectName;
import  javax.management.remote.JMXConnector;
import  javax.management.remote.JMXConnectorFactory;
import  javax.management.remote.JMXServiceURL;
import  javax.naming.Context;
 
public  class  PrintServerState {
     private  static  MBeanServerConnection connection;
     private  static  JMXConnector connector;
     private  static  final  ObjectName service;
     
     
        static  {
           try  {
              service =  new  ObjectName( "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean" );
           } catch  (MalformedObjectNameException e) {
              throw  new  AssertionError(e.getMessage());
           }
        }
        
        public  static  void  initConnection(String hostname, String portString,
           String username, String password)  throws  IOException,
           MalformedURLException {
           String protocol =  "t3" ;
           Integer portInteger = Integer.valueOf(portString);
           int  port = portInteger.intValue();
           String jndiroot =  "/jndi/" ;
           String mserver =  "weblogic.management.mbeanservers.domainruntime" ;
           JMXServiceURL serviceURL =  new  JMXServiceURL(protocol, hostname,
              port, jndiroot + mserver);
           Hashtable h =  new  Hashtable();
           h.put(Context.SECURITY_PRINCIPAL, username);
           h.put(Context.SECURITY_CREDENTIALS, password);
           h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
              "weblogic.management.remote" );
           connector = JMXConnectorFactory.connect(serviceURL, h);
           connection = connector.getMBeanServerConnection();
        }
        
        public  static  ObjectName[] getServerRuntimes()  throws  Exception {
           return  (ObjectName[]) connection.getAttribute(service,
              "ServerRuntimes" );
        }
        
        public  static  ObjectName[] getApplicationRuntimes(ObjectName serverRuntime)  throws  Exception {
            return  (ObjectName[])connection.getAttribute(serverRuntime,  "ApplicationRuntimes" );
        }
        
        public  static  ObjectName[] getComponentRuntimes(ObjectName componentRutime)  throws  Exception {
            return  (ObjectName[]) connection.getAttribute(componentRutime,  "ComponentRuntimes" );
        }
        
        
        public  void  printSessionState()  throws  Exception {
            ObjectName[] serverRT = getServerRuntimes();
            int  length = ( int ) serverRT.length;
           
            for ( int  i =  0 ;i
                ObjectName[] applicationRT = getApplicationRuntimes(serverRT[i]);
                int  length_app = ( int )applicationRT.length;
              
                for ( int  y =  0 ;y
                    String applicationName =  (String)connection.getAttribute(applicationRT[y],  "ApplicationName" );
                   
                    if (applicationName.equals( "helloworld" )){
                       
                    ObjectName[]  componentRuntime = getComponentRuntimes(applicationRT[y]);
                    int  length_component = ( int )componentRuntime.length;
                    System.out.println( "length_component=="  + length_component);
                    for ( int  z =  0 ;z
                        

你可能感兴趣的:(weblogic)