From c76c5061f8717a5045132e60804a03936dd35489 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 19 Jun 2007 00:08:09 +0200 Subject: [PATCH] unixsock plugin: Implemented the `LISTVAL' command. The LISTVAL command returnes a list of all available values and their timestamps. --- src/collectd-unixsock.pod | 19 ++++++++++++++++ src/unixsock.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/collectd-unixsock.pod b/src/collectd-unixsock.pod index 7c0e316e..da4f2b22 100644 --- a/src/collectd-unixsock.pod +++ b/src/collectd-unixsock.pod @@ -50,6 +50,25 @@ Example: -> | GETVAL myhost/cpu-0/cpu-user <- | 1 value=1.260000e+00 +=item B + +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 +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 I I Submits a value (identified by I, see below) to the daemon which diff --git a/src/unixsock.c b/src/unixsock.c index 6302b17f..82c15a82 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -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]); -- 2.11.0