]>
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>
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 },
56 static int usage(char *name
, int exit_code
)
60 "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n"
61 "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n"
64 " -V --version Print Version of Program\n"
65 " -c --count <count> "
66 "Print <count> number of intervals\n"
68 "Dump list of available files/keys\n"
70 "Display in JSON format\n"
71 " -f --file <file> Statistics file to use\n"
72 " -h --help This help message\n"
73 " -i --interval <intv> "
74 "Set interval to 'intv' seconds\n"
75 " -k --keys k,k,k,... Display only keys specified\n"
76 " -s --subject [0-2] Control header printing:\n"
79 " 2 = every 20 lines (default))\n"
80 " -w --width n,n,n,... Width for each field\n"
82 name
, LNSTAT_VERSION
);
89 struct lnstat_field
*lf
;
97 struct field_param params
[MAX_FIELDS
];
100 static void print_line(FILE *of
, const struct lnstat_file
*lnstat_files
,
101 const struct field_params
*fp
)
105 for (i
= 0; i
< fp
->num
; i
++) {
106 const struct lnstat_field
*lf
= fp
->params
[i
].lf
;
108 fprintf(of
, "%*lu|", fp
->params
[i
].print
.width
, lf
->result
);
113 static void print_json(FILE *of
, const struct lnstat_file
*lnstat_files
,
114 const struct field_params
*fp
)
116 json_writer_t
*jw
= jsonw_new(of
);
119 jsonw_start_object(jw
);
120 for (i
= 0; i
< fp
->num
; i
++) {
121 const struct lnstat_field
*lf
= fp
->params
[i
].lf
;
123 jsonw_uint_field(jw
, lf
->name
, lf
->result
);
125 jsonw_end_object(jw
);
129 /* find lnstat_field according to user specification */
130 static int map_field_params(struct lnstat_file
*lnstat_files
,
131 struct field_params
*fps
, int interval
)
134 struct lnstat_file
*lf
;
136 /* no field specification on commandline, need to build default */
138 for (lf
= lnstat_files
; lf
; lf
= lf
->next
) {
139 for (i
= 0; i
< lf
->num_fields
; i
++) {
140 fps
->params
[j
].lf
= &lf
->fields
[i
];
141 fps
->params
[j
].lf
->file
->interval
.tv_sec
=
143 if (!fps
->params
[j
].print
.width
)
144 fps
->params
[j
].print
.width
=
147 if (++j
>= MAX_FIELDS
- 1) {
149 "WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
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
);
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
;
177 char *hdr
[HDR_LINES
];
180 static struct table_hdr
*build_hdr_string(struct lnstat_file
*lnstat_files
,
181 struct field_params
*fps
,
185 static struct table_hdr th
;
188 for (i
= 0; i
< HDR_LINES
; i
++)
189 th
.hdr
[i
] = calloc(1, HDR_LINE_LENGTH
);
191 for (i
= 0; i
< fps
->num
; i
++) {
192 char *cname
, *fname
= fps
->params
[i
].lf
->name
;
193 unsigned int width
= fps
->params
[i
].print
.width
;
195 snprintf(th
.hdr
[0]+ofs
, width
+2, "%*.*s|", width
, width
,
196 fps
->params
[i
].lf
->file
->basename
);
199 for (h
= 1; h
< HDR_LINES
; h
++) {
200 if (cname
- fname
>= strlen(fname
))
201 snprintf(th
.hdr
[h
]+ofs
, width
+2,
202 "%*.*s|", width
, width
, "");
205 snprintf(th
.hdr
[h
]+ofs
, width
+2,
206 "%*.*s|", width
, width
, cname
);
213 for (h
= 1; h
<= th
.num_lines
; h
++) {
214 for (i
= 0; i
< ofs
; i
++) {
215 if (th
.hdr
[h
][i
] == '\0')
223 static int print_hdr(FILE *of
, struct table_hdr
*th
)
227 for (i
= 0; i
< th
->num_lines
; i
++) {
228 fputs(th
->hdr
[i
], of
);
235 int main(int argc
, char **argv
)
237 struct lnstat_file
*lnstat_files
;
238 const char *basename
;
240 int interval
= DEFAULT_INTERVAL
;
246 } mode
= MODE_NORMAL
;
247 unsigned long count
= 0;
248 struct table_hdr
*header
;
249 static struct field_params fp
;
250 int num_req_files
= 0;
251 char *req_files
[LNSTAT_MAX_FILES
];
253 /* backwards compatibility mode for old tools */
254 basename
= strrchr(argv
[0], '/');
256 basename
+= 1; /* name after slash */
258 basename
= argv
[0]; /* no slash */
260 if (!strcmp(basename
, "rtstat")) {
261 /* rtstat compatibility mode */
262 req_files
[0] = "rt_cache";
264 } else if (!strcmp(basename
, "ctstat")) {
265 /* ctstat compatibility mode */
266 req_files
[0] = "ip_conntrack";
270 while ((c
= getopt_long(argc
, argv
, "Vc:djpf:h?i:k:s:w:",
271 opts
, NULL
)) != -1) {
277 count
= strtoul(optarg
, NULL
, 0);
286 req_files
[num_req_files
++] = strdup(optarg
);
293 sscanf(optarg
, "%u", &interval
);
296 tmp
= strdup(optarg
);
299 for (tok
= strtok(tmp
, ",");
301 tok
= strtok(NULL
, ",")) {
302 if (fp
.num
>= MAX_FIELDS
) {
304 "WARN: too many keys requested: (%d max)\n",
308 fp
.params
[fp
.num
++].name
= tok
;
312 sscanf(optarg
, "%u", &hdr
);
315 tmp
= strdup(optarg
);
319 for (tok
= strtok(tmp
, ",");
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
;
329 for (i
= 0; i
< MAX_FIELDS
; i
++)
330 fp
.params
[i
].print
.width
= len
;
339 lnstat_files
= lnstat_scan_dir(PROC_NET_STAT
, num_req_files
,
340 (const char **) req_files
);
344 lnstat_dump(stdout
, lnstat_files
);
349 if (!map_field_params(lnstat_files
, &fp
, interval
))
352 header
= build_hdr_string(lnstat_files
, &fp
, 80);
359 for (i
= 0; i
< count
|| !count
; i
++) {
360 lnstat_update(lnstat_files
);
361 if (mode
== MODE_JSON
)
362 print_json(stdout
, lnstat_files
, &fp
);
364 if ((hdr
> 1 && !(i
% 20)) ||
365 (hdr
== 1 && i
== 0))
366 print_hdr(stdout
, header
);
367 print_line(stdout
, lnstat_files
, &fp
);
370 if (i
< count
- 1 || !count
)