Browse code

counters: cnt.help and api update

- new RPC for displaying a counter description, e.g.:
$ sercmd cnt.help tcp local_reject
number of rejected incoming connections.

- counter_register() update (takes an extra parameter: the counter
description)

Andrei Pelinescu-Onciul authored on 09/08/2010 15:09:03
Showing 1 changed files
... ...
@@ -92,6 +92,10 @@ static const char* cnt_grp_get_all_doc[] = {
92 92
 	"list all counter names and values in a specified group", 0
93 93
 };
94 94
 
95
+static void cnt_help_rpc(rpc_t* rpc, void* ctx);
96
+static const char* cnt_help_doc[] = {
97
+	"print the description of a counter (group and counter name required).", 0
98
+};
95 99
 
96 100
 
97 101
 
... ...
@@ -102,6 +106,7 @@ static rpc_export_t counters_rpc[] = {
102 102
 	{"cnt.grps_list", cnt_grps_list_rpc, cnt_grps_list_doc, RET_ARRAY },
103 103
 	{"cnt.var_list", cnt_var_list_rpc, cnt_var_list_doc, RET_ARRAY },
104 104
 	{"cnt.grp_get_all", cnt_grp_get_all_rpc, cnt_grp_get_all_doc, 0 },
105
+	{"cnt.help", cnt_help_rpc, cnt_help_doc, 0},
105 106
 	{ 0, 0, 0, 0}
106 107
 };
107 108
 
... ...
@@ -131,7 +136,7 @@ static int add_script_counter(modparam_t type, void* val)
131 131
 		goto error;
132 132
 	}
133 133
 	name = (char*) val;
134
-	ret = counter_register(&h, cnt_script_grp, name, 0, 0, 0, 0);
134
+	ret = counter_register(&h, cnt_script_grp, name, 0, 0, 0, "script counter", 0);
135 135
 	if (ret < 0) {
136 136
 		if (ret == -2) {
137 137
 			ERR("counter %s.%s already registered\n", cnt_script_grp, name);
... ...
@@ -365,4 +370,30 @@ static void cnt_grp_get_all_rpc(rpc_t* rpc, void* c)
365 365
 	counter_iterate_grp_vars(group, rpc_print_name_val, &packed_params);
366 366
 }
367 367
 
368
+
369
+
370
+static void cnt_help_rpc(rpc_t* rpc, void* ctx)
371
+{
372
+	char* group;
373
+	char* name;
374
+	char* desc;
375
+	counter_handle_t h;
376
+	
377
+	if (rpc->scan(ctx, "ss", &group, &name) < 2) {
378
+		/* rpc->fault(c, 400, "group and counter name required"); */
379
+		return;
380
+	}
381
+	if (counter_lookup(&h, group, name) < 0) {
382
+		rpc->fault(ctx, 400, "non-existent counter %s.%s\n", group, name);
383
+		return;
384
+	}
385
+	desc = counter_get_doc(h);
386
+	if (desc)
387
+		rpc->add(ctx, "s", desc);
388
+	else
389
+		rpc->fault(ctx, 400, "no description for counter %s.%s\n",
390
+					group, name);
391
+	return;
392
+}
393
+
368 394
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */