Created
June 7, 2017 19:26
-
-
Save Vegasq/84116a87f290fc4edd43f77d7a148778 to your computer and use it in GitHub Desktop.
validate_compaction_strategies.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
try: | |
from cassandra.cluster import Cluster | |
except ImportError: | |
print("Please, install python-cassandra-driver.\n\nsudo apt install python-cassandra-driver") | |
exit() | |
from ConfigParser import RawConfigParser | |
from pprint import pprint | |
RED = "\033[1;31m" | |
GREEN = "\033[0;32m" | |
BOLD = "\033[;1m" | |
RESET = "\033[0;0m" | |
def bold(text): | |
sys.stdout.write(BOLD) | |
print(text) | |
sys.stdout.write(RESET) | |
def red(text): | |
sys.stdout.write(RED) | |
print(text) | |
sys.stdout.write(RESET) | |
def green(text): | |
sys.stdout.write(GREEN) | |
print(text) | |
sys.stdout.write(RESET) | |
class ContrailConfigParser(RawConfigParser): | |
def __init__(self): | |
RawConfigParser.__init__(self) | |
self.read([ | |
"/etc/contrail/contrail-api.conf", | |
"/etc/contrail/contrail-analytics-api.conf" | |
]) | |
@property | |
def cassandra_server_list(self): | |
servers = [s.split(":")[0] for s in self.get("DEFAULTS", "cassandra_server_list").split(" ")] | |
return servers | |
@property | |
def cassandra_server_port(self): | |
return self.get("DEFAULTS", "cassandra_server_list").split(" ")[0].split(":")[1] | |
def collect_compaction_strategies(): | |
compaction_strategies = [] | |
ccp = ContrailConfigParser() | |
cluster = Cluster(ccp.cassandra_server_list) | |
cluster.connect() | |
for ks_name in cluster.metadata.keyspaces: | |
if ks_name == "system": | |
continue | |
ks = cluster.metadata.keyspaces[ks_name] | |
for tb_name in ks.tables.keys(): | |
tb = ks.tables[tb_name] | |
compaction_strategy = tb.options["compaction_strategy_class"] | |
compaction_strategies.append({ | |
"keyspace": ks_name, | |
"table": tb_name, | |
"compaction_strategy_class": compaction_strategy | |
}) | |
return compaction_strategies | |
class StrategiesValidation(object): | |
non_flow_table = "org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy" | |
flow_table = "org.apache.cassandra.db.compaction.DateTieredCompactionStrategy" | |
def __init__(self): | |
self._all_tables_info = collect_compaction_strategies() | |
def _filter_flow_tables(self): | |
result = [] | |
for item in self._all_tables_info: | |
if "flow" in item["table"]: | |
result.append(item) | |
return result | |
def _filter_non_flow_tables(self): | |
result = [] | |
for item in self._all_tables_info: | |
if "flow" not in item["table"]: | |
result.append(item) | |
return result | |
def validate(self): | |
bold("Check all FLOW tables:") | |
for item in self._filter_flow_tables(): | |
print("Validating {keyspace}->{table}".format(**item)) | |
if item["compaction_strategy_class"] == self.flow_table: | |
green("[VALID]") | |
else: | |
red("[INVALID]") | |
bold("Check all NOT FLOW tables:") | |
for item in self._filter_non_flow_tables(): | |
print("Validating {keyspace}->{table}".format(**item)) | |
if item["compaction_strategy_class"] == self.non_flow_table: | |
green("[VALID]") | |
else: | |
red("[INVALID]") | |
sv = StrategiesValidation() | |
sv.validate() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment