From 2c23a06de59e9cdb91ad9f72dc379733970e7a33 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 17 Nov 2012 10:23:39 +0100 Subject: [PATCH] libcollectdclient: Update to the high-resolution time format. --- src/collectd-tg.c | 46 +++++++++++++++++++++++++++------ src/libcollectdclient/Makefile.am | 2 +- src/libcollectdclient/client.c | 8 +++--- src/libcollectdclient/collectd/client.h | 4 +-- src/libcollectdclient/network_buffer.c | 17 +++++++++--- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/collectd-tg.c b/src/collectd-tg.c index e5b2d1f1..9fec3402 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -1,6 +1,6 @@ /** * collectd-td - collectd traffic generator - * Copyright (C) 2010 Florian octo Forster + * Copyright (C) 2010-2012 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -16,7 +16,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian Forster + * Florian Forster **/ #if HAVE_CONFIG_H @@ -56,12 +56,12 @@ #define DEF_NUM_HOSTS 1000 #define DEF_NUM_PLUGINS 20 #define DEF_NUM_VALUES 100000 -#define DEF_INTERVAL 10 +#define DEF_INTERVAL 10.0 static int conf_num_hosts = DEF_NUM_HOSTS; static int conf_num_plugins = DEF_NUM_PLUGINS; static int conf_num_values = DEF_NUM_VALUES; -static int conf_interval = DEF_INTERVAL; +static double conf_interval = DEF_INTERVAL; static const char *conf_destination = NET_DEFAULT_V6_ADDR; static const char *conf_service = NET_DEFAULT_PORT; @@ -86,14 +86,14 @@ static void exit_usage (int exit_status) /* {{{ */ " -n Number of value lists. (Default: %i)\n" " -H Number of hosts to emulate. (Default: %i)\n" " -p Number of plugins to emulate. (Default: %i)\n" - " -i Interval of each value in seconds. (Default: %i)\n" + " -i Interval of each value in seconds. (Default: %.3f)\n" " -d Destination address of the network packets.\n" " (Default: %s)\n" " -D Destination port of the network packets.\n" " (Default: %s)\n" " -h Print usage information (this output).\n" "\n" - "Copyright (C) 2010 Florian Forster\n" + "Copyright (C) 2010-2012 Florian Forster\n" "Licensed under the GNU General Public License, version 2 (GPLv2)\n", DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS, DEF_INTERVAL, @@ -168,7 +168,8 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ host_num = get_boundet_random (0, conf_num_hosts); vl->interval = conf_interval; - vl->time = time (NULL) + (host_num % vl->interval) + 1; + vl->time = 1.0 + time (NULL) + + (host_num % (1 + (int) vl->interval)); if (get_boundet_random (0, 2) == 0) vl->values_types[0] = LCC_TYPE_GAUGE; @@ -245,6 +246,35 @@ static int get_integer_opt (const char *str, int *ret_value) /* {{{ */ return (0); } /* }}} int get_integer_opt */ +static int get_double_opt (const char *str, double *ret_value) /* {{{ */ +{ + char *endptr; + double tmp; + + errno = 0; + endptr = NULL; + tmp = strtod (str, &endptr); + if (errno != 0) + { + fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n", + str, strerror (errno)); + exit (EXIT_FAILURE); + } + else if (endptr == str) + { + fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str); + exit (EXIT_FAILURE); + } + else if (*endptr != 0) + { + fprintf (stderr, "Garbage after end of value: \"%s\"\n", str); + exit (EXIT_FAILURE); + } + + *ret_value = tmp; + return (0); +} /* }}} int get_double_opt */ + static int read_options (int argc, char **argv) /* {{{ */ { int opt; @@ -266,7 +296,7 @@ static int read_options (int argc, char **argv) /* {{{ */ break; case 'i': - get_integer_opt (optarg, &conf_interval); + get_double_opt (optarg, &conf_interval); break; case 'd': diff --git a/src/libcollectdclient/Makefile.am b/src/libcollectdclient/Makefile.am index baf7a21b..39e1e1ad 100644 --- a/src/libcollectdclient/Makefile.am +++ b/src/libcollectdclient/Makefile.am @@ -12,7 +12,7 @@ BUILT_SOURCES = collectd/lcc_features.h libcollectdclient_la_SOURCES = client.c network.c network_buffer.c libcollectdclient_la_CPPFLAGS = $(AM_CPPFLAGS) -libcollectdclient_la_LDFLAGS = -version-info 0:0:0 +libcollectdclient_la_LDFLAGS = -version-info 1:0:0 libcollectdclient_la_LIBADD = if BUILD_WITH_LIBGCRYPT libcollectdclient_la_CPPFLAGS += $(GCRYPT_CPPFLAGS) diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index 850edaaf..726f25d4 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -767,11 +767,11 @@ int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */ SSTRCATF (command, "PUTVAL %s", lcc_strescape (ident_esc, ident_str, sizeof (ident_esc))); - if (vl->interval > 0) - SSTRCATF (command, " interval=%i", vl->interval); + if (vl->interval > 0.0) + SSTRCATF (command, " interval=%.3f", vl->interval); - if (vl->time > 0) - SSTRCATF (command, " %u", (unsigned int) vl->time); + if (vl->time > 0.0) + SSTRCATF (command, " %.3f", vl->time); else SSTRCAT (command, " N"); diff --git a/src/libcollectdclient/collectd/client.h b/src/libcollectdclient/collectd/client.h index 146f3424..6ae85984 100644 --- a/src/libcollectdclient/collectd/client.h +++ b/src/libcollectdclient/collectd/client.h @@ -84,8 +84,8 @@ struct lcc_value_list_s value_t *values; int *values_types; size_t values_len; - time_t time; - int interval; + double time; + double interval; lcc_identifier_t identifier; }; typedef struct lcc_value_list_s lcc_value_list_t; diff --git a/src/libcollectdclient/network_buffer.c b/src/libcollectdclient/network_buffer.c index 3da3b44f..c795a101 100644 --- a/src/libcollectdclient/network_buffer.c +++ b/src/libcollectdclient/network_buffer.c @@ -44,12 +44,14 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL; #define TYPE_HOST 0x0000 #define TYPE_TIME 0x0001 +#define TYPE_TIME_HR 0x0008 #define TYPE_PLUGIN 0x0002 #define TYPE_PLUGIN_INSTANCE 0x0003 #define TYPE_TYPE 0x0004 #define TYPE_TYPE_INSTANCE 0x0005 #define TYPE_VALUES 0x0006 #define TYPE_INTERVAL 0x0007 +#define TYPE_INTERVAL_HR 0x0009 /* Types to transmit notifications */ #define TYPE_MESSAGE 0x0100 @@ -346,6 +348,15 @@ static int nb_add_number (char **ret_buffer, /* {{{ */ return (0); } /* }}} int nb_add_number */ +static int nb_add_time (char **ret_buffer, /* {{{ */ + size_t *ret_buffer_len, + uint16_t type, double value) +{ + /* Convert to collectd's "cdtime" representation. */ + uint64_t cdtime_value = (uint64_t) (value * 1073741824.0); + return (nb_add_number (ret_buffer, ret_buffer_len, type, cdtime_value)); +} /* }}} int nb_add_time */ + static int nb_add_string (char **ret_buffer, /* {{{ */ size_t *ret_buffer_len, uint16_t type, const char *str, size_t str_len) @@ -446,16 +457,14 @@ static int nb_add_value_list (lcc_network_buffer_t *nb, /* {{{ */ if (nb->state.time != vl->time) { - if (nb_add_number (&buffer, &buffer_size, TYPE_TIME, - (uint64_t) vl->time)) + if (nb_add_time (&buffer, &buffer_size, TYPE_TIME_HR, vl->time)) return (-1); nb->state.time = vl->time; } if (nb->state.interval != vl->interval) { - if (nb_add_number (&buffer, &buffer_size, TYPE_INTERVAL, - (uint64_t) vl->interval)) + if (nb_add_time (&buffer, &buffer_size, TYPE_INTERVAL_HR, vl->interval)) return (-1); nb->state.interval = vl->interval; } -- 2.11.0