]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpcli: global stats command
authorroopa <roopa@cumulusnetworks.com>
Mon, 6 May 2013 13:39:54 +0000 (06:39 -0700)
committerVincent Bernat <bernat@luffy.cx>
Mon, 6 May 2013 19:31:56 +0000 (21:31 +0200)
Signed-off-by: Roopa Prabhu <roopa@cumulusnetworks.com>
src/client/display.c
src/client/show.c

index c548994f554264ffd71e240634f2913cbbac5f57..648fd04850b83c74985c256d65f5b2b32776df1b 100644 (file)
@@ -567,56 +567,50 @@ display_interfaces(lldpctl_conn_t *conn, struct writer *w,
 }
 
 void
-display_interface_stats(lldpctl_conn_t *conn, struct writer *w,
-               lldpctl_atom_t *port)
+display_stat(struct writer *w, const char *tag, const char *descr,
+       long unsigned int cnt)
 {
        char buf[20];
 
+       tag_start(w, tag, descr);
+       memset(buf, 0, sizeof(buf));
+       snprintf(buf, sizeof(buf), "%lu", cnt );
+       tag_attr(w, tag, "", buf);
+       tag_end(w);
+}
+
+void
+display_interface_stats(lldpctl_conn_t *conn, struct writer *w,
+               lldpctl_atom_t *port)
+{
        tag_start(w, "interface", "Interface");
        tag_attr(w, "name", "",
            lldpctl_atom_get_str(port, lldpctl_k_port_name));
 
-       tag_start(w, "tx", "Transmitted");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_tx_cnt));
-       tag_attr(w, "tx", "", buf);
-       tag_end(w);
+       display_stat(w, "tx", "Transmitted",
+                       lldpctl_atom_get_int(port, lldpctl_k_tx_cnt));
+       display_stat(w, "rx", "Received",
+                       lldpctl_atom_get_int(port, lldpctl_k_rx_cnt));
 
-       tag_start(w, "rx", "Received");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_rx_cnt));
-       tag_attr(w, "rx", "", buf);
-       tag_end(w);
+       display_stat(w, "rx_discarded_cnt", "Discarded",
+                       lldpctl_atom_get_int(port,
+                       lldpctl_k_rx_discarded_cnt));
 
-       tag_start(w, "rx_discarded_cnt", "Discarded");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_rx_discarded_cnt));
-       tag_attr(w, "rx_discarded_cnt", "", buf);
-       tag_end(w);
+       display_stat(w, "rx_unrecognized_cnt", "Unrecognized",
+                       lldpctl_atom_get_int(port,
+                       lldpctl_k_rx_unrecognized_cnt));
 
-       tag_start(w, "rx_unrecognized_cnt", "Unrecognized");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_rx_unrecognized_cnt));
-       tag_attr(w, "rx_unrecognized_cnt", "", buf);
-       tag_end(w);
+       display_stat(w, "ageout_cnt", "Ageout",
+                       lldpctl_atom_get_int(port,
+                       lldpctl_k_ageout_cnt));
 
-       tag_start(w, "ageout_cnt", "Ageout");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_ageout_cnt));
-       tag_attr(w, "ageout_cnt", "", buf);
-       tag_end(w);
+       display_stat(w, "insert_cnt", "Inserted",
+                       lldpctl_atom_get_int(port,
+                       lldpctl_k_insert_cnt));
 
-       tag_start(w, "insert_cnt", "Inserted");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_insert_cnt));
-       tag_attr(w, "insert_cnt", "", buf);
-       tag_end(w);
-
-       tag_start(w, "delete_cnt", "Deleted");
-       memset(buf, 0, sizeof(buf));
-       snprintf(buf, sizeof(buf), "%lu",  lldpctl_atom_get_int(port, lldpctl_k_delete_cnt));
-       tag_attr(w, "delete_cnt", "", buf);
-       tag_end(w);
+       display_stat(w, "delete_cnt", "Deleted",
+                       lldpctl_atom_get_int(port,
+                       lldpctl_k_delete_cnt));
 
        tag_end(w);
 }
@@ -635,12 +629,60 @@ display_interfaces_stats(lldpctl_conn_t *conn, struct writer *w,
     struct cmd_env *env)
 {
        lldpctl_atom_t *iface;
+       int summary = 0;
+       u_int64_t h_tx_cnt = 0;
+       u_int64_t h_rx_cnt = 0;
+       u_int64_t h_rx_discarded_cnt = 0;
+       u_int64_t h_rx_unrecognized_cnt = 0;
+       u_int64_t h_ageout_cnt = 0;
+       u_int64_t h_insert_cnt = 0;
+       u_int64_t h_delete_cnt = 0;
+       char buf[20];
+
+       if (cmdenv_get(env, "summary"))
+               summary = 1;
 
-       tag_start(w, "lldp", "LLDP statistics");
+       tag_start(w, "lldp", (summary ? "LLDP Global statistics" :
+               "LLDP statistics"));
        while ((iface = cmd_iterate_on_interfaces(conn, env))) {
                lldpctl_atom_t *port;
                port      = lldpctl_get_port(iface);
-               display_interface_stats(conn, w, port);
+               if (!summary)
+                       display_interface_stats(conn, w, port);
+               else {
+                       h_tx_cnt += lldpctl_atom_get_int(port,
+                                       lldpctl_k_tx_cnt);
+                       h_rx_cnt += lldpctl_atom_get_int(port,
+                                       lldpctl_k_rx_cnt);
+                       h_rx_discarded_cnt += lldpctl_atom_get_int(port,
+                                       lldpctl_k_rx_discarded_cnt);
+                       h_rx_unrecognized_cnt += lldpctl_atom_get_int(port,
+                                       lldpctl_k_rx_unrecognized_cnt);
+                       h_ageout_cnt += lldpctl_atom_get_int(port,
+                                               lldpctl_k_ageout_cnt);
+                       h_insert_cnt += lldpctl_atom_get_int(port,
+                                               lldpctl_k_insert_cnt);
+                       h_delete_cnt += lldpctl_atom_get_int(port,
+                                               lldpctl_k_delete_cnt);
+               }
+       }
+
+       if (summary) {
+               tag_start(w, "sum", "Sum of stats over all interfaces");
+               display_stat(w, "tx", "Transmitted", h_tx_cnt);
+               display_stat(w, "rx", "Received", h_rx_cnt);
+               display_stat(w, "rx_discarded_cnt", "Discarded",
+                       h_rx_discarded_cnt);
+
+               display_stat(w, "rx_unrecognized_cnt", "Unrecognized",
+                       h_rx_unrecognized_cnt);
+
+               display_stat(w, "ageout_cnt", "Ageout", h_ageout_cnt);
+
+               display_stat(w, "insert_cnt", "Inserted", h_insert_cnt);
+
+               display_stat(w, "delete_cnt", "Deleted", h_delete_cnt);
+               tag_end(w);
        }
        tag_end(w);
 }
index ff406aaf15a2348d4b11ac5198efda637b4a5d04..715dc4ac9aa46f4aca283cace6bbea019feba436 100644 (file)
@@ -51,6 +51,7 @@ cmd_show_neighbors(struct lldpctl_conn_t *conn, struct writer *w,
  *
  * The environment will contain the following keys:
  *  - C{ports} list of ports we want to restrict showing.
+ *  - C{summary} summary of stats
  */
 static int
 cmd_show_interface_stats(struct lldpctl_conn_t *conn, struct writer *w,
@@ -58,8 +59,11 @@ cmd_show_interface_stats(struct lldpctl_conn_t *conn, struct writer *w,
 {
        log_debug("lldpctl", "show stats data");
        if (cmdenv_get(env, "ports"))
-               log_debug("lldpctl", "restrict to the following ports: %s",
+               log_debug("lldpctl", "*restrict to the following ports: %s",
                    cmdenv_get(env, "ports"));
+       if (cmdenv_get(env, "summary"))
+               log_debug("lldpctl", "show sum of stats accross ports ",
+                   cmdenv_get(env, "summary"));
 
        display_interfaces_stats(conn, w, env);
 
@@ -176,6 +180,18 @@ register_common_commands(struct cmd_node *root)
        cmd_restrict_ports(root);
 }
 
+/**
+ * Register sub command summary
+ */
+void
+register_summary_command(struct cmd_node *root)
+{
+       commands_new(root,
+                       "summary",
+                       "With less details",
+                       cmd_check_no_detailed_nor_summary, cmd_store_env_and_pop, "summary");
+}
+
 /**
  * Register subcommands to `show`
  *
@@ -216,6 +232,7 @@ register_commands_show(struct cmd_node *root)
            NULL, cmd_show_interface_stats, NULL);
 
        cmd_restrict_ports(stats);
+       register_summary_command(stats);
 
        /* Register "show configuration" and "show running-configuration" */
        commands_new(