unixsock plugin: Implemented the `LISTVAL' command.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 18 Jun 2007 22:08:09 +0000 (00:08 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 18 Jun 2007 22:08:09 +0000 (00:08 +0200)
The LISTVAL command returnes a list of all available values and their timestamps.

src/collectd-unixsock.pod
src/unixsock.c

index 7c0e316..da4f2b2 100644 (file)
@@ -50,6 +50,25 @@ Example:
   -> | GETVAL myhost/cpu-0/cpu-user
   <- | 1 value=1.260000e+00
 
+=item B<LISTVAL>
+
+Returnes a list of the values available in the value cache together with the
+time of the last update, so that querying applications can issue a B<GETVAL>
+command for the values that have changed.
+
+The first line's status number is the number of identifiers returned or less
+than zero if an error occured. Each of the following lines containes the
+update time as an epoch value and the identifier, seperated by a space.
+
+Example:
+  -> | LISTVAL
+  <- | 69 Values found
+  <- | 1182204284 leeloo/cpu-0/cpu-idle
+  <- | 1182204284 leeloo/cpu-0/cpu-nice
+  <- | 1182204284 leeloo/cpu-0/cpu-system
+  <- | 1182204284 leeloo/cpu-0/cpu-user
+  ...
+
 =item B<PUTVAL> I<Identifier> I<Valuelist>
 
 Submits a value (identified by I<Identifier>, see below) to the daemon which
index 6302b17..82c15a8 100644 (file)
@@ -329,7 +329,7 @@ static void cache_flush (int max_age)
        } /* while (this != NULL) */
 
        pthread_mutex_unlock (&cache_lock);
-} /* int cache_flush */
+} /* void cache_flush */
 
 static int us_open_socket (void)
 {
@@ -630,6 +630,56 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
        return (0);
 } /* int us_handle_putval */
 
+static int us_handle_listval (FILE *fh, char **fields, int fields_num)
+{
+       char buffer[1024];
+       char **value_list = NULL;
+       int value_list_len = 0;
+       value_cache_t *entry;
+       int i;
+
+       if (fields_num != 1)
+       {
+               DEBUG ("unixsock plugin: us_handle_listval: "
+                               "Wrong number of fields: %i", fields_num);
+               fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
+                               fields_num);
+               fflush (fh);
+               return (-1);
+       }
+
+       pthread_mutex_lock (&cache_lock);
+
+       for (entry = cache_head; entry != NULL; entry = entry->next)
+       {
+               char **tmp;
+
+               snprintf (buffer, sizeof (buffer), "%u %s\n",
+                               (unsigned int) entry->time, entry->name);
+               buffer[sizeof (buffer) - 1] = '\0';
+               
+               tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
+               if (tmp == NULL)
+                       continue;
+               value_list = tmp;
+
+               value_list[value_list_len] = strdup (buffer);
+
+               if (value_list[value_list_len] != NULL)
+                       value_list_len++;
+       } /* for (entry) */
+
+       pthread_mutex_unlock (&cache_lock);
+
+       DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
+       fprintf (fh, "%i Values found\n", value_list_len);
+       for (i = 0; i < value_list_len; i++)
+               fputs (value_list[i], fh);
+       fflush (fh);
+
+       return (0);
+} /* int us_handle_listval */
+
 static void *us_handle_client (void *arg)
 {
        int fd;
@@ -685,6 +735,10 @@ static void *us_handle_client (void *arg)
                {
                        us_handle_putval (fh, fields, fields_num);
                }
+               else if (strcasecmp (fields[0], "listval") == 0)
+               {
+                       us_handle_listval (fh, fields, fields_num);
+               }
                else
                {
                        fprintf (fh, "-1 Unknown command: %s\n", fields[0]);