]>
Commit | Line | Data |
---|---|---|
0bb187ca SH |
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 */ | |
f493dc30 | 20 | #define MAX_FIELDS 128 |
0bb187ca SH |
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 "lnstat.h" | |
40 | ||
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 | { "key", 1, NULL, 'k' }, | |
49 | { "subject", 1, NULL, 's' }, | |
50 | { "width", 1, NULL, 'w' }, | |
51 | }; | |
52 | ||
53 | static int usage(char *name, int exit_code) | |
54 | { | |
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 --dumpt\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"); | |
74 | ||
75 | exit(exit_code); | |
76 | } | |
77 | ||
78 | struct field_param { | |
79 | const char *name; | |
80 | struct lnstat_field *lf; | |
81 | struct { | |
82 | unsigned int width; | |
83 | } print; | |
84 | }; | |
85 | ||
86 | struct field_params { | |
87 | unsigned int num; | |
88 | struct field_param params[MAX_FIELDS]; | |
89 | }; | |
90 | ||
91 | static void print_line(FILE *of, const struct lnstat_file *lnstat_files, | |
92 | const struct field_params *fp) | |
93 | { | |
94 | int i; | |
95 | ||
96 | for (i = 0; i < fp->num; i++) { | |
97 | struct lnstat_field *lf = fp->params[i].lf; | |
98 | char formatbuf[255]; | |
99 | ||
100 | snprintf(formatbuf, sizeof(formatbuf)-1, "%%%ulu|", | |
101 | fp->params[i].print.width); | |
102 | fprintf(of, formatbuf, lf->result); | |
103 | } | |
104 | fputc('\n', of); | |
105 | } | |
106 | ||
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) | |
110 | { | |
111 | int i, j = 0; | |
112 | struct lnstat_file *lf; | |
113 | ||
114 | /* no field specification on commandline, need to build default */ | |
115 | if (!fps->num) { | |
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 = | |
120 | interval; | |
121 | if (!fps->params[j].print.width) | |
ae665a52 | 122 | fps->params[j].print.width = |
0bb187ca | 123 | FIELD_WIDTH_DEFAULT; |
f493dc30 SH |
124 | |
125 | if (++j >= MAX_FIELDS - 1) | |
126 | goto full; | |
0bb187ca SH |
127 | } |
128 | } | |
f493dc30 | 129 | full: |
0bb187ca SH |
130 | fps->num = j; |
131 | return 1; | |
132 | } | |
133 | ||
134 | for (i = 0; i < fps->num; i++) { | |
135 | fps->params[i].lf = lnstat_find_field(lnstat_files, | |
136 | fps->params[i].name); | |
137 | if (!fps->params[i].lf) { | |
138 | fprintf(stderr, "Field `%s' unknown\n", | |
139 | fps->params[i].name); | |
140 | return 0; | |
141 | } | |
142 | fps->params[i].lf->file->interval.tv_sec = interval; | |
143 | if (!fps->params[i].print.width) | |
144 | fps->params[i].print.width = FIELD_WIDTH_DEFAULT; | |
145 | } | |
146 | return 1; | |
147 | } | |
148 | ||
149 | struct table_hdr { | |
150 | int num_lines; | |
151 | char *hdr[HDR_LINES]; | |
152 | }; | |
153 | ||
154 | static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files, | |
155 | struct field_params *fps, | |
156 | int linewidth) | |
157 | { | |
158 | int h,i; | |
159 | static struct table_hdr th; | |
160 | int ofs = 0; | |
161 | ||
162 | for (i = 0; i < HDR_LINES; i++) { | |
163 | th.hdr[i] = malloc(HDR_LINE_LENGTH); | |
164 | memset(th.hdr[i], 0, sizeof(th.hdr[i])); | |
165 | } | |
166 | ||
167 | for (i = 0; i < fps->num; i++) { | |
168 | char *cname, *fname = fps->params[i].lf->name; | |
169 | char fmt[12]; | |
170 | unsigned int width = fps->params[i].print.width; | |
171 | ||
172 | snprintf(fmt, sizeof(fmt)-1, "%%%u.%us|", width, width); | |
173 | ||
174 | snprintf(th.hdr[0]+ofs, width+2, fmt, | |
175 | fps->params[i].lf->file->basename); | |
176 | ||
177 | cname = fname; | |
178 | for (h = 1; h < HDR_LINES; h++) { | |
179 | if (cname - fname >= strlen(fname)) | |
180 | snprintf(th.hdr[h]+ofs, width+2, fmt, ""); | |
181 | else { | |
182 | th.num_lines = h+1; | |
183 | snprintf(th.hdr[h]+ofs, width+2, fmt, cname); | |
184 | } | |
185 | cname += width; | |
186 | } | |
187 | ofs += width+1; | |
188 | } | |
189 | /* fill in spaces */ | |
190 | for (h = 1; h <= th.num_lines; h++) { | |
191 | for (i = 0; i < ofs; i++) { | |
192 | if (th.hdr[h][i] == '\0') | |
193 | th.hdr[h][i] = ' '; | |
194 | } | |
195 | } | |
196 | ||
197 | return &th; | |
198 | } | |
199 | ||
200 | static int print_hdr(FILE *of, struct table_hdr *th) | |
201 | { | |
202 | int i; | |
203 | ||
204 | for (i = 0; i < th->num_lines; i++) { | |
205 | fputs(th->hdr[i], of); | |
206 | fputc('\n', of); | |
207 | } | |
208 | return 0; | |
209 | } | |
ae665a52 | 210 | |
0bb187ca SH |
211 | |
212 | int main(int argc, char **argv) | |
213 | { | |
214 | struct lnstat_file *lnstat_files; | |
215 | const char *basename; | |
216 | int c; | |
217 | int interval = DEFAULT_INTERVAL; | |
218 | int hdr = 2; | |
219 | enum { | |
220 | MODE_DUMP, | |
221 | MODE_NORMAL, | |
222 | } mode = MODE_NORMAL; | |
223 | ||
49843e29 | 224 | unsigned long count = 1; |
0bb187ca SH |
225 | static struct field_params fp; |
226 | int num_req_files = 0; | |
227 | char *req_files[LNSTAT_MAX_FILES]; | |
ae665a52 | 228 | |
0bb187ca SH |
229 | /* backwards compatibility mode for old tools */ |
230 | basename = strrchr(argv[0], '/'); | |
ae665a52 | 231 | if (basename) |
0bb187ca SH |
232 | basename += 1; /* name after slash */ |
233 | else | |
234 | basename = argv[0]; /* no slash */ | |
235 | ||
236 | if (!strcmp(basename, "rtstat")) { | |
237 | /* rtstat compatibility mode */ | |
238 | req_files[0] = "rt_cache"; | |
239 | num_req_files = 1; | |
240 | } else if (!strcmp(basename, "ctstat")) { | |
241 | /* ctstat compatibility mode */ | |
242 | req_files[0] = "ip_conntrack"; | |
243 | num_req_files = 1; | |
244 | } | |
245 | ||
ae665a52 | 246 | while ((c = getopt_long(argc, argv,"Vc:df:h?i:k:s:w:", |
0bb187ca | 247 | opts, NULL)) != -1) { |
e796b958 SH |
248 | int i, len = 0; |
249 | char *tmp, *tok; | |
250 | ||
0bb187ca | 251 | switch (c) { |
0bb187ca SH |
252 | case 'c': |
253 | count = strtoul(optarg, NULL, 0); | |
254 | break; | |
255 | case 'd': | |
256 | mode = MODE_DUMP; | |
257 | break; | |
258 | case 'f': | |
259 | req_files[num_req_files++] = strdup(optarg); | |
260 | break; | |
261 | case '?': | |
ae665a52 | 262 | case 'h': |
0bb187ca SH |
263 | usage(argv[0], 0); |
264 | break; | |
265 | case 'i': | |
266 | sscanf(optarg, "%u", &interval); | |
267 | break; | |
268 | case 'k': | |
269 | tmp = strdup(optarg); | |
270 | if (!tmp) | |
271 | break; | |
272 | for (tok = strtok(tmp, ","); | |
273 | tok; | |
274 | tok = strtok(NULL, ",")) { | |
275 | if (fp.num >= MAX_FIELDS) | |
276 | break; | |
277 | fp.params[fp.num++].name = tok; | |
278 | } | |
279 | break; | |
280 | case 's': | |
281 | sscanf(optarg, "%u", &hdr); | |
282 | break; | |
283 | case 'w': | |
284 | tmp = strdup(optarg); | |
285 | if (!tmp) | |
286 | break; | |
287 | i = 0; | |
288 | for (tok = strtok(tmp, ","); | |
289 | tok; | |
290 | tok = strtok(NULL, ",")) { | |
291 | len = strtoul(tok, NULL, 0); | |
292 | if (len > FIELD_WIDTH_MAX) | |
293 | len = FIELD_WIDTH_MAX; | |
294 | fp.params[i].print.width = len; | |
295 | i++; | |
296 | } | |
297 | if (i == 1) { | |
298 | for (i = 0; i < MAX_FIELDS; i++) | |
299 | fp.params[i].print.width = len; | |
300 | } | |
301 | break; | |
302 | default: | |
303 | usage(argv[0], 1); | |
304 | break; | |
305 | } | |
306 | } | |
307 | ||
308 | lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files, | |
309 | (const char **) req_files); | |
310 | ||
311 | switch (mode) { | |
312 | int i; | |
313 | struct table_hdr *header; | |
314 | case MODE_DUMP: | |
315 | lnstat_dump(stderr, lnstat_files); | |
316 | break; | |
317 | case MODE_NORMAL: | |
318 | ||
319 | if (!map_field_params(lnstat_files, &fp, interval)) | |
320 | exit(1); | |
ae665a52 | 321 | |
0bb187ca SH |
322 | header = build_hdr_string(lnstat_files, &fp, 80); |
323 | if (!header) | |
324 | exit(1); | |
325 | ||
ae665a52 | 326 | if (interval < 1 ) |
0bb187ca SH |
327 | interval=1; |
328 | ||
329 | for (i = 0; i < count; i++) { | |
330 | if ((hdr > 1 && (! (i % 20))) || (hdr == 1 && i == 0)) | |
331 | print_hdr(stdout, header); | |
332 | lnstat_update(lnstat_files); | |
333 | print_line(stdout, lnstat_files, &fp); | |
f50332c5 | 334 | fflush(stdout); |
0bb187ca SH |
335 | sleep(interval); |
336 | } | |
337 | } | |
338 | ||
339 | return 1; | |
340 | } | |
341 |