-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjbosscli.py
More file actions
executable file
·215 lines (169 loc) · 7.08 KB
/
Copy pathjbosscli.py
File metadata and controls
executable file
·215 lines (169 loc) · 7.08 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding: utf-8 -*
import sys
import logging
import json
import requests
from tabulate import tabulate
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("jbosscli")
# disable requests logging
logging.getLogger("requests").setLevel(logging.CRITICAL)
class JBossHelper(object):
"""JBoss Application ServerのREST管理APIを簡単に利用するためのヘルパークラス。"""
def __init__(self, url="http://localhost:9990/management", auth="admin:admin"):
self.url = url
self.credentials = auth.split(":")
self._get_server_info()
def _get_server_info(self):
command = {"operation":"read-resource"}
result = self._invoke(command)
result = result['result']
self.product_name = result['product-name']
self.product_version = result['product-version']
self.release_codename = result['release-codename']
self.release_version = result['release-version']
def _invoke(self, command):
log.debug("Making request to %s with command %s"%(self.url, command))
headers = {"Content-type": "application/json"}
r = requests.post(
self.url,
data=json.dumps(command),
headers=headers,
auth=requests.auth.HTTPDigestAuth(self.credentials[0], self.credentials[1])
)
log.debug("Response code: %s"%r.status_code)
if (r.status_code >= 400 and not r.text):
raise JBossException("Got %s code from server."%(r.status_code))
response = r.json()
if 'outcome' not in response:
raise JBossException("Unknown response: %s"%(r.text), response)
if response['outcome'] != 'success':
raise JBossException(response['failure-description'], response)
return response
def __str__(self):
msg = "Connected to %s\nServer: %s (%s)"%(self.url, self.product_name, self.product_version)
return msg
def _get_mbean(self, mbean_type):
command = {
"operation":"read-resource",
"include-runtime":"true",
"address":[
{"core-service":"platform-mbean"},
{"type":mbean_type}
]
}
return self._invoke(command)
def get_used_heap(self):
"""使用しているヒープメモリと最大ヒープメモリサイズを取得する。
Returns:
使用ヒープ(バイト)と最大ヒープサイズ(バイト)
"""
result = self._get_mbean('memory')
heap_memory_usage = result['result']['heap-memory-usage']
used_heap = heap_memory_usage['used']
max_heap = heap_memory_usage['max']
return (used_heap, max_heap)
def get_thread_count(self):
"""稼働しているスレッド数と過去の最大スレッド数を取得する。
Returns:
スレッド数と過去の最大スレッド数
"""
result = self._get_mbean('threading')
thread_count = result['result']['thread-count']
peak_thread_count = result['result']['peak-thread-count']
return (thread_count, peak_thread_count)
def get_jboss_status(self):
"""サーバ状況(running, stoppedなど)を取得する。"""
command = {
"operation":"read-attribute",
"name":"server-state",
"include-runtime":"true"
}
result = self._invoke(command)
return result['result']
def list_mdbs_by_deployment(self, deployment):
"""指定したdeploymentに対してMDB名をリスト化する。
Args:
deployment (str): デプロイメント名
"""
command = {
"operation":"read-children-names",
"child-type":"message-driven-bean",
"address":[
{"deployment":deployment},
{"subsystem":"ejb3"}
]
}
result = self._invoke(command)
return result['result']
def get_mdbs_by_deployment(self, deployment):
"""指定したデプロイメントに対してMDBの統計情報などを取得し、リストとして返す。
Args:
deployment (str): デプロイメント名
Returns:
MessageDrivenBeanのリスト
"""
command = {
"operation":"read-children-resources",
"child-type":"message-driven-bean",
"include-runtime":"true",
"address":[
{"deployment":deployment},
{"subsystem":"ejb3"}
]
}
result = self._invoke(command)
result = result['result']
mdbs = []
for bean in result:
info = result[bean]
mdb = MessageDrivenBean(bean, info['invocations'], info['delivery-active'], info['pool-current-size'])
mdbs.append(mdb)
return mdbs
def get_mdb_status(self, deployment, mdb_list):
"""MDBのdelivery-active項目を表示する。
Args:
deployment (strg): デプロイメント名
mdb_list (list): MDBの名前
"""
for mdb in mdb_list:
command = {
"operation":"read-resource",
"include-runtime":"true",
"address":[
{'deployment':deployment},
{"subsystem":"ejb3"},
{"message-driven-bean":mdb}
]
}
result = self._invoke(command)
print("MDB[%s]: %s"%(mdb,result['result']['delivery-active']))
class MessageDrivenBean(object):
def __init__(self, name, invocations, delivery, pool_count):
self.name = name
self.invocations = invocations
self.delivery = delivery
self.pool_count = pool_count
def __str__(self):
return "%s: Invocations = %s, Delivery = %s, Pool Count = %s"%(self.name, self.invocations, self.delivery, self.pool_count)
class JBossException(Exception):
def __init__(self, msg, raw=None):
self.msg = msg;
self.raw = raw if raw else self.msg
def __str__(self):
return repr(self.msg)
def tabulate_mdb(mdbs, tablefmt='simple'):
table = [[m.name, m.invocations, m.delivery, m.pool_count] for m in mdbs]
print tabulate(table, headers=['Message Driven Bean', 'Invocations', 'Delivery', 'Pool Count'], tablefmt=tablefmt)
if __name__=="__main__":
jboss = JBossHelper(auth="admin:Admin#70365")
print(jboss)
print("JBoss is %s."%jboss.get_jboss_status())
print("HEAP: used:%s max:%s"%jboss.get_used_heap())
print("THREAD: current:%s peak:%s"%jboss.get_thread_count())
print("# get_mdbs_by_deployment()")
mdbs = jboss.get_mdbs_by_deployment("wildfly-helloworld-mdb.war")
tabulate_mdb(mdbs, tablefmt='grid')
print("# MDB Delivery Active?")
jboss.get_mdb_status('wildfly-helloworld-mdb.war',['HelloWorldQueueMDB','HelloWorldQTopicMDB'])