osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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> |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 29 | #include <getopt.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 30 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 31 | #include <json_writer.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 32 | #include <SNAPSHOT.h> |
Phil Sutter | 62000e5 | 2016-06-28 18:42:15 +0200 | [diff] [blame] | 33 | #include "utils.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 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; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | double W; |
| 45 | char **patterns; |
| 46 | int npatterns; |
| 47 | |
| 48 | char info_source[128]; |
| 49 | int source_mismatch; |
| 50 | |
Yu Zhiguo | 4ffc44c | 2008-06-20 09:50:16 +0800 | [diff] [blame] | 51 | static int generic_proc_open(const char *env, char *name) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | { |
| 53 | char store[128]; |
| 54 | char *p = getenv(env); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 55 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 56 | if (!p) { |
| 57 | p = getenv("PROC_ROOT") ? : "/proc"; |
| 58 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); |
| 59 | p = store; |
| 60 | } |
Yu Zhiguo | 4ffc44c | 2008-06-20 09:50:16 +0800 | [diff] [blame] | 61 | return open(p, O_RDONLY); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 64 | static int net_netstat_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 65 | { |
| 66 | return generic_proc_open("PROC_NET_NETSTAT", "net/netstat"); |
| 67 | } |
| 68 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 69 | static int net_snmp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 70 | { |
| 71 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); |
| 72 | } |
| 73 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 74 | static int net_snmp6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 75 | { |
| 76 | return generic_proc_open("PROC_NET_SNMP6", "net/snmp6"); |
| 77 | } |
| 78 | |
Hangbin Liu | 45a0dc1 | 2016-09-05 11:35:02 +0800 | [diff] [blame] | 79 | static int net_sctp_snmp_open(void) |
| 80 | { |
| 81 | return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp"); |
| 82 | } |
| 83 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 84 | struct nstat_ent { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 85 | struct nstat_ent *next; |
| 86 | char *id; |
| 87 | unsigned long long val; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 88 | double rate; |
| 89 | }; |
| 90 | |
| 91 | struct nstat_ent *kern_db; |
| 92 | struct nstat_ent *hist_db; |
| 93 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 94 | static const char *useless_numbers[] = { |
| 95 | "IpForwarding", "IpDefaultTTL", |
| 96 | "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax", |
| 97 | "TcpMaxConn", "TcpCurrEstab" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 100 | static int useless_number(const char *id) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 101 | { |
| 102 | int i; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 103 | |
Phil Sutter | 62000e5 | 2016-06-28 18:42:15 +0200 | [diff] [blame] | 104 | for (i = 0; i < ARRAY_SIZE(useless_numbers); i++) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 105 | if (strcmp(id, useless_numbers[i]) == 0) |
| 106 | return 1; |
| 107 | return 0; |
| 108 | } |
| 109 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 110 | static int match(const char *id) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 111 | { |
| 112 | int i; |
| 113 | |
| 114 | if (npatterns == 0) |
| 115 | return 1; |
| 116 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 117 | for (i = 0; i < npatterns; i++) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 118 | if (!fnmatch(patterns[i], id, 0)) |
| 119 | return 1; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 124 | static void load_good_table(FILE *fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
net[shemminger]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 134 | char idbuf[sizeof(buf)]; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 135 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 136 | 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]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 140 | info_source[0] = 0; |
| 141 | strncat(info_source, buf+1, sizeof(info_source)-1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 142 | continue; |
| 143 | } |
net[shemminger]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 144 | /* idbuf is as big as buf, so this is safe */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Eric Dumazet | d471791 | 2014-12-05 18:10:08 -0800 | [diff] [blame] | 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 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 178 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 179 | static void load_ugly_table(FILE *fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) { |
net[shemminger]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 186 | char idbuf[sizeof(buf)]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 187 | int off; |
| 188 | char *p; |
Eric Dumazet | d471791 | 2014-12-05 18:10:08 -0800 | [diff] [blame] | 189 | int count1, count2, skip = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 190 | |
| 191 | p = strchr(buf, ':'); |
| 192 | if (!p) |
| 193 | abort(); |
Eric Dumazet | d471791 | 2014-12-05 18:10:08 -0800 | [diff] [blame] | 194 | count1 = count_spaces(buf); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 195 | *p = 0; |
net[shemminger]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 196 | idbuf[0] = 0; |
| 197 | strncat(idbuf, buf, sizeof(idbuf) - 1); |
| 198 | off = p - buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | p += 2; |
| 200 | |
| 201 | while (*p) { |
| 202 | char *next; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 203 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 204 | if ((next = strchr(p, ' ')) != NULL) |
| 205 | *next++ = 0; |
| 206 | else if ((next = strchr(p, '\n')) != NULL) |
| 207 | *next++ = 0; |
net[shemminger]!shemminger | b482ffa | 2004-10-19 20:02:59 +0000 | [diff] [blame] | 208 | if (off < sizeof(idbuf)) { |
| 209 | idbuf[off] = 0; |
| 210 | strncat(idbuf, p, sizeof(idbuf) - off - 1); |
| 211 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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(); |
Eric Dumazet | d471791 | 2014-12-05 18:10:08 -0800 | [diff] [blame] | 224 | count2 = count_spaces(buf); |
| 225 | if (count2 > count1) |
| 226 | skip = count2 - count1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 227 | do { |
| 228 | p = strrchr(buf, ' '); |
| 229 | if (!p) |
| 230 | abort(); |
| 231 | *p = 0; |
Eric Dumazet | cdb2227 | 2014-08-25 07:27:54 -0700 | [diff] [blame] | 232 | if (sscanf(p+1, "%llu", &n->val) != 1) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 233 | abort(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 234 | /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */ |
Eric Dumazet | d471791 | 2014-12-05 18:10:08 -0800 | [diff] [blame] | 235 | if (skip) |
| 236 | skip--; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Hangbin Liu | 45a0dc1 | 2016-09-05 11:35:02 +0800 | [diff] [blame] | 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 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 265 | static void load_snmp(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 266 | { |
| 267 | FILE *fp = fdopen(net_snmp_open(), "r"); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 268 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 269 | if (fp) { |
| 270 | load_ugly_table(fp); |
| 271 | fclose(fp); |
| 272 | } |
| 273 | } |
| 274 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 275 | static void load_snmp6(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 276 | { |
| 277 | FILE *fp = fdopen(net_snmp6_open(), "r"); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 278 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 279 | if (fp) { |
| 280 | load_good_table(fp); |
| 281 | fclose(fp); |
| 282 | } |
| 283 | } |
| 284 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 285 | static void load_netstat(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 286 | { |
| 287 | FILE *fp = fdopen(net_netstat_open(), "r"); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 288 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 289 | if (fp) { |
| 290 | load_ugly_table(fp); |
| 291 | fclose(fp); |
| 292 | } |
| 293 | } |
| 294 | |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 295 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 296 | static void dump_kern_db(FILE *fp, int to_hist) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 297 | { |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 298 | json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 299 | struct nstat_ent *n, *h; |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 300 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 301 | h = hist_db; |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 302 | if (jw) { |
Anuradha Karuppiah | d721a14 | 2016-06-22 06:45:51 -0700 | [diff] [blame] | 303 | jsonw_start_object(jw); |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 304 | jsonw_pretty(jw, pretty); |
| 305 | jsonw_name(jw, info_source); |
| 306 | jsonw_start_object(jw); |
| 307 | } else |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 308 | fprintf(fp, "#%s\n", info_source); |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 309 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 310 | for (n = kern_db; n; n = n->next) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 311 | unsigned long long val = n->val; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 312 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 313 | if (!dump_zeros && !val && !n->rate) |
| 314 | continue; |
| 315 | if (!match(n->id)) { |
| 316 | struct nstat_ent *h1; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 317 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 328 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 329 | if (jw) |
| 330 | jsonw_uint_field(jw, n->id, val); |
| 331 | else |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 332 | fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 333 | } |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 334 | |
| 335 | if (jw) { |
| 336 | jsonw_end_object(jw); |
Anuradha Karuppiah | d721a14 | 2016-06-22 06:45:51 -0700 | [diff] [blame] | 337 | |
| 338 | jsonw_end_object(jw); |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 339 | jsonw_destroy(&jw); |
| 340 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 343 | static void dump_incr_db(FILE *fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 344 | { |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 345 | json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 346 | struct nstat_ent *n, *h; |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 347 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 348 | h = hist_db; |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 349 | if (jw) { |
Anuradha Karuppiah | d721a14 | 2016-06-22 06:45:51 -0700 | [diff] [blame] | 350 | jsonw_start_object(jw); |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 351 | jsonw_pretty(jw, pretty); |
| 352 | jsonw_name(jw, info_source); |
| 353 | jsonw_start_object(jw); |
| 354 | } else |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 355 | fprintf(fp, "#%s\n", info_source); |
| 356 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 357 | for (n = kern_db; n; n = n->next) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 358 | int ovfl = 0; |
| 359 | unsigned long long val = n->val; |
| 360 | struct nstat_ent *h1; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 361 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 377 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 378 | if (jw) |
| 379 | jsonw_uint_field(jw, n->id, val); |
| 380 | else |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 381 | fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val, |
| 382 | n->rate, ovfl?" (overflow)":""); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 383 | } |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 384 | |
| 385 | if (jw) { |
| 386 | jsonw_end_object(jw); |
Anuradha Karuppiah | d721a14 | 2016-06-22 06:45:51 -0700 | [diff] [blame] | 387 | |
| 388 | jsonw_end_object(jw); |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 389 | jsonw_destroy(&jw); |
| 390 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int children; |
| 394 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 395 | static void sigchild(int signo) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 396 | { |
| 397 | } |
| 398 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 399 | static void update_db(int interval) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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(); |
Hangbin Liu | 45a0dc1 | 2016-09-05 11:35:02 +0800 | [diff] [blame] | 409 | load_sctp_snmp(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 410 | |
| 411 | h = kern_db; |
| 412 | kern_db = n; |
| 413 | |
| 414 | for (n = kern_db; n; n = n->next) { |
| 415 | struct nstat_ent *h1; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 416 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 417 | for (h1 = h; h1; h1 = h1->next) { |
| 418 | if (strcmp(h1->id, n->id) == 0) { |
| 419 | double sample; |
Eric Dumazet | cdb2227 | 2014-08-25 07:27:54 -0700 | [diff] [blame] | 420 | unsigned long long incr = h1->val - n->val; |
| 421 | |
| 422 | n->val = h1->val; |
| 423 | sample = (double)incr * 1000.0 / interval; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 431 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 432 | n->rate += w*(sample-n->rate); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | while (h != h1) { |
| 437 | struct nstat_ent *tmp = h; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 438 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 452 | #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 453 | |
| 454 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 455 | static void server_loop(int fd) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 456 | { |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 457 | struct timeval snaptime = { 0 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 458 | struct pollfd p; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 459 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 460 | p.fd = fd; |
| 461 | p.events = p.revents = POLLIN; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 462 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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(); |
Hangbin Liu | 45a0dc1 | 2016-09-05 11:35:02 +0800 | [diff] [blame] | 469 | load_sctp_snmp(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 470 | |
| 471 | for (;;) { |
| 472 | int status; |
Phil Sutter | dd81ee0 | 2016-03-02 16:56:27 +0100 | [diff] [blame] | 473 | time_t tdiff; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 474 | struct timeval now; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 475 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Phil Sutter | dd81ee0 | 2016-03-02 16:56:27 +0100 | [diff] [blame] | 483 | if (poll(&p, 1, scan_interval - tdiff) > 0 |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 484 | && (p.revents&POLLIN)) { |
| 485 | int clnt = accept(fd, NULL, NULL); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 486 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 487 | if (clnt >= 0) { |
| 488 | pid_t pid; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 489 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 490 | if (children >= 5) { |
| 491 | close(clnt); |
| 492 | } else if ((pid = fork()) != 0) { |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 493 | if (pid > 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 494 | children++; |
| 495 | close(clnt); |
| 496 | } else { |
| 497 | FILE *fp = fdopen(clnt, "w"); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 498 | |
Phil Sutter | dd81ee0 | 2016-03-02 16:56:27 +0100 | [diff] [blame] | 499 | if (fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 500 | dump_kern_db(fp, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 501 | exit(0); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | while (children && waitpid(-1, &status, WNOHANG) > 0) |
| 506 | children--; |
| 507 | } |
| 508 | } |
| 509 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 510 | static int verify_forging(int fd) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 511 | { |
| 512 | struct ucred cred; |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 513 | socklen_t olen = sizeof(cred); |
| 514 | |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 515 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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, |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 528 | "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n" |
Mike Frysinger | eca7a74 | 2016-11-15 22:34:14 -0500 | [diff] [blame] | 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"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 540 | exit(-1); |
| 541 | } |
| 542 | |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 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' }, |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 551 | { "pretty", 0, 0, 'p' }, |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 552 | { "interval", 1, 0, 't' }, |
| 553 | { "version", 0, 0, 'V' }, |
| 554 | { "zeros", 0, 0, 'z' }, |
| 555 | { 0 } |
| 556 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 557 | |
| 558 | int main(int argc, char *argv[]) |
| 559 | { |
Stephen Hemminger | 896ebd6 | 2009-12-26 10:21:13 -0800 | [diff] [blame] | 560 | char *hist_name; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 561 | struct sockaddr_un sun; |
| 562 | FILE *hist_fp = NULL; |
| 563 | int ch; |
| 564 | int fd; |
| 565 | |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 566 | while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp", |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 567 | longopts, NULL)) != EOF) { |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 568 | switch (ch) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
Stephen Hemminger | d48ed3f | 2013-09-13 10:31:41 -0700 | [diff] [blame] | 594 | case 'j': |
| 595 | json_output = 1; |
| 596 | break; |
Stephen Hemminger | fcc16c2 | 2015-08-21 14:14:51 -0700 | [diff] [blame] | 597 | case 'p': |
| 598 | pretty = 1; |
| 599 | break; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 627 | if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 628 | perror("nstat: bind"); |
| 629 | exit(-1); |
| 630 | } |
| 631 | if (listen(fd, 5) < 0) { |
| 632 | perror("nstat: listen"); |
| 633 | exit(-1); |
| 634 | } |
Mike Frysinger | a7a9ddb | 2009-11-06 06:04:39 -0500 | [diff] [blame] | 635 | if (daemon(0, 0)) { |
| 636 | perror("nstat: daemon"); |
| 637 | exit(-1); |
| 638 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Stephen Hemminger | 896ebd6 | 2009-12-26 10:21:13 -0800 | [diff] [blame] | 648 | if ((hist_name = getenv("NSTAT_HISTORY")) == NULL) { |
| 649 | hist_name = malloc(128); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 650 | sprintf(hist_name, "/tmp/.nstat.u%d", getuid()); |
Stephen Hemminger | 896ebd6 | 2009-12-26 10:21:13 -0800 | [diff] [blame] | 651 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
Dan McGee | 9a23077 | 2011-08-31 12:16:36 -0700 | [diff] [blame] | 682 | long uptime = -1; |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 683 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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"); |
Phil Sutter | d572ed4 | 2015-11-28 01:00:04 +0100 | [diff] [blame] | 691 | if (ftruncate(fileno(hist_fp), 0) < 0) |
| 692 | perror("nstat: ftruncate"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 && |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 703 | (connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0 |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 704 | || (strcpy(sun.sun_path+1, "nstat0"), |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 705 | connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 706 | && verify_forging(fd) == 0) { |
| 707 | FILE *sfp = fdopen(fd, "r"); |
Stephen Hemminger | acd1e43 | 2016-03-21 11:56:36 -0700 | [diff] [blame] | 708 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 709 | 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 Liu | 45a0dc1 | 2016-09-05 11:35:02 +0800 | [diff] [blame] | 726 | load_sctp_snmp(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 727 | 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 Sutter | d572ed4 | 2015-11-28 01:00:04 +0100 | [diff] [blame] | 738 | if (ftruncate(fileno(hist_fp), 0) < 0) |
| 739 | perror("nstat: ftruncate"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 740 | rewind(hist_fp); |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 741 | |
| 742 | json_output = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 743 | dump_kern_db(hist_fp, 1); |
Stephen Hemminger | 404582c | 2013-09-24 11:54:45 -0700 | [diff] [blame] | 744 | fclose(hist_fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 745 | } |
| 746 | exit(0); |
| 747 | } |