osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 1 | /* lnstat - Unified linux network statistics |
| 2 | * |
| 3 | * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * Development of this code was funded by Astaro AG, http://www.astaro.com/ |
| 6 | * |
| 7 | * Based on original concept and ideas from predecessor rtstat.c: |
| 8 | * |
| 9 | * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se> |
| 10 | * Uppsala University, Sweden |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* Maximum number of fields that can be displayed */ |
Stephen Hemminger | f493dc3 | 2008-06-30 10:37:28 -0700 | [diff] [blame] | 20 | #define MAX_FIELDS 128 |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 21 | |
| 22 | /* Maximum number of header lines */ |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 23 | #define HDR_LINES 10 |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 24 | |
| 25 | /* default field width if none specified */ |
| 26 | #define FIELD_WIDTH_DEFAULT 8 |
| 27 | #define FIELD_WIDTH_MAX 20 |
| 28 | |
| 29 | #define DEFAULT_INTERVAL 2 |
| 30 | |
| 31 | #define HDR_LINE_LENGTH (MAX_FIELDS*FIELD_WIDTH_MAX) |
| 32 | |
| 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <getopt.h> |
| 38 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 39 | #include <json_writer.h> |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 40 | #include "lnstat.h" |
| 41 | |
| 42 | static struct option opts[] = { |
| 43 | { "version", 0, NULL, 'V' }, |
| 44 | { "count", 1, NULL, 'c' }, |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 45 | { "dump", 0, NULL, 'd' }, |
| 46 | { "json", 0, NULL, 'j' }, |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 47 | { "file", 1, NULL, 'f' }, |
| 48 | { "help", 0, NULL, 'h' }, |
| 49 | { "interval", 1, NULL, 'i' }, |
Petr Sabata | 7e8bd80 | 2011-10-06 14:45:33 +0200 | [diff] [blame] | 50 | { "keys", 1, NULL, 'k' }, |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 51 | { "subject", 1, NULL, 's' }, |
| 52 | { "width", 1, NULL, 'w' }, |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 53 | { "oneline", 0, NULL, 0 }, |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static int usage(char *name, int exit_code) |
| 57 | { |
| 58 | fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 59 | fprintf(stderr, "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n"); |
| 60 | fprintf(stderr, "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n\n"); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 61 | fprintf(stderr, "Parameters:\n"); |
| 62 | fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n"); |
| 63 | fprintf(stderr, "\t-c --count <count>\t" |
| 64 | "Print <count> number of intervals\n"); |
Petr Sabata | 7e8bd80 | 2011-10-06 14:45:33 +0200 | [diff] [blame] | 65 | fprintf(stderr, "\t-d --dump\t\t" |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 66 | "Dump list of available files/keys\n"); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 67 | fprintf(stderr, "\t-j --json\t\t" |
| 68 | "Display in JSON format\n"); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 69 | fprintf(stderr, "\t-f --file <file>\tStatistics file to use\n"); |
| 70 | fprintf(stderr, "\t-h --help\t\tThis help message\n"); |
| 71 | fprintf(stderr, "\t-i --interval <intv>\t" |
| 72 | "Set interval to 'intv' seconds\n"); |
| 73 | fprintf(stderr, "\t-k --keys k,k,k,...\tDisplay only keys specified\n"); |
Phil Sutter | 869fcab | 2015-11-18 16:57:46 +0100 | [diff] [blame] | 74 | fprintf(stderr, "\t-s --subject [0-2]\tControl header printing:\n"); |
| 75 | fprintf(stderr, "\t\t\t\t0 = never\n"); |
| 76 | fprintf(stderr, "\t\t\t\t1 = once\n"); |
| 77 | fprintf(stderr, "\t\t\t\t2 = every 20 lines (default))\n"); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 78 | fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n"); |
| 79 | fprintf(stderr, "\n"); |
| 80 | |
| 81 | exit(exit_code); |
| 82 | } |
| 83 | |
| 84 | struct field_param { |
| 85 | const char *name; |
| 86 | struct lnstat_field *lf; |
| 87 | struct { |
| 88 | unsigned int width; |
| 89 | } print; |
| 90 | }; |
| 91 | |
| 92 | struct field_params { |
| 93 | unsigned int num; |
| 94 | struct field_param params[MAX_FIELDS]; |
| 95 | }; |
| 96 | |
| 97 | static void print_line(FILE *of, const struct lnstat_file *lnstat_files, |
| 98 | const struct field_params *fp) |
| 99 | { |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < fp->num; i++) { |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 103 | const struct lnstat_field *lf = fp->params[i].lf; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 104 | |
Stephen Hemminger | 468dec7 | 2014-05-29 10:49:55 -0700 | [diff] [blame] | 105 | fprintf(of, "%*lu|", fp->params[i].print.width, lf->result); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 106 | } |
| 107 | fputc('\n', of); |
| 108 | } |
| 109 | |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 110 | static void print_json(FILE *of, const struct lnstat_file *lnstat_files, |
| 111 | const struct field_params *fp) |
| 112 | { |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 113 | json_writer_t *jw = jsonw_new(of); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 114 | int i; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 115 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 116 | jsonw_start_object(jw); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 117 | for (i = 0; i < fp->num; i++) { |
| 118 | const struct lnstat_field *lf = fp->params[i].lf; |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 119 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 120 | jsonw_uint_field(jw, lf->name, lf->result); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 121 | } |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 122 | jsonw_end_object(jw); |
| 123 | jsonw_destroy(&jw); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 124 | } |
| 125 | |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 126 | /* find lnstat_field according to user specification */ |
| 127 | static int map_field_params(struct lnstat_file *lnstat_files, |
| 128 | struct field_params *fps, int interval) |
| 129 | { |
| 130 | int i, j = 0; |
| 131 | struct lnstat_file *lf; |
| 132 | |
| 133 | /* no field specification on commandline, need to build default */ |
| 134 | if (!fps->num) { |
| 135 | for (lf = lnstat_files; lf; lf = lf->next) { |
| 136 | for (i = 0; i < lf->num_fields; i++) { |
| 137 | fps->params[j].lf = &lf->fields[i]; |
| 138 | fps->params[j].lf->file->interval.tv_sec = |
| 139 | interval; |
| 140 | if (!fps->params[j].print.width) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 141 | fps->params[j].print.width = |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 142 | FIELD_WIDTH_DEFAULT; |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 143 | |
Stephen Hemminger | f309d0a | 2008-06-30 11:57:13 -0700 | [diff] [blame] | 144 | if (++j >= MAX_FIELDS - 1) { |
| 145 | fprintf(stderr, |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 146 | "WARN: MAX_FIELDS (%d) reached, truncating number of keys\n", |
Stephen Hemminger | f309d0a | 2008-06-30 11:57:13 -0700 | [diff] [blame] | 147 | MAX_FIELDS); |
Stephen Hemminger | f493dc3 | 2008-06-30 10:37:28 -0700 | [diff] [blame] | 148 | goto full; |
Stephen Hemminger | f309d0a | 2008-06-30 11:57:13 -0700 | [diff] [blame] | 149 | } |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 152 | full: |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 153 | fps->num = j; |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | for (i = 0; i < fps->num; i++) { |
| 158 | fps->params[i].lf = lnstat_find_field(lnstat_files, |
| 159 | fps->params[i].name); |
| 160 | if (!fps->params[i].lf) { |
| 161 | fprintf(stderr, "Field `%s' unknown\n", |
| 162 | fps->params[i].name); |
| 163 | return 0; |
| 164 | } |
| 165 | fps->params[i].lf->file->interval.tv_sec = interval; |
| 166 | if (!fps->params[i].print.width) |
| 167 | fps->params[i].print.width = FIELD_WIDTH_DEFAULT; |
| 168 | } |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | struct table_hdr { |
| 173 | int num_lines; |
| 174 | char *hdr[HDR_LINES]; |
| 175 | }; |
| 176 | |
| 177 | static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files, |
| 178 | struct field_params *fps, |
| 179 | int linewidth) |
| 180 | { |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 181 | int h, i; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 182 | static struct table_hdr th; |
| 183 | int ofs = 0; |
| 184 | |
Phil Sutter | f89bb02 | 2016-07-18 16:48:43 +0200 | [diff] [blame] | 185 | for (i = 0; i < HDR_LINES; i++) |
| 186 | th.hdr[i] = calloc(1, HDR_LINE_LENGTH); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 187 | |
| 188 | for (i = 0; i < fps->num; i++) { |
| 189 | char *cname, *fname = fps->params[i].lf->name; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 190 | unsigned int width = fps->params[i].print.width; |
| 191 | |
Stephen Hemminger | 468dec7 | 2014-05-29 10:49:55 -0700 | [diff] [blame] | 192 | snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width, |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 193 | fps->params[i].lf->file->basename); |
| 194 | |
| 195 | cname = fname; |
| 196 | for (h = 1; h < HDR_LINES; h++) { |
| 197 | if (cname - fname >= strlen(fname)) |
Stephen Hemminger | 468dec7 | 2014-05-29 10:49:55 -0700 | [diff] [blame] | 198 | snprintf(th.hdr[h]+ofs, width+2, |
| 199 | "%*.*s|", width, width, ""); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 200 | else { |
| 201 | th.num_lines = h+1; |
Stephen Hemminger | 468dec7 | 2014-05-29 10:49:55 -0700 | [diff] [blame] | 202 | snprintf(th.hdr[h]+ofs, width+2, |
| 203 | "%*.*s|", width, width, cname); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 204 | } |
| 205 | cname += width; |
| 206 | } |
| 207 | ofs += width+1; |
| 208 | } |
| 209 | /* fill in spaces */ |
| 210 | for (h = 1; h <= th.num_lines; h++) { |
| 211 | for (i = 0; i < ofs; i++) { |
| 212 | if (th.hdr[h][i] == '\0') |
| 213 | th.hdr[h][i] = ' '; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return &th; |
| 218 | } |
| 219 | |
| 220 | static int print_hdr(FILE *of, struct table_hdr *th) |
| 221 | { |
| 222 | int i; |
| 223 | |
| 224 | for (i = 0; i < th->num_lines; i++) { |
| 225 | fputs(th->hdr[i], of); |
| 226 | fputc('\n', of); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 230 | |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 231 | |
| 232 | int main(int argc, char **argv) |
| 233 | { |
| 234 | struct lnstat_file *lnstat_files; |
| 235 | const char *basename; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 236 | int i, c; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 237 | int interval = DEFAULT_INTERVAL; |
| 238 | int hdr = 2; |
| 239 | enum { |
| 240 | MODE_DUMP, |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 241 | MODE_JSON, |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 242 | MODE_NORMAL, |
| 243 | } mode = MODE_NORMAL; |
Pavel Šimerda | e7e2913 | 2015-04-13 16:01:01 +0200 | [diff] [blame] | 244 | unsigned long count = 0; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 245 | struct table_hdr *header; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 246 | static struct field_params fp; |
| 247 | int num_req_files = 0; |
| 248 | char *req_files[LNSTAT_MAX_FILES]; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 249 | |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 250 | /* backwards compatibility mode for old tools */ |
| 251 | basename = strrchr(argv[0], '/'); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 252 | if (basename) |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 253 | basename += 1; /* name after slash */ |
| 254 | else |
| 255 | basename = argv[0]; /* no slash */ |
| 256 | |
| 257 | if (!strcmp(basename, "rtstat")) { |
| 258 | /* rtstat compatibility mode */ |
| 259 | req_files[0] = "rt_cache"; |
| 260 | num_req_files = 1; |
| 261 | } else if (!strcmp(basename, "ctstat")) { |
| 262 | /* ctstat compatibility mode */ |
| 263 | req_files[0] = "ip_conntrack"; |
| 264 | num_req_files = 1; |
| 265 | } |
| 266 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 267 | while ((c = getopt_long(argc, argv, "Vc:djpf:h?i:k:s:w:", |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 268 | opts, NULL)) != -1) { |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 269 | int len = 0; |
osdl.net!shemminger | e796b95 | 2005-03-18 19:40:19 +0000 | [diff] [blame] | 270 | char *tmp, *tok; |
| 271 | |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 272 | switch (c) { |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 273 | case 'c': |
| 274 | count = strtoul(optarg, NULL, 0); |
| 275 | break; |
| 276 | case 'd': |
| 277 | mode = MODE_DUMP; |
| 278 | break; |
| 279 | case 'j': |
| 280 | mode = MODE_JSON; |
| 281 | break; |
| 282 | case 'f': |
| 283 | req_files[num_req_files++] = strdup(optarg); |
| 284 | break; |
| 285 | case '?': |
| 286 | case 'h': |
| 287 | usage(argv[0], 0); |
| 288 | break; |
| 289 | case 'i': |
| 290 | sscanf(optarg, "%u", &interval); |
| 291 | break; |
| 292 | case 'k': |
| 293 | tmp = strdup(optarg); |
| 294 | if (!tmp) |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 295 | break; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 296 | for (tok = strtok(tmp, ","); |
| 297 | tok; |
| 298 | tok = strtok(NULL, ",")) { |
| 299 | if (fp.num >= MAX_FIELDS) { |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 300 | fprintf(stderr, |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 301 | "WARN: too many keys requested: (%d max)\n", |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 302 | MAX_FIELDS); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 303 | break; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 304 | } |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 305 | fp.params[fp.num++].name = tok; |
| 306 | } |
| 307 | break; |
| 308 | case 's': |
| 309 | sscanf(optarg, "%u", &hdr); |
| 310 | break; |
| 311 | case 'w': |
| 312 | tmp = strdup(optarg); |
| 313 | if (!tmp) |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 314 | break; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 315 | i = 0; |
| 316 | for (tok = strtok(tmp, ","); |
| 317 | tok; |
| 318 | tok = strtok(NULL, ",")) { |
| 319 | len = strtoul(tok, NULL, 0); |
| 320 | if (len > FIELD_WIDTH_MAX) |
| 321 | len = FIELD_WIDTH_MAX; |
| 322 | fp.params[i].print.width = len; |
| 323 | i++; |
| 324 | } |
| 325 | if (i == 1) { |
| 326 | for (i = 0; i < MAX_FIELDS; i++) |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 327 | fp.params[i].print.width = len; |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 328 | } |
| 329 | break; |
| 330 | default: |
| 331 | usage(argv[0], 1); |
| 332 | break; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files, |
| 337 | (const char **) req_files); |
| 338 | |
| 339 | switch (mode) { |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 340 | case MODE_DUMP: |
Pavel Šimerda | b1410e0 | 2015-04-13 16:01:00 +0200 | [diff] [blame] | 341 | lnstat_dump(stdout, lnstat_files); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 342 | break; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 343 | |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 344 | case MODE_NORMAL: |
| 345 | case MODE_JSON: |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 346 | if (!map_field_params(lnstat_files, &fp, interval)) |
| 347 | exit(1); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 348 | |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 349 | header = build_hdr_string(lnstat_files, &fp, 80); |
| 350 | if (!header) |
| 351 | exit(1); |
| 352 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 353 | if (interval < 1) |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 354 | interval = 1; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 355 | |
Phil Sutter | fdb347f | 2015-11-18 16:57:47 +0100 | [diff] [blame] | 356 | for (i = 0; i < count || !count; i++) { |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 357 | lnstat_update(lnstat_files); |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 358 | if (mode == MODE_JSON) |
| 359 | print_json(stdout, lnstat_files, &fp); |
| 360 | else { |
Phil Sutter | fdb347f | 2015-11-18 16:57:47 +0100 | [diff] [blame] | 361 | if ((hdr > 1 && !(i % 20)) || |
| 362 | (hdr == 1 && i == 0)) |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 363 | print_hdr(stdout, header); |
| 364 | print_line(stdout, lnstat_files, &fp); |
| 365 | } |
Eric Dumazet | f50332c | 2007-09-27 14:26:09 +0200 | [diff] [blame] | 366 | fflush(stdout); |
Pavel Šimerda | e7e2913 | 2015-04-13 16:01:01 +0200 | [diff] [blame] | 367 | if (i < count - 1 || !count) |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 368 | sleep(interval); |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 369 | } |
Stephen Hemminger | a4f9e8d | 2013-09-24 11:55:27 -0700 | [diff] [blame] | 370 | break; |
osdl.net!shemminger | 0bb187c | 2004-10-19 20:47:13 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return 1; |
| 374 | } |