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