forked from farshidce/pystatcollector
/
stats.py
557 lines (498 loc) · 22.1 KB
/
stats.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import sys
import uuid
sys.path.append(".")
sys.path.append("lib")
import logging.config
from logging import config
import json
import logger
from optparse import OptionParser
import os
import re
from threading import Thread
import time
from input_parser import TestInputParser
from rest_client import RestConnection
from remote_util import RemoteMachineShellConnection, RemoteMachineHelper
import testconstants
import gzip
logging.config.fileConfig("logging.conf.sample")
# The histo dict is returned by add_timing_sample().
# The percentiles must be sorted, ascending, like [0.90, 0.99].
def histo_percentile(histo, percentiles):
v_sum = 0
bins = histo.keys()
bins.sort()
for bin in bins:
v_sum += histo[bin]
v_sum = float(v_sum)
v_cur = 0 # Running total.
rv = []
for bin in bins:
if not percentiles:
return rv
v_cur += histo[bin]
while percentiles and (v_cur / v_sum) >= percentiles[0]:
rv.append((percentiles[0], bin))
percentiles.pop(0)
return rv
class StatsCollector(object):
_task = {}
_verbosity = True
def __init__(self, verbosity):
self._verbosity = verbosity
self.is_leader = False
self.log = logger.Logger.get_logger()
#this function starts collecting stats from all nodes with the given
#frequency
def start(self, nodes, bucket, pnames, name, frequency, client_id='',
collect_server_stats=True):
self._task = {"state": "running", "threads": [],
"name": name, "time": time.time(),
"ops": [], "totalops": [],
"ops-temp": [], "latency": {},
"data_size_stats": []}
rest = RestConnection(nodes[0])
info = rest.get_nodes_self()
self.data_path = info.storage[0].get_data_path()
self.client_id = str(client_id)
if collect_server_stats:
mbstats_thread = Thread(target=self.membase_stats,
args=(nodes, bucket, frequency, self._verbosity))
mbstats_thread.start()
sysstats_thread = Thread(target=self.system_stats,
args=(nodes, pnames, frequency, self._verbosity))
sysstats_thread.start()
diskstats_thread = Thread(target=self.disk_stats,
args=(nodes, frequency, self._verbosity))
diskstats_thread.start()
ns_server_stats_thread = Thread(target=self.ns_server_stats,
args=([nodes[0]], bucket, 10, self._verbosity))
ns_server_stats_thread.start()
rest = RestConnection(nodes[0])
bucket_size_thead = Thread(target=self.get_bucket_size,
args=(bucket, rest, frequency))
bucket_size_thead.start()
#data_size_thread = Thread(target=self.get_data_file_size,
# args=(nodes, 60, bucket))
#data_size_thread.start()
self._task["threads"] = [sysstats_thread, ns_server_stats_thread, bucket_size_thead,
mbstats_thread]
#data_size_thread ]
# Getting build/machine stats from only one node in the cluster
self.build_stats(nodes)
self.machine_stats(nodes)
def stop(self):
self.log.info("stopping the stat collection")
self._task["state"] = "stopped"
for t in self._task["threads"]:
t.join()
self._task["time"] = time.time() - self._task["time"]
def sample(self, cur):
pass
def export(self, name, test_params):
for latency in self._task["latency"].keys():
histos = self._task["latency"].get(latency, [])
key = 'percentile-' + latency
self._task["latency"][key] = []
for histo in histos:
temp = []
delta = histo['delta']
del histo['delta']
p = histo_percentile(histo, [0.90, 0.95, 0.99])
# p is list of tuples
for val in p:
temp.append(val[-1])
temp.append(self.client_id)
temp.append(delta)
self._task["latency"][key].append(temp)
obj = {"buildinfo": self._task.get("buildstats", {}),
"machineinfo": self._task.get("machinestats", {}),
"membasestats": self._task.get("membasestats", []),
"systemstats": self._task.get("systemstats", []),
"name": name,
"totalops": self._task["totalops"],
"ops": self._task["ops"],
"time": self._task["time"],
"info": test_params,
"ns_server_data": self._task.get("ns_server_stats", []),
"ns_server_data_system": self._task.get("ns_server_stats_system", []),
"timings": self._task.get("timings", []),
"dispatcher": self._task.get("dispatcher", []),
"tap": self._task.get("tap", []),
"checkpoint": self._task.get("checkpoint", []),
"bucket-size": self._task.get("bucket_size", []),
"data-size": self._task.get("data_size_stats", []),
"latency-set": self._task["latency"].get('percentile-latency-set', []),
"latency-set-recent": self._task["latency"].get('percentile-latency-set-recent', []),
"latency-get": self._task["latency"].get('percentile-latency-get', []),
"latency-get-recent": self._task["latency"].get('percentile-latency-get-recent', []),
"latency-delete": self._task["latency"].get('percentile-latency-delete', []),
"latency-delete-recent": self._task["latency"].get('percentile-latency-delete-recent', []),
}
if self.client_id:
filename = str(self.client_id) + '.loop'
if re.search('load$', self._task["name"]):
filename = str(self.client_id) + '.load'
file = gzip.open("{0}.json.gz".format(filename), 'wb')
file.write("{0}".format(json.dumps(obj)))
file.close()
else:
file = gzip.open("{0}.json.gz".format(name), 'wb')
file.write("{0}".format(json.dumps(obj)))
file.close()
def get_bucket_size(self, bucket, rest, frequency):
self._task["bucket_size"] = []
d = []
while not self._aborted():
self.log.info("collecting bucket size stats from {0}:{1}".format(rest.ip, rest.port))
status, db_size = rest.get_database_disk_size(bucket)
if status:
d.append(db_size)
else:
print "Enable to read bucket stats"
time.sleep(frequency)
self._task["bucket_size"] = d
self.log.info("the thread finished collecting bucket stats")
def get_data_file_size(self, nodes, frequency, bucket):
shells = []
for node in nodes:
try:
shells.append(RemoteMachineShellConnection(node))
except:
pass
paths = []
if shells[0].is_membase_installed():
paths.append(self.data_path + '/{0}-data'.format(bucket))
else:
bucket_path = self.data_path + '/{0}'.format(bucket)
paths.append(bucket_path)
view_path = bucket_path + '/set_view_{0}_design'.format(bucket)
paths.append(view_path)
d = {"snapshots": []}
start_time = str(self._task["time"])
while not self._aborted():
time.sleep(frequency)
current_time = time.time()
i = 0
for shell in shells:
node = nodes[i]
unique_id = node.ip + '-' + start_time
value = {}
for path in paths:
size = shell.get_data_file_size(path)
value["file"] = path.split('/')[-1]
value["size"] = size
value["unique_id"] = unique_id
value["time"] = current_time
value["ip"] = node.ip
d["snapshots"].append(value.copy())
i += 1
self._task["data_size_stats"] = d["snapshots"]
self.log.info(" finished data_size_stats")
#ops stats
#{'tot-sets': 899999, 'tot-gets': 1, 'tot-items': 899999, 'tot-creates': 899999}
def ops_stats(self, ops_stat):
ops_stat["time"] = time.time()
self._task["ops-temp"].append(ops_stat)
if len(self._task["ops-temp"]) >= 100:
merged = self._merge()
self._task["ops"].append(merged)
self._task["ops-temp"] = []
#if self._task["ops"] has more than 1000 elements try to aggregate them ?
def latency_stats(self, latency_cmd, latency_stat):
if self._task["latency"].get(latency_cmd) is None:
self._task["latency"][latency_cmd] = []
temp_latency_stat = latency_stat.copy()
temp_latency_stat['delta'] = time.time() - self._task['time']
self._task["latency"][latency_cmd].append(temp_latency_stat)
def _merge(self):
first = self._task["ops-temp"][0]
merged = {"startTime": first["start-time"]}
totalgets = 0
totalsets = 0
delta = 0
i = 0
for i in range(len(self._task["ops-temp"]) - 1):
current = self._task["ops-temp"][i]
next = self._task["ops-temp"][i + 1]
totalgets += current["tot-gets"]
totalsets += current["tot-sets"]
delta += (next["start-time"] - current["start-time"])
merged["endTime"] = merged["startTime"] + delta
merged["totalSets"] = totalsets
merged["totalGets"] = totalgets
return merged
def total_stats(self, ops_stat):
ops_stat["time"] = time.time()
self._task["totalops"].append(ops_stat)
def build_stats(self, nodes):
json_response = StatUtil.build_info(nodes[0])
self._task["buildstats"] = json_response
def machine_stats(self, nodes):
try:
machine_stats = StatUtil.machine_info(nodes[0])
self._task["machinestats"] = machine_stats
except:
self._aborted()
def _extract_proc_info(self, shell, pid):
o, r = shell.execute_command("cat /proc/{0}/stat".format(pid))
fields = ('pid comm state ppid pgrp session tty_nr tpgid flags minflt '
'cminflt majflt cmajflt utime stime cutime cstime priority '
'nice num_threads itrealvalue starttime vsize rss rsslim '
'startcode endcode startstack kstkesp kstkeip signal blocked '
'sigignore sigcatch wchan nswap cnswap exit_signal '
'processor rt_priority policy delayacct_blkio_ticks '
'guest_time cguest_time ').split(' ')
d = dict(zip(fields, o[0].split(' ')))
return d
def _extrace_disk_states(self, shell):
o, r = shell.execute_command("iostat -dkx 1 2 /dev/sda2")
fields = ('rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util').split(' ')
y = []
for x in o[6]:
if x and x != ' ':
y.extend(x)
d = dict(zip(fields, y))
return d
def disk_stats(self, nodes, frequency, verbosity=False):
shells = []
for node in nodes:
try:
bucket = RestConnection(node).get_buckets()[0].name
MemcachedClientHelper.direct_client(node, bucket)
shells.append(RemoteMachineShellConnection(node))
except:
pass
d = {"snapshots": []}
# "pname":"x","pid":"y","snapshots":[{"time":time,"value":value}]
start_time = str(self._task["time"])
while not self._aborted():
time.sleep(frequency)
i = 0
for shell in shells:
node = nodes[i]
unique_id = node.ip + '-' + start_time
value = self._extrace_disk_states(shell)
value["unique_id"] = unique_id
value["time"] = time.time()
value["ip"] = node.ip
d["snapshots"].append(value)
i += 1
self._task["diskstats"] = d["snapshots"]
self.log.info(" finished diskstats")
def system_stats(self, nodes, pnames, frequency, verbosity=False):
try:
shells = []
for node in nodes:
try:
bucket = RestConnection(node).get_buckets()[0].name
MemcachedClientHelper.direct_client(node, bucket)
shells.append(RemoteMachineShellConnection(node))
except:
pass
d = {"snapshots": []}
# "pname":"x","pid":"y","snapshots":[{"time":time,"value":value}]
start_time = str(self._task["time"])
while not self._aborted():
time.sleep(frequency)
current_time = time.time()
i = 0
for shell in shells:
node = nodes[i]
unique_id = node.ip + '-' + start_time
for pname in pnames:
obj = RemoteMachineHelper(shell).is_process_running(pname)
if obj and obj.pid:
value = self._extract_proc_info(shell, obj.pid)
value["name"] = pname
value["id"] = obj.pid
value["unique_id"] = unique_id
value["time"] = current_time
value["ip"] = node.ip
d["snapshots"].append(value)
i += 1
self._task["systemstats"] = d["snapshots"]
self.log.info(" finished system_stats")
except:
self._aborted()
def couchdb_stats(nodes):
pass
def membase_stats(self, nodes, bucket, frequency, verbose=False):
try:
mcs = []
for node in nodes:
try:
bucket = RestConnection(node).get_buckets()[0].name
mcs.append(MemcachedClientHelper.direct_client(node, bucket))
except:
pass
self._task["membasestats"] = []
self._task["timings"] = []
self._task["dispatcher"] = []
self._task["tap"] = []
self._task["checkpoint"] = []
d = {}
# "pname":"x","pid":"y","snapshots":[{"time":time,"value":value}]
for mc in mcs:
d[mc.host] = {"snapshots": [], "timings": [], "dispatcher": [], "tap": [], "checkpoint": []}
while not self._aborted():
time_left = frequency
# at minimum we want to check for aborted every minute
while not self._aborted() and time_left > 0:
time.sleep(min(time_left, 60))
if time_left >= 60:
time_left -= 60
else:
time_left = 0
for mc in mcs:
stats = mc.stats()
stats["time"] = time.time()
stats["ip"] = mc.host
d[mc.host]["snapshots"].append(stats)
timings = mc.stats('timings')
d[mc.host]["timings"].append(timings)
dispatcher = mc.stats('dispatcher')
d[mc.host]["dispatcher"].append(dispatcher)
tap = mc.stats('tap')
d[mc.host]["tap"].append(tap)
checkpoint = mc.stats('checkpoint')
d[mc.host]["checkpoint"].append(checkpoint)
print len(d[mc.host]["checkpoint"])
start_time = str(self._task["time"])
for mc in mcs:
ip = mc.host
unique_id = ip + '-' + start_time
current_time = time.time()
for snapshot in d[mc.host]["snapshots"]:
snapshot['unique_id'] = unique_id
snapshot['time'] = current_time
snapshot['ip'] = ip
self._task["membasestats"].append(snapshot)
for timing in d[mc.host]["timings"]:
timing['unique_id'] = unique_id
timing['time'] = current_time
timing['ip'] = ip
self._task["timings"].append(timing)
for dispatcher in d[mc.host]["dispatcher"]:
dispatcher['unique_id'] = unique_id
dispatcher['time'] = current_time
dispatcher['ip'] = ip
self._task["dispatcher"].append(dispatcher)
for tap in d[mc.host]["tap"]:
tap['unique_id'] = unique_id
tap['time'] = current_time
tap['ip'] = ip
self._task["tap"].append(tap)
for checkpoint in d[mc.host]["checkpoint"]:
checkpoint['unique_id'] = unique_id
checkpoint['time'] = current_time
checkpoint['ip'] = ip
self._task["checkpoint"].append(checkpoint)
self.log.info(" finished membase_stats")
except:
self._aborted()
def ns_server_stats(self, nodes, bucket, frequency, verbose=False):
try:
self._task["ns_server_stats"] = []
self._task["ns_server_stats_system"] = []
d = {}
for node in nodes:
d[node] = {"snapshots": [], "system_snapshots": []}
while not self._aborted():
time.sleep(frequency)
self.log.info("collecting ns_server_stats from all nodes in the cluster")
for node in nodes:
self.log.info("running ")
os.system(
"curl -X GET http://{2}:{3}@{1}:8091/pools/{0}/buckets/{0}/stats?zoom=minute -o ns_server_data".format(
bucket, node.ip,nodes[0].rest_username,nodes[0].rest_password))
#f.close()
dict = open("./ns_server_data", "r").read()
data_json = json.loads(dict)
d[node]["snapshots"].append(data_json)
os.system(
"curl -X GET http://{2}:{3}@{1}:8091/pools/{0} -o ns_server_data_system_stats".format(
bucket, node.ip,nodes[0].rest_username,nodes[0].rest_password))
#f.close()
dict = open("./ns_server_data_system_stats", "r").read()
data_json = json.loads(dict)
d[node]["system_snapshots"].append(data_json)
for node in nodes:
for snapshot in d[node]["snapshots"]:
self._task["ns_server_stats"].append(snapshot)
for snapshot in d[node]["system_snapshots"]:
self._task["ns_server_stats_system"].append(snapshot)
self.log.info(" finished ns_server_stats")
except:
self._aborted()
def _aborted(self):
return self._task["state"] == "stopped"
# Invokes optional callback when registered levels have been reached
# during stats sample()'ing.
#
class CallbackStatsCollector(StatsCollector):
def __init__(self, verbosity):
# Tuples of level_name, level, callback.
self.level_callbacks = []
super(CallbackStatsCollector, self).__init__(verbosity)
def sample(self, cur):
for level_name, level, callback in self.level_callbacks:
if level < cur.get(level_name, -1):
callback(cur)
return super(CallbackStatsCollector, self).sample(cur)
class StatUtil(object):
@staticmethod
def build_info(node):
rest = RestConnection(node)
api = rest.baseUrl + 'nodes/self'
status, content = rest._http_request(api)
json_parsed = json.loads(content)
return json_parsed
@staticmethod
def machine_info(node):
shell = RemoteMachineShellConnection(node)
info = shell.extract_remote_info()
return {"type": info.type, "distribution": info.distribution_type,
"version": info.distribution_version, "ram": info.ram,
"cpu": info.cpu, "disk": info.disk, "hostname": info.hostname}
if __name__ == "__main__":
parser = OptionParser()
uid = str(uuid.uuid4())[0:7]
parser.add_option("-f", "--frequency", dest="frequency", default="10")
parser.add_option("-n", "--name", dest="name", default="standalone-statscollection-{0}".format(uid))
parser.add_option("-d", "--duration", dest="duration", default="60")
parser.add_option("-o", "--outputfile", dest="output", default="stats")
parser.add_option("-i", "--ini",
dest="ini", help="Path to .ini file containing server information,e.g -i tmp/local.ini")
parser.add_option("-p", "--params",
dest="params", help="Optional key=value parameters, comma-separated -p k=v,k2=v2,...",
default="")
options, args = parser.parse_args()
if not options.ini:
parser.error("you need to specify a resource file")
parser.print_help()
ini_file = options.ini
frequency = int(options.frequency)
#capture all these stats into
sc = StatsCollector(False)
input = TestInputParser.parse_from_file(ini_file)
nodes = input.servers
input.test_params['test_name'] = options.name
input.test_params['test_time'] = time.time()
sc.start(nodes, "default", ["memcached", "beam.smp"],
str(uuid.uuid4())[0:7], frequency, options.output, True)
try:
time.sleep(int(options.duration))
sc.stop()
except KeyboardInterrupt:
print "stopping stat collectors"
if sc:
sc.stop()
except:
print "stopping stat collectors"
if sc:
sc.stop()
if sc:
print "exporting the tdata to {0}".format(options.output)
sc.export(options.output, input.test_params)