savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import abc |
| 2 | import os |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 3 | import re |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 4 | import time |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 5 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 6 | from cfg_checker.common import const |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 7 | from cfg_checker.common import logger_cli |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 8 | from cfg_checker.common.file_utils import read_file_as_lines |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 9 | |
| 10 | import jinja2 |
| 11 | |
| 12 | import six |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 13 | |
| 14 | pkg_dir = os.path.dirname(__file__) |
Alex Savatieiev | 6d010be | 2019-03-11 10:36:59 -0500 | [diff] [blame] | 15 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 16 | pkg_dir = os.path.normpath(pkg_dir) |
| 17 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 18 | # % threshhold values |
| 19 | _disk_warn = 80 |
| 20 | _disk_critical = 90 |
| 21 | _ram_warn = 5 |
| 22 | _ram_critical = 3 |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 23 | _softnet_interval = 5 |
| 24 | |
| 25 | UP = const.NODE_UP |
| 26 | DOWN = const.NODE_DOWN |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 27 | SKIP = const.NODE_SKIP |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 28 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 29 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 30 | def line_breaks(text): |
| 31 | # replace python linebreaks with html breaks |
| 32 | return text.replace("\n", "<br />") |
| 33 | |
| 34 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 35 | def tabstops(text): |
| 36 | # replace python linebreaks with html breaks |
| 37 | return text.replace("\t", "	") |
| 38 | |
| 39 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 40 | def get_sorted_keys(td): |
| 41 | # detect if we can sort by desc |
| 42 | # Yes, this is slow, but bullet-proof from empty desc |
| 43 | _desc = all([bool(td[k]['desc']) for k in td.keys()]) |
| 44 | # Get sorted list |
| 45 | if not _desc: |
| 46 | return sorted(td.keys()) |
| 47 | else: |
| 48 | return sorted( |
| 49 | td.keys(), |
| 50 | key=lambda k: ( |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 51 | td[k]['desc']['section'], |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 52 | td[k]['desc']['app'], |
| 53 | k |
| 54 | ) |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | def get_max(_list): |
| 59 | return sorted(_list)[-1] |
| 60 | |
| 61 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 62 | def make_pkg_action_label(act): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 63 | _act_labels = { |
| 64 | const.ACT_UPGRADE: "Upgrade possible", |
| 65 | const.ACT_NEED_UP: "Needs upgrade", |
| 66 | const.ACT_NEED_DOWN: "Needs downgrade", |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 67 | const.ACT_REPO: "Repo update", |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 68 | const.ACT_NA: "" |
| 69 | } |
| 70 | return _act_labels[act] |
| 71 | |
| 72 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 73 | def make_pkg_action_class(act): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 74 | _act_classes = { |
| 75 | const.ACT_UPGRADE: "possible", |
| 76 | const.ACT_NEED_UP: "needs_up", |
| 77 | const.ACT_NEED_DOWN: "needs_down", |
| 78 | const.ACT_REPO: "needs_repo", |
| 79 | const.ACT_NA: "" |
| 80 | } |
| 81 | return _act_classes[act] |
| 82 | |
| 83 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 84 | def make_pkg_status_label(sts): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 85 | _status_labels = { |
| 86 | const.VERSION_OK: "OK", |
| 87 | const.VERSION_UP: "Upgraded", |
| 88 | const.VERSION_DOWN: "Downgraded", |
Alex | 26b8a8c | 2019-10-09 17:09:07 -0500 | [diff] [blame] | 89 | const.VERSION_WARN: "WARNING", |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 90 | const.VERSION_ERR: "ERROR", |
| 91 | const.VERSION_NA: "N/A" |
| 92 | } |
| 93 | return _status_labels[sts] |
| 94 | |
| 95 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 96 | def make_pkg_status_class(sts): |
| 97 | return const.all_pkg_statuses[sts] |
| 98 | |
| 99 | |
| 100 | def make_node_status(sts): |
| 101 | return const.node_status[sts] |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 102 | |
| 103 | |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 104 | def make_repo_info(repos): |
| 105 | _text = "" |
| 106 | for r in repos: |
| 107 | # tag |
| 108 | _text += r['tag'] + ": " |
| 109 | # repo header |
| 110 | _text += " ".join([ |
| 111 | r['subset'], |
| 112 | r['release'], |
| 113 | r['ubuntu-release'], |
| 114 | r['type'], |
| 115 | r['arch'] |
| 116 | ]) + ", " |
| 117 | # maintainer w/o email |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 118 | _text += ascii(r['maintainer'][:r['maintainer'].find('<')-1]) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 119 | # newline |
| 120 | _text += "<br />" |
| 121 | return _text |
| 122 | |
| 123 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 124 | def to_gb(bytes_str): |
| 125 | _bytes = int(bytes_str) |
| 126 | _gb = _bytes / 1024 / 1024 / 1024 |
| 127 | return "{}".format(round(_gb, 2)) |
| 128 | |
| 129 | |
| 130 | def to_mb(bytes_str): |
| 131 | _bytes = int(bytes_str) |
| 132 | _mb = _bytes / 1024 / 1024 |
| 133 | return "{}".format(round(_mb, 2)) |
| 134 | |
| 135 | |
| 136 | def get_bucket_item_name(id, cmap): |
| 137 | for buck in cmap["buckets"]: |
| 138 | if id == buck["id"]: |
| 139 | return buck["name"] |
| 140 | for dev in cmap["devices"]: |
| 141 | if id == dev["id"]: |
| 142 | return dev["name"] |
| 143 | return id |
| 144 | |
| 145 | |
| 146 | def get_rule_steps(steps): |
| 147 | _steps = [] |
| 148 | for step in steps: |
| 149 | _ops = step.pop("op").split('_') |
| 150 | if "take" in _ops: |
| 151 | _steps.append( |
| 152 | "step {} {}".format( |
| 153 | " ".join(_ops), |
| 154 | step["item_name"] |
| 155 | ) |
| 156 | ) |
| 157 | else: |
| 158 | _steps.append( |
| 159 | "step {} {}".format( |
| 160 | " ".join(_ops), |
| 161 | " ".join(["{} {}".format(k, v) for k, v in step.items()]) |
| 162 | ) |
| 163 | ) |
| 164 | return _steps |
| 165 | |
| 166 | |
| 167 | def get_osdmap(cs): |
| 168 | _osdmap = cs |
| 169 | while True: |
| 170 | _keys = list(_osdmap.keys()) |
| 171 | for _k in _keys: |
| 172 | if _k == "osdmap": |
| 173 | _osdmap = _osdmap[_k] |
| 174 | break |
| 175 | elif _k == 'epoch': |
| 176 | return _osdmap |
| 177 | return { |
| 178 | "epoch": 0, |
| 179 | "num_osds": 0, |
| 180 | "num_up_osds": 0, |
| 181 | "osd_up_since": 0, |
| 182 | "num_in_osds": 0, |
| 183 | "osd_in_since": 0, |
| 184 | "num_remapped_pgs": 0 |
| 185 | } |
| 186 | |
| 187 | |
| 188 | def get_pool_stats(id, pgdump): |
| 189 | _stats = {} |
| 190 | for pool in pgdump["pg_map"]["pool_stats"]: |
| 191 | if id == pool["poolid"]: |
| 192 | _stats = pool |
| 193 | return _stats |
| 194 | |
| 195 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 196 | @six.add_metaclass(abc.ABCMeta) |
| 197 | class _Base(object): |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 198 | def __init__(self, master=None): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 199 | self.jinja2_env = self.init_jinja2_env() |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 200 | self.master = master |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 201 | |
| 202 | @abc.abstractmethod |
| 203 | def __call__(self, payload): |
| 204 | pass |
| 205 | |
| 206 | @staticmethod |
| 207 | def init_jinja2_env(): |
| 208 | return jinja2.Environment( |
| 209 | loader=jinja2.FileSystemLoader(os.path.join(pkg_dir, 'templates')), |
| 210 | trim_blocks=True, |
| 211 | lstrip_blocks=True) |
| 212 | |
| 213 | |
| 214 | class _TMPLBase(_Base): |
| 215 | @abc.abstractproperty |
| 216 | def tmpl(self): |
| 217 | pass |
| 218 | |
| 219 | @staticmethod |
| 220 | def _count_totals(data): |
| 221 | data['counters']['total_nodes'] = len(data['nodes']) |
| 222 | |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 223 | def __call__(self, payload): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 224 | # init data structures |
| 225 | data = self.common_data() |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 226 | # payload should have pre-sorted structure according to report called |
| 227 | # nodes, openstack_release, mcp_release, etc... |
| 228 | data.update(payload) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 229 | |
| 230 | # add template specific data |
| 231 | self._extend_data(data) |
| 232 | |
| 233 | # do counts global |
| 234 | self._count_totals(data) |
| 235 | |
| 236 | # specific filters |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 237 | self.jinja2_env.filters['linebreaks'] = line_breaks |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 238 | self.jinja2_env.filters['get_max'] = get_max |
| 239 | |
| 240 | self.jinja2_env.filters['get_sorted_keys'] = get_sorted_keys |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 241 | self.jinja2_env.filters['pkg_status_label'] = make_pkg_status_label |
| 242 | self.jinja2_env.filters['pkg_status_class'] = make_pkg_status_class |
| 243 | self.jinja2_env.filters['pkg_action_label'] = make_pkg_action_label |
| 244 | self.jinja2_env.filters['pkg_action_class'] = make_pkg_action_class |
| 245 | self.jinja2_env.filters['node_status_class'] = make_node_status |
Alex | c6566d8 | 2019-09-23 16:07:17 -0500 | [diff] [blame] | 246 | self.jinja2_env.filters['pkg_repo_info'] = make_repo_info |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 247 | self.jinja2_env.filters['to_gb'] = to_gb |
| 248 | self.jinja2_env.filters['to_mb'] = to_mb |
| 249 | self.jinja2_env.filters['get_bucket_item_name'] = get_bucket_item_name |
| 250 | self.jinja2_env.filters['get_rule_steps'] = get_rule_steps |
| 251 | self.jinja2_env.filters['get_pool_stats'] = get_pool_stats |
| 252 | self.jinja2_env.filters['get_osdmap'] = get_osdmap |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 253 | |
| 254 | # render! |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 255 | logger_cli.info("-> Using template: {}".format(self.tmpl)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 256 | tmpl = self.jinja2_env.get_template(self.tmpl) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 257 | logger_cli.info("-> Rendering") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 258 | return tmpl.render(data) |
| 259 | |
| 260 | def common_data(self): |
| 261 | return { |
| 262 | 'counters': {}, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 263 | 'salt_info': {}, |
| 264 | 'gen_date': time.strftime("%m/%d/%Y %H:%M:%S") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | def _extend_data(self, data): |
| 268 | pass |
| 269 | |
| 270 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 271 | # HTML Package versions report |
| 272 | class CSVAllPackages(_TMPLBase): |
| 273 | tmpl = "pkg_versions_csv.j2" |
| 274 | |
| 275 | |
| 276 | # HTML Package versions report |
savex | ce010ba | 2018-04-27 09:49:23 +0200 | [diff] [blame] | 277 | class HTMLPackageCandidates(_TMPLBase): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 278 | tmpl = "pkg_versions_html.j2" |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 279 | |
| 280 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 281 | # HTML Ceph information report |
| 282 | class HTMLCephInfo(_TMPLBase): |
| 283 | tmpl = "ceph_info_html.j2" |
| 284 | |
| 285 | |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 286 | # Package versions report |
| 287 | class HTMLModelCompare(_TMPLBase): |
| 288 | tmpl = "model_tree_cmp_tmpl.j2" |
| 289 | |
| 290 | def _extend_data(self, data): |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 291 | # move names into separate place |
Alex | b8af13a | 2019-04-16 18:38:12 -0500 | [diff] [blame] | 292 | data["names"] = data["diffs"].pop("diff_names") |
| 293 | data["tabs"] = data.pop("diffs") |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 294 | |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 295 | # counters - mdl_diff |
Alex Savatieiev | 4f149d0 | 2019-02-28 17:15:29 -0600 | [diff] [blame] | 296 | for _tab in data["tabs"].keys(): |
| 297 | data['counters'][_tab] = len(data["tabs"][_tab]["diffs"].keys()) |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 298 | |
| 299 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 300 | class HTMLNetworkReport(_TMPLBase): |
| 301 | tmpl = "network_check_tmpl.j2" |
| 302 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 303 | def _extend_data(self, data): |
| 304 | def get_bytes(value): |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame^] | 305 | _size_i = True if value[-1] == 'i' else False |
| 306 | _char = value[-2] if _size_i else value[-1] |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 307 | _ord = ord(_char) |
| 308 | if _ord > 47 and _ord < 58: |
| 309 | # bytes comes with no Char |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 310 | return int(value) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 311 | else: |
| 312 | _sizes = ["*", "K", "M", "G", "T"] |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame^] | 313 | _flo = float(value[:-2]) if _size_i else float(value[:-1]) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 314 | _pwr = 1 |
| 315 | if _char in _sizes: |
| 316 | _pwr = _sizes.index(_char) |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame^] | 317 | return int(1024**_pwr*_flo) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 318 | |
Alex | 2e213b2 | 2019-12-05 10:40:29 -0600 | [diff] [blame] | 319 | def _dmidecode(_dict, type=0): |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 320 | # _key = "dmi" |
Alex | 2e213b2 | 2019-12-05 10:40:29 -0600 | [diff] [blame] | 321 | _key_r = "dmi_r" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 322 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | 2e213b2 | 2019-12-05 10:40:29 -0600 | [diff] [blame] | 323 | _cmd = "dmidecode -t {}".format(type) |
| 324 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
| 325 | # TODO: parse BIOS output or given type |
| 326 | pass |
| 327 | |
| 328 | def _lsblk(_dict): |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 329 | # _key = "lsblk" |
Alex | 2e213b2 | 2019-12-05 10:40:29 -0600 | [diff] [blame] | 330 | _key_r = "lsblk_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 331 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | 2e213b2 | 2019-12-05 10:40:29 -0600 | [diff] [blame] | 332 | _columns = [ |
| 333 | "NAME", |
| 334 | "HCTL", |
| 335 | "TYPE", |
| 336 | "SIZE", |
| 337 | "VENDOR", |
| 338 | "MODEL", |
| 339 | "SERIAL", |
| 340 | "REV", |
| 341 | "TRAN" |
| 342 | ] |
| 343 | _cmd = "lsblk -S --output {}".format(",".join(_columns)) |
| 344 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
| 345 | # TODO: parse lsblk output |
| 346 | pass |
| 347 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 348 | def _lscpu(_dict): |
| 349 | _key = "lscpu" |
| 350 | _key_r = "lscpu_raw" |
| 351 | # get all of the values |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 352 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 353 | _cmd = "lscpu | sed -n '/\\:/s/ \\+/ /gp'" |
| 354 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
| 355 | # parse them and put into dict |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 356 | for node, dt in _dict.items(): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 357 | dt[_key] = {} |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 358 | if dt['status'] == DOWN or dt['status'] == SKIP: |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 359 | continue |
| 360 | if not dt[_key_r]: |
| 361 | # no stats collected, put negatives |
| 362 | dt.pop(_key_r) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 363 | continue |
| 364 | lines = dt[_key_r].splitlines() |
| 365 | for line in lines: |
| 366 | li = line.split(':') |
| 367 | _var_name = li[0].lower() |
| 368 | _var_name = re.sub(' ', '_', _var_name) |
| 369 | _var_name = re.sub('|'.join(['\\(', '\\)']), '', _var_name) |
| 370 | _var_value = li[1].strip() |
| 371 | dt[_key][_var_name] = _var_value |
| 372 | dt.pop(_key_r) |
| 373 | # detect virtual nodes |
| 374 | if "hypervisor_vendor" in dt[_key]: |
| 375 | dt['node_type'] = "virtual" |
| 376 | else: |
| 377 | dt['node_type'] = "physical" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 378 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 379 | def _free(_dict): |
| 380 | _key = "ram" |
| 381 | _key_r = "ram_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 382 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame^] | 383 | _cmd = "free -h | grep 'Mem' | sed -n '/Mem/s/ \\+/ /gp'" |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 384 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
| 385 | # parse them and put into dict |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 386 | for node, dt in _dict.items(): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 387 | dt[_key] = {} |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 388 | if dt['status'] == DOWN or dt['status'] == SKIP: |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 389 | continue |
| 390 | if not dt[_key_r]: |
| 391 | # no stats collected, put negatives |
| 392 | dt.pop(_key_r) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 393 | continue |
| 394 | li = dt[_key_r].split() |
| 395 | dt[_key]['total'] = li[1] |
| 396 | dt[_key]['used'] = li[2] |
| 397 | dt[_key]['free'] = li[3] |
| 398 | dt[_key]['shared'] = li[4] |
| 399 | dt[_key]['cache'] = li[5] |
| 400 | dt[_key]['available'] = li[6] |
| 401 | |
| 402 | _total = get_bytes(li[1]) |
| 403 | _avail = get_bytes(li[6]) |
| 404 | _m = _avail * 100.0 / _total |
| 405 | if _m < _ram_critical: |
| 406 | dt[_key]["status"] = "fail" |
| 407 | elif _m < _ram_warn: |
| 408 | dt[_key]["status"] = "warn" |
| 409 | else: |
| 410 | dt[_key]["status"] = "" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 411 | |
| 412 | def _services(_dict): |
| 413 | _key = "services" |
| 414 | _key_r = "services_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 415 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 416 | _cmd = "service --status-all" |
| 417 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 418 | for node, dt in _dict.items(): |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 419 | dt[_key] = {} |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 420 | if dt['status'] == DOWN or dt['status'] == SKIP: |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 421 | continue |
| 422 | if not dt[_key_r]: |
| 423 | # no stats collected, put negatives |
| 424 | dt.pop(_key_r) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 425 | continue |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 426 | lines = dt[_key_r].splitlines() |
| 427 | for line in lines: |
| 428 | li = line.split() |
| 429 | _status = li[1] |
| 430 | _name = li[3] |
| 431 | if _status == '-': |
| 432 | dt[_key][_name] = False |
| 433 | elif _status == '+': |
| 434 | dt[_key][_name] = True |
| 435 | else: |
| 436 | dt[_key][_name] = None |
| 437 | dt.pop(_key_r) |
| 438 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 439 | def _vcp_status(_dict): |
| 440 | _key = "virsh" |
| 441 | _key_r = "virsh_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 442 | self.master.get_cmd_for_nodes( |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 443 | "virsh list --all | sed -n -e '/[0-9]/s/ \\+/ /gp'", |
| 444 | _key_r, |
| 445 | target_dict=_dict, |
| 446 | nodes="kvm*" |
| 447 | ) |
| 448 | _kvm = filter(lambda x: x.find("kvm") >= 0, _dict.keys()) |
| 449 | for node in _kvm: |
| 450 | dt = _dict[node] |
| 451 | dt[_key] = {} |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 452 | if dt['status'] == DOWN or dt['status'] == SKIP: |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 453 | continue |
| 454 | if not dt[_key_r]: |
| 455 | # no stats collected, put negatives |
| 456 | dt.pop(_key_r) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 457 | continue |
| 458 | lines = dt[_key_r].splitlines() |
| 459 | for line in lines: |
| 460 | li = line.split() |
| 461 | _id = li[0] |
| 462 | _name = li[1] |
| 463 | _status = li[2] |
| 464 | dt[_key][_name] = { |
| 465 | 'id': _id, |
| 466 | 'status': _status |
| 467 | } |
| 468 | dt.pop(_key_r) |
| 469 | |
| 470 | # query per-cpu and count totals |
| 471 | # total (0), dropped(1), squeezed (2), collision (7) |
| 472 | def _soft_net_stats(_dict): |
| 473 | _key = "net_stats" |
| 474 | _key_r = "net_stats_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 475 | _f_cmd = self.master.get_cmd_for_nodes |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 476 | _cmd = "cat /proc/net/softnet_stat; echo \\#; " \ |
| 477 | "sleep {}; cat /proc/net/softnet_stat".format( |
| 478 | _softnet_interval |
| 479 | ) |
| 480 | _f_cmd(_cmd, _key_r, target_dict=_dict) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 481 | for node, dt in _dict.items(): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 482 | _cpuindex = 1 |
| 483 | _add_mode = True |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 484 | # totals for start mark |
| 485 | _ts = [0, 0, 0, 0] |
| 486 | # skip if node is down |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 487 | if dt['status'] == DOWN or dt['status'] == SKIP: |
Alex | c96fdd3 | 2019-10-15 12:48:59 -0500 | [diff] [blame] | 488 | dt[_key] = { |
| 489 | "total": [-1, -1, -1, -1] |
| 490 | } |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 491 | continue |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 492 | if not dt[_key_r]: |
| 493 | # no stats collected, put negatives |
| 494 | dt.pop(_key_r) |
| 495 | dt[_key] = { |
| 496 | "total": [-1, -1, -1, -1] |
| 497 | } |
| 498 | continue |
| 499 | # final totals |
| 500 | dt[_key] = { |
| 501 | "total": [0, 0, 0, 0] |
| 502 | } |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 503 | lines = dt[_key_r].splitlines() |
| 504 | for line in lines: |
| 505 | if line.startswith("#"): |
| 506 | _add_mode = False |
| 507 | _cpuindex = 1 |
| 508 | continue |
| 509 | li = line.split() |
| 510 | _c = [ |
| 511 | int(li[0], 16), |
| 512 | int(li[1], 16), |
| 513 | int(li[2], 16), |
| 514 | int(li[7], 16) |
| 515 | ] |
| 516 | _id = "cpu{:02}".format(_cpuindex) |
| 517 | if _id not in dt[_key]: |
| 518 | dt[_key][_id] = [] |
| 519 | _dc = dt[_key][_id] |
| 520 | if _add_mode: |
| 521 | # saving values and adding totals |
| 522 | dt[_key][_id] = _c |
| 523 | # save start totals |
| 524 | _ts = [_ts[i]+_c[i] for i in range(0, len(_c))] |
| 525 | else: |
| 526 | # this is second measurement |
| 527 | # subtract all values |
| 528 | for i in range(len(_c)): |
| 529 | dt[_key][_id][i] = _c[i] - _dc[i] |
| 530 | dt[_key]["total"][i] += _c[i] |
| 531 | _cpuindex += 1 |
| 532 | # finally, subtract initial totals |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 533 | for k, v in dt[_key].items(): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 534 | if k != "total": |
| 535 | dt[_key][k] = [v[i] / 5. for i in range(len(v))] |
| 536 | else: |
| 537 | dt[_key][k] = [(v[i]-_ts[i])/5. for i in range(len(v))] |
| 538 | dt.pop(_key_r) |
| 539 | |
| 540 | # prepare yellow and red marker values |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 541 | data["const"] = { |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 542 | "net_interval": _softnet_interval, |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 543 | "ram_warn": _ram_warn, |
| 544 | "ram_critical": _ram_critical, |
| 545 | "disk_warn": _disk_warn, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 546 | "disk_critical": _disk_critical, |
| 547 | "services": read_file_as_lines( |
| 548 | os.path.join( |
| 549 | pkg_dir, |
| 550 | 'etc', |
| 551 | 'services.list' |
| 552 | ) |
| 553 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | # get kernel version |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 557 | self.master.get_cmd_for_nodes( |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 558 | "uname -r", |
| 559 | "kernel", |
| 560 | target_dict=data["nodes"] |
| 561 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 562 | # process lscpu data |
| 563 | _lscpu(data["nodes"]) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 564 | |
| 565 | # free ram |
| 566 | # sample: 16425392 14883144 220196 |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 567 | _free(data["nodes"]) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 568 | |
| 569 | # disk space |
| 570 | # sample: /dev/vda1 78G 33G 45G 43% |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 571 | _key = "disk" |
| 572 | _key_r = "disk_raw" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 573 | self.master.get_cmd_for_nodes( |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 574 | "df -h | sed -n '/^\\/dev/s/ \\+/ /gp' | cut -d\" \" -f 1-5", |
| 575 | "disk_raw", |
| 576 | target_dict=data["nodes"] |
| 577 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 578 | for dt in data["nodes"].values(): |
Alex | c96fdd3 | 2019-10-15 12:48:59 -0500 | [diff] [blame] | 579 | dt["disk"] = {} |
| 580 | dt["disk_max_dev"] = None |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 581 | if dt['status'] == DOWN: |
Alex | c96fdd3 | 2019-10-15 12:48:59 -0500 | [diff] [blame] | 582 | dt["disk"]["unknown"] = {} |
| 583 | dt["disk_max_dev"] = "unknown" |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 584 | continue |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 585 | if dt['status'] == SKIP: |
| 586 | dt["disk"]["skipped"] = {} |
| 587 | dt["disk_max_dev"] = "skipped" |
| 588 | continue |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 589 | if not dt[_key_r]: |
| 590 | # no stats collected, put negatives |
| 591 | dt.pop(_key_r) |
| 592 | dt[_key] = {} |
| 593 | continue |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 594 | # show first device row by default |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 595 | _d = dt["disk"] |
| 596 | _r = dt["disk_raw"] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 597 | _r = _r.splitlines() |
| 598 | _max = -1 |
| 599 | for idx in range(0, len(_r)): |
| 600 | _t = _r[idx].split() |
| 601 | _d[_t[0]] = {} |
| 602 | _d[_t[0]]['v'] = _t[1:] |
| 603 | _chk = int(_t[-1].split('%')[0]) |
| 604 | if _chk > _max: |
Alex | e65ff4e | 2019-10-11 14:45:09 -0500 | [diff] [blame] | 605 | dt["disk_max_dev"] = _t[0] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 606 | _max = _chk |
| 607 | if _chk > _disk_critical: |
| 608 | _d[_t[0]]['f'] = "fail" |
| 609 | elif _chk > _disk_warn: |
| 610 | _d[_t[0]]['f'] = "warn" |
| 611 | else: |
| 612 | _d[_t[0]]['f'] = "" |
| 613 | |
| 614 | # prepare networks data for report |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 615 | for net, net_v in data['map'].items(): |
| 616 | for node, ifs in net_v.items(): |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 617 | for d in ifs: |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 618 | _e = "fail" |
| 619 | d['interface_error'] = _e if 'interface_error' in d else "" |
| 620 | d['mtu_error'] = _e if 'mtu_error' in d else "" |
| 621 | d['status_error'] = _e if 'status_error' in d else "" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 622 | d['subnet_gateway_error'] = \ |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 623 | _e if 'subnet_gateway_error' in d else "" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 624 | |
| 625 | _services(data["nodes"]) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 626 | # vcp status |
| 627 | # query virsh and prepare for report |
| 628 | _vcp_status(data["nodes"]) |
| 629 | |
| 630 | # soft net stats |
| 631 | _soft_net_stats(data["nodes"]) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 632 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 633 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 634 | class ReportToFile(object): |
| 635 | def __init__(self, report, target): |
| 636 | self.report = report |
| 637 | self.target = target |
| 638 | |
| 639 | def __call__(self, payload): |
| 640 | payload = self.report(payload) |
| 641 | |
| 642 | if isinstance(self.target, six.string_types): |
| 643 | self._wrapped_dump(payload) |
| 644 | else: |
| 645 | self._dump(payload, self.target) |
| 646 | |
| 647 | def _wrapped_dump(self, payload): |
| 648 | with open(self.target, 'wt') as target: |
| 649 | self._dump(payload, target) |
| 650 | |
| 651 | @staticmethod |
| 652 | def _dump(payload, target): |
| 653 | target.write(payload) |