summaryrefslogtreecommitdiff
path: root/misc/lnstat.c
blob: c3f2999cc2558445888181e72203fe86d1fa44cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* lnstat - Unified linux network statistics
 *
 * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
 *
 * Development of this code was funded by Astaro AG, http://www.astaro.com/
 *
 * Based on original concept and ideas from predecessor rtstat.c:
 *
 * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
 *                                 Uppsala University, Sweden
 */

/* Maximum number of fields that can be displayed */
#define MAX_FIELDS		128

/* Maximum number of header lines */
#define HDR_LINES		10

/* default field width if none specified */
#define FIELD_WIDTH_DEFAULT	8
#define FIELD_WIDTH_MAX		20

#define DEFAULT_INTERVAL	2

#define HDR_LINE_LENGTH		(MAX_FIELDS*FIELD_WIDTH_MAX)

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include <json_writer.h>
#include "lnstat.h"
#include "version.h"

static struct option opts[] = {
	{ "version", 0, NULL, 'V' },
	{ "count", 1, NULL, 'c' },
	{ "dump", 0, NULL, 'd' },
	{ "json", 0, NULL, 'j' },
	{ "file", 1, NULL, 'f' },
	{ "help", 0, NULL, 'h' },
	{ "interval", 1, NULL, 'i' },
	{ "keys", 1, NULL, 'k' },
	{ "subject", 1, NULL, 's' },
	{ "width", 1, NULL, 'w' },
	{ "oneline", 0, NULL, 0 },
};

static int usage(char *name, int exit_code)
{
	fprintf(stderr,
		"%s Version %s\n"
		"Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n"
		"This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n"
		"\n"
		"Parameters:\n"
		"	-V --version		Print Version of Program\n"
		"	-c --count <count>	"
		"Print <count> number of intervals\n"
		"	-d --dump		"
		"Dump list of available files/keys\n"
		"	-j --json		"
		"Display in JSON format\n"
		"	-f --file <file>	Statistics file to use\n"
		"	-h --help		This help message\n"
		"	-i --interval <intv>	"
		"Set interval to 'intv' seconds\n"
		"	-k --keys k,k,k,...	Display only keys specified\n"
		"	-s --subject [0-2]	Control header printing:\n"
		"				0 = never\n"
		"				1 = once\n"
		"				2 = every 20 lines (default))\n"
		"	-w --width n,n,n,...	Width for each field\n"
		"\n",
		name, version);

	exit(exit_code);
}

struct field_param {
	const char *name;
	struct lnstat_field *lf;
	struct {
		unsigned int width;
	} print;
};

struct field_params {
	unsigned int num;
	struct field_param params[MAX_FIELDS];
};

static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
		       const struct field_params *fp)
{
	int i;

	for (i = 0; i < fp->num; i++) {
		const struct lnstat_field *lf = fp->params[i].lf;

		fprintf(of, "%*lu|", fp->params[i].print.width, lf->result);
	}
	fputc('\n', of);
}

static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
		       const struct field_params *fp)
{
	json_writer_t *jw = jsonw_new(of);
	int i;

	jsonw_start_object(jw);
	for (i = 0; i < fp->num; i++) {
		const struct lnstat_field *lf = fp->params[i].lf;

		jsonw_uint_field(jw, lf->name, lf->result);
	}
	jsonw_end_object(jw);
	jsonw_destroy(&jw);
}

/* find lnstat_field according to user specification */
static int map_field_params(struct lnstat_file *lnstat_files,
			    struct field_params *fps, int interval)
{
	int i, j = 0;
	struct lnstat_file *lf;

	/* no field specification on commandline, need to build default */
	if (!fps->num) {
		for (lf = lnstat_files; lf; lf = lf->next) {
			for (i = 0; i < lf->num_fields; i++) {
				fps->params[j].lf = &lf->fields[i];
				fps->params[j].lf->file->interval.tv_sec =
								interval;
				if (!fps->params[j].print.width)
					fps->params[j].print.width =
							FIELD_WIDTH_DEFAULT;

				if (++j >= MAX_FIELDS - 1) {
					fprintf(stderr,
						"WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
						MAX_FIELDS);
					goto full;
				}
			}
		}
full:
		fps->num = j;
		return 1;
	}

	for (i = 0; i < fps->num; i++) {
		fps->params[i].lf = lnstat_find_field(lnstat_files,
						      fps->params[i].name);
		if (!fps->params[i].lf) {
			fprintf(stderr, "Field `%s' unknown\n",
				fps->params[i].name);
			return 0;
		}
		fps->params[i].lf->file->interval.tv_sec = interval;
		if (!fps->params[i].print.width)
			fps->params[i].print.width = FIELD_WIDTH_DEFAULT;
	}
	return 1;
}

struct table_hdr {
	int num_lines;
	char *hdr[HDR_LINES];
};

static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
					  struct field_params *fps,
					  int linewidth)
{
	int h, i;
	static struct table_hdr th;
	int ofs = 0;

	for (i = 0; i < HDR_LINES; i++)
		th.hdr[i] = calloc(1, HDR_LINE_LENGTH);

	for (i = 0; i < fps->num; i++) {
		char *cname, *fname = fps->params[i].lf->name;
		unsigned int width = fps->params[i].print.width;

		snprintf(th.hdr[0]+ofs, width+2, "%*.*s|", width, width,
			 fps->params[i].lf->file->basename);

		cname = fname;
		for (h = 1; h < HDR_LINES; h++) {
			if (cname - fname >= strlen(fname))
				snprintf(th.hdr[h]+ofs, width+2,
					 "%*.*s|", width, width, "");
			else {
				th.num_lines = h+1;
				snprintf(th.hdr[h]+ofs, width+2,
					 "%*.*s|", width, width, cname);
			}
			cname += width;
		}
		ofs += width+1;
	}

	/* fill in spaces */
	for (h = 1; h < th.num_lines; h++) {
		for (i = 0; i < ofs; i++) {
			if (th.hdr[h][i] == '\0')
				th.hdr[h][i] = ' ';
		}
	}

	return &th;
}

static int print_hdr(FILE *of, struct table_hdr *th)
{
	int i;

	for (i = 0; i < th->num_lines; i++) {
		fputs(th->hdr[i], of);
		fputc('\n', of);
	}
	return 0;
}


int main(int argc, char **argv)
{
	struct lnstat_file *lnstat_files;
	const char *basename;
	int i, c;
	int interval = DEFAULT_INTERVAL;
	int hdr = 2;
	enum {
		MODE_DUMP,
		MODE_JSON,
		MODE_NORMAL,
	} mode = MODE_NORMAL;
	unsigned long count = 0;
	struct table_hdr *header;
	static struct field_params fp;
	int num_req_files = 0;
	char *req_files[LNSTAT_MAX_FILES];

	/* backwards compatibility mode for old tools */
	basename = strrchr(argv[0], '/');
	if (basename)
		basename += 1;	  /* name after slash */
	else
		basename = argv[0]; /* no slash */

	if (!strcmp(basename, "rtstat")) {
		/* rtstat compatibility mode */
		req_files[0] = "rt_cache";
		num_req_files = 1;
	} else if (!strcmp(basename, "ctstat")) {
		/* ctstat compatibility mode */
		req_files[0] = "ip_conntrack";
		num_req_files = 1;
	}

	while ((c = getopt_long(argc, argv, "Vc:djpf:h?i:k:s:w:",
				opts, NULL)) != -1) {
		int len = 0;
		char *tmp, *tok;

		switch (c) {
		case 'c':
			count = strtoul(optarg, NULL, 0);
			break;
		case 'd':
			mode = MODE_DUMP;
			break;
		case 'j':
			mode = MODE_JSON;
			break;
		case 'f':
			req_files[num_req_files++] = strdup(optarg);
			break;
		case '?':
		case 'h':
			usage(argv[0], 0);
			break;
		case 'i':
			sscanf(optarg, "%u", &interval);
			break;
		case 'k':
			tmp = strdup(optarg);
			if (!tmp)
				break;
			for (tok = strtok(tmp, ",");
			     tok;
			     tok = strtok(NULL, ",")) {
				if (fp.num >= MAX_FIELDS) {
					fprintf(stderr,
						"WARN: too many keys requested: (%d max)\n",
						MAX_FIELDS);
					break;
				}
				fp.params[fp.num++].name = tok;
			}
			break;
		case 's':
			sscanf(optarg, "%u", &hdr);
			break;
		case 'w':
			tmp = strdup(optarg);
			if (!tmp)
				break;
			i = 0;
			for (tok = strtok(tmp, ",");
			     tok;
			     tok = strtok(NULL, ",")) {
				len  = strtoul(tok, NULL, 0);
				if (len > FIELD_WIDTH_MAX)
					len = FIELD_WIDTH_MAX;
				fp.params[i].print.width = len;
				i++;
			}
			if (i == 1) {
				for (i = 0; i < MAX_FIELDS; i++)
					fp.params[i].print.width = len;
			}
			free(tmp);
			break;
		default:
			usage(argv[0], 1);
			break;
		}
	}

	lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
				       (const char **) req_files);

	switch (mode) {
	case MODE_DUMP:
		lnstat_dump(stdout, lnstat_files);
		break;

	case MODE_NORMAL:
	case MODE_JSON:
		if (!map_field_params(lnstat_files, &fp, interval))
			exit(1);

		header = build_hdr_string(lnstat_files, &fp, 80);
		if (!header)
			exit(1);

		if (interval < 1)
			interval = 1;

		for (i = 0; i < count || !count; i++) {
			lnstat_update(lnstat_files);
			if (mode == MODE_JSON)
				print_json(stdout, lnstat_files, &fp);
			else {
				if  ((hdr > 1 && !(i % 20)) ||
				     (hdr == 1 && i == 0))
					print_hdr(stdout, header);
				print_line(stdout, lnstat_files, &fp);
			}
			fflush(stdout);
			if (i < count - 1 || !count)
				sleep(interval);
		}
		break;
	}

	return 1;
}