Change in libosmocore[master]: CTRL: expose stat_item groups on CTRL

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

neels gerrit-no-reply at lists.osmocom.org
Mon Aug 23 22:03:22 UTC 2021


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/25224 )


Change subject: CTRL: expose stat_item groups on CTRL
......................................................................

CTRL: expose stat_item groups on CTRL

Expose all stat items as RO variables of the form

  stat_item.last.group_name.N.item_name

Include the 'last' token to ease future extension, like 'max'.
Put this token in the beginning, similarly to rate_ctr variables, which
begin with 'per_sec', 'per_hour', ...

Related: SYS#5542
Change-Id: Idace66b37492fe96b2f2e133a69cac7960ca279c
---
M src/ctrl/control_if.c
1 file changed, 100 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/24/25224/1

diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 0a893ba..782f369 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -53,6 +53,7 @@
 
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/rate_ctr.h>
+#include <osmocom/core/stat_item.h>
 #include <osmocom/core/select.h>
 #include <osmocom/core/counter.h>
 #include <osmocom/core/talloc.h>
@@ -729,6 +730,102 @@
 	return 0;
 }
 
+/* Expose all stat_item groups on CTRL, as read-only variables of the form:
+ * stat_item.(last|...).group_name.N.item_name
+ */
+CTRL_CMD_DEFINE(stat_item, "stat_item *");
+static int get_stat_item(struct ctrl_cmd *cmd, void *data)
+{
+	char *dup;
+	char *saveptr;
+	char *value_type;
+	char *group_name;
+	char *group_idx_str;
+	int idx;
+	char *item_name;
+	char *tmp;
+	int32_t val;
+	struct osmo_stat_item_group *statg;
+	const struct osmo_stat_item *stat_item;
+
+	/* cmd will be freed in control_if.c after handling here, so no need to free the dup string. */
+	dup = talloc_strdup(cmd, cmd->variable);
+	if (!dup) {
+		cmd->reply = "OOM";
+		return CTRL_CMD_ERROR;
+	}
+
+	/* Split off the first "stat_item." part */
+	tmp = strtok_r(dup, ".", &saveptr);
+	if (!tmp || strcmp(tmp, "stat_item") != 0)
+		goto format_error;
+
+	/* Split off the "last." part (validated further below) */
+	value_type = strtok_r(NULL, ".", &saveptr);
+	if (!value_type)
+		goto format_error;
+
+	/* Split off the "group_name." part */
+	group_name = strtok_r(NULL, ".", &saveptr);
+	group_idx_str = strtok_r(NULL, ".", &saveptr);
+	if (!group_name || !group_idx_str)
+		goto format_error;
+
+	/* Split off the "N." part */
+	idx = atoi(group_idx_str);
+	statg = osmo_stat_item_get_group_by_name_idx(group_name, idx);
+	if (!statg) {
+		cmd->reply = "Stat group with given name and index not found";
+		return CTRL_CMD_ERROR;
+	}
+
+	/* Split off the "item_name" part */
+	item_name = strtok_r(NULL, ".", &saveptr);
+	if (!item_name)
+		goto format_error;
+	stat_item = osmo_stat_item_get_by_name(statg, item_name);
+	if (!stat_item) {
+		cmd->reply = "No such stat item found";
+		return CTRL_CMD_ERROR;
+	}
+
+	tmp = strtok_r(NULL, "", &saveptr);
+	if (tmp) {
+		cmd->reply = "Garbage after stat item name";
+		return CTRL_CMD_ERROR;
+	}
+
+	if (!strcmp(value_type, "last")) {
+		val = osmo_stat_item_get_last(stat_item);
+	} else {
+		goto format_error;
+	}
+
+	cmd->reply = talloc_asprintf(cmd, "%"PRIu32, val);
+	if (!cmd->reply) {
+		cmd->reply = "OOM";
+		return CTRL_CMD_ERROR;
+	}
+
+	return CTRL_CMD_REPLY;
+
+format_error:
+	cmd->reply = "Stat item must be of form stat_item.type.group_name.N.item_name,"
+		" e.g. 'stat_item.last.bsc.0.msc_num:connected'";
+	return CTRL_CMD_ERROR;
+}
+
+static int set_stat_item(struct ctrl_cmd *cmd, void *data)
+{
+	cmd->reply = "Stat items are read-only";
+	return CTRL_CMD_ERROR;
+}
+
+static int verify_stat_item(struct ctrl_cmd *cmd, const char *value, void *data)
+{
+	return 0;
+}
+
 /* counter */
 CTRL_CMD_DEFINE(counter, "counter *");
 static int get_counter(struct ctrl_cmd *cmd, void *data)
@@ -816,6 +913,9 @@
 	ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr);
 	if (ret)
 		goto err_vec;
+	ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_stat_item);
+	if (ret)
+		goto err_vec;
 	ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter);
 	if (ret)
 		goto err_vec;

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/25224
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Idace66b37492fe96b2f2e133a69cac7960ca279c
Gerrit-Change-Number: 25224
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210823/5fb0b0eb/attachment.htm>


More information about the gerrit-log mailing list