Debian Codegraph @

misc/lnstat.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* lnstat - Unified linux network statistics
3 *
4 * Copyright (C) 2004 by Harald Welte <[email protected]>
5 *
6 * Development of this code was funded by Astaro AG, http://www.astaro.com/
7 *
8 * Based on original concept and ideas from predecessor rtstat.c:
9 *
10 * Copyright 2001 by Robert Olsson <[email protected]>
11 * Uppsala University, Sweden
12 */
13
14 /* Maximum number of fields that can be displayed */
15 #define MAX_FIELDS 128
16
17 /* Maximum number of header lines */
18 #define HDR_LINES 10
19
20 /* default field width if none specified */
21 #define FIELD_WIDTH_DEFAULT 8
22 #define FIELD_WIDTH_MAX 20
23
24 #define DEFAULT_INTERVAL 2
25
26 #define HDR_LINE_LENGTH (MAX_FIELDS*FIELD_WIDTH_MAX)
27
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <linux/types.h>
34
35 #include "json_print.h"
36 #include "lnstat.h"
37 #include "version.h"
38
39 static struct option opts[] = {
40 { "version", 0, NULL, 'V' },
41 { "count", 1, NULL, 'c' },
42 { "dump", 0, NULL, 'd' },
43 { "json", 0, NULL, 'j' },
44 { "file", 1, NULL, 'f' },
45 { "help", 0, NULL, 'h' },
46 { "interval", 1, NULL, 'i' },
47 { "keys", 1, NULL, 'k' },
48 { "subject", 1, NULL, 's' },
49 { "width", 1, NULL, 'w' },
50 { "oneline", 0, NULL, 0 },
51 };
52
53 static int usage(char *name, int exit_code)
54 {
55 fprintf(stderr,
56 "%s Version %s\n"
57 "Copyright (C) 2004 by Harald Welte <[email protected]>\n"
58 "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n"
59 "\n"
60 "Parameters:\n"
61 " -V --version Print Version of Program\n"
62 " -c --count <count> "
63 "Print <count> number of intervals\n"
64 " -d --dump "
65 "Dump list of available files/keys\n"
66 " -j --json "
67 "Display in JSON format\n"
68 " -f --file <file> Statistics file to use\n"
69 " -h --help This help message\n"
70 " -i --interval <intv> "
71 "Set interval to 'intv' seconds\n"
72 " -k --keys k,k,k,... Display only keys specified\n"
73 " -s --subject [0-2] Control header printing:\n"
74 " 0 = never\n"
75 " 1 = once\n"
76 " 2 = every 20 lines (default))\n"
77 " -w --width n,n,n,... Width for each field\n"
78 "\n",
79 name, version);
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++) {
103 const struct lnstat_field *lf = fp->params[i].lf;
104
105 fprintf(of, "%*lu|", fp->params[i].print.width, lf->result);
106 }
107 fputc('\n', of);
108 }
109
110 static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
111 const struct field_params *fp)
112 {
113 int i;
114
115 new_json_obj_plain(1);
116 open_json_object(NULL);
117 for (i = 0; i < fp->num; i++) {
118 const struct lnstat_field *lf = fp->params[i].lf;
119
120 print_luint(PRINT_JSON, lf->name, NULL, lf->result);
121 }
122 close_json_object();
123 delete_json_obj_plain();
124 }
125
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)
141 fps->params[j].print.width =
142 FIELD_WIDTH_DEFAULT;
143
144 if (++j >= MAX_FIELDS - 1) {
145 fprintf(stderr,
146 "WARN: MAX_FIELDS (%d) reached, 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] = calloc(1, HDR_LINE_LENGTH);
187
188 for (i = 0; i < fps->num; i++) {
189 char *cname, *fname = fps->params[i].lf->name;
190 unsigned int width = fps->params[i].print.width;
191
192 snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width,
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))
198 snprintf(th.hdr[h]+ofs, width+2,
199 "%*.*s|", width, width, "");
200 else {
201 th.num_lines = h+1;
202 snprintf(th.hdr[h]+ofs, width+2,
203 "%*.*s|", width, width, cname);
204 }
205 cname += width;
206 }
207 ofs += width+1;
208 }
209
210 /* fill in spaces */
211 for (h = 1; h < th.num_lines; h++) {
212 for (i = 0; i < ofs; i++) {
213 if (th.hdr[h][i] == '\0')
214 th.hdr[h][i] = ' ';
215 }
216 }
217
218 return &th;
219 }
220
221 static int print_hdr(FILE *of, struct table_hdr *th)
222 {
223 int i;
224
225 for (i = 0; i < th->num_lines; i++) {
226 fputs(th->hdr[i], of);
227 fputc('\n', of);
228 }
229 return 0;
230 }
231
232
233 int main(int argc, char **argv)
234 {
235 struct lnstat_file *lnstat_files;
236 const char *basename;
237 int i, c;
238 int interval = DEFAULT_INTERVAL;
239 int hdr = 2;
240 enum {
241 MODE_DUMP,
242 MODE_JSON,
243 MODE_NORMAL,
244 } mode = MODE_NORMAL;
245 unsigned long count = 0;
246 struct table_hdr *header;
247 static struct field_params fp;
248 int num_req_files = 0;
249 char *req_files[LNSTAT_MAX_FILES];
250
251 /* backwards compatibility mode for old tools */
252 basename = strrchr(argv[0], '/');
253 if (basename)
254 basename += 1; /* name after slash */
255 else
256 basename = argv[0]; /* no slash */
257
258 if (!strcmp(basename, "rtstat")) {
259 /* rtstat compatibility mode */
260 req_files[0] = "rt_cache";
261 num_req_files = 1;
262 } else if (!strcmp(basename, "ctstat")) {
263 /* ctstat compatibility mode */
264 req_files[0] = "ip_conntrack";
265 num_req_files = 1;
266 }
267
268 while ((c = getopt_long(argc, argv, "Vc:djpf:h?i:k:s:w:",
269 opts, NULL)) != -1) {
270 int len = 0;
271 char *tmp, *tok;
272
273 switch (c) {
274 case 'c':
275 count = strtoul(optarg, NULL, 0);
276 break;
277 case 'd':
278 mode = MODE_DUMP;
279 break;
280 case 'j':
281 mode = MODE_JSON;
282 break;
283 case 'f':
284 req_files[num_req_files++] = strdup(optarg);
285 break;
286 case '?':
287 case 'h':
288 usage(argv[0], 0);
289 break;
290 case 'i':
291 sscanf(optarg, "%u", &interval);
292 break;
293 case 'k':
294 tmp = strdup(optarg);
295 if (!tmp)
296 break;
297 for (tok = strtok(tmp, ",");
298 tok;
299 tok = strtok(NULL, ",")) {
300 if (fp.num >= MAX_FIELDS) {
301 fprintf(stderr,
302 "WARN: too many keys requested: (%d max)\n",
303 MAX_FIELDS);
304 break;
305 }
306 fp.params[fp.num++].name = tok;
307 }
308 break;
309 case 's':
310 sscanf(optarg, "%u", &hdr);
311 break;
312 case 'w':
313 tmp = strdup(optarg);
314 if (!tmp)
315 break;
316 i = 0;
317 for (tok = strtok(tmp, ",");
318 tok;
319 tok = strtok(NULL, ",")) {
320 len = strtoul(tok, NULL, 0);
321 if (len > FIELD_WIDTH_MAX)
322 len = FIELD_WIDTH_MAX;
323 fp.params[i].print.width = len;
324 i++;
325 }
326 if (i == 1) {
327 for (i = 0; i < MAX_FIELDS; i++)
328 fp.params[i].print.width = len;
329 }
330 free(tmp);
331 break;
332 default:
333 usage(argv[0], 1);
334 break;
335 }
336 }
337
338 lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
339 (const char **) req_files);
340
341 switch (mode) {
342 case MODE_DUMP:
343 lnstat_dump(stdout, lnstat_files);
344 break;
345
346 case MODE_NORMAL:
347 case MODE_JSON:
348 if (!map_field_params(lnstat_files, &fp, interval))
349 exit(1);
350
351 header = build_hdr_string(lnstat_files, &fp, 80);
352 if (!header)
353 exit(1);
354
355 if (interval < 1)
356 interval = 1;
357
358 for (i = 0; i < count || !count; i++) {
359 lnstat_update(lnstat_files);
360 if (mode == MODE_JSON)
361 print_json(stdout, lnstat_files, &fp);
362 else {
363 if ((hdr > 1 && !(i % 20)) ||
364 (hdr == 1 && i == 0))
365 print_hdr(stdout, header);
366 print_line(stdout, lnstat_files, &fp);
367 }
368 fflush(stdout);
369 if (i < count - 1 || !count)
370 sleep(interval);
371 }
372 break;
373 }
374
375 return 1;
376 }