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)
41 static struct option opts[] = {
42 { "version", 0, NULL, 'V' },
43 { "count", 1, NULL, 'c' },
44 { "dump", 1, NULL, 'd' },
45 { "file", 1, NULL, 'f' },
46 { "help", 0, NULL, 'h' },
47 { "interval", 1, NULL, 'i' },
48 { "keys", 1, NULL, 'k' },
49 { "subject", 1, NULL, 's' },
50 { "width", 1, NULL, 'w' },
53 static int usage(char *name, int exit_code)
55 fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
56 fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
57 "<laforge@gnumonks.org>\n");
58 fprintf(stderr, "This program is free software licensed under GNU GPLv2"
59 "\nwith ABSOLUTELY NO WARRANTY.\n\n");
60 fprintf(stderr, "Parameters:\n");
61 fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
62 fprintf(stderr, "\t-c --count <count>\t"
63 "Print <count> number of intervals\n");
64 fprintf(stderr, "\t-d --dump\t\t"
65 "Dump list of available files/keys\n");
66 fprintf(stderr, "\t-f --file <file>\tStatistics file to use\n");
67 fprintf(stderr, "\t-h --help\t\tThis help message\n");
68 fprintf(stderr, "\t-i --interval <intv>\t"
69 "Set interval to 'intv' seconds\n");
70 fprintf(stderr, "\t-k --keys k,k,k,...\tDisplay only keys specified\n");
71 fprintf(stderr, "\t-s --subject [0-2]\t?\n");
72 fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
73 fprintf(stderr, "\n");
80 struct lnstat_field *lf;
88 struct field_param params[MAX_FIELDS];
91 static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
92 const struct field_params *fp)
96 for (i = 0; i < fp->num; i++) {
97 struct lnstat_field *lf = fp->params[i].lf;
100 snprintf(formatbuf, sizeof(formatbuf)-1, "%%%ulu|",
101 fp->params[i].print.width);
102 fprintf(of, formatbuf, lf->result);
107 /* find lnstat_field according to user specification */
108 static int map_field_params(struct lnstat_file *lnstat_files,
109 struct field_params *fps, int interval)
112 struct lnstat_file *lf;
114 /* no field specification on commandline, need to build default */
116 for (lf = lnstat_files; lf; lf = lf->next) {
117 for (i = 0; i < lf->num_fields; i++) {
118 fps->params[j].lf = &lf->fields[i];
119 fps->params[j].lf->file->interval.tv_sec =
121 if (!fps->params[j].print.width)
122 fps->params[j].print.width =
125 if (++j >= MAX_FIELDS - 1) {
127 "WARN: MAX_FIELDS (%d) reached,"
128 " truncating number of keys\n",
139 for (i = 0; i < fps->num; i++) {
140 fps->params[i].lf = lnstat_find_field(lnstat_files,
141 fps->params[i].name);
142 if (!fps->params[i].lf) {
143 fprintf(stderr, "Field `%s' unknown\n",
144 fps->params[i].name);
147 fps->params[i].lf->file->interval.tv_sec = interval;
148 if (!fps->params[i].print.width)
149 fps->params[i].print.width = FIELD_WIDTH_DEFAULT;
156 char *hdr[HDR_LINES];
159 static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
160 struct field_params *fps,
164 static struct table_hdr th;
167 for (i = 0; i < HDR_LINES; i++) {
168 th.hdr[i] = malloc(HDR_LINE_LENGTH);
169 memset(th.hdr[i], 0, sizeof(th.hdr[i]));
172 for (i = 0; i < fps->num; i++) {
173 char *cname, *fname = fps->params[i].lf->name;
175 unsigned int width = fps->params[i].print.width;
177 snprintf(fmt, sizeof(fmt)-1, "%%%u.%us|", width, width);
179 snprintf(th.hdr[0]+ofs, width+2, fmt,
180 fps->params[i].lf->file->basename);
183 for (h = 1; h < HDR_LINES; h++) {
184 if (cname - fname >= strlen(fname))
185 snprintf(th.hdr[h]+ofs, width+2, fmt, "");
188 snprintf(th.hdr[h]+ofs, width+2, fmt, cname);
195 for (h = 1; h <= th.num_lines; h++) {
196 for (i = 0; i < ofs; i++) {
197 if (th.hdr[h][i] == '\0')
205 static int print_hdr(FILE *of, struct table_hdr *th)
209 for (i = 0; i < th->num_lines; i++) {
210 fputs(th->hdr[i], of);
217 int main(int argc, char **argv)
219 struct lnstat_file *lnstat_files;
220 const char *basename;
222 int interval = DEFAULT_INTERVAL;
227 } mode = MODE_NORMAL;
229 unsigned long count = 1;
230 static struct field_params fp;
231 int num_req_files = 0;
232 char *req_files[LNSTAT_MAX_FILES];
234 /* backwards compatibility mode for old tools */
235 basename = strrchr(argv[0], '/');
237 basename += 1; /* name after slash */
239 basename = argv[0]; /* no slash */
241 if (!strcmp(basename, "rtstat")) {
242 /* rtstat compatibility mode */
243 req_files[0] = "rt_cache";
245 } else if (!strcmp(basename, "ctstat")) {
246 /* ctstat compatibility mode */
247 req_files[0] = "ip_conntrack";
251 while ((c = getopt_long(argc, argv,"Vc:df:h?i:k:s:w:",
252 opts, NULL)) != -1) {
258 count = strtoul(optarg, NULL, 0);
264 req_files[num_req_files++] = strdup(optarg);
271 sscanf(optarg, "%u", &interval);
274 tmp = strdup(optarg);
277 for (tok = strtok(tmp, ",");
279 tok = strtok(NULL, ",")) {
280 if (fp.num >= MAX_FIELDS) {
282 "WARN: too many keys"
283 " requested: (%d max)\n",
287 fp.params[fp.num++].name = tok;
291 sscanf(optarg, "%u", &hdr);
294 tmp = strdup(optarg);
298 for (tok = strtok(tmp, ",");
300 tok = strtok(NULL, ",")) {
301 len = strtoul(tok, NULL, 0);
302 if (len > FIELD_WIDTH_MAX)
303 len = FIELD_WIDTH_MAX;
304 fp.params[i].print.width = len;
308 for (i = 0; i < MAX_FIELDS; i++)
309 fp.params[i].print.width = len;
318 lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
319 (const char **) req_files);
323 struct table_hdr *header;
325 lnstat_dump(stderr, lnstat_files);
329 if (!map_field_params(lnstat_files, &fp, interval))
332 header = build_hdr_string(lnstat_files, &fp, 80);
339 for (i = 0; i < count; i++) {
340 if ((hdr > 1 && (! (i % 20))) || (hdr == 1 && i == 0))
341 print_hdr(stdout, header);
342 lnstat_update(lnstat_files);
343 print_line(stdout, lnstat_files, &fp);