Add a "resetcounters" command to ipsec, clearing global or connection counters
authorMartin Willi <martin@revosec.ch>
Fri, 15 Mar 2013 09:55:22 +0000 (10:55 +0100)
committerMartin Willi <martin@revosec.ch>
Fri, 15 Mar 2013 09:55:22 +0000 (10:55 +0100)
src/ipsec/_ipsec.in
src/libcharon/plugins/stroke/stroke_counter.c
src/libcharon/plugins/stroke/stroke_counter.h
src/libcharon/plugins/stroke/stroke_socket.c
src/stroke/stroke.c
src/stroke/stroke_keywords.h
src/stroke/stroke_keywords.txt
src/stroke/stroke_msg.h

index 8b6ad66..6b406f6 100644 (file)
@@ -59,7 +59,8 @@ case "$1" in
        echo "  listalgs|listpubkeys|listcerts [--utc]"
        echo "  listcacerts|listaacerts|listocspcerts [--utc]"
        echo "  listacerts|listgroups|listcainfos [--utc]"
-       echo "  listcrls|listocsp|listcards|listplugins|listcounters|listall [--utc]"
+       echo "  listcrls|listocsp|listcards|listplugins|listall [--utc]"
+       echo "  listcounters|resetcounters [name]"
        echo "  leases [<poolname> [<address>]]"
        echo "  rereadsecrets|rereadgroups"
        echo "  rereadcacerts|rereadaacerts|rereadocspcerts"
@@ -149,10 +150,10 @@ leases)
 listalgs|listpubkeys|listplugins|\
 listcerts|listcacerts|listaacerts|\
 listacerts|listgroups|listocspcerts|\
-listcainfos|listcrls|listocsp|listcounters|listall|\
+listcainfos|listcrls|listocsp|listall|\
 rereadsecrets|rereadcacerts|rereadaacerts|\
 rereadacerts|rereadocspcerts|rereadcrls|\
-rereadall|purgeocsp)
+rereadall|purgeocsp|listcounters|resetcounters)
        op="$1"
        rc=7
        shift
index e64415f..ff4746b 100644 (file)
@@ -336,6 +336,27 @@ METHOD(stroke_counter_t, print, void,
        }
 }
 
+METHOD(stroke_counter_t, reset, void,
+       private_stroke_counter_t *this, char *name)
+{
+       this->lock->lock(this->lock);
+       if (name)
+       {
+               entry_t *entry;
+
+               entry = this->conns->remove(this->conns, name);
+               if (entry)
+               {
+                       destroy_entry(entry);
+               }
+       }
+       else
+       {
+               memset(&this->counter, 0, sizeof(this->counter));
+       }
+       this->lock->unlock(this->lock);
+}
+
 METHOD(stroke_counter_t, destroy, void,
        private_stroke_counter_t *this)
 {
@@ -370,6 +391,7 @@ stroke_counter_t *stroke_counter_create()
                                .message = _message_hook,
                        },
                        .print = _print,
+                       .reset = _reset,
                        .destroy = _destroy,
                },
                .conns = hashtable_create((hashtable_hash_t)hash,
index b39fcb8..fecf39f 100644 (file)
@@ -92,6 +92,13 @@ struct stroke_counter_t {
        void (*print)(stroke_counter_t *this, FILE *out, char *name);
 
        /**
+        * Reset global or connection specific counters.
+        *
+        * @param name          name of connection counters to reset, NULL for global
+        */
+       void (*reset)(stroke_counter_t *this, char *name);
+
+       /**
         * Destroy a stroke_counter_t.
         */
        void (*destroy)(stroke_counter_t *this);
index ebb3c72..7d41682 100644 (file)
@@ -509,7 +509,14 @@ static void stroke_counters(private_stroke_socket_t *this,
 {
        pop_string(msg, &msg->counters.name);
 
-       this->counter->print(this->counter, out, msg->counters.name);
+       if (msg->counters.reset)
+       {
+               this->counter->reset(this->counter, msg->counters.name);
+       }
+       else
+       {
+               this->counter->print(this->counter, out, msg->counters.name);
+       }
 }
 
 /**
@@ -675,6 +682,7 @@ static job_requeue_t process(stroke_job_context_t *ctx)
                        break;
                case STR_COUNTERS:
                        stroke_counters(this, msg, out);
+                       break;
                default:
                        DBG1(DBG_CFG, "received unknown stroke");
                        break;
index 1be275c..c8c8e7c 100644 (file)
@@ -364,13 +364,15 @@ static int user_credentials(char *name, char *user, char *pass)
        return send_stroke_msg(&msg);
 }
 
-static int counters(char *name)
+static int counters(int reset, char *name)
 {
        stroke_msg_t msg;
 
        msg.type = STR_COUNTERS;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.counters.name = push_string(&msg, name);
+       msg.counters.reset = reset;
+
        return send_stroke_msg(&msg);
 }
 
@@ -605,14 +607,9 @@ int main(int argc, char *argv[])
                        res = user_credentials(argv[2], argv[3], argc > 4 ? argv[4] : NULL);
                        break;
                case STROKE_COUNTERS:
-                       if (argc > 2)
-                       {
-                               res = counters(argv[2]);
-                       }
-                       else
-                       {
-                               res = counters(NULL);
-                       }
+               case STROKE_COUNTERS_RESET:
+                       res = counters(token->kw == STROKE_COUNTERS_RESET,
+                                                  argc > 2 ? argv[2] : NULL);
                        break;
                default:
                        exit_usage(NULL);
index 12abce1..f5979a0 100644 (file)
@@ -59,6 +59,7 @@ typedef enum {
        STROKE_MEMUSAGE,
        STROKE_USER_CREDS,
        STROKE_COUNTERS,
+       STROKE_COUNTERS_RESET,
 } stroke_keyword_t;
 
 #define STROKE_LIST_FIRST              STROKE_LIST_PUBKEYS
index 12c5909..5d2ebd9 100644 (file)
@@ -66,3 +66,4 @@ leases,          STROKE_LEASES
 memusage,        STROKE_MEMUSAGE
 user-creds,      STROKE_USER_CREDS
 listcounters,    STROKE_COUNTERS
+resetcounters,   STROKE_COUNTERS_RESET
index 494acba..c415a92 100644 (file)
@@ -223,7 +223,7 @@ struct stroke_msg_t {
                STR_MEMUSAGE,
                /* set username and password for a connection */
                STR_USER_CREDS,
-               /* print counters */
+               /* print/reset counters */
                STR_COUNTERS,
                /* more to come */
        } type;
@@ -357,6 +357,8 @@ struct stroke_msg_t {
 
                /* data for STR_COUNTERS */
                struct {
+                       /* reset or print counters? */
+                       int reset;
                        char *name;
                } counters;
        };