]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: add a way to limit the number of events received by "watch"
authorVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 09:10:55 +0000 (11:10 +0200)
committerVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 09:10:55 +0000 (11:10 +0200)
src/client/lldpcli.8.in
src/client/show.c

index fb1ca7a4f7580c160450ab8ae608305962007899..b06e4b8c4598417f729e19ff5a77c16fe8dad4c1 100644 (file)
@@ -142,6 +142,7 @@ view.
 .Op ports Ar ethX Op ,...
 .Op Cd details | summary
 .Op Cd hidden
+.Op Cd limit Ar X
 .Bd -ragged -offset XXXXXX
 Watch for any neighbor changes and report them as soon as they
 happen. When specifying ports, the changes are only reported when
@@ -149,7 +150,11 @@ happening on the given ports.
 .Cd hidden , summary
 and
 .Cd details
-have the same meaning than previously described.
+have the same meaning than previously described. If
+.Cd limit
+is specificed,
+.Nm
+will exit after receiving the specified number of events.
 .Ed
 
 .Cd show configuration
index f61fbff3771561a24d6cad54403d19cbd9625de7..57fc8b20eda0a752a97c437f31c7422debf2697f 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "client.h"
 #include <string.h>
+#include <limits.h>
 
 /**
  * Show neighbors.
@@ -117,6 +118,7 @@ cmd_show_configuration(struct lldpctl_conn_t *conn, struct writer *w,
 struct watcharg {
        struct cmd_env *env;
        struct writer *w;
+       size_t nb;
 };
 
 /**
@@ -175,6 +177,7 @@ watchcb(lldpctl_conn_t *conn,
            cmdenv_get(env, "detailed")?DISPLAY_DETAILS:
            DISPLAY_NORMAL, protocol);
        tag_end(w);
+       wa->nb++;
 }
 
 /**
@@ -187,8 +190,21 @@ cmd_watch_neighbors(struct lldpctl_conn_t *conn, struct writer *w,
        int watch = 1;
        struct watcharg wa = {
                .env = env,
-               .w = w
+               .w = w,
+               .nb = 0
        };
+       const char *limit_str = cmdenv_get(env, "limit");
+       size_t limit = 0;
+
+       if (limit_str) {
+               const char *errstr;
+               limit = strtonum(limit_str, 1, LLONG_MAX, &errstr);
+               if (errstr != NULL) {
+                       log_warnx("lldpctl", "specified limit (%s) is %s and ignored",
+                           limit_str, errstr);
+               }
+       }
+
        log_debug("lldpctl", "watch for neighbor changes");
        if (lldpctl_watch_callback(conn, watchcb, &wa) < 0) {
                log_warnx("lldpctl", "unable to watch for neighbors. %s",
@@ -201,6 +217,8 @@ cmd_watch_neighbors(struct lldpctl_conn_t *conn, struct writer *w,
                            lldpctl_last_strerror(conn));
                        watch = 0;
                }
+               if (limit > 0 && wa.nb >= limit)
+                       watch = 0;
        }
        return 0;
 }
@@ -339,11 +357,19 @@ register_commands_watch(struct cmd_node *root)
                "Monitor neighbor changes",
                NULL, NULL, NULL);
 
-       /* Neighbors data */
        commands_new(watch,
            NEWLINE,
            "Monitor neighbors change",
            NULL, cmd_watch_neighbors, NULL);
 
+       commands_new(
+               commands_new(watch,
+                   "limit",
+                   "Don't show more than X events",
+                   cmd_check_no_env, NULL, "limit"),
+               NULL,
+               "Stop after getting X events",
+               NULL, cmd_store_env_value_and_pop2, "limit");
+
        register_common_commands(watch, 1);
 }