Cobar.py
12.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import collectd
import MySQLdb
import copy
import re
Cobar_config = {
'host': '127.0.0.1',
'port': '9066',
'user': 'test',
'pwd': 'test',
'tag': '0'
}
#Cobar_vars = {
# 'tp_active_count': 'gauge',
# 'tp_task_queue_size': 'gauge',
# 'tp_task_complete_size': 'gauge',
#'con_net_in': 'gauge',
#'con_net_out': 'gauge',
#'con_fc_count': 'gauge',
#'sch_max_concurrent': 'gauge',
#'sch_rcount': 'gauge',
#'sch_wcount': 'gauge',
#'sch_net_in': 'gauge',
#'sch_net_out': 'gauge',
#'sch_cobar_10': 'gauge',
#'sch_cobar_200': 'gauge',
#'sch_cobar_1000': 'gauge',
#'sch_cobar_2000': 'gauge',
#'sch_node_10': 'gauge',
#'sch_node_200': 'gauge',
#'sch_node_1000': 'gauge',
#'sch_node_2000': 'gauge',
#'tb_max_concurrent': 'gauge',
#'tb_rcount': 'gauge',
#'tb_wcount': 'gauge',
#'tb_cobar_10': 'gauge',
#'tb_cobar_200': 'gauge',
#'tb_cobar_1000': 'gauge',
#'tb_cobar_2000': 'gauge',
#'tb_node_10': 'gauge',
#'tb_node_200': 'gauge',
#'tb_node_1000': 'gauge',
#'tb_node_2000': 'gauge',
#}
class CobarTpStates(object):
tp_active_count = 0
tp_task_queue_size = 0
tp_task_complete_size = 0
g_tp_states = CobarTpStates()
class CobarConnStates(object):
con_net_in = 0
con_net_out = 0
con_fc_count = 0
g_con_states = CobarConnStates()
class CobarSchemaStates(object):
schema = ''
sch_max_concurrent = 0
sch_rcount = 0
sch_wcount = 0
sch_net_in = 0
sch_net_out = 0
sch_cobar_10 = 0
sch_cobar_50 = 0
sch_cobar_200 = 0
sch_cobar_1000 = 0
sch_cobar_2000 = 0
sch_node_10 = 0
sch_node_50 = 0
sch_node_200 = 0
sch_node_1000 = 0
sch_node_2000 = 0
g_schema_states = dict()
class CobarTableStates(object):
table = ''
tb_rcount = 0
tb_wcount = 0
tb_cobar_10 = 0
tb_cobar_50 = 0
tb_cobar_200 = 0
tb_cobar_1000 = 0
tb_cobar_2000 = 0
tb_node_10 = 0
tb_node_50 = 0
tb_node_200 = 0
tb_node_1000 = 0
tb_node_2000 = 0
g_table_states = dict()
def get_cobar_tp_states(con):
res = get_cobar_query(con, 'show @@threadpool')
cur_states = CobarTpStates()
for row in res.fetchall():
if str(row['NAME']).startswith('Process'):
cur_states.tp_active_count = cur_states.tp_active_count + row['ACTIVE_COUNT']
cur_states.tp_task_queue_size = cur_states.tp_task_queue_size + row['TASK_QUEUE_SIZE']
cur_states.tp_task_complete_size = cur_states.tp_task_complete_size + row['COMPLETED_TASK']
global g_tp_states
tp_states = CobarTpStates()
tp_states.tp_active_count = cur_states.tp_active_count
collectd.info("global:"+str(g_tp_states.tp_task_complete_size));
collectd.info("cur:"+str(cur_states.tp_task_complete_size));
tp_states.tp_task_complete_size = cur_states.tp_task_complete_size - g_tp_states.tp_task_complete_size
tp_states.tp_task_queue_size = cur_states.tp_task_queue_size - g_tp_states.tp_task_queue_size
g_tp_states = copy.deepcopy(cur_states)
dispatch_states(tp_states)
def get_cobar_con_states(con):
res = get_cobar_query(con, 'show @@processor')
cur_states = CobarConnStates()
for row in res.fetchall():
if str(row['NAME']).startswith('Process'):
cur_states.con_fc_count = cur_states.con_fc_count + row['FC_COUNT']
cur_states.con_net_in = cur_states.con_net_in + row['NET_IN']
cur_states.con_net_out = cur_states.con_net_out + row['NET_OUT']
global g_con_states
collectd.info("global:"+str(g_con_states.con_net_in))
collectd.info("curr:"+str(cur_states.con_net_in))
con_states = CobarConnStates()
con_states.con_fc_count = cur_states.con_fc_count
con_states.con_net_in = cur_states.con_net_in - g_con_states.con_net_in
con_states.con_net_out = cur_states.con_net_out - g_con_states.con_net_out
g_con_states = copy.deepcopy(cur_states)
dispatch_states(con_states)
def get_cobar_tb_states(con):
res = get_cobar_query(con, 'show @@sql.stat.table')
global g_table_states
for row in res.fetchall():
tmp_tb_states = CobarTableStates()
tmp_tb_states.table = row['TABLE'].lower()
tmp_tb_states.tb_rcount = int(row['R'])
tmp_tb_states.tb_wcount = int(row['W'])
tmp_ttl_tb = row['ALL_TTL_COUNT']
tmp_ttl_tb = tmp_ttl_tb.lstrip('[')
tmp_ttl_tb = tmp_ttl_tb.rstrip(']')
ttl_tb = re.split(r'[;,\s]\s*', tmp_ttl_tb)
tmp_tb_states.tb_cobar_10 = int(ttl_tb[0])
tmp_tb_states.tb_cobar_50 = int(ttl_tb[1])
tmp_tb_states.tb_cobar_200 = int(ttl_tb[2])
tmp_tb_states.tb_cobar_1000 = int(ttl_tb[3])
tmp_tb_states.tb_cobar_2000 = int(ttl_tb[4])
tmp_ttl_tb = row['NODE_TTL_COUNT']
tmp_ttl_tb = tmp_ttl_tb.lstrip('[')
tmp_ttl_tb = tmp_ttl_tb.rstrip(']')
ttl_tb = re.split(r'[;,\s]\s*', tmp_ttl_tb)
tmp_tb_states.tb_node_10 = int(ttl_tb[0])
tmp_tb_states.tb_node_50 = int(ttl_tb[1])
tmp_tb_states.tb_node_200 = int(ttl_tb[2])
tmp_tb_states.tb_node_1000 = int(ttl_tb[3])
tmp_tb_states.tb_node_2000 = int(ttl_tb[4])
old_states = g_table_states.get(tmp_tb_states.table)
if old_states is None:
g_table_states[tmp_tb_states.table] = tmp_tb_states
# dispatch_states(tmp_tb_states.table, tmp_tb_states)
else:
tb_states = CobarTableStates()
tb_states.table = tmp_tb_states.table
tb_states.tb_rcount = tmp_tb_states.tb_rcount - old_states.tb_rcount
tb_states.tb_wcount = tmp_tb_states.tb_wcount - old_states.tb_wcount
tb_states.tb_cobar_10 = tmp_tb_states.tb_cobar_10 - old_states.tb_cobar_10
tb_states.tb_cobar_50 = tmp_tb_states.tb_cobar_50 - old_states.tb_cobar_50
tb_states.tb_cobar_200 = tmp_tb_states.tb_cobar_200 - old_states.tb_cobar_200
tb_states.tb_cobar_1000 = tmp_tb_states.tb_cobar_1000 - old_states.tb_cobar_1000
tb_states.tb_cobar_2000 = tmp_tb_states.tb_cobar_2000 - old_states.tb_cobar_2000
tb_states.tb_node_10 = tmp_tb_states.tb_node_10 - old_states.tb_node_10
tb_states.tb_node_50 = tmp_tb_states.tb_node_50 - old_states.tb_node_50
tb_states.tb_node_200 = tmp_tb_states.tb_node_200 - old_states.tb_node_200
tb_states.tb_node_1000 = tmp_tb_states.tb_node_1000 - old_states.tb_node_1000
tb_states.tb_node_2000 = tmp_tb_states.tb_node_2000 - old_states.tb_node_2000
g_table_states[tmp_tb_states.table] = copy.deepcopy(tmp_tb_states)
dispatch_nstates('table', tb_states)
def get_cobar_schema_states(con):
res = get_cobar_query(con, 'show @@sql.stat.schema')
global g_schema_states
for row in res.fetchall():
tmp_schema_states = CobarSchemaStates()
tmp_schema_states.schema = row['SCHEMA'].lower()
tmp_schema_states.sch_max_concurrent = int(row['MAX'])
tmp_schema_states.sch_net_in = int(row['NET_IN'])
tmp_schema_states.sch_net_out = int(row['NET_OUT'])
tmp_schema_states.sch_rcount = int(row['R'])
tmp_schema_states.sch_wcount = int(row['W'])
tmp_ttl_sch = row['ALL_TTL_COUNT']
tmp_ttl_sch = tmp_ttl_sch.lstrip('[')
tmp_ttl_sch = tmp_ttl_sch.rstrip(']')
ttl_sch = re.split(r'[;,\s]\s*', tmp_ttl_sch)
tmp_schema_states.sch_cobar_10 = int(ttl_sch[0])
tmp_schema_states.sch_cobar_50 = int(ttl_sch[1])
tmp_schema_states.sch_cobar_200 = int(ttl_sch[2])
tmp_schema_states.sch_cobar_1000 = int(ttl_sch[3])
tmp_schema_states.sch_cobar_2000 = int(ttl_sch[4])
tmp_ttl_sch = row['NODE_TTL_COUNT']
tmp_ttl_sch = tmp_ttl_sch.lstrip('[')
tmp_ttl_sch = tmp_ttl_sch.rstrip(']')
ttl_sch = re.split(r'[;,\s]\s*', tmp_ttl_sch)
tmp_schema_states.sch_node_10 = int(ttl_sch[0])
tmp_schema_states.sch_node_50 = int(ttl_sch[1])
tmp_schema_states.sch_node_200 = int(ttl_sch[2])
tmp_schema_states.sch_node_1000 = int(ttl_sch[3])
tmp_schema_states.sch_node_2000 = int(ttl_sch[4])
old_states = g_schema_states.get(tmp_schema_states.schema)
if old_states is None:
g_schema_states[tmp_schema_states.schema] = tmp_schema_states
# dispatch_states(tmp_schema_states.schema, tmp_schema_states)
else:
sch_states = CobarSchemaStates()
sch_states.schema = tmp_schema_states.schema
sch_states.sch_max_concurrent = tmp_schema_states.sch_max_concurrent
sch_states.sch_net_in = tmp_schema_states.sch_net_in - old_states.sch_net_in
sch_states.sch_net_out = tmp_schema_states.sch_net_out - old_states.sch_net_out
sch_states.sch_wcount = tmp_schema_states.sch_wcount - old_states.sch_wcount
sch_states.sch_rcount = tmp_schema_states.sch_rcount - old_states.sch_rcount
sch_states.sch_cobar_10 = tmp_schema_states.sch_cobar_10 - old_states.sch_cobar_10
sch_states.sch_cobar_50 = tmp_schema_states.sch_cobar_50 - old_states.sch_cobar_50
sch_states.sch_cobar_200 = tmp_schema_states.sch_cobar_200 - old_states.sch_cobar_200
sch_states.sch_cobar_1000 = tmp_schema_states.sch_cobar_1000 - old_states.sch_cobar_1000
sch_states.sch_cobar_2000 = tmp_schema_states.sch_cobar_2000 - old_states.sch_cobar_2000
sch_states.sch_node_10 = tmp_schema_states.sch_node_10 - old_states.sch_node_10
sch_states.sch_node_50 = tmp_schema_states.sch_node_50 - old_states.sch_node_50
sch_states.sch_node_200 = tmp_schema_states.sch_node_200 - old_states.sch_node_200
sch_states.sch_node_1000 = tmp_schema_states.sch_node_1000 - old_states.sch_node_1000
sch_states.sch_node_2000 = tmp_schema_states.sch_node_2000 - old_states.sch_node_2000
g_schema_states[tmp_schema_states.schema] = copy.deepcopy(tmp_schema_states)
dispatch_nstates('schema', sch_states)
def configure_callback(conf=None):
global Cobar_config
for c in conf.children:
key = c.key.lower()
value = c.values[0]
Cobar_config[key] = value
Cobar_config['port'] = int(Cobar_config['port'])
def get_cobar_con():
return MySQLdb.connect(host=Cobar_config['host'],
port=Cobar_config['port'],
user=Cobar_config['user'],
passwd=Cobar_config['pwd'])
def get_cobar_query(con, query):
cur = con.cursor(MySQLdb.cursors.DictCursor)
cur.execute(query)
return cur
def dispatch_states(states):
if not states:
return
data_dic = vars(states).iteritems()
for key, value in data_dic:
dispatch_value(key, value, key, None)
# type-table_test01 type_instance-maxConcurrent
def dispatch_nstates(type, states):
pass
if not states:
return
prefix = ''
if type == 'schema':
prefix = states.schema
else:
prefix = states.table
data_dic = vars(states).iteritems()
for key, value in data_dic:
if str(key) == 'schema' or str(key) == 'table':
continue
dispatch_value(key, value, type, prefix)
def dispatch_value(key, value, type, type_instance=None):
if not type_instance:
type_instance = key
else:
type_instance = type_instance+'_'+key
if value is None:
return
try:
value = int(value)
except ValueError:
value = float(value)
val = collectd.Values(plugin='cobar', plugin_instance=Cobar_config['tag'])
val.type = type
val.type_instance = type_instance
val.values = [value]
val.dispatch()
def read_callback():
pass
collectd.info("user read info....")
connection = get_cobar_con()
get_cobar_tp_states(connection)
get_cobar_con_states(connection)
get_cobar_schema_states(connection)
get_cobar_tb_states(connection)
def write_callback(vl):
collectd.info('cb: %s' % vl)
collectd.register_config(configure_callback)
collectd.register_read(read_callback)
#collectd.register_write(write_callback)