From d74858511c39ddbb8ac8b8ee7cde7aaf1e83f92e Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 31 May 2007 23:16:06 +0200 Subject: [PATCH] unixsock plugin: Moved the `parse_identifier' code to `common.c' The new function `parse_values' in `common.c' can be used to parse `time:value0:value1:...', as used by RRDTool, the unixsock plugin and, in the very near future, the exec plugin. --- src/common.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 6 ++++ src/perl.c | 2 +- src/unixsock.c | 54 +++-------------------------------- 4 files changed, 99 insertions(+), 51 deletions(-) diff --git a/src/common.c b/src/common.c index 9a28cdbf..1addb323 100644 --- a/src/common.c +++ b/src/common.c @@ -555,3 +555,91 @@ int format_name (char *ret, int ret_len, return (-1); return (0); } /* int format_name */ + +int parse_identifier (char *str, char **ret_host, + char **ret_plugin, char **ret_plugin_instance, + char **ret_type, char **ret_type_instance) +{ + char *hostname = NULL; + char *plugin = NULL; + char *plugin_instance = NULL; + char *type = NULL; + char *type_instance = NULL; + + hostname = str; + if (hostname == NULL) + return (-1); + + plugin = strchr (hostname, '/'); + if (plugin == NULL) + return (-1); + *plugin = '\0'; plugin++; + + type = strchr (plugin, '/'); + if (type == NULL) + return (-1); + *type = '\0'; type++; + + plugin_instance = strchr (plugin, '-'); + if (plugin_instance != NULL) + { + *plugin_instance = '\0'; + plugin_instance++; + } + + type_instance = strchr (type, '-'); + if (type_instance != NULL) + { + *type_instance = '\0'; + type_instance++; + } + + *ret_host = hostname; + *ret_plugin = plugin; + *ret_plugin_instance = plugin_instance; + *ret_type = type; + *ret_type_instance = type_instance; + return (0); +} /* int parse_identifier */ + +int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) +{ + int i; + char *dummy; + char *ptr; + char *saveptr; + + i = -1; + dummy = buffer; + saveptr = NULL; + while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL) + { + dummy = NULL; + + if (i >= vl->values_len) + break; + + if (i == -1) + { + if (strcmp ("N", ptr) == 0) + vl->time = time (NULL); + else + vl->time = (time_t) atoi (ptr); + } + else + { + if (strcmp ("U", ptr) == 0) + vl->values[i].gauge = NAN; + else if (ds->ds[i].type == DS_TYPE_COUNTER) + vl->values[i].counter = atoll (ptr); + else if (ds->ds[i].type == DS_TYPE_GAUGE) + vl->values[i].gauge = atof (ptr); + } + + i++; + } /* while (strtok_r) */ + + if ((ptr != NULL) || (i != vl->values_len)) + return (-1); + return (0); +} /* int parse_values */ diff --git a/src/common.h b/src/common.h index 7808ac93..b07db9d4 100644 --- a/src/common.h +++ b/src/common.h @@ -24,6 +24,7 @@ #define COMMON_H #include "collectd.h" +#include "plugin.h" #define sfree(ptr) \ if((ptr) != NULL) { \ @@ -166,4 +167,9 @@ int format_name (char *ret, int ret_len, format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \ (ds)->type, (vl)->type_instance) +int parse_identifier (char *str, char **ret_host, + char **ret_plugin, char **ret_plugin_instance, + char **ret_type, char **ret_type_instance); +int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds); + #endif /* COMMON_H */ diff --git a/src/perl.c b/src/perl.c index bcf7bb17..1ad72487 100644 --- a/src/perl.c +++ b/src/perl.c @@ -25,7 +25,6 @@ */ #include "collectd.h" -#include "common.h" #include "configfile.h" @@ -41,6 +40,7 @@ /* ... while we want the definition found in plugin.h. */ #include "plugin.h" +#include "common.h" #define PLUGIN_INIT 0 #define PLUGIN_READ 1 diff --git a/src/unixsock.c b/src/unixsock.c index 97936592..215abdd0 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -84,52 +84,6 @@ static unsigned int cache_oldest = UINT_MAX; /* * Functions */ -static int parse_identifier (char *str, char **ret_host, - char **ret_plugin, char **ret_plugin_instance, - char **ret_type, char **ret_type_instance) -{ - char *hostname = NULL; - char *plugin = NULL; - char *plugin_instance = NULL; - char *type = NULL; - char *type_instance = NULL; - - hostname = str; - if (hostname == NULL) - return (-1); - - plugin = strchr (hostname, '/'); - if (plugin == NULL) - return (-1); - *plugin = '\0'; plugin++; - - type = strchr (plugin, '/'); - if (type == NULL) - return (-1); - *type = '\0'; type++; - - plugin_instance = strchr (plugin, '-'); - if (plugin_instance != NULL) - { - *plugin_instance = '\0'; - plugin_instance++; - } - - type_instance = strchr (type, '-'); - if (type_instance != NULL) - { - *type_instance = '\0'; - type_instance++; - } - - *ret_host = hostname; - *ret_plugin = plugin; - *ret_plugin_instance = plugin_instance; - *ret_type = type; - *ret_type_instance = type_instance; - return (0); -} /* int parse_identifier */ - static value_cache_t *cache_search (const char *name) { value_cache_t *vc; @@ -568,12 +522,12 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) return (-1); } - if ((strlen (hostname) > sizeof (vl.host)) - || (strlen (plugin) > sizeof (vl.plugin)) + if ((strlen (hostname) >= sizeof (vl.host)) + || (strlen (plugin) >= sizeof (vl.plugin)) || ((plugin_instance != NULL) - && (strlen (plugin_instance) > sizeof (vl.plugin_instance))) + && (strlen (plugin_instance) >= sizeof (vl.plugin_instance))) || ((type_instance != NULL) - && (strlen (type_instance) > sizeof (vl.type_instance)))) + && (strlen (type_instance) >= sizeof (vl.type_instance)))) { fprintf (fh, "-1 Identifier too long."); return (-1); -- 2.11.0