X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fcollectd-tg.c;h=80473e0ef79816fe53becb2ab41f4f86c5c86715;hb=1b4d95b869063e619bd7aae54cf37c5a1b91fbee;hp=c5d7eb0db69d5fea0842cdb3aa7a5232f784f9ba;hpb=4d7643e5ca26341ded63d68ef97e991e4657aaf0;p=collectd.git diff --git a/src/collectd-tg.c b/src/collectd-tg.c index c5d7eb0d..80473e0e 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -1,40 +1,33 @@ /** - * collectd-td - collectd traffic generator - * Copyright (C) 2010 Florian octo Forster + * collectd-tg - src/collectd-tg.c + * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian Forster + * Florian Forster **/ #if HAVE_CONFIG_H # include "config.h" #endif -#ifndef _ISOC99_SOURCE -# define _ISOC99_SOURCE -#endif - -#ifndef _POSIX_C_SOURCE -# define _POSIX_C_SOURCE 200809L -#endif - -#ifndef _XOPEN_SOURCE -# define _XOPEN_SOURCE 700 -#endif - #if !__GNUC__ # define __attribute__(x) /**/ #endif @@ -46,6 +39,7 @@ #include #include #include +#include #include "utils_heap.h" @@ -53,13 +47,15 @@ #include "libcollectdclient/collectd/network.h" #include "libcollectdclient/collectd/network_buffer.h" -#define DEF_NUM_HOSTS 1000 -#define DEF_NUM_PLUGINS 20 +#define DEF_NUM_HOSTS 1000 +#define DEF_NUM_PLUGINS 20 #define DEF_NUM_VALUES 100000 +#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 double conf_interval = DEF_INTERVAL; static const char *conf_destination = NET_DEFAULT_V6_ADDR; static const char *conf_service = NET_DEFAULT_PORT; @@ -84,15 +80,17 @@ 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: %.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" - "Licensed under the GNU General Public License, version 2 (GPLv2)\n", + "Copyright (C) 2010-2012 Florian Forster\n" + "Licensed under the MIT license.\n", DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS, + DEF_INTERVAL, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT); exit (exit_status); } /* }}} void exit_usage */ @@ -102,6 +100,16 @@ static void signal_handler (int signal) /* {{{ */ loop = 0; } /* }}} void signal_handler */ +static double dtime (void) /* {{{ */ +{ + struct timespec ts = { 0 }; + + if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) + perror ("clock_gettime"); + + return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9); +} /* }}} double dtime */ + static int compare_time (const void *v0, const void *v1) /* {{{ */ { const lcc_value_list_t *vl0 = v0; @@ -163,8 +171,9 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ host_num = get_boundet_random (0, conf_num_hosts); - vl->interval = 10; - vl->time = time (NULL) - (host_num % vl->interval); + vl->interval = conf_interval; + vl->time = 1.0 + dtime () + + (host_num % (1 + (int) vl->interval)); if (get_boundet_random (0, 2) == 0) vl->values_types[0] = LCC_TYPE_GAUGE; @@ -201,7 +210,7 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ if (vl->values_types[0] == LCC_TYPE_GAUGE) vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0); else - vl->values[0].derive += get_boundet_random (0, 100); + vl->values[0].derive += (derive_t) get_boundet_random (0, 100); status = lcc_network_values_send (net, vl); if (status != 0) @@ -241,6 +250,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; @@ -261,6 +299,10 @@ static int read_options (int argc, char **argv) /* {{{ */ get_integer_opt (optarg, &conf_num_plugins); break; + case 'i': + get_double_opt (optarg, &conf_interval); + break; + case 'd': conf_destination = optarg; break; @@ -283,7 +325,7 @@ static int read_options (int argc, char **argv) /* {{{ */ int main (int argc, char **argv) /* {{{ */ { int i; - time_t last_time; + double last_time; int values_sent = 0; read_options (argc, argv); @@ -356,14 +398,21 @@ int main (int argc, char **argv) /* {{{ */ printf ("%i values have been sent.\n", values_sent); /* Check if we need to sleep */ - time_t now = time (NULL); + double now = dtime (); while (now < vl->time) { /* 1 / 100 second */ struct timespec ts = { 0, 10000000 }; + + ts.tv_sec = (time_t) now; + ts.tv_nsec = (long) ((now - ((double) ts.tv_sec)) * 1e9); + nanosleep (&ts, /* remaining = */ NULL); - now = time (NULL); + now = dtime (); + + if (!loop) + break; } last_time = vl->time; }