]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * nstat.c handy utility to read counters /proc/net/netstat and snmp | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
15 | #include <fcntl.h> | |
16 | #include <string.h> | |
17 | #include <errno.h> | |
18 | #include <time.h> | |
19 | #include <sys/time.h> | |
20 | #include <fnmatch.h> | |
21 | #include <sys/file.h> | |
22 | #include <sys/socket.h> | |
23 | #include <sys/un.h> | |
24 | #include <sys/poll.h> | |
25 | #include <sys/wait.h> | |
26 | #include <sys/stat.h> | |
27 | #include <signal.h> | |
28 | #include <math.h> | |
404582c8 | 29 | #include <getopt.h> |
aba5acdf | 30 | |
fcc16c22 | 31 | #include <json_writer.h> |
aba5acdf | 32 | #include <SNAPSHOT.h> |
62000e51 | 33 | #include "utils.h" |
aba5acdf | 34 | |
acd1e437 SH |
35 | int dump_zeros; |
36 | int reset_history; | |
37 | int ignore_history; | |
38 | int no_output; | |
39 | int json_output; | |
40 | int pretty; | |
41 | int no_update; | |
42 | int scan_interval; | |
43 | int time_constant; | |
aba5acdf SH |
44 | double W; |
45 | char **patterns; | |
46 | int npatterns; | |
47 | ||
48 | char info_source[128]; | |
49 | int source_mismatch; | |
50 | ||
4ffc44ca | 51 | static int generic_proc_open(const char *env, char *name) |
aba5acdf SH |
52 | { |
53 | char store[128]; | |
54 | char *p = getenv(env); | |
acd1e437 | 55 | |
aba5acdf SH |
56 | if (!p) { |
57 | p = getenv("PROC_ROOT") ? : "/proc"; | |
58 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
59 | p = store; | |
60 | } | |
4ffc44ca | 61 | return open(p, O_RDONLY); |
aba5acdf SH |
62 | } |
63 | ||
d1f28cf1 | 64 | static int net_netstat_open(void) |
aba5acdf SH |
65 | { |
66 | return generic_proc_open("PROC_NET_NETSTAT", "net/netstat"); | |
67 | } | |
68 | ||
d1f28cf1 | 69 | static int net_snmp_open(void) |
aba5acdf SH |
70 | { |
71 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); | |
72 | } | |
73 | ||
d1f28cf1 | 74 | static int net_snmp6_open(void) |
aba5acdf SH |
75 | { |
76 | return generic_proc_open("PROC_NET_SNMP6", "net/snmp6"); | |
77 | } | |
78 | ||
45a0dc16 HL |
79 | static int net_sctp_snmp_open(void) |
80 | { | |
81 | return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp"); | |
82 | } | |
83 | ||
acd1e437 | 84 | struct nstat_ent { |
aba5acdf SH |
85 | struct nstat_ent *next; |
86 | char *id; | |
87 | unsigned long long val; | |
aba5acdf SH |
88 | double rate; |
89 | }; | |
90 | ||
91 | struct nstat_ent *kern_db; | |
92 | struct nstat_ent *hist_db; | |
93 | ||
d1f28cf1 SH |
94 | static const char *useless_numbers[] = { |
95 | "IpForwarding", "IpDefaultTTL", | |
96 | "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax", | |
97 | "TcpMaxConn", "TcpCurrEstab" | |
aba5acdf SH |
98 | }; |
99 | ||
d1f28cf1 | 100 | static int useless_number(const char *id) |
aba5acdf SH |
101 | { |
102 | int i; | |
acd1e437 | 103 | |
62000e51 | 104 | for (i = 0; i < ARRAY_SIZE(useless_numbers); i++) |
aba5acdf SH |
105 | if (strcmp(id, useless_numbers[i]) == 0) |
106 | return 1; | |
107 | return 0; | |
108 | } | |
109 | ||
d1f28cf1 | 110 | static int match(const char *id) |
aba5acdf SH |
111 | { |
112 | int i; | |
113 | ||
114 | if (npatterns == 0) | |
115 | return 1; | |
116 | ||
acd1e437 | 117 | for (i = 0; i < npatterns; i++) { |
aba5acdf SH |
118 | if (!fnmatch(patterns[i], id, 0)) |
119 | return 1; | |
120 | } | |
121 | return 0; | |
122 | } | |
123 | ||
d1f28cf1 | 124 | static void load_good_table(FILE *fp) |
aba5acdf SH |
125 | { |
126 | char buf[4096]; | |
127 | struct nstat_ent *db = NULL; | |
128 | struct nstat_ent *n; | |
129 | ||
130 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
131 | int nr; | |
132 | unsigned long long val; | |
133 | double rate; | |
b482ffa6 | 134 | char idbuf[sizeof(buf)]; |
acd1e437 | 135 | |
aba5acdf SH |
136 | if (buf[0] == '#') { |
137 | buf[strlen(buf)-1] = 0; | |
138 | if (info_source[0] && strcmp(info_source, buf+1)) | |
139 | source_mismatch = 1; | |
b482ffa6 | 140 | info_source[0] = 0; |
141 | strncat(info_source, buf+1, sizeof(info_source)-1); | |
aba5acdf SH |
142 | continue; |
143 | } | |
b482ffa6 | 144 | /* idbuf is as big as buf, so this is safe */ |
aba5acdf SH |
145 | nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate); |
146 | if (nr < 2) | |
147 | abort(); | |
148 | if (nr < 3) | |
149 | rate = 0; | |
150 | if (useless_number(idbuf)) | |
151 | continue; | |
152 | if ((n = malloc(sizeof(*n))) == NULL) | |
153 | abort(); | |
154 | n->id = strdup(idbuf); | |
aba5acdf SH |
155 | n->val = val; |
156 | n->rate = rate; | |
157 | n->next = db; | |
158 | db = n; | |
159 | } | |
160 | ||
161 | while (db) { | |
162 | n = db; | |
163 | db = db->next; | |
164 | n->next = kern_db; | |
165 | kern_db = n; | |
166 | } | |
167 | } | |
168 | ||
d4717914 ED |
169 | static int count_spaces(const char *line) |
170 | { | |
171 | int count = 0; | |
172 | char c; | |
173 | ||
174 | while ((c = *line++) != 0) | |
175 | count += c == ' ' || c == '\n'; | |
176 | return count; | |
177 | } | |
aba5acdf | 178 | |
d1f28cf1 | 179 | static void load_ugly_table(FILE *fp) |
aba5acdf SH |
180 | { |
181 | char buf[4096]; | |
182 | struct nstat_ent *db = NULL; | |
183 | struct nstat_ent *n; | |
184 | ||
185 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
b482ffa6 | 186 | char idbuf[sizeof(buf)]; |
aba5acdf SH |
187 | int off; |
188 | char *p; | |
d4717914 | 189 | int count1, count2, skip = 0; |
aba5acdf SH |
190 | |
191 | p = strchr(buf, ':'); | |
192 | if (!p) | |
193 | abort(); | |
d4717914 | 194 | count1 = count_spaces(buf); |
aba5acdf | 195 | *p = 0; |
b482ffa6 | 196 | idbuf[0] = 0; |
197 | strncat(idbuf, buf, sizeof(idbuf) - 1); | |
198 | off = p - buf; | |
aba5acdf SH |
199 | p += 2; |
200 | ||
201 | while (*p) { | |
202 | char *next; | |
acd1e437 | 203 | |
aba5acdf SH |
204 | if ((next = strchr(p, ' ')) != NULL) |
205 | *next++ = 0; | |
206 | else if ((next = strchr(p, '\n')) != NULL) | |
207 | *next++ = 0; | |
b482ffa6 | 208 | if (off < sizeof(idbuf)) { |
209 | idbuf[off] = 0; | |
210 | strncat(idbuf, p, sizeof(idbuf) - off - 1); | |
211 | } | |
aba5acdf SH |
212 | n = malloc(sizeof(*n)); |
213 | if (!n) | |
214 | abort(); | |
215 | n->id = strdup(idbuf); | |
216 | n->rate = 0; | |
217 | n->next = db; | |
218 | db = n; | |
219 | p = next; | |
220 | } | |
221 | n = db; | |
222 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
223 | abort(); | |
d4717914 ED |
224 | count2 = count_spaces(buf); |
225 | if (count2 > count1) | |
226 | skip = count2 - count1; | |
aba5acdf SH |
227 | do { |
228 | p = strrchr(buf, ' '); | |
229 | if (!p) | |
230 | abort(); | |
231 | *p = 0; | |
cdb2227e | 232 | if (sscanf(p+1, "%llu", &n->val) != 1) |
aba5acdf | 233 | abort(); |
aba5acdf | 234 | /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */ |
d4717914 ED |
235 | if (skip) |
236 | skip--; | |
aba5acdf SH |
237 | else |
238 | n = n->next; | |
239 | } while (p > buf + off + 2); | |
240 | } | |
241 | ||
242 | while (db) { | |
243 | n = db; | |
244 | db = db->next; | |
245 | if (useless_number(n->id)) { | |
246 | free(n->id); | |
247 | free(n); | |
248 | } else { | |
249 | n->next = kern_db; | |
250 | kern_db = n; | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
45a0dc16 HL |
255 | static void load_sctp_snmp(void) |
256 | { | |
257 | FILE *fp = fdopen(net_sctp_snmp_open(), "r"); | |
258 | ||
259 | if (fp) { | |
260 | load_good_table(fp); | |
261 | fclose(fp); | |
262 | } | |
263 | } | |
264 | ||
d1f28cf1 | 265 | static void load_snmp(void) |
aba5acdf SH |
266 | { |
267 | FILE *fp = fdopen(net_snmp_open(), "r"); | |
acd1e437 | 268 | |
aba5acdf SH |
269 | if (fp) { |
270 | load_ugly_table(fp); | |
271 | fclose(fp); | |
272 | } | |
273 | } | |
274 | ||
d1f28cf1 | 275 | static void load_snmp6(void) |
aba5acdf SH |
276 | { |
277 | FILE *fp = fdopen(net_snmp6_open(), "r"); | |
acd1e437 | 278 | |
aba5acdf SH |
279 | if (fp) { |
280 | load_good_table(fp); | |
281 | fclose(fp); | |
282 | } | |
283 | } | |
284 | ||
d1f28cf1 | 285 | static void load_netstat(void) |
aba5acdf SH |
286 | { |
287 | FILE *fp = fdopen(net_netstat_open(), "r"); | |
acd1e437 | 288 | |
aba5acdf SH |
289 | if (fp) { |
290 | load_ugly_table(fp); | |
291 | fclose(fp); | |
292 | } | |
293 | } | |
294 | ||
d48ed3f4 | 295 | |
d1f28cf1 | 296 | static void dump_kern_db(FILE *fp, int to_hist) |
aba5acdf | 297 | { |
fcc16c22 | 298 | json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; |
aba5acdf | 299 | struct nstat_ent *n, *h; |
d48ed3f4 | 300 | |
aba5acdf | 301 | h = hist_db; |
fcc16c22 | 302 | if (jw) { |
d721a145 | 303 | jsonw_start_object(jw); |
fcc16c22 SH |
304 | jsonw_pretty(jw, pretty); |
305 | jsonw_name(jw, info_source); | |
306 | jsonw_start_object(jw); | |
307 | } else | |
d48ed3f4 | 308 | fprintf(fp, "#%s\n", info_source); |
404582c8 | 309 | |
acd1e437 | 310 | for (n = kern_db; n; n = n->next) { |
aba5acdf | 311 | unsigned long long val = n->val; |
acd1e437 | 312 | |
aba5acdf SH |
313 | if (!dump_zeros && !val && !n->rate) |
314 | continue; | |
315 | if (!match(n->id)) { | |
316 | struct nstat_ent *h1; | |
acd1e437 | 317 | |
aba5acdf SH |
318 | if (!to_hist) |
319 | continue; | |
320 | for (h1 = h; h1; h1 = h1->next) { | |
321 | if (strcmp(h1->id, n->id) == 0) { | |
322 | val = h1->val; | |
323 | h = h1->next; | |
324 | break; | |
325 | } | |
326 | } | |
327 | } | |
d48ed3f4 | 328 | |
fcc16c22 SH |
329 | if (jw) |
330 | jsonw_uint_field(jw, n->id, val); | |
331 | else | |
d48ed3f4 | 332 | fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate); |
aba5acdf | 333 | } |
fcc16c22 SH |
334 | |
335 | if (jw) { | |
d721a145 AK |
336 | jsonw_end_object(jw); |
337 | ||
fcc16c22 SH |
338 | jsonw_end_object(jw); |
339 | jsonw_destroy(&jw); | |
340 | } | |
aba5acdf SH |
341 | } |
342 | ||
d1f28cf1 | 343 | static void dump_incr_db(FILE *fp) |
aba5acdf | 344 | { |
fcc16c22 | 345 | json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; |
aba5acdf | 346 | struct nstat_ent *n, *h; |
d48ed3f4 | 347 | |
aba5acdf | 348 | h = hist_db; |
fcc16c22 | 349 | if (jw) { |
d721a145 | 350 | jsonw_start_object(jw); |
fcc16c22 SH |
351 | jsonw_pretty(jw, pretty); |
352 | jsonw_name(jw, info_source); | |
353 | jsonw_start_object(jw); | |
354 | } else | |
d48ed3f4 SH |
355 | fprintf(fp, "#%s\n", info_source); |
356 | ||
acd1e437 | 357 | for (n = kern_db; n; n = n->next) { |
aba5acdf SH |
358 | int ovfl = 0; |
359 | unsigned long long val = n->val; | |
360 | struct nstat_ent *h1; | |
acd1e437 | 361 | |
aba5acdf SH |
362 | for (h1 = h; h1; h1 = h1->next) { |
363 | if (strcmp(h1->id, n->id) == 0) { | |
364 | if (val < h1->val) { | |
365 | ovfl = 1; | |
366 | val = h1->val; | |
367 | } | |
368 | val -= h1->val; | |
369 | h = h1->next; | |
370 | break; | |
371 | } | |
372 | } | |
373 | if (!dump_zeros && !val && !n->rate) | |
374 | continue; | |
375 | if (!match(n->id)) | |
376 | continue; | |
d48ed3f4 | 377 | |
fcc16c22 SH |
378 | if (jw) |
379 | jsonw_uint_field(jw, n->id, val); | |
380 | else | |
d48ed3f4 SH |
381 | fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val, |
382 | n->rate, ovfl?" (overflow)":""); | |
aba5acdf | 383 | } |
fcc16c22 SH |
384 | |
385 | if (jw) { | |
d721a145 AK |
386 | jsonw_end_object(jw); |
387 | ||
fcc16c22 SH |
388 | jsonw_end_object(jw); |
389 | jsonw_destroy(&jw); | |
390 | } | |
aba5acdf SH |
391 | } |
392 | ||
393 | static int children; | |
394 | ||
d1f28cf1 | 395 | static void sigchild(int signo) |
aba5acdf SH |
396 | { |
397 | } | |
398 | ||
d1f28cf1 | 399 | static void update_db(int interval) |
aba5acdf SH |
400 | { |
401 | struct nstat_ent *n, *h; | |
402 | ||
403 | n = kern_db; | |
404 | kern_db = NULL; | |
405 | ||
406 | load_netstat(); | |
407 | load_snmp6(); | |
408 | load_snmp(); | |
45a0dc16 | 409 | load_sctp_snmp(); |
aba5acdf SH |
410 | |
411 | h = kern_db; | |
412 | kern_db = n; | |
413 | ||
414 | for (n = kern_db; n; n = n->next) { | |
415 | struct nstat_ent *h1; | |
acd1e437 | 416 | |
aba5acdf SH |
417 | for (h1 = h; h1; h1 = h1->next) { |
418 | if (strcmp(h1->id, n->id) == 0) { | |
419 | double sample; | |
cdb2227e ED |
420 | unsigned long long incr = h1->val - n->val; |
421 | ||
422 | n->val = h1->val; | |
423 | sample = (double)incr * 1000.0 / interval; | |
aba5acdf SH |
424 | if (interval >= scan_interval) { |
425 | n->rate += W*(sample-n->rate); | |
426 | } else if (interval >= 1000) { | |
427 | if (interval >= time_constant) { | |
428 | n->rate = sample; | |
429 | } else { | |
430 | double w = W*(double)interval/scan_interval; | |
acd1e437 | 431 | |
aba5acdf SH |
432 | n->rate += w*(sample-n->rate); |
433 | } | |
434 | } | |
435 | ||
436 | while (h != h1) { | |
437 | struct nstat_ent *tmp = h; | |
acd1e437 | 438 | |
aba5acdf SH |
439 | h = h->next; |
440 | free(tmp->id); | |
441 | free(tmp); | |
442 | }; | |
443 | h = h1->next; | |
444 | free(h1->id); | |
445 | free(h1); | |
446 | break; | |
447 | } | |
448 | } | |
449 | } | |
450 | } | |
451 | ||
acd1e437 | 452 | #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000) |
aba5acdf SH |
453 | |
454 | ||
d1f28cf1 | 455 | static void server_loop(int fd) |
aba5acdf | 456 | { |
737f15f6 | 457 | struct timeval snaptime = { 0 }; |
aba5acdf | 458 | struct pollfd p; |
acd1e437 | 459 | |
aba5acdf SH |
460 | p.fd = fd; |
461 | p.events = p.revents = POLLIN; | |
ae665a52 | 462 | |
aba5acdf SH |
463 | sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d", |
464 | getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); | |
465 | ||
466 | load_netstat(); | |
467 | load_snmp6(); | |
468 | load_snmp(); | |
45a0dc16 | 469 | load_sctp_snmp(); |
aba5acdf SH |
470 | |
471 | for (;;) { | |
472 | int status; | |
dd81ee04 | 473 | time_t tdiff; |
aba5acdf | 474 | struct timeval now; |
acd1e437 | 475 | |
aba5acdf SH |
476 | gettimeofday(&now, NULL); |
477 | tdiff = T_DIFF(now, snaptime); | |
478 | if (tdiff >= scan_interval) { | |
479 | update_db(tdiff); | |
480 | snaptime = now; | |
481 | tdiff = 0; | |
482 | } | |
dd81ee04 | 483 | if (poll(&p, 1, scan_interval - tdiff) > 0 |
aba5acdf SH |
484 | && (p.revents&POLLIN)) { |
485 | int clnt = accept(fd, NULL, NULL); | |
acd1e437 | 486 | |
aba5acdf SH |
487 | if (clnt >= 0) { |
488 | pid_t pid; | |
acd1e437 | 489 | |
aba5acdf SH |
490 | if (children >= 5) { |
491 | close(clnt); | |
492 | } else if ((pid = fork()) != 0) { | |
acd1e437 | 493 | if (pid > 0) |
aba5acdf SH |
494 | children++; |
495 | close(clnt); | |
496 | } else { | |
497 | FILE *fp = fdopen(clnt, "w"); | |
acd1e437 | 498 | |
dd81ee04 | 499 | if (fp) |
aba5acdf | 500 | dump_kern_db(fp, 0); |
aba5acdf SH |
501 | exit(0); |
502 | } | |
503 | } | |
504 | } | |
505 | while (children && waitpid(-1, &status, WNOHANG) > 0) | |
506 | children--; | |
507 | } | |
508 | } | |
509 | ||
d1f28cf1 | 510 | static int verify_forging(int fd) |
aba5acdf SH |
511 | { |
512 | struct ucred cred; | |
737f15f6 SH |
513 | socklen_t olen = sizeof(cred); |
514 | ||
acd1e437 | 515 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) || |
aba5acdf SH |
516 | olen < sizeof(cred)) |
517 | return -1; | |
518 | if (cred.uid == getuid() || cred.uid == 0) | |
519 | return 0; | |
520 | return -1; | |
521 | } | |
522 | ||
523 | static void usage(void) __attribute__((noreturn)); | |
524 | ||
525 | static void usage(void) | |
526 | { | |
527 | fprintf(stderr, | |
404582c8 | 528 | "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n" |
eca7a742 MF |
529 | " -h, --help this message\n" |
530 | " -a, --ignore ignore history\n" | |
531 | " -d, --scan=SECS sample every statistics every SECS\n" | |
532 | " -j, --json format output in JSON\n" | |
533 | " -n, --nooutput do history only\n" | |
534 | " -p, --pretty pretty print\n" | |
535 | " -r, --reset reset history\n" | |
536 | " -s, --noupdate don't update history\n" | |
537 | " -t, --interval=SECS report average over the last SECS\n" | |
538 | " -V, --version output version information\n" | |
539 | " -z, --zeros show entries with zero activity\n"); | |
aba5acdf SH |
540 | exit(-1); |
541 | } | |
542 | ||
404582c8 SH |
543 | static const struct option longopts[] = { |
544 | { "help", 0, 0, 'h' }, | |
545 | { "ignore", 0, 0, 'a' }, | |
546 | { "scan", 1, 0, 'd'}, | |
547 | { "nooutput", 0, 0, 'n' }, | |
548 | { "json", 0, 0, 'j' }, | |
549 | { "reset", 0, 0, 'r' }, | |
550 | { "noupdate", 0, 0, 's' }, | |
fcc16c22 | 551 | { "pretty", 0, 0, 'p' }, |
404582c8 SH |
552 | { "interval", 1, 0, 't' }, |
553 | { "version", 0, 0, 'V' }, | |
554 | { "zeros", 0, 0, 'z' }, | |
555 | { 0 } | |
556 | }; | |
aba5acdf SH |
557 | |
558 | int main(int argc, char *argv[]) | |
559 | { | |
896ebd6c | 560 | char *hist_name; |
aba5acdf SH |
561 | struct sockaddr_un sun; |
562 | FILE *hist_fp = NULL; | |
563 | int ch; | |
564 | int fd; | |
565 | ||
fcc16c22 | 566 | while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp", |
404582c8 | 567 | longopts, NULL)) != EOF) { |
acd1e437 | 568 | switch (ch) { |
aba5acdf SH |
569 | case 'z': |
570 | dump_zeros = 1; | |
571 | break; | |
572 | case 'r': | |
573 | reset_history = 1; | |
574 | break; | |
575 | case 'a': | |
576 | ignore_history = 1; | |
577 | break; | |
578 | case 's': | |
579 | no_update = 1; | |
580 | break; | |
581 | case 'n': | |
582 | no_output = 1; | |
583 | break; | |
584 | case 'd': | |
585 | scan_interval = 1000*atoi(optarg); | |
586 | break; | |
587 | case 't': | |
588 | if (sscanf(optarg, "%d", &time_constant) != 1 || | |
589 | time_constant <= 0) { | |
590 | fprintf(stderr, "nstat: invalid time constant divisor\n"); | |
591 | exit(-1); | |
592 | } | |
593 | break; | |
d48ed3f4 SH |
594 | case 'j': |
595 | json_output = 1; | |
596 | break; | |
fcc16c22 SH |
597 | case 'p': |
598 | pretty = 1; | |
599 | break; | |
aba5acdf SH |
600 | case 'v': |
601 | case 'V': | |
602 | printf("nstat utility, iproute2-ss%s\n", SNAPSHOT); | |
603 | exit(0); | |
604 | case 'h': | |
605 | case '?': | |
606 | default: | |
607 | usage(); | |
608 | } | |
609 | } | |
610 | ||
611 | argc -= optind; | |
612 | argv += optind; | |
613 | ||
614 | sun.sun_family = AF_UNIX; | |
615 | sun.sun_path[0] = 0; | |
616 | sprintf(sun.sun_path+1, "nstat%d", getuid()); | |
617 | ||
618 | if (scan_interval > 0) { | |
619 | if (time_constant == 0) | |
620 | time_constant = 60; | |
621 | time_constant *= 1000; | |
622 | W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant); | |
623 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
624 | perror("nstat: socket"); | |
625 | exit(-1); | |
626 | } | |
acd1e437 | 627 | if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) { |
aba5acdf SH |
628 | perror("nstat: bind"); |
629 | exit(-1); | |
630 | } | |
631 | if (listen(fd, 5) < 0) { | |
632 | perror("nstat: listen"); | |
633 | exit(-1); | |
634 | } | |
a7a9ddbb MF |
635 | if (daemon(0, 0)) { |
636 | perror("nstat: daemon"); | |
637 | exit(-1); | |
638 | } | |
aba5acdf SH |
639 | signal(SIGPIPE, SIG_IGN); |
640 | signal(SIGCHLD, sigchild); | |
641 | server_loop(fd); | |
642 | exit(0); | |
643 | } | |
644 | ||
645 | patterns = argv; | |
646 | npatterns = argc; | |
647 | ||
896ebd6c SH |
648 | if ((hist_name = getenv("NSTAT_HISTORY")) == NULL) { |
649 | hist_name = malloc(128); | |
aba5acdf | 650 | sprintf(hist_name, "/tmp/.nstat.u%d", getuid()); |
896ebd6c | 651 | } |
aba5acdf SH |
652 | |
653 | if (reset_history) | |
654 | unlink(hist_name); | |
655 | ||
656 | if (!ignore_history || !no_update) { | |
657 | struct stat stb; | |
658 | ||
659 | fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600); | |
660 | if (fd < 0) { | |
661 | perror("nstat: open history file"); | |
662 | exit(-1); | |
663 | } | |
664 | if ((hist_fp = fdopen(fd, "r+")) == NULL) { | |
665 | perror("nstat: fdopen history file"); | |
666 | exit(-1); | |
667 | } | |
668 | if (flock(fileno(hist_fp), LOCK_EX)) { | |
669 | perror("nstat: flock history file"); | |
670 | exit(-1); | |
671 | } | |
672 | if (fstat(fileno(hist_fp), &stb) != 0) { | |
673 | perror("nstat: fstat history file"); | |
674 | exit(-1); | |
675 | } | |
676 | if (stb.st_nlink != 1 || stb.st_uid != getuid()) { | |
677 | fprintf(stderr, "nstat: something is so wrong with history file, that I prefer not to proceed.\n"); | |
678 | exit(-1); | |
679 | } | |
680 | if (!ignore_history) { | |
681 | FILE *tfp; | |
9a230771 | 682 | long uptime = -1; |
acd1e437 | 683 | |
aba5acdf SH |
684 | if ((tfp = fopen("/proc/uptime", "r")) != NULL) { |
685 | if (fscanf(tfp, "%ld", &uptime) != 1) | |
686 | uptime = -1; | |
687 | fclose(tfp); | |
688 | } | |
689 | if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) { | |
690 | fprintf(stderr, "nstat: history is aged out, resetting\n"); | |
d572ed4d PS |
691 | if (ftruncate(fileno(hist_fp), 0) < 0) |
692 | perror("nstat: ftruncate"); | |
aba5acdf SH |
693 | } |
694 | } | |
695 | ||
696 | load_good_table(hist_fp); | |
697 | ||
698 | hist_db = kern_db; | |
699 | kern_db = NULL; | |
700 | } | |
701 | ||
702 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && | |
acd1e437 | 703 | (connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0 |
aba5acdf | 704 | || (strcpy(sun.sun_path+1, "nstat0"), |
acd1e437 | 705 | connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0)) |
aba5acdf SH |
706 | && verify_forging(fd) == 0) { |
707 | FILE *sfp = fdopen(fd, "r"); | |
acd1e437 | 708 | |
6d02518f PS |
709 | if (!sfp) { |
710 | fprintf(stderr, "nstat: fdopen failed: %s\n", | |
711 | strerror(errno)); | |
712 | close(fd); | |
713 | } else { | |
714 | load_good_table(sfp); | |
715 | if (hist_db && source_mismatch) { | |
716 | fprintf(stderr, "nstat: history is stale, ignoring it.\n"); | |
717 | hist_db = NULL; | |
718 | } | |
719 | fclose(sfp); | |
aba5acdf | 720 | } |
aba5acdf SH |
721 | } else { |
722 | if (fd >= 0) | |
723 | close(fd); | |
724 | if (hist_db && info_source[0] && strcmp(info_source, "kernel")) { | |
725 | fprintf(stderr, "nstat: history is stale, ignoring it.\n"); | |
726 | hist_db = NULL; | |
727 | info_source[0] = 0; | |
728 | } | |
729 | load_netstat(); | |
730 | load_snmp6(); | |
731 | load_snmp(); | |
45a0dc16 | 732 | load_sctp_snmp(); |
aba5acdf SH |
733 | if (info_source[0] == 0) |
734 | strcpy(info_source, "kernel"); | |
735 | } | |
736 | ||
737 | if (!no_output) { | |
738 | if (ignore_history || hist_db == NULL) | |
739 | dump_kern_db(stdout, 0); | |
740 | else | |
741 | dump_incr_db(stdout); | |
742 | } | |
743 | if (!no_update) { | |
d572ed4d PS |
744 | if (ftruncate(fileno(hist_fp), 0) < 0) |
745 | perror("nstat: ftruncate"); | |
aba5acdf | 746 | rewind(hist_fp); |
404582c8 SH |
747 | |
748 | json_output = 0; | |
aba5acdf | 749 | dump_kern_db(hist_fp, 1); |
404582c8 | 750 | fclose(hist_fp); |
aba5acdf SH |
751 | } |
752 | exit(0); | |
753 | } |