Code

libcollectdclient: Implement the lcc_listval_with_selection() function.
authorFlorian Forster <octo@huhu.verplant.org>
Thu, 17 Mar 2011 08:42:38 +0000 (09:42 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Thu, 17 Mar 2011 08:42:38 +0000 (09:42 +0100)
This function exports the new selection feature of the LISTVAL command
with a straight forward C API. The existing lcc_listval() function
remains for backwards compatibility (and ease of use) and will
internally use the new and more complex function.

src/libcollectdclient/client.c
src/libcollectdclient/client.h

index 3eb0d05536721a7d5b154ddf505c123321ea9fcf..ab7548804407088410bcaa2db22ca51268eeaf16 100644 (file)
@@ -856,9 +856,15 @@ int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
 
 /* TODO: Implement lcc_putnotif */
 
-int lcc_listval (lcc_connection_t *c, /* {{{ */
+int lcc_listval_with_selection (lcc_connection_t *c, /* {{{ */
+    const char *re_host,
+    const char *re_plugin,
+    const char *re_plugin_instance,
+    const char *re_type,
+    const char *re_type_instance,
     lcc_identifier_t **ret_ident, size_t *ret_ident_num)
 {
+  char command[1024]; /* Buffer size copied from src/unixsock.c */
   lcc_response_t res;
   size_t i;
   int status;
@@ -875,7 +881,28 @@ int lcc_listval (lcc_connection_t *c, /* {{{ */
     return (-1);
   }
 
-  status = lcc_sendreceive (c, "LISTVAL", &res);
+  strncpy (command, "LISTVAL", sizeof (command));
+
+#define ADD_SELECTOR(field)                                                  \
+  if (re_##field != NULL)                                                    \
+  {                                                                          \
+    char tmp_re[sizeof (command)];                                           \
+      char tmp_cmd[sizeof (command)];                                        \
+      snprintf (tmp_cmd, sizeof (tmp_cmd), "%s %s=%s", command, #field,      \
+          lcc_strescape (tmp_re, re_##field, sizeof (tmp_re)));              \
+      memcpy (command, tmp_cmd, sizeof (command));                           \
+      command[sizeof (command) - 1] = 0;                                     \
+  }
+
+  ADD_SELECTOR (host)
+  ADD_SELECTOR (plugin)
+  ADD_SELECTOR (plugin_instance)
+  ADD_SELECTOR (type)
+  ADD_SELECTOR (type_instance)
+
+#undef ADD_SELECTOR
+
+  status = lcc_sendreceive (c, command, &res);
   if (status != 0)
     return (status);
 
@@ -937,6 +964,18 @@ int lcc_listval (lcc_connection_t *c, /* {{{ */
   *ret_ident_num = ident_num;
 
   return (0);
+} /* }}} int lcc_listval_with_selection */
+
+int lcc_listval (lcc_connection_t *c, /* {{{ */
+    lcc_identifier_t **ret_ident, size_t *ret_ident_num)
+{
+  return (lcc_listval_with_selection (c,
+        /* host            = */ NULL,
+        /* plugin          = */ NULL,
+        /* plugin_instance = */ NULL,
+        /* type            = */ NULL,
+        /* type_instance   = */ NULL,
+        ret_ident, ret_ident_num));
 } /* }}} int lcc_listval */
 
 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
index 990035381c75d4198cf8fd6e1c202cb9891bf9ea..ac251cb9d00557b3d3618c0abbaf161a28adde53 100644 (file)
@@ -107,6 +107,14 @@ int lcc_flush (lcc_connection_t *c, const char *plugin,
 int lcc_listval (lcc_connection_t *c,
     lcc_identifier_t **ret_ident, size_t *ret_ident_num);
 
+int lcc_listval_with_selection (lcc_connection_t *c,
+    const char *re_host,
+    const char *re_plugin,
+    const char *re_plugin_instance,
+    const char *re_type,
+    const char *re_type_instance,
+    lcc_identifier_t **ret_ident, size_t *ret_ident_num);
+
 /* TODO: putnotif */
 
 const char *lcc_strerror (lcc_connection_t *c);