blob: 1212b1f2c81287772ace0b24d4530757e23c84f3 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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>
Stephen Hemminger404582c2013-09-24 11:54:45 -070029#include <getopt.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -070031#include <json_writer.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032#include <SNAPSHOT.h>
Phil Sutter62000e52016-06-28 18:42:15 +020033#include "utils.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070035int dump_zeros;
36int reset_history;
37int ignore_history;
38int no_output;
39int json_output;
40int pretty;
41int no_update;
42int scan_interval;
43int time_constant;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044double W;
45char **patterns;
46int npatterns;
47
48char info_source[128];
49int source_mismatch;
50
Yu Zhiguo4ffc44c2008-06-20 09:50:16 +080051static int generic_proc_open(const char *env, char *name)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000052{
53 char store[128];
54 char *p = getenv(env);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070055
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000056 if (!p) {
57 p = getenv("PROC_ROOT") ? : "/proc";
58 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
59 p = store;
60 }
Yu Zhiguo4ffc44c2008-06-20 09:50:16 +080061 return open(p, O_RDONLY);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062}
63
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080064static int net_netstat_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000065{
66 return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
67}
68
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080069static int net_snmp_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000070{
71 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
72}
73
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080074static int net_snmp6_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075{
76 return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
77}
78
Hangbin Liu45a0dc12016-09-05 11:35:02 +080079static int net_sctp_snmp_open(void)
80{
81 return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp");
82}
83
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070084struct nstat_ent {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000085 struct nstat_ent *next;
86 char *id;
87 unsigned long long val;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000088 double rate;
89};
90
91struct nstat_ent *kern_db;
92struct nstat_ent *hist_db;
93
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080094static const char *useless_numbers[] = {
95 "IpForwarding", "IpDefaultTTL",
96 "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax",
97 "TcpMaxConn", "TcpCurrEstab"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000098};
99
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800100static int useless_number(const char *id)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101{
102 int i;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700103
Phil Sutter62000e52016-06-28 18:42:15 +0200104 for (i = 0; i < ARRAY_SIZE(useless_numbers); i++)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000105 if (strcmp(id, useless_numbers[i]) == 0)
106 return 1;
107 return 0;
108}
109
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800110static int match(const char *id)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000111{
112 int i;
113
114 if (npatterns == 0)
115 return 1;
116
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700117 for (i = 0; i < npatterns; i++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118 if (!fnmatch(patterns[i], id, 0))
119 return 1;
120 }
121 return 0;
122}
123
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800124static void load_good_table(FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000125{
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;
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000134 char idbuf[sizeof(buf)];
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700135
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 if (buf[0] == '#') {
137 buf[strlen(buf)-1] = 0;
138 if (info_source[0] && strcmp(info_source, buf+1))
139 source_mismatch = 1;
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000140 info_source[0] = 0;
141 strncat(info_source, buf+1, sizeof(info_source)-1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142 continue;
143 }
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000144 /* idbuf is as big as buf, so this is safe */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000145 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);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000155 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
Eric Dumazetd4717912014-12-05 18:10:08 -0800169static 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}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000178
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800179static void load_ugly_table(FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000180{
181 char buf[4096];
182 struct nstat_ent *db = NULL;
183 struct nstat_ent *n;
184
185 while (fgets(buf, sizeof(buf), fp) != NULL) {
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000186 char idbuf[sizeof(buf)];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000187 int off;
188 char *p;
Eric Dumazetd4717912014-12-05 18:10:08 -0800189 int count1, count2, skip = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000190
191 p = strchr(buf, ':');
192 if (!p)
193 abort();
Eric Dumazetd4717912014-12-05 18:10:08 -0800194 count1 = count_spaces(buf);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000195 *p = 0;
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000196 idbuf[0] = 0;
197 strncat(idbuf, buf, sizeof(idbuf) - 1);
198 off = p - buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199 p += 2;
200
201 while (*p) {
202 char *next;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700203
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 if ((next = strchr(p, ' ')) != NULL)
205 *next++ = 0;
206 else if ((next = strchr(p, '\n')) != NULL)
207 *next++ = 0;
net[shemminger]!shemmingerb482ffa2004-10-19 20:02:59 +0000208 if (off < sizeof(idbuf)) {
209 idbuf[off] = 0;
210 strncat(idbuf, p, sizeof(idbuf) - off - 1);
211 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000212 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();
Eric Dumazetd4717912014-12-05 18:10:08 -0800224 count2 = count_spaces(buf);
225 if (count2 > count1)
226 skip = count2 - count1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000227 do {
228 p = strrchr(buf, ' ');
229 if (!p)
230 abort();
231 *p = 0;
Eric Dumazetcdb22272014-08-25 07:27:54 -0700232 if (sscanf(p+1, "%llu", &n->val) != 1)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000233 abort();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000234 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
Eric Dumazetd4717912014-12-05 18:10:08 -0800235 if (skip)
236 skip--;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000237 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
Hangbin Liu45a0dc12016-09-05 11:35:02 +0800255static 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
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800265static void load_snmp(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000266{
267 FILE *fp = fdopen(net_snmp_open(), "r");
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700268
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000269 if (fp) {
270 load_ugly_table(fp);
271 fclose(fp);
272 }
273}
274
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800275static void load_snmp6(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000276{
277 FILE *fp = fdopen(net_snmp6_open(), "r");
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700278
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000279 if (fp) {
280 load_good_table(fp);
281 fclose(fp);
282 }
283}
284
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800285static void load_netstat(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000286{
287 FILE *fp = fdopen(net_netstat_open(), "r");
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700288
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000289 if (fp) {
290 load_ugly_table(fp);
291 fclose(fp);
292 }
293}
294
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700295
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800296static void dump_kern_db(FILE *fp, int to_hist)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000297{
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700298 json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000299 struct nstat_ent *n, *h;
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700300
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000301 h = hist_db;
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700302 if (jw) {
Anuradha Karuppiahd721a142016-06-22 06:45:51 -0700303 jsonw_start_object(jw);
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700304 jsonw_pretty(jw, pretty);
305 jsonw_name(jw, info_source);
306 jsonw_start_object(jw);
307 } else
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700308 fprintf(fp, "#%s\n", info_source);
Stephen Hemminger404582c2013-09-24 11:54:45 -0700309
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700310 for (n = kern_db; n; n = n->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000311 unsigned long long val = n->val;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700312
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000313 if (!dump_zeros && !val && !n->rate)
314 continue;
315 if (!match(n->id)) {
316 struct nstat_ent *h1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700317
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000318 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 }
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700328
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700329 if (jw)
330 jsonw_uint_field(jw, n->id, val);
331 else
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700332 fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000333 }
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700334
335 if (jw) {
336 jsonw_end_object(jw);
Anuradha Karuppiahd721a142016-06-22 06:45:51 -0700337
338 jsonw_end_object(jw);
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700339 jsonw_destroy(&jw);
340 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341}
342
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800343static void dump_incr_db(FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000344{
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700345 json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000346 struct nstat_ent *n, *h;
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700347
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000348 h = hist_db;
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700349 if (jw) {
Anuradha Karuppiahd721a142016-06-22 06:45:51 -0700350 jsonw_start_object(jw);
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700351 jsonw_pretty(jw, pretty);
352 jsonw_name(jw, info_source);
353 jsonw_start_object(jw);
354 } else
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700355 fprintf(fp, "#%s\n", info_source);
356
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700357 for (n = kern_db; n; n = n->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000358 int ovfl = 0;
359 unsigned long long val = n->val;
360 struct nstat_ent *h1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700361
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000362 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;
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700377
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700378 if (jw)
379 jsonw_uint_field(jw, n->id, val);
380 else
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700381 fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val,
382 n->rate, ovfl?" (overflow)":"");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000383 }
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700384
385 if (jw) {
386 jsonw_end_object(jw);
Anuradha Karuppiahd721a142016-06-22 06:45:51 -0700387
388 jsonw_end_object(jw);
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700389 jsonw_destroy(&jw);
390 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000391}
392
393static int children;
394
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800395static void sigchild(int signo)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000396{
397}
398
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800399static void update_db(int interval)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000400{
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();
Hangbin Liu45a0dc12016-09-05 11:35:02 +0800409 load_sctp_snmp();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000410
411 h = kern_db;
412 kern_db = n;
413
414 for (n = kern_db; n; n = n->next) {
415 struct nstat_ent *h1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700416
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417 for (h1 = h; h1; h1 = h1->next) {
418 if (strcmp(h1->id, n->id) == 0) {
419 double sample;
Eric Dumazetcdb22272014-08-25 07:27:54 -0700420 unsigned long long incr = h1->val - n->val;
421
422 n->val = h1->val;
423 sample = (double)incr * 1000.0 / interval;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000424 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;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700431
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000432 n->rate += w*(sample-n->rate);
433 }
434 }
435
436 while (h != h1) {
437 struct nstat_ent *tmp = h;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700438
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000439 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
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700452#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000453
454
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800455static void server_loop(int fd)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000456{
shemminger737f15f2005-07-08 22:08:47 +0000457 struct timeval snaptime = { 0 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000458 struct pollfd p;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700459
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000460 p.fd = fd;
461 p.events = p.revents = POLLIN;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800462
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000463 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();
Hangbin Liu45a0dc12016-09-05 11:35:02 +0800469 load_sctp_snmp();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000470
471 for (;;) {
472 int status;
Phil Sutterdd81ee02016-03-02 16:56:27 +0100473 time_t tdiff;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000474 struct timeval now;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700475
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000476 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 }
Phil Sutterdd81ee02016-03-02 16:56:27 +0100483 if (poll(&p, 1, scan_interval - tdiff) > 0
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000484 && (p.revents&POLLIN)) {
485 int clnt = accept(fd, NULL, NULL);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700486
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000487 if (clnt >= 0) {
488 pid_t pid;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700489
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000490 if (children >= 5) {
491 close(clnt);
492 } else if ((pid = fork()) != 0) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700493 if (pid > 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000494 children++;
495 close(clnt);
496 } else {
497 FILE *fp = fdopen(clnt, "w");
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700498
Phil Sutterdd81ee02016-03-02 16:56:27 +0100499 if (fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000500 dump_kern_db(fp, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000501 exit(0);
502 }
503 }
504 }
505 while (children && waitpid(-1, &status, WNOHANG) > 0)
506 children--;
507 }
508}
509
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800510static int verify_forging(int fd)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000511{
512 struct ucred cred;
shemminger737f15f2005-07-08 22:08:47 +0000513 socklen_t olen = sizeof(cred);
514
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700515 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000516 olen < sizeof(cred))
517 return -1;
518 if (cred.uid == getuid() || cred.uid == 0)
519 return 0;
520 return -1;
521}
522
523static void usage(void) __attribute__((noreturn));
524
525static void usage(void)
526{
527 fprintf(stderr,
Stephen Hemminger404582c2013-09-24 11:54:45 -0700528"Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
Mike Frysingereca7a742016-11-15 22:34:14 -0500529" -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");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000540 exit(-1);
541}
542
Stephen Hemminger404582c2013-09-24 11:54:45 -0700543static 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' },
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700551 { "pretty", 0, 0, 'p' },
Stephen Hemminger404582c2013-09-24 11:54:45 -0700552 { "interval", 1, 0, 't' },
553 { "version", 0, 0, 'V' },
554 { "zeros", 0, 0, 'z' },
555 { 0 }
556};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000557
558int main(int argc, char *argv[])
559{
Stephen Hemminger896ebd62009-12-26 10:21:13 -0800560 char *hist_name;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000561 struct sockaddr_un sun;
562 FILE *hist_fp = NULL;
563 int ch;
564 int fd;
565
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700566 while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp",
Stephen Hemminger404582c2013-09-24 11:54:45 -0700567 longopts, NULL)) != EOF) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700568 switch (ch) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000569 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;
Stephen Hemmingerd48ed3f2013-09-13 10:31:41 -0700594 case 'j':
595 json_output = 1;
596 break;
Stephen Hemmingerfcc16c22015-08-21 14:14:51 -0700597 case 'p':
598 pretty = 1;
599 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000600 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 }
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700627 if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000628 perror("nstat: bind");
629 exit(-1);
630 }
631 if (listen(fd, 5) < 0) {
632 perror("nstat: listen");
633 exit(-1);
634 }
Mike Frysingera7a9ddb2009-11-06 06:04:39 -0500635 if (daemon(0, 0)) {
636 perror("nstat: daemon");
637 exit(-1);
638 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000639 signal(SIGPIPE, SIG_IGN);
640 signal(SIGCHLD, sigchild);
641 server_loop(fd);
642 exit(0);
643 }
644
645 patterns = argv;
646 npatterns = argc;
647
Stephen Hemminger896ebd62009-12-26 10:21:13 -0800648 if ((hist_name = getenv("NSTAT_HISTORY")) == NULL) {
649 hist_name = malloc(128);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000650 sprintf(hist_name, "/tmp/.nstat.u%d", getuid());
Stephen Hemminger896ebd62009-12-26 10:21:13 -0800651 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000652
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;
Dan McGee9a230772011-08-31 12:16:36 -0700682 long uptime = -1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700683
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000684 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");
Phil Sutterd572ed42015-11-28 01:00:04 +0100691 if (ftruncate(fileno(hist_fp), 0) < 0)
692 perror("nstat: ftruncate");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000693 }
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 &&
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700703 (connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000704 || (strcpy(sun.sun_path+1, "nstat0"),
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700705 connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000706 && verify_forging(fd) == 0) {
707 FILE *sfp = fdopen(fd, "r");
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700708
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000709 load_good_table(sfp);
710 if (hist_db && source_mismatch) {
711 fprintf(stderr, "nstat: history is stale, ignoring it.\n");
712 hist_db = NULL;
713 }
714 fclose(sfp);
715 } else {
716 if (fd >= 0)
717 close(fd);
718 if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
719 fprintf(stderr, "nstat: history is stale, ignoring it.\n");
720 hist_db = NULL;
721 info_source[0] = 0;
722 }
723 load_netstat();
724 load_snmp6();
725 load_snmp();
Hangbin Liu45a0dc12016-09-05 11:35:02 +0800726 load_sctp_snmp();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000727 if (info_source[0] == 0)
728 strcpy(info_source, "kernel");
729 }
730
731 if (!no_output) {
732 if (ignore_history || hist_db == NULL)
733 dump_kern_db(stdout, 0);
734 else
735 dump_incr_db(stdout);
736 }
737 if (!no_update) {
Phil Sutterd572ed42015-11-28 01:00:04 +0100738 if (ftruncate(fileno(hist_fp), 0) < 0)
739 perror("nstat: ftruncate");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000740 rewind(hist_fp);
Stephen Hemminger404582c2013-09-24 11:54:45 -0700741
742 json_output = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000743 dump_kern_db(hist_fp, 1);
Stephen Hemminger404582c2013-09-24 11:54:45 -0700744 fclose(hist_fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000745 }
746 exit(0);
747}