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
usage(char * name,int exit_code)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]\tControl header printing:\n");
77 fprintf(stderr, "\t\t\t\t0 = never\n");
78 fprintf(stderr, "\t\t\t\t1 = once\n");
79 fprintf(stderr, "\t\t\t\t2 = every 20 lines (default))\n");
80 fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
81 fprintf(stderr, "\n");
82
83 exit(exit_code);
84 }
85
86 struct field_param {
87 const char *name;
88 struct lnstat_field *lf;
89 struct {
90 unsigned int width;
91 } print;
92 };
93
94 struct field_params {
95 unsigned int num;
96 struct field_param params[MAX_FIELDS];
97 };
98
print_line(FILE * of,const struct lnstat_file * lnstat_files,const struct field_params * fp)99 static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
100 const struct field_params *fp)
101 {
102 int i;
103
104 for (i = 0; i < fp->num; i++) {
105 const struct lnstat_field *lf = fp->params[i].lf;
106
107 fprintf(of, "%*lu|", fp->params[i].print.width, lf->result);
108 }
109 fputc('\n', of);
110 }
111
print_json(FILE * of,const struct lnstat_file * lnstat_files,const struct field_params * fp)112 static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
113 const struct field_params *fp)
114 {
115 json_writer_t *jw = jsonw_new(of);
116 int i;
117
118 jsonw_start_object(jw);
119 for (i = 0; i < fp->num; i++) {
120 const struct lnstat_field *lf = fp->params[i].lf;
121
122 jsonw_uint_field(jw, lf->name, lf->result);
123 }
124 jsonw_end_object(jw);
125 jsonw_destroy(&jw);
126 }
127
128 /* find lnstat_field according to user specification */
map_field_params(struct lnstat_file * lnstat_files,struct field_params * fps,int interval)129 static int map_field_params(struct lnstat_file *lnstat_files,
130 struct field_params *fps, int interval)
131 {
132 int i, j = 0;
133 struct lnstat_file *lf;
134
135 /* no field specification on commandline, need to build default */
136 if (!fps->num) {
137 for (lf = lnstat_files; lf; lf = lf->next) {
138 for (i = 0; i < lf->num_fields; i++) {
139 fps->params[j].lf = &lf->fields[i];
140 fps->params[j].lf->file->interval.tv_sec =
141 interval;
142 if (!fps->params[j].print.width)
143 fps->params[j].print.width =
144 FIELD_WIDTH_DEFAULT;
145
146 if (++j >= MAX_FIELDS - 1) {
147 fprintf(stderr,
148 "WARN: MAX_FIELDS (%d) reached,"
149 " truncating number of keys\n",
150 MAX_FIELDS);
151 goto full;
152 }
153 }
154 }
155 full:
156 fps->num = j;
157 return 1;
158 }
159
160 for (i = 0; i < fps->num; i++) {
161 fps->params[i].lf = lnstat_find_field(lnstat_files,
162 fps->params[i].name);
163 if (!fps->params[i].lf) {
164 fprintf(stderr, "Field `%s' unknown\n",
165 fps->params[i].name);
166 return 0;
167 }
168 fps->params[i].lf->file->interval.tv_sec = interval;
169 if (!fps->params[i].print.width)
170 fps->params[i].print.width = FIELD_WIDTH_DEFAULT;
171 }
172 return 1;
173 }
174
175 struct table_hdr {
176 int num_lines;
177 char *hdr[HDR_LINES];
178 };
179
build_hdr_string(struct lnstat_file * lnstat_files,struct field_params * fps,int linewidth)180 static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
181 struct field_params *fps,
182 int linewidth)
183 {
184 int h,i;
185 static struct table_hdr th;
186 int ofs = 0;
187
188 for (i = 0; i < HDR_LINES; i++) {
189 th.hdr[i] = malloc(HDR_LINE_LENGTH);
190 memset(th.hdr[i], 0, HDR_LINE_LENGTH);
191 }
192
193 for (i = 0; i < fps->num; i++) {
194 char *cname, *fname = fps->params[i].lf->name;
195 unsigned int width = fps->params[i].print.width;
196
197 snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width,
198 fps->params[i].lf->file->basename);
199
200 cname = fname;
201 for (h = 1; h < HDR_LINES; h++) {
202 if (cname - fname >= strlen(fname))
203 snprintf(th.hdr[h]+ofs, width+2,
204 "%*.*s|", width, width, "");
205 else {
206 th.num_lines = h+1;
207 snprintf(th.hdr[h]+ofs, width+2,
208 "%*.*s|", width, width, cname);
209 }
210 cname += width;
211 }
212 ofs += width+1;
213 }
214 /* fill in spaces */
215 for (h = 1; h <= th.num_lines; h++) {
216 for (i = 0; i < ofs; i++) {
217 if (th.hdr[h][i] == '\0')
218 th.hdr[h][i] = ' ';
219 }
220 }
221
222 return &th;
223 }
224
print_hdr(FILE * of,struct table_hdr * th)225 static int print_hdr(FILE *of, struct table_hdr *th)
226 {
227 int i;
228
229 for (i = 0; i < th->num_lines; i++) {
230 fputs(th->hdr[i], of);
231 fputc('\n', of);
232 }
233 return 0;
234 }
235
236
main(int argc,char ** argv)237 int main(int argc, char **argv)
238 {
239 struct lnstat_file *lnstat_files;
240 const char *basename;
241 int i, c;
242 int interval = DEFAULT_INTERVAL;
243 int hdr = 2;
244 enum {
245 MODE_DUMP,
246 MODE_JSON,
247 MODE_NORMAL,
248 } mode = MODE_NORMAL;
249 unsigned long count = 0;
250 struct table_hdr *header;
251 static struct field_params fp;
252 int num_req_files = 0;
253 char *req_files[LNSTAT_MAX_FILES];
254
255 /* backwards compatibility mode for old tools */
256 basename = strrchr(argv[0], '/');
257 if (basename)
258 basename += 1; /* name after slash */
259 else
260 basename = argv[0]; /* no slash */
261
262 if (!strcmp(basename, "rtstat")) {
263 /* rtstat compatibility mode */
264 req_files[0] = "rt_cache";
265 num_req_files = 1;
266 } else if (!strcmp(basename, "ctstat")) {
267 /* ctstat compatibility mode */
268 req_files[0] = "ip_conntrack";
269 num_req_files = 1;
270 }
271
272 while ((c = getopt_long(argc, argv,"Vc:djpf:h?i:k:s:w:",
273 opts, NULL)) != -1) {
274 int len = 0;
275 char *tmp, *tok;
276
277 switch (c) {
278 case 'c':
279 count = strtoul(optarg, NULL, 0);
280 break;
281 case 'd':
282 mode = MODE_DUMP;
283 break;
284 case 'j':
285 mode = MODE_JSON;
286 break;
287 case 'f':
288 req_files[num_req_files++] = strdup(optarg);
289 break;
290 case '?':
291 case 'h':
292 usage(argv[0], 0);
293 break;
294 case 'i':
295 sscanf(optarg, "%u", &interval);
296 break;
297 case 'k':
298 tmp = strdup(optarg);
299 if (!tmp)
300 break;
301 for (tok = strtok(tmp, ",");
302 tok;
303 tok = strtok(NULL, ",")) {
304 if (fp.num >= MAX_FIELDS) {
305 fprintf(stderr,
306 "WARN: too many keys"
307 " requested: (%d max)\n",
308 MAX_FIELDS);
309 break;
310 }
311 fp.params[fp.num++].name = tok;
312 }
313 break;
314 case 's':
315 sscanf(optarg, "%u", &hdr);
316 break;
317 case 'w':
318 tmp = strdup(optarg);
319 if (!tmp)
320 break;
321 i = 0;
322 for (tok = strtok(tmp, ",");
323 tok;
324 tok = strtok(NULL, ",")) {
325 len = strtoul(tok, NULL, 0);
326 if (len > FIELD_WIDTH_MAX)
327 len = FIELD_WIDTH_MAX;
328 fp.params[i].print.width = len;
329 i++;
330 }
331 if (i == 1) {
332 for (i = 0; i < MAX_FIELDS; i++)
333 fp.params[i].print.width = len;
334 }
335 break;
336 default:
337 usage(argv[0], 1);
338 break;
339 }
340 }
341
342 lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
343 (const char **) req_files);
344
345 switch (mode) {
346 case MODE_DUMP:
347 lnstat_dump(stdout, lnstat_files);
348 break;
349
350 case MODE_NORMAL:
351 case MODE_JSON:
352 if (!map_field_params(lnstat_files, &fp, interval))
353 exit(1);
354
355 header = build_hdr_string(lnstat_files, &fp, 80);
356 if (!header)
357 exit(1);
358
359 if (interval < 1 )
360 interval = 1;
361
362 for (i = 0; i < count || !count; i++) {
363 lnstat_update(lnstat_files);
364 if (mode == MODE_JSON)
365 print_json(stdout, lnstat_files, &fp);
366 else {
367 if ((hdr > 1 && !(i % 20)) ||
368 (hdr == 1 && i == 0))
369 print_hdr(stdout, header);
370 print_line(stdout, lnstat_files, &fp);
371 }
372 fflush(stdout);
373 if (i < count - 1 || !count)
374 sleep(interval);
375 }
376 break;
377 }
378
379 return 1;
380 }
381