]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/nstat.c
Merge branch 'master' of git://git.kernel.org/pub/scm/network/iproute2/iproute2-next
[mirror_iproute2.git] / misc / nstat.c
index a9e0f20789e3c15036ca45b7f7315c2876a9c2a3..23113b223b22d5db1c82bf28c26105936ee6776b 100644 (file)
 
 #include <json_writer.h>
 #include <SNAPSHOT.h>
+#include "utils.h"
 
 int dump_zeros;
 int reset_history;
 int ignore_history;
 int no_output;
 int json_output;
-int pretty;
 int no_update;
 int scan_interval;
 int time_constant;
@@ -75,6 +75,11 @@ static int net_snmp6_open(void)
        return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
 }
 
+static int net_sctp_snmp_open(void)
+{
+       return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp");
+}
+
 struct nstat_ent {
        struct nstat_ent *next;
        char             *id;
@@ -95,7 +100,7 @@ static int useless_number(const char *id)
 {
        int i;
 
-       for (i = 0; i < sizeof(useless_numbers)/sizeof(*useless_numbers); i++)
+       for (i = 0; i < ARRAY_SIZE(useless_numbers); i++)
                if (strcmp(id, useless_numbers[i]) == 0)
                        return 1;
        return 0;
@@ -172,12 +177,14 @@ static int count_spaces(const char *line)
 
 static void load_ugly_table(FILE *fp)
 {
-       char buf[4096];
+       char *buf = NULL;
+       size_t buflen = 0;
+       ssize_t nread;
        struct nstat_ent *db = NULL;
        struct nstat_ent *n;
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               char idbuf[sizeof(buf)];
+       while ((nread = getline(&buf, &buflen, fp)) != -1) {
+               char idbuf[4096];
                int  off;
                char *p;
                int count1, count2, skip = 0;
@@ -213,7 +220,8 @@ static void load_ugly_table(FILE *fp)
                        p = next;
                }
                n = db;
-               if (fgets(buf, sizeof(buf), fp) == NULL)
+               nread = getline(&buf, &buflen, fp);
+               if (nread == -1)
                        abort();
                count2 = count_spaces(buf);
                if (count2 > count1)
@@ -232,6 +240,7 @@ static void load_ugly_table(FILE *fp)
                                n = n->next;
                } while (p > buf + off + 2);
        }
+       free(buf);
 
        while (db) {
                n = db;
@@ -246,6 +255,16 @@ static void load_ugly_table(FILE *fp)
        }
 }
 
+static void load_sctp_snmp(void)
+{
+       FILE *fp = fdopen(net_sctp_snmp_open(), "r");
+
+       if (fp) {
+               load_good_table(fp);
+               fclose(fp);
+       }
+}
+
 static void load_snmp(void)
 {
        FILE *fp = fdopen(net_snmp_open(), "r");
@@ -284,6 +303,7 @@ static void dump_kern_db(FILE *fp, int to_hist)
 
        h = hist_db;
        if (jw) {
+               jsonw_start_object(jw);
                jsonw_pretty(jw, pretty);
                jsonw_name(jw, info_source);
                jsonw_start_object(jw);
@@ -316,6 +336,8 @@ static void dump_kern_db(FILE *fp, int to_hist)
        }
 
        if (jw) {
+               jsonw_end_object(jw);
+
                jsonw_end_object(jw);
                jsonw_destroy(&jw);
        }
@@ -328,6 +350,7 @@ static void dump_incr_db(FILE *fp)
 
        h = hist_db;
        if (jw) {
+               jsonw_start_object(jw);
                jsonw_pretty(jw, pretty);
                jsonw_name(jw, info_source);
                jsonw_start_object(jw);
@@ -363,6 +386,8 @@ static void dump_incr_db(FILE *fp)
        }
 
        if (jw) {
+               jsonw_end_object(jw);
+
                jsonw_end_object(jw);
                jsonw_destroy(&jw);
        }
@@ -384,6 +409,7 @@ static void update_db(int interval)
        load_netstat();
        load_snmp6();
        load_snmp();
+       load_sctp_snmp();
 
        h = kern_db;
        kern_db = n;
@@ -443,6 +469,7 @@ static void server_loop(int fd)
        load_netstat();
        load_snmp6();
        load_snmp();
+       load_sctp_snmp();
 
        for (;;) {
                int status;
@@ -501,18 +528,18 @@ static void usage(void) __attribute__((noreturn));
 static void usage(void)
 {
        fprintf(stderr,
-"Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
-"   -h, --help         this message\n"
-"   -a, --ignore       ignore history\n"
-"   -d, --scan=SECS    sample every statistics every SECS\n"
-"   -j, --json          format output in JSON\n"
-"   -n, --nooutput     do history only\n"
-"   -p, --pretty        pretty print\n"
-"   -r, --reset                reset history\n"
-"   -s, --noupdate     don\'t update history\n"
-"   -t, --interval=SECS        report average over the last SECS\n"
-"   -V, --version      output version information\n"
-"   -z, --zeros                show entries with zero activity\n");
+               "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
+               "   -h, --help          this message\n"
+               "   -a, --ignore        ignore history\n"
+               "   -d, --scan=SECS     sample every statistics every SECS\n"
+               "   -j, --json          format output in JSON\n"
+               "   -n, --nooutput      do history only\n"
+               "   -p, --pretty        pretty print\n"
+               "   -r, --reset         reset history\n"
+               "   -s, --noupdate      don't update history\n"
+               "   -t, --interval=SECS report average over the last SECS\n"
+               "   -V, --version       output version information\n"
+               "   -z, --zeros         show entries with zero activity\n");
        exit(-1);
 }
 
@@ -682,12 +709,18 @@ int main(int argc, char *argv[])
            && verify_forging(fd) == 0) {
                FILE *sfp = fdopen(fd, "r");
 
-               load_good_table(sfp);
-               if (hist_db && source_mismatch) {
-                       fprintf(stderr, "nstat: history is stale, ignoring it.\n");
-                       hist_db = NULL;
+               if (!sfp) {
+                       fprintf(stderr, "nstat: fdopen failed: %s\n",
+                               strerror(errno));
+                       close(fd);
+               } else {
+                       load_good_table(sfp);
+                       if (hist_db && source_mismatch) {
+                               fprintf(stderr, "nstat: history is stale, ignoring it.\n");
+                               hist_db = NULL;
+                       }
+                       fclose(sfp);
                }
-               fclose(sfp);
        } else {
                if (fd >= 0)
                        close(fd);
@@ -699,6 +732,7 @@ int main(int argc, char *argv[])
                load_netstat();
                load_snmp6();
                load_snmp();
+               load_sctp_snmp();
                if (info_source[0] == 0)
                        strcpy(info_source, "kernel");
        }