]>
git.proxmox.com Git - mirror_iproute2.git/blob - misc/lnstat.c
1 /* lnstat - Unified linux network statistics
3 * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
5 * Development of this code was funded by Astaro AG, http://www.astaro.com/
7 * Based on original concept and ideas from predecessor rtstat.c:
9 * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
10 * Uppsala University, Sweden
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.
19 /* Maximum number of fields that can be displayed */
20 #define MAX_FIELDS 128
22 /* Maximum number of header lines */
25 /* default field width if none specified */
26 #define FIELD_WIDTH_DEFAULT 8
27 #define FIELD_WIDTH_MAX 20
29 #define DEFAULT_INTERVAL 2
31 #define HDR_LINE_LENGTH (MAX_FIELDS*FIELD_WIDTH_MAX)
39 #include <json_writer.h>
43 static struct option opts
[] = {
44 { "version", 0, NULL
, 'V' },
45 { "count", 1, NULL
, 'c' },
46 { "dump", 0, NULL
, 'd' },
47 { "json", 0, NULL
, 'j' },
48 { "file", 1, NULL
, 'f' },
49 { "help", 0, NULL
, 'h' },
50 { "interval", 1, NULL
, 'i' },
51 { "keys", 1, NULL
, 'k' },
52 { "subject", 1, NULL
, 's' },
53 { "width", 1, NULL
, 'w' },
54 { "oneline", 0, NULL
, 0 },
57 static int usage(char *name
, int exit_code
)
61 "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n"
62 "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n"
65 " -V --version Print Version of Program\n"
66 " -c --count <count> "
67 "Print <count> number of intervals\n"
69 "Dump list of available files/keys\n"
71 "Display in JSON format\n"
72 " -f --file <file> Statistics file to use\n"
73 " -h --help This help message\n"
74 " -i --interval <intv> "
75 "Set interval to 'intv' seconds\n"
76 " -k --keys k,k,k,... Display only keys specified\n"
77 " -s --subject [0-2] Control header printing:\n"
80 " 2 = every 20 lines (default))\n"
81 " -w --width n,n,n,... Width for each field\n"
90 struct lnstat_field
*lf
;
98 struct field_param params
[MAX_FIELDS
];
101 static void print_line(FILE *of
, const struct lnstat_file
*lnstat_files
,
102 const struct field_params
*fp
)
106 for (i
= 0; i
< fp
->num
; i
++) {
107 const struct lnstat_field
*lf
= fp
->params
[i
].lf
;
109 fprintf(of
, "%*lu|", fp
->params
[i
].print
.width
, lf
->result
);
114 static void print_json(FILE *of
, const struct lnstat_file
*lnstat_files
,
115 const struct field_params
*fp
)
117 json_writer_t
*jw
= jsonw_new(of
);
120 jsonw_start_object(jw
);
121 for (i
= 0; i
< fp
->num
; i
++) {
122 const struct lnstat_field
*lf
= fp
->params
[i
].lf
;
124 jsonw_uint_field(jw
, lf
->name
, lf
->result
);
126 jsonw_end_object(jw
);
130 /* find lnstat_field according to user specification */
131 static int map_field_params(struct lnstat_file
*lnstat_files
,
132 struct field_params
*fps
, int interval
)
135 struct lnstat_file
*lf
;
137 /* no field specification on commandline, need to build default */
139 for (lf
= lnstat_files
; lf
; lf
= lf
->next
) {
140 for (i
= 0; i
< lf
->num_fields
; i
++) {
141 fps
->params
[j
].lf
= &lf
->fields
[i
];
142 fps
->params
[j
].lf
->file
->interval
.tv_sec
=
144 if (!fps
->params
[j
].print
.width
)
145 fps
->params
[j
].print
.width
=
148 if (++j
>= MAX_FIELDS
- 1) {
150 "WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
161 for (i
= 0; i
< fps
->num
; i
++) {
162 fps
->params
[i
].lf
= lnstat_find_field(lnstat_files
,
163 fps
->params
[i
].name
);
164 if (!fps
->params
[i
].lf
) {
165 fprintf(stderr
, "Field `%s' unknown\n",
166 fps
->params
[i
].name
);
169 fps
->params
[i
].lf
->file
->interval
.tv_sec
= interval
;
170 if (!fps
->params
[i
].print
.width
)
171 fps
->params
[i
].print
.width
= FIELD_WIDTH_DEFAULT
;
178 char *hdr
[HDR_LINES
];
181 static struct table_hdr
*build_hdr_string(struct lnstat_file
*lnstat_files
,
182 struct field_params
*fps
,
186 static struct table_hdr th
;
189 for (i
= 0; i
< HDR_LINES
; i
++)
190 th
.hdr
[i
] = calloc(1, HDR_LINE_LENGTH
);
192 for (i
= 0; i
< fps
->num
; i
++) {
193 char *cname
, *fname
= fps
->params
[i
].lf
->name
;
194 unsigned int width
= fps
->params
[i
].print
.width
;
196 snprintf(th
.hdr
[0]+ofs
, width
+2, "%*.*s|", width
, width
,
197 fps
->params
[i
].lf
->file
->basename
);
200 for (h
= 1; h
< HDR_LINES
; h
++) {
201 if (cname
- fname
>= strlen(fname
))
202 snprintf(th
.hdr
[h
]+ofs
, width
+2,
203 "%*.*s|", width
, width
, "");
206 snprintf(th
.hdr
[h
]+ofs
, width
+2,
207 "%*.*s|", width
, width
, cname
);
214 for (h
= 1; h
<= th
.num_lines
; h
++) {
215 for (i
= 0; i
< ofs
; i
++) {
216 if (th
.hdr
[h
][i
] == '\0')
224 static int print_hdr(FILE *of
, struct table_hdr
*th
)
228 for (i
= 0; i
< th
->num_lines
; i
++) {
229 fputs(th
->hdr
[i
], of
);
236 int main(int argc
, char **argv
)
238 struct lnstat_file
*lnstat_files
;
239 const char *basename
;
241 int interval
= DEFAULT_INTERVAL
;
247 } mode
= MODE_NORMAL
;
248 unsigned long count
= 0;
249 struct table_hdr
*header
;
250 static struct field_params fp
;
251 int num_req_files
= 0;
252 char *req_files
[LNSTAT_MAX_FILES
];
254 /* backwards compatibility mode for old tools */
255 basename
= strrchr(argv
[0], '/');
257 basename
+= 1; /* name after slash */
259 basename
= argv
[0]; /* no slash */
261 if (!strcmp(basename
, "rtstat")) {
262 /* rtstat compatibility mode */
263 req_files
[0] = "rt_cache";
265 } else if (!strcmp(basename
, "ctstat")) {
266 /* ctstat compatibility mode */
267 req_files
[0] = "ip_conntrack";
271 while ((c
= getopt_long(argc
, argv
, "Vc:djpf:h?i:k:s:w:",
272 opts
, NULL
)) != -1) {
278 count
= strtoul(optarg
, NULL
, 0);
287 req_files
[num_req_files
++] = strdup(optarg
);
294 sscanf(optarg
, "%u", &interval
);
297 tmp
= strdup(optarg
);
300 for (tok
= strtok(tmp
, ",");
302 tok
= strtok(NULL
, ",")) {
303 if (fp
.num
>= MAX_FIELDS
) {
305 "WARN: too many keys requested: (%d max)\n",
309 fp
.params
[fp
.num
++].name
= tok
;
313 sscanf(optarg
, "%u", &hdr
);
316 tmp
= strdup(optarg
);
320 for (tok
= strtok(tmp
, ",");
322 tok
= strtok(NULL
, ",")) {
323 len
= strtoul(tok
, NULL
, 0);
324 if (len
> FIELD_WIDTH_MAX
)
325 len
= FIELD_WIDTH_MAX
;
326 fp
.params
[i
].print
.width
= len
;
330 for (i
= 0; i
< MAX_FIELDS
; i
++)
331 fp
.params
[i
].print
.width
= len
;
340 lnstat_files
= lnstat_scan_dir(PROC_NET_STAT
, num_req_files
,
341 (const char **) req_files
);
345 lnstat_dump(stdout
, lnstat_files
);
350 if (!map_field_params(lnstat_files
, &fp
, interval
))
353 header
= build_hdr_string(lnstat_files
, &fp
, 80);
360 for (i
= 0; i
< count
|| !count
; i
++) {
361 lnstat_update(lnstat_files
);
362 if (mode
== MODE_JSON
)
363 print_json(stdout
, lnstat_files
, &fp
);
365 if ((hdr
> 1 && !(i
% 20)) ||
366 (hdr
== 1 && i
== 0))
367 print_hdr(stdout
, header
);
368 print_line(stdout
, lnstat_files
, &fp
);
371 if (i
< count
- 1 || !count
)