How to convert confd_decimal64 to double?

Hi,

I am trying to read few statistics through MAAPI and write it to a file.
But there are few confd defined data types like confd_decimal64.

I am trying to fetch the data from confd in the following way.

struct confd_decimal64 d64;
if (maapi_get_decimal64_elem(sock, th, &d64, "/interface/stat/average") == CONFD_OK)`

Is there a function to convert confd_decimal64 to double?

Also is there a way to convert the enum to string?

int32_t state;
maapi_get_enum_value_elem(sock, th, &state, "/interface/stat/state");

When the above stats are polled through cli, I am able to see them giving processed output.

#show interface
stat average 3572384792.74
stat state up

Is there a confd API which can be used for my case as well?

Thanks

You can use the confd_cmd tool to read values using maapi. For example:

confd_cmd -dd -c 'maapi_get /interface/stat/average'

The confd_cmd tool source code is available at $CONFD_DIR/src/confd/tools/confd_cmd.c
So if you want to write your own program that does the same thing, you can reuse the “maapi_get” implementation that use maapi_get_elem() and confd_val2str() to get a string representation of any value:

static void do_maapi_get(char *argv[]) /* <key> */
{
    confd_value_t val;

    OK(maapi_get_elem(ms, mtid, &val, argv[0]));
    print_value(&val, NULL, argv[0], "\n");
    confd_free_value(&val);
}

static void print_value(confd_value_t *valp,
                        struct confd_cs_node *start, char *path, char *eol)
{
    char tmpbuf[BUFSIZ];
    struct confd_cs_node *node = NULL;

    /* Don't call with start NULL and path "", gives ugly error */
    if (start != NULL || path != NULL) {
        node = confd_cs_node_cd(start, path ? path : "");
    }

    if ((node == NULL) ||
        (confd_val2str(node->info.type, valp, tmpbuf, BUFSIZ) == CONFD_ERR)) {
        confd_pp_value(tmpbuf, BUFSIZ, valp);
    }
    printf("%s%s", tmpbuf, eol);
}
1 Like