Skip to content

Instantly share code, notes, and snippets.

@ibspoof
Last active December 28, 2019 00:22
Show Gist options
  • Save ibspoof/de7e23b53bfbd5fccf43e1e844f6adab to your computer and use it in GitHub Desktop.
Save ibspoof/de7e23b53bfbd5fccf43e1e844f6adab to your computer and use it in GitHub Desktop.
nodetool tablestat difference tool
#!/usr/bin/env python
import re
import sys
REGEX_KEYSPACE = re.compile(r'^Keyspace: (.*)|^Keyspace : (.*)')
REGEX_TABLE = re.compile(r'^\t\tTable: (.*)')
REGEX_READ_CNT = re.compile(r'^\t\tLocal read count: (.*)')
REGEX_WRITE_CNT = re.compile(r'^\t\tLocal write count: (.*)')
##
## nodetool tablestats > nodetool_tbl_stats_1.txt && sleep 60 && nodetool tablestats > nodetool_tbl_stats_2.txt
##
if __name__ == """__main__""":
table_stats_input_one = open(sys.argv[1]).read()
table_stats_input_two = open(sys.argv[2]).read()
output = 'csv'
if len(sys.argv) > 3:
output = sys.argv[3]
TABLE_STATS_DETAILS = {}
keyspace = None
table = None
for line in table_stats_input_one.split("\n"):
keyspace_regex = REGEX_KEYSPACE.match(line)
if keyspace_regex is not None and keyspace is not keyspace_regex.groups(1):
keyspace = keyspace_regex.groups()[1]
if keyspace not in TABLE_STATS_DETAILS:
TABLE_STATS_DETAILS[keyspace] = {}
table_regex = REGEX_TABLE.match(line)
if table_regex is not None and table is not table_regex.groups(1):
table = table_regex.group(1)
if table not in TABLE_STATS_DETAILS[keyspace]:
TABLE_STATS_DETAILS[keyspace][table] = {'start': {}, 'stop': {}}
read_count_regex = REGEX_READ_CNT.match(line)
if read_count_regex is not None:
read_count = read_count_regex.group(1)
TABLE_STATS_DETAILS[keyspace][table]['start']['read_cnt'] = long(read_count)
write_count_regex = REGEX_WRITE_CNT.match(line)
if write_count_regex is not None:
write_count = write_count_regex.group(1)
TABLE_STATS_DETAILS[keyspace][table]['start']['write_cnt'] = long(write_count)
for line in table_stats_input_two.split("\n"):
keyspace_regex = REGEX_KEYSPACE.match(line)
if keyspace_regex is not None and keyspace is not keyspace_regex.groups(1):
keyspace = keyspace_regex.groups()[1]
if keyspace not in TABLE_STATS_DETAILS:
TABLE_STATS_DETAILS[keyspace] = {}
table_regex = REGEX_TABLE.match(line)
if table_regex is not None and table is not table_regex.groups(1):
table = table_regex.group(1)
if table not in TABLE_STATS_DETAILS[keyspace]:
TABLE_STATS_DETAILS[keyspace][table] = {'start': {}, 'stop': {}}
read_count_regex = REGEX_READ_CNT.match(line)
if read_count_regex is not None:
read_count = read_count_regex.group(1)
TABLE_STATS_DETAILS[keyspace][table]['stop']['read_cnt'] = long(read_count)
write_count_regex = REGEX_WRITE_CNT.match(line)
if write_count_regex is not None:
write_count = write_count_regex.group(1)
TABLE_STATS_DETAILS[keyspace][table]['stop']['write_cnt'] = long(write_count)
if output is 'csv':
print "%s,%s,%s,%s" % ('keyspace', 'table', 'read_diff', 'write_diff')
for keyspace in TABLE_STATS_DETAILS:
for table in TABLE_STATS_DETAILS[keyspace]:
start_read_cnt = TABLE_STATS_DETAILS[keyspace][table]['start']['read_cnt']
stop_read_cnt = TABLE_STATS_DETAILS[keyspace][table]['stop']['read_cnt']
start_write_cnt = TABLE_STATS_DETAILS[keyspace][table]['start']['write_cnt']
stop_write_cnt = TABLE_STATS_DETAILS[keyspace][table]['stop']['write_cnt']
if start_read_cnt < stop_read_cnt or start_write_cnt < stop_write_cnt:
read_diff = (stop_read_cnt - start_read_cnt)
write_diff = (stop_write_cnt - start_write_cnt)
if output is not 'csv':
print "%s" % keyspace
print " Table: %s" % table
if start_read_cnt < stop_read_cnt:
print " Read Diff: %d" % read_diff
if start_write_cnt < stop_write_cnt:
print " Write Diff: %d" % write_diff
else:
print "%s,%s,%d,%d" % (keyspace, table, read_diff, write_diff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment