blob: 0385cbb5e30b906025dc72246998924e3bf497ba [file] [log] [blame]
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +00001/* 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 Hemmingerf493dc32008-06-30 10:37:28 -070020#define MAX_FIELDS 128
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000021
22/* Maximum number of header lines */
Stephen Hemminger3d0b7432014-12-20 15:47:17 -080023#define HDR_LINES 10
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000024
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
39#include "lnstat.h"
40
41static struct option opts[] = {
42 { "version", 0, NULL, 'V' },
43 { "count", 1, NULL, 'c' },
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -070044 { "dump", 0, NULL, 'd' },
45 { "json", 0, NULL, 'j' },
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000046 { "file", 1, NULL, 'f' },
47 { "help", 0, NULL, 'h' },
48 { "interval", 1, NULL, 'i' },
Petr Sabata7e8bd802011-10-06 14:45:33 +020049 { "keys", 1, NULL, 'k' },
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000050 { "subject", 1, NULL, 's' },
51 { "width", 1, NULL, 'w' },
52};
53
54static int usage(char *name, int exit_code)
55{
56 fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
57 fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
58 "<laforge@gnumonks.org>\n");
59 fprintf(stderr, "This program is free software licensed under GNU GPLv2"
60 "\nwith ABSOLUTELY NO WARRANTY.\n\n");
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 Sabata7e8bd802011-10-06 14:45:33 +020065 fprintf(stderr, "\t-d --dump\t\t"
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000066 "Dump list of available files/keys\n");
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -070067 fprintf(stderr, "\t-j --json\t\t"
68 "Display in JSON format\n");
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +000069 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");
74 fprintf(stderr, "\t-s --subject [0-2]\t?\n");
75 fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
76 fprintf(stderr, "\n");
77
78 exit(exit_code);
79}
80
81struct field_param {
82 const char *name;
83 struct lnstat_field *lf;
84 struct {
85 unsigned int width;
86 } print;
87};
88
89struct field_params {
90 unsigned int num;
91 struct field_param params[MAX_FIELDS];
92};
93
94static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
95 const struct field_params *fp)
96{
97 int i;
98
99 for (i = 0; i < fp->num; i++) {
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700100 const struct lnstat_field *lf = fp->params[i].lf;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000101
Stephen Hemminger468dec72014-05-29 10:49:55 -0700102 fprintf(of, "%*lu|", fp->params[i].print.width, lf->result);
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000103 }
104 fputc('\n', of);
105}
106
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700107static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
108 const struct field_params *fp)
109{
110 int i;
111 const char *sep;
112 const char *base = NULL;
113
114 fputs("{\n", of);
115 for (i = 0; i < fp->num; i++) {
116 const struct lnstat_field *lf = fp->params[i].lf;
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800117
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700118 if (!base || lf->file->basename != base) {
119 if (base) fputs("},\n", of);
120 base = lf->file->basename;
121 sep = "\n\t";
122 fprintf(of, " \"%s\":{", base);
123 }
124 fprintf(of, "%s\"%s\":%lu", sep,
125 lf->name, lf->result);
126 sep = ",\n\t";
127 }
128 fputs("}\n}\n", of);
129}
130
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000131/* find lnstat_field according to user specification */
132static int map_field_params(struct lnstat_file *lnstat_files,
133 struct field_params *fps, int interval)
134{
135 int i, j = 0;
136 struct lnstat_file *lf;
137
138 /* no field specification on commandline, need to build default */
139 if (!fps->num) {
140 for (lf = lnstat_files; lf; lf = lf->next) {
141 for (i = 0; i < lf->num_fields; i++) {
142 fps->params[j].lf = &lf->fields[i];
143 fps->params[j].lf->file->interval.tv_sec =
144 interval;
145 if (!fps->params[j].print.width)
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800146 fps->params[j].print.width =
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000147 FIELD_WIDTH_DEFAULT;
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800148
Stephen Hemmingerf309d0a2008-06-30 11:57:13 -0700149 if (++j >= MAX_FIELDS - 1) {
150 fprintf(stderr,
151 "WARN: MAX_FIELDS (%d) reached,"
152 " truncating number of keys\n",
153 MAX_FIELDS);
Stephen Hemmingerf493dc32008-06-30 10:37:28 -0700154 goto full;
Stephen Hemmingerf309d0a2008-06-30 11:57:13 -0700155 }
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000156 }
157 }
Stephen Hemmingerf493dc32008-06-30 10:37:28 -0700158 full:
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000159 fps->num = j;
160 return 1;
161 }
162
163 for (i = 0; i < fps->num; i++) {
164 fps->params[i].lf = lnstat_find_field(lnstat_files,
165 fps->params[i].name);
166 if (!fps->params[i].lf) {
167 fprintf(stderr, "Field `%s' unknown\n",
168 fps->params[i].name);
169 return 0;
170 }
171 fps->params[i].lf->file->interval.tv_sec = interval;
172 if (!fps->params[i].print.width)
173 fps->params[i].print.width = FIELD_WIDTH_DEFAULT;
174 }
175 return 1;
176}
177
178struct table_hdr {
179 int num_lines;
180 char *hdr[HDR_LINES];
181};
182
183static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
184 struct field_params *fps,
185 int linewidth)
186{
187 int h,i;
188 static struct table_hdr th;
189 int ofs = 0;
190
191 for (i = 0; i < HDR_LINES; i++) {
192 th.hdr[i] = malloc(HDR_LINE_LENGTH);
Kamil Rytarowski824c8432013-05-03 06:52:21 +0000193 memset(th.hdr[i], 0, HDR_LINE_LENGTH);
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000194 }
195
196 for (i = 0; i < fps->num; i++) {
197 char *cname, *fname = fps->params[i].lf->name;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000198 unsigned int width = fps->params[i].print.width;
199
Stephen Hemminger468dec72014-05-29 10:49:55 -0700200 snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width,
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000201 fps->params[i].lf->file->basename);
202
203 cname = fname;
204 for (h = 1; h < HDR_LINES; h++) {
205 if (cname - fname >= strlen(fname))
Stephen Hemminger468dec72014-05-29 10:49:55 -0700206 snprintf(th.hdr[h]+ofs, width+2,
207 "%*.*s|", width, width, "");
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000208 else {
209 th.num_lines = h+1;
Stephen Hemminger468dec72014-05-29 10:49:55 -0700210 snprintf(th.hdr[h]+ofs, width+2,
211 "%*.*s|", width, width, cname);
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000212 }
213 cname += width;
214 }
215 ofs += width+1;
216 }
217 /* fill in spaces */
218 for (h = 1; h <= th.num_lines; h++) {
219 for (i = 0; i < ofs; i++) {
220 if (th.hdr[h][i] == '\0')
221 th.hdr[h][i] = ' ';
222 }
223 }
224
225 return &th;
226}
227
228static int print_hdr(FILE *of, struct table_hdr *th)
229{
230 int i;
231
232 for (i = 0; i < th->num_lines; i++) {
233 fputs(th->hdr[i], of);
234 fputc('\n', of);
235 }
236 return 0;
237}
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800238
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000239
240int main(int argc, char **argv)
241{
242 struct lnstat_file *lnstat_files;
243 const char *basename;
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700244 int i, c;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000245 int interval = DEFAULT_INTERVAL;
246 int hdr = 2;
247 enum {
248 MODE_DUMP,
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700249 MODE_JSON,
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000250 MODE_NORMAL,
251 } mode = MODE_NORMAL;
Pavel Šimerdae7e29132015-04-13 16:01:01 +0200252 unsigned long count = 0;
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700253 struct table_hdr *header;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000254 static struct field_params fp;
255 int num_req_files = 0;
256 char *req_files[LNSTAT_MAX_FILES];
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800257
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000258 /* backwards compatibility mode for old tools */
259 basename = strrchr(argv[0], '/');
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800260 if (basename)
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000261 basename += 1; /* name after slash */
262 else
263 basename = argv[0]; /* no slash */
264
265 if (!strcmp(basename, "rtstat")) {
266 /* rtstat compatibility mode */
267 req_files[0] = "rt_cache";
268 num_req_files = 1;
269 } else if (!strcmp(basename, "ctstat")) {
270 /* ctstat compatibility mode */
271 req_files[0] = "ip_conntrack";
272 num_req_files = 1;
273 }
274
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700275 while ((c = getopt_long(argc, argv,"Vc:djf:h?i:k:s:w:",
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000276 opts, NULL)) != -1) {
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700277 int len = 0;
osdl.net!shemmingere796b952005-03-18 19:40:19 +0000278 char *tmp, *tok;
279
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000280 switch (c) {
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700281 case 'c':
282 count = strtoul(optarg, NULL, 0);
283 break;
284 case 'd':
285 mode = MODE_DUMP;
286 break;
287 case 'j':
288 mode = MODE_JSON;
289 break;
290 case 'f':
291 req_files[num_req_files++] = strdup(optarg);
292 break;
293 case '?':
294 case 'h':
295 usage(argv[0], 0);
296 break;
297 case 'i':
298 sscanf(optarg, "%u", &interval);
299 break;
300 case 'k':
301 tmp = strdup(optarg);
302 if (!tmp)
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000303 break;
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700304 for (tok = strtok(tmp, ",");
305 tok;
306 tok = strtok(NULL, ",")) {
307 if (fp.num >= MAX_FIELDS) {
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800308 fprintf(stderr,
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700309 "WARN: too many keys"
310 " requested: (%d max)\n",
311 MAX_FIELDS);
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000312 break;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000313 }
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700314 fp.params[fp.num++].name = tok;
315 }
316 break;
317 case 's':
318 sscanf(optarg, "%u", &hdr);
319 break;
320 case 'w':
321 tmp = strdup(optarg);
322 if (!tmp)
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000323 break;
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700324 i = 0;
325 for (tok = strtok(tmp, ",");
326 tok;
327 tok = strtok(NULL, ",")) {
328 len = strtoul(tok, NULL, 0);
329 if (len > FIELD_WIDTH_MAX)
330 len = FIELD_WIDTH_MAX;
331 fp.params[i].print.width = len;
332 i++;
333 }
334 if (i == 1) {
335 for (i = 0; i < MAX_FIELDS; i++)
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000336 fp.params[i].print.width = len;
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700337 }
338 break;
339 default:
340 usage(argv[0], 1);
341 break;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000342 }
343 }
344
345 lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
346 (const char **) req_files);
347
348 switch (mode) {
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000349 case MODE_DUMP:
Pavel Šimerdab1410e02015-04-13 16:01:00 +0200350 lnstat_dump(stdout, lnstat_files);
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000351 break;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000352
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700353 case MODE_NORMAL:
354 case MODE_JSON:
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000355 if (!map_field_params(lnstat_files, &fp, interval))
356 exit(1);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800357
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000358 header = build_hdr_string(lnstat_files, &fp, 80);
359 if (!header)
360 exit(1);
361
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800362 if (interval < 1 )
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700363 interval = 1;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000364
Pavel Šimerdae7e29132015-04-13 16:01:01 +0200365 for (i = 0; i < count || !count; ) {
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000366 lnstat_update(lnstat_files);
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700367 if (mode == MODE_JSON)
368 print_json(stdout, lnstat_files, &fp);
369 else {
370 if ((hdr > 1 &&
371 (! (i % 20))) || (hdr == 1 && i == 0))
372 print_hdr(stdout, header);
373 print_line(stdout, lnstat_files, &fp);
374 }
Eric Dumazetf50332c2007-09-27 14:26:09 +0200375 fflush(stdout);
Pavel Šimerdae7e29132015-04-13 16:01:01 +0200376 if (i < count - 1 || !count)
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700377 sleep(interval);
Pavel Šimerdae7e29132015-04-13 16:01:01 +0200378 if (count)
379 ++i;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000380 }
Stephen Hemmingera4f9e8d2013-09-24 11:55:27 -0700381 break;
osdl.net!shemminger0bb187c2004-10-19 20:47:13 +0000382 }
383
384 return 1;
385}
386