|
| 1 | +from datetime import datetime, timedelta |
| 2 | +import json |
| 3 | +from sos.report.plugins import Plugin, UbuntuPlugin, PluginOpt |
| 4 | +import glob |
| 5 | +import re |
| 6 | + |
| 7 | +PATHS = { |
| 8 | + "CONF": "/var/snap/charmed-kafka/current/etc/kafka", |
| 9 | + "LOGS": "/var/snap/charmed-kafka/common/var/log/kafka", |
| 10 | +} |
| 11 | + |
| 12 | +DATE_FORMAT = "%Y-%m-%d-%H" |
| 13 | + |
| 14 | + |
| 15 | +class CharmedKafka(Plugin, UbuntuPlugin): |
| 16 | + short_desc = "Charmed Kafka" |
| 17 | + plugin_name = "charmed_kafka" |
| 18 | + |
| 19 | + current_date = datetime.now() |
| 20 | + default_date_from = "1970-01-01-00" |
| 21 | + default_date_to = (current_date + timedelta(hours=1)).strftime(DATE_FORMAT) |
| 22 | + |
| 23 | + option_list = [ |
| 24 | + PluginOpt( |
| 25 | + "date-from", |
| 26 | + default="1970-01-01-00", |
| 27 | + desc="date from which to filter logs, in format YYYY-MM-DD-HH", |
| 28 | + val_type=str, |
| 29 | + ), |
| 30 | + PluginOpt( |
| 31 | + "date-to", |
| 32 | + default=default_date_to, |
| 33 | + desc="date to which to filter logs, in format YYYY-MM-DD-HH", |
| 34 | + val_type=str, |
| 35 | + ), |
| 36 | + ] |
| 37 | + |
| 38 | + @property |
| 39 | + def bootstrap_server(self) -> str | None: |
| 40 | + lines = [] |
| 41 | + with open(f"{PATHS['CONF']}/client.properties") as f: |
| 42 | + lines = f.readlines() |
| 43 | + |
| 44 | + for line in lines: |
| 45 | + if "bootstrap" in line: |
| 46 | + # ensure using internal address if not set in client.properties |
| 47 | + return re.sub(r":(?!1)(\d+)", r":1\1", line.split("=")[1]) |
| 48 | + |
| 49 | + @property |
| 50 | + def default_bin_args(self) -> str: |
| 51 | + if not self.bootstrap_server: |
| 52 | + return "" |
| 53 | + |
| 54 | + return f"--bootstrap-server {self.bootstrap_server} --command-config {PATHS['CONF']}/client.properties" |
| 55 | + |
| 56 | + def setup(self): |
| 57 | + # --- FILE EXCLUSIONS --- |
| 58 | + |
| 59 | + for file in glob.glob(f"{PATHS['LOGS']}/*"): |
| 60 | + date = re.search( |
| 61 | + pattern=r"([0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2})", string=file |
| 62 | + ) |
| 63 | + |
| 64 | + # include files without date, aka current files |
| 65 | + if not date: |
| 66 | + continue |
| 67 | + |
| 68 | + file_dt = datetime.strptime(date.group(1), DATE_FORMAT) |
| 69 | + |
| 70 | + if file_dt < datetime.strptime( |
| 71 | + str(self.get_option("date-from")), DATE_FORMAT |
| 72 | + ) or file_dt > datetime.strptime( |
| 73 | + str(self.get_option("date-to")), DATE_FORMAT |
| 74 | + ): |
| 75 | + # skip files outside given range |
| 76 | + self.add_forbidden_path(file) |
| 77 | + |
| 78 | + # hide keys/stores |
| 79 | + self.add_forbidden_path( |
| 80 | + [ |
| 81 | + f"{PATHS['CONF']}/*.pem", |
| 82 | + f"{PATHS['CONF']}/*.key", |
| 83 | + f"{PATHS['CONF']}/*.p12", |
| 84 | + f"{PATHS['CONF']}/*.jks", |
| 85 | + ] |
| 86 | + ) |
| 87 | + |
| 88 | + # --- FILE INCLUSIONS --- |
| 89 | + |
| 90 | + self.add_copy_spec( |
| 91 | + [ |
| 92 | + f"{PATHS['CONF']}", |
| 93 | + f"{PATHS['LOGS']}", |
| 94 | + "/var/log/juju", |
| 95 | + ] |
| 96 | + ) |
| 97 | + |
| 98 | + # --- SNAP LOGS --- |
| 99 | + |
| 100 | + self.add_cmd_output( |
| 101 | + "snap logs charmed-kafka.daemon -n 100000", suggest_filename="snap-logs" |
| 102 | + ) |
| 103 | + |
| 104 | + # --- TOPICS --- |
| 105 | + |
| 106 | + self.add_cmd_output( |
| 107 | + f"charmed-kafka.topics --describe {self.default_bin_args}", |
| 108 | + env={"KAFKA_OPTS": ""}, |
| 109 | + suggest_filename="kafka-topics", |
| 110 | + ) |
| 111 | + |
| 112 | + # --- CONFIGS --- |
| 113 | + |
| 114 | + for entity in ["topics", "clients", "users", "brokers", "ips"]: |
| 115 | + self.add_cmd_output( |
| 116 | + f"charmed-kafka.configs --describe --all --entity-type {entity} {self.default_bin_args}", |
| 117 | + env={"KAFKA_OPTS": ""}, |
| 118 | + suggest_filename=f"kafka-configs-{entity}", |
| 119 | + ) |
| 120 | + |
| 121 | + # --- ACLs --- |
| 122 | + |
| 123 | + self.add_cmd_output( |
| 124 | + f"charmed-kafka.acls --list {self.default_bin_args}", |
| 125 | + env={"KAFKA_OPTS": ""}, |
| 126 | + suggest_filename="kafka-acls", |
| 127 | + ) |
| 128 | + |
| 129 | + # --- LOG DIRS --- |
| 130 | + |
| 131 | + log_dirs_output = self.exec_cmd( |
| 132 | + f"charmed-kafka.log-dirs --describe {self.default_bin_args}", |
| 133 | + env={"KAFKA_OPTS": ""}, |
| 134 | + ) |
| 135 | + log_dirs = {} |
| 136 | + |
| 137 | + if log_dirs_output and log_dirs_output["status"] == 0: |
| 138 | + for line in log_dirs_output["output"].splitlines(): |
| 139 | + try: |
| 140 | + log_dirs = json.loads(line) |
| 141 | + break |
| 142 | + except json.JSONDecodeError: |
| 143 | + continue |
| 144 | + |
| 145 | + with self.collection_file("kafka-log-dirs") as f: |
| 146 | + f.write(json.dumps(log_dirs, indent=4)) |
| 147 | + |
| 148 | + # --- TRANSACTIONS --- |
| 149 | + |
| 150 | + transactions_list = self.exec_cmd( |
| 151 | + f"charmed-kafka.transactions {self.default_bin_args} list", |
| 152 | + env={"KAFKA_OPTS": ""}, |
| 153 | + ) |
| 154 | + transactional_ids = [] |
| 155 | + |
| 156 | + if transactions_list and transactions_list["status"] == 0: |
| 157 | + transactional_ids = transactions_list["output"].splitlines()[1:] |
| 158 | + |
| 159 | + transactions_outputs = [] |
| 160 | + for transactional_id in transactional_ids: |
| 161 | + transactions_describe = self.exec_cmd( |
| 162 | + f"charmed-kafka.transactions {self.default_bin_args} describe --transactional-id {transactional_id}" |
| 163 | + ) |
| 164 | + |
| 165 | + if transactions_describe and transactions_describe["status"] == 0: |
| 166 | + transactions_outputs.append(transactions_describe["output"]) |
| 167 | + |
| 168 | + with self.collection_file("kafka-transactions") as f: |
| 169 | + f.write("\n".join(transactions_outputs)) |
| 170 | + |
| 171 | + # --- JMX METRICS --- |
| 172 | + |
| 173 | + self.add_cmd_output("curl localhost:9101/metrics", "jmx-metrics") |
| 174 | + |
| 175 | + def postproc(self): |
| 176 | + # --- SCRUB PASSWORDS --- |
| 177 | + |
| 178 | + self.do_path_regex_sub( |
| 179 | + f"{PATHS['CONF']}/*", |
| 180 | + r'(password=")[^"]*', |
| 181 | + r"\1*********", |
| 182 | + ) |
0 commit comments