]> git.proxmox.com Git - mirror_iproute2.git/blob - misc/lnstat.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / misc / lnstat.c
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 */
20 #define MAX_FIELDS 128
21
22 /* Maximum number of header lines */
23 #define HDR_LINES 10
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
39 #include <json_writer.h>
40 #include "lnstat.h"
41
42 static struct option opts[] = {
43 { "version", 0, NULL, 'V' },
44 { "count", 1, NULL, 'c' },
45 { "dump", 0, NULL, 'd' },
46 { "json", 0, NULL, 'j' },
47 { "file", 1, NULL, 'f' },
48 { "help", 0, NULL, 'h' },
49 { "interval", 1, NULL, 'i' },
50 { "keys", 1, NULL, 'k' },
51 { "subject", 1, NULL, 's' },
52 { "width", 1, NULL, 'w' },
53 { "oneline", 0, NULL, 0 },
54 };
55
56 static int usage(char *name, int exit_code)
57 {
58 fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
59 fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
60 "<laforge@gnumonks.org>\n");
61 fprintf(stderr, "This program is free software licensed under GNU GPLv2"
62 "\nwith ABSOLUTELY NO WARRANTY.\n\n");
63 fprintf(stderr, "Parameters:\n");
64 fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
65 fprintf(stderr, "\t-c --count <count>\t"
66 "Print <count> number of intervals\n");
67 fprintf(stderr, "\t-d --dump\t\t"
68 "Dump list of available files/keys\n");
69 fprintf(stderr, "\t-j --json\t\t"
70 "Display in JSON format\n");
71 fprintf(stderr, "\t-f --file <file>\tStatistics file to use\n");
72 fprintf(stderr, "\t-h --help\t\tThis help message\n");
73 fprintf(stderr, "\t-i --interval <intv>\t"
74 "Set interval to 'intv' seconds\n");
75 fprintf(stderr, "\t-k --keys k,k,k,...\tDisplay only keys specified\n");
76 fprintf(stderr, "\t-s --subject [0-2]\t?\n");
77 fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
78 fprintf(stderr, "\n");
79
80 exit(exit_code);
81 }
82
83 struct field_param {
84 const char *name;
85 struct lnstat_field *lf;
86 struct {
87 unsigned int width;
88 } print;
89 };
90
91 struct field_params {
92 unsigned int num;
93 struct field_param params[MAX_FIELDS];
94 };
95
96 static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
97 const struct field_params *fp)
98 {
99 int i;
100
101 for (i = 0; i < fp->num; i++) {
102 const struct lnstat_field *lf = fp->params[i].lf;
103
104 fprintf(of, "%*lu|", fp->params[i].print.width, lf->result);
105 }
106 fputc('\n', of);
107 }
108
109 static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
110 const struct field_params *fp)
111 {
112 json_writer_t *jw = jsonw_new(of);
113 int i;
114
115 jsonw_start_object(jw);
116 for (i = 0; i < fp->num; i++) {
117 const struct lnstat_field *lf = fp->params[i].lf;
118
119 jsonw_uint_field(jw, lf->name, lf->result);
120 }
121 jsonw_end_object(jw);
122 jsonw_destroy(&jw);
123 }
124
125 /* find lnstat_field according to user specification */
126 static int map_field_params(struct lnstat_file *lnstat_files,
127 struct field_params *fps, int interval)
128 {
129 int i, j = 0;
130 struct lnstat_file *lf;
131
132 /* no field specification on commandline, need to build default */
133 if (!fps->num) {
134 for (lf = lnstat_files; lf; lf = lf->next) {
135 for (i = 0; i < lf->num_fields; i++) {
136 fps->params[j].lf = &lf->fields[i];
137 fps->params[j].lf->file->interval.tv_sec =
138 interval;
139 if (!fps->params[j].print.width)
140 fps->params[j].print.width =
141 FIELD_WIDTH_DEFAULT;
142
143 if (++j >= MAX_FIELDS - 1) {
144 fprintf(stderr,
145 "WARN: MAX_FIELDS (%d) reached,"
146 " truncating number of keys\n",
147 MAX_FIELDS);
148 goto full;
149 }
150 }
151 }
152 full:
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 {
181 int h,i;
182 static struct table_hdr th;
183 int ofs = 0;
184
185 for (i = 0; i < HDR_LINES; i++) {
186 th.hdr[i] = malloc(HDR_LINE_LENGTH);
187 memset(th.hdr[i], 0, HDR_LINE_LENGTH);
188 }
189
190 for (i = 0; i < fps->num; i++) {
191 char *cname, *fname = fps->params[i].lf->name;
192 unsigned int width = fps->params[i].print.width;
193
194 snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width,
195 fps->params[i].lf->file->basename);
196
197 cname = fname;
198 for (h = 1; h < HDR_LINES; h++) {
199 if (cname - fname >= strlen(fname))
200 snprintf(th.hdr[h]+ofs, width+2,
201 "%*.*s|", width, width, "");
202 else {
203 th.num_lines = h+1;
204 snprintf(th.hdr[h]+ofs, width+2,
205 "%*.*s|", width, width, cname);
206 }
207 cname += width;
208 }
209 ofs += width+1;
210 }
211 /* fill in spaces */
212 for (h = 1; h <= th.num_lines; h++) {
213 for (i = 0; i < ofs; i++) {
214 if (th.hdr[h][i] == '\0')
215 th.hdr[h][i] = ' ';
216 }
217 }
218
219 return &th;
220 }
221
222 static int print_hdr(FILE *of, struct table_hdr *th)
223 {
224 int i;
225
226 for (i = 0; i < th->num_lines; i++) {
227 fputs(th->hdr[i], of);
228 fputc('\n', of);
229 }
230 return 0;
231 }
232
233
234 int main(int argc, char **argv)
235 {
236 struct lnstat_file *lnstat_files;
237 const char *basename;
238 int i, c;
239 int interval = DEFAULT_INTERVAL;
240 int hdr = 2;
241 enum {
242 MODE_DUMP,
243 MODE_JSON,
244 MODE_NORMAL,
245 } mode = MODE_NORMAL;
246 unsigned long count = 0;
247 struct table_hdr *header;
248 static struct field_params fp;
249 int num_req_files = 0;
250 char *req_files[LNSTAT_MAX_FILES];
251
252 /* backwards compatibility mode for old tools */
253 basename = strrchr(argv[0], '/');
254 if (basename)
255 basename += 1; /* name after slash */
256 else
257 basename = argv[0]; /* no slash */
258
259 if (!strcmp(basename, "rtstat")) {
260 /* rtstat compatibility mode */
261 req_files[0] = "rt_cache";
262 num_req_files = 1;
263 } else if (!strcmp(basename, "ctstat")) {
264 /* ctstat compatibility mode */
265 req_files[0] = "ip_conntrack";
266 num_req_files = 1;
267 }
268
269 while ((c = getopt_long(argc, argv,"Vc:djpf:h?i:k:s:w:",
270 opts, NULL)) != -1) {
271 int len = 0;
272 char *tmp, *tok;
273
274 switch (c) {
275 case 'c':
276 count = strtoul(optarg, NULL, 0);
277 break;
278 case 'd':
279 mode = MODE_DUMP;
280 break;
281 case 'j':
282 mode = MODE_JSON;
283 break;
284 case 'f':
285 req_files[num_req_files++] = strdup(optarg);
286 break;
287 case '?':
288 case 'h':
289 usage(argv[0], 0);
290 break;
291 case 'i':
292 sscanf(optarg, "%u", &interval);
293 break;
294 case 'k':
295 tmp = strdup(optarg);
296 if (!tmp)
297 break;
298 for (tok = strtok(tmp, ",");
299 tok;
300 tok = strtok(NULL, ",")) {
301 if (fp.num >= MAX_FIELDS) {
302 fprintf(stderr,
303 "WARN: too many keys"
304 " requested: (%d max)\n",
305 MAX_FIELDS);
306 break;
307 }
308 fp.params[fp.num++].name = tok;
309 }
310 break;
311 case 's':
312 sscanf(optarg, "%u", &hdr);
313 break;
314 case 'w':
315 tmp = strdup(optarg);
316 if (!tmp)
317 break;
318 i = 0;
319 for (tok = strtok(tmp, ",");
320 tok;
321 tok = strtok(NULL, ",")) {
322 len = strtoul(tok, NULL, 0);
323 if (len > FIELD_WIDTH_MAX)
324 len = FIELD_WIDTH_MAX;
325 fp.params[i].print.width = len;
326 i++;
327 }
328 if (i == 1) {
329 for (i = 0; i < MAX_FIELDS; i++)
330 fp.params[i].print.width = len;
331 }
332 break;
333 default:
334 usage(argv[0], 1);
335 break;
336 }
337 }
338
339 lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
340 (const char **) req_files);
341
342 switch (mode) {
343 case MODE_DUMP:
344 lnstat_dump(stdout, lnstat_files);
345 break;
346
347 case MODE_NORMAL:
348 case MODE_JSON:
349 if (!map_field_params(lnstat_files, &fp, interval))
350 exit(1);
351
352 header = build_hdr_string(lnstat_files, &fp, 80);
353 if (!header)
354 exit(1);
355
356 if (interval < 1 )
357 interval = 1;
358
359 for (i = 0; i < count || !count; ) {
360 lnstat_update(lnstat_files);
361 if (mode == MODE_JSON)
362 print_json(stdout, lnstat_files, &fp);
363 else {
364 if ((hdr > 1 &&
365 (! (i % 20))) || (hdr == 1 && i == 0))
366 print_hdr(stdout, header);
367 print_line(stdout, lnstat_files, &fp);
368 }
369 fflush(stdout);
370 if (i < count - 1 || !count)
371 sleep(interval);
372 if (count)
373 ++i;
374 }
375 break;
376 }
377
378 return 1;
379 }