Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 2 | |
| 3 | from __future__ import print_function |
| 4 | from cffi import FFI |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 5 | import time |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 6 | |
| 7 | ffi = FFI() |
| 8 | ffi.cdef(""" |
| 9 | typedef uint64_t counter_t; |
| 10 | typedef struct { |
| 11 | counter_t packets; |
| 12 | counter_t bytes; |
| 13 | } vlib_counter_t; |
| 14 | |
| 15 | typedef enum { |
| 16 | STAT_DIR_TYPE_ILLEGAL = 0, |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 17 | STAT_DIR_TYPE_SCALAR_INDEX, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 18 | STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE, |
| 19 | STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED, |
| 20 | STAT_DIR_TYPE_ERROR_INDEX, |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 21 | STAT_DIR_TYPE_NAME_VECTOR, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 22 | } stat_directory_type_t; |
| 23 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 24 | typedef struct |
| 25 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 26 | stat_directory_type_t type; |
| 27 | union { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 28 | uint64_t index; |
| 29 | uint64_t value; |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 30 | uint64_t *data; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 31 | }; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 32 | char name[128]; // TODO change this to pointer to "somewhere" |
| 33 | } stat_segment_directory_entry_t; |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | char *name; |
| 38 | stat_directory_type_t type; |
| 39 | union |
| 40 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 41 | double scalar_value; |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 42 | counter_t *error_vector; |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 43 | counter_t **simple_counter_vec; |
| 44 | vlib_counter_t **combined_counter_vec; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 45 | uint8_t **name_vector; |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 46 | }; |
| 47 | } stat_segment_data_t; |
| 48 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 49 | typedef struct |
| 50 | { |
Ole Troan | b63dbc5 | 2019-06-14 10:26:14 +0200 | [diff] [blame] | 51 | uint64_t version; |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 52 | void *base; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 53 | uint64_t epoch; |
| 54 | uint64_t in_progress; |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 55 | stat_segment_directory_entry_t *directory_vector; |
| 56 | uint64_t **error_vector; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 57 | } stat_segment_shared_header_t; |
| 58 | |
| 59 | typedef struct |
| 60 | { |
| 61 | uint64_t current_epoch; |
| 62 | stat_segment_shared_header_t *shared_header; |
| 63 | stat_segment_directory_entry_t *directory_vector; |
| 64 | ssize_t memory_size; |
| 65 | } stat_client_main_t; |
| 66 | |
| 67 | stat_client_main_t * stat_client_get(void); |
| 68 | void stat_client_free(stat_client_main_t * sm); |
| 69 | int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 70 | int stat_segment_connect (char *socket_name); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 71 | void stat_segment_disconnect_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 72 | void stat_segment_disconnect (void); |
| 73 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 74 | uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 75 | uint32_t *stat_segment_ls (uint8_t ** pattern); |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 76 | stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, |
| 77 | stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 78 | stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 79 | void stat_segment_data_free (stat_segment_data_t * res); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 80 | |
| 81 | double stat_segment_heartbeat_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 82 | int stat_segment_vec_len(void *vec); |
| 83 | uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string); |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 84 | char *stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm); |
Ole Troan | b63dbc5 | 2019-06-14 10:26:14 +0200 | [diff] [blame] | 85 | uint64_t stat_segment_version(void); |
| 86 | uint64_t stat_segment_version_r(stat_client_main_t *sm); |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 87 | void free(void *ptr); |
Ole Troan | 65fa036 | 2020-09-30 10:43:00 +0200 | [diff] [blame^] | 88 | void vac_mem_init (size_t size); |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 89 | """) # noqa: E501 |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 90 | |
| 91 | |
| 92 | # Utility functions |
| 93 | def make_string_vector(api, strings): |
| 94 | vec = ffi.NULL |
| 95 | if type(strings) is not list: |
| 96 | strings = [strings] |
| 97 | for s in strings: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 98 | vec = api.stat_segment_string_vector(vec, ffi.new("char []", |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 99 | s.encode('utf-8'))) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 100 | return vec |
| 101 | |
| 102 | |
| 103 | def make_string_list(api, vec): |
| 104 | vec_len = api.stat_segment_vec_len(vec) |
| 105 | return [ffi.string(vec[i]) for i in range(vec_len)] |
| 106 | |
| 107 | |
| 108 | # 2-dimensonal array of thread, index |
| 109 | def simple_counter_vec_list(api, e): |
| 110 | vec = [] |
| 111 | for thread in range(api.stat_segment_vec_len(e)): |
| 112 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 113 | if_per_thread = [e[thread][interfaces] |
| 114 | for interfaces in range(len_interfaces)] |
| 115 | vec.append(if_per_thread) |
| 116 | return vec |
| 117 | |
| 118 | |
| 119 | def vlib_counter_dict(c): |
| 120 | return {'packets': c.packets, |
| 121 | 'bytes': c.bytes} |
| 122 | |
| 123 | |
| 124 | def combined_counter_vec_list(api, e): |
| 125 | vec = [] |
| 126 | for thread in range(api.stat_segment_vec_len(e)): |
| 127 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 128 | if_per_thread = [vlib_counter_dict(e[thread][interfaces]) |
| 129 | for interfaces in range(len_interfaces)] |
| 130 | vec.append(if_per_thread) |
| 131 | return vec |
| 132 | |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 133 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 134 | def error_vec_list(api, e): |
| 135 | vec = [] |
| 136 | for thread in range(api.stat_segment_vec_len(e)): |
| 137 | vec.append(e[thread]) |
| 138 | return vec |
| 139 | |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 140 | |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 141 | def name_vec_list(api, e): |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 142 | return [ffi.string(e[i]).decode('utf-8') for i in |
| 143 | range(api.stat_segment_vec_len(e)) if e[i] != ffi.NULL] |
| 144 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 145 | |
| 146 | def stat_entry_to_python(api, e): |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 147 | # Scalar index |
| 148 | if e.type == 1: |
| 149 | return e.scalar_value |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 150 | if e.type == 2: |
| 151 | return simple_counter_vec_list(api, e.simple_counter_vec) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 152 | if e.type == 3: |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 153 | return combined_counter_vec_list(api, e.combined_counter_vec) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 154 | if e.type == 4: |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 155 | return error_vec_list(api, e.error_vector) |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 156 | if e.type == 5: |
| 157 | return name_vec_list(api, e.name_vector) |
| 158 | raise NotImplementedError() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 159 | |
| 160 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 161 | class VPPStatsIOError(IOError): |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 162 | message = "Stat segment client connection returned: " \ |
| 163 | "%(retval)s %(strerror)s." |
| 164 | |
| 165 | strerror = {-1: "Stat client couldn't open socket", |
| 166 | -2: "Stat client socket open but couldn't connect", |
| 167 | -3: "Receiving file descriptor failed", |
| 168 | -4: "mmap fstat failed", |
| 169 | -5: "mmap map failed" |
| 170 | } |
| 171 | |
| 172 | def __init__(self, message=None, **kwargs): |
| 173 | if 'retval' in kwargs: |
| 174 | self.retval = kwargs['retval'] |
| 175 | kwargs['strerror'] = self.strerror[int(self.retval)] |
| 176 | |
| 177 | if not message: |
| 178 | try: |
| 179 | message = self.message % kwargs |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 180 | except Exception: |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 181 | message = self.message |
| 182 | else: |
| 183 | message = message % kwargs |
| 184 | |
| 185 | super(VPPStatsIOError, self).__init__(message) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 186 | |
| 187 | |
| 188 | class VPPStatsClientLoadError(RuntimeError): |
| 189 | pass |
| 190 | |
| 191 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 192 | class VPPStats(object): |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 193 | VPPStatsIOError = VPPStatsIOError |
| 194 | |
YohanPipereau | 71dd9d5 | 2019-06-06 16:34:14 +0200 | [diff] [blame] | 195 | default_socketname = '/run/vpp/stats.sock' |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 196 | sharedlib_name = 'libvppapiclient.so' |
| 197 | |
| 198 | def __init__(self, socketname=default_socketname, timeout=10): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 199 | self.socketname = socketname |
| 200 | self.timeout = timeout |
| 201 | self.connected = False |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 202 | try: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 203 | self.api = ffi.dlopen(VPPStats.sharedlib_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 204 | except Exception: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 205 | raise VPPStatsClientLoadError("Could not open: %s" % |
| 206 | VPPStats.sharedlib_name) |
Ole Troan | 65fa036 | 2020-09-30 10:43:00 +0200 | [diff] [blame^] | 207 | self.api.vac_mem_init(0) |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 208 | |
| 209 | def connect(self): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 210 | self.client = self.api.stat_client_get() |
| 211 | |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 212 | poll_end_time = time.time() + self.timeout |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 213 | while time.time() < poll_end_time: |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 214 | rv = self.api.stat_segment_connect_r( |
| 215 | self.socketname.encode('utf-8'), self.client) |
Ole Troan | c424de7 | 2019-06-04 12:33:32 +0200 | [diff] [blame] | 216 | # Break out if success or any other error than "no such file" |
| 217 | # (indicating that VPP hasn't started yet) |
| 218 | if rv == 0 or ffi.errno != 2: |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 219 | self.connected = True |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 220 | break |
| 221 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 222 | if rv != 0: |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 223 | raise VPPStatsIOError(retval=rv) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 224 | |
| 225 | def heartbeat(self): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 226 | if not self.connected: |
| 227 | self.connect() |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 228 | return self.api.stat_segment_heartbeat_r(self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 229 | |
| 230 | def ls(self, patterns): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 231 | if not self.connected: |
| 232 | self.connect() |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 233 | return self.api.stat_segment_ls_r(make_string_vector(self.api, |
| 234 | patterns), |
| 235 | self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 236 | |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 237 | def lsstr(self, patterns): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 238 | if not self.connected: |
| 239 | self.connect() |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 240 | rv = self.api.stat_segment_ls_r(make_string_vector(self.api, |
| 241 | patterns), |
| 242 | self.client) |
| 243 | |
| 244 | if rv == ffi.NULL: |
| 245 | raise VPPStatsIOError() |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 246 | return [ffi.string(self.api.stat_segment_index_to_name_r( |
| 247 | rv[i], self.client)).decode('utf-8') |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 248 | for i in range(self.api.stat_segment_vec_len(rv))] |
| 249 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 250 | def dump(self, counters): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 251 | if not self.connected: |
| 252 | self.connect() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 253 | stats = {} |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 254 | rv = self.api.stat_segment_dump_r(counters, self.client) |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 255 | # Raise exception and retry |
| 256 | if rv == ffi.NULL: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 257 | raise VPPStatsIOError() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 258 | rv_len = self.api.stat_segment_vec_len(rv) |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 259 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 260 | for i in range(rv_len): |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 261 | n = ffi.string(rv[i].name).decode('utf-8') |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 262 | e = stat_entry_to_python(self.api, rv[i]) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 263 | if e is not None: |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 264 | stats[n] = e |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 265 | return stats |
| 266 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 267 | def get_counter(self, name): |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 268 | retries = 0 |
| 269 | while True: |
| 270 | try: |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 271 | d = self.ls(name) |
| 272 | s = self.dump(d) |
| 273 | if len(s) > 1: |
| 274 | raise AttributeError('Matches multiple counters {}' |
| 275 | .format(name)) |
| 276 | k, v = s.popitem() |
| 277 | return v |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 278 | except VPPStatsIOError: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 279 | if retries > 10: |
| 280 | return None |
| 281 | retries += 1 |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 282 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 283 | def get_err_counter(self, name): |
| 284 | """Get an error counter. The errors from each worker thread |
| 285 | are summed""" |
| 286 | return sum(self.get_counter(name)) |
| 287 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 288 | def disconnect(self): |
Paul Vinciguerra | e090f4d | 2019-11-29 17:41:20 -0500 | [diff] [blame] | 289 | try: |
| 290 | self.api.stat_segment_disconnect_r(self.client) |
| 291 | self.api.stat_client_free(self.client) |
| 292 | self.connected = False |
| 293 | del self.client |
| 294 | except AttributeError: |
| 295 | # no need to disconnect if we're not connected |
| 296 | pass |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 297 | |
| 298 | def set_errors(self): |
| 299 | '''Return all errors counters > 0''' |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 300 | retries = 0 |
| 301 | while True: |
| 302 | try: |
| 303 | error_names = self.ls(['/err/']) |
| 304 | error_counters = self.dump(error_names) |
| 305 | break |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 306 | except VPPStatsIOError: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 307 | if retries > 10: |
| 308 | return None |
| 309 | retries += 1 |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 310 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 311 | return {k: sum(error_counters[k]) |
| 312 | for k in error_counters.keys() if sum(error_counters[k])} |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 313 | |
| 314 | def set_errors_str(self): |
| 315 | '''Return all errors counters > 0 pretty printed''' |
Paul Vinciguerra | 5770c91 | 2019-06-29 08:33:05 -0400 | [diff] [blame] | 316 | s = ['ERRORS:'] |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 317 | error_counters = self.set_errors() |
| 318 | for k in sorted(error_counters): |
Paul Vinciguerra | 5770c91 | 2019-06-29 08:33:05 -0400 | [diff] [blame] | 319 | s.append('{:<60}{:>10}'.format(k, error_counters[k])) |
| 320 | return '%s\n' % '\n'.join(s) |