X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectd-tg.c;h=45e788c22206c5c733d696bc6ab778ab732643ec;hb=f5adf265a374e5e0dba89a4a9903e7719dc57039;hp=5b31cc9d13a423e4b66a2384a0581177e221943b;hpb=f45b8f6be49a380c0b1dce4328f47b08da802bce;p=collectd.git diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 5b31cc9d..45e788c2 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -1,32 +1,56 @@ /** - * collectd - src/collectd-tg.c - * 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 + #include +#include #include #include #include +#include +#include #include "utils_heap.h" @@ -34,26 +58,70 @@ #include "libcollectdclient/collectd/network.h" #include "libcollectdclient/collectd/network_buffer.h" -static int conf_num_hosts = 1000; -static int conf_num_plugins = 20; -static int conf_num_values = 100000; +#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; static lcc_network_t *net; static c_heap_t *values_heap = NULL; +static struct sigaction sigint_action; +static struct sigaction sigterm_action; + +static _Bool loop = 1; + +__attribute__((noreturn)) +static void exit_usage (int exit_status) /* {{{ */ +{ + fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout, + "collectd-tg -- collectd traffic generator\n" + "\n" + " Usage: collectd-ng [OPTION]\n" + "\n" + " Valid options:\n" + " -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-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 */ + +static void signal_handler (int signal) /* {{{ */ +{ + loop = 0; +} /* }}} void signal_handler */ + static int compare_time (const void *v0, const void *v1) /* {{{ */ { - lcc_value_list_t * const *vl0 = v0; - lcc_value_list_t * const *vl1 = v1; + const lcc_value_list_t *vl0 = v0; + const lcc_value_list_t *vl1 = v1; - if ((*vl0)->time < (*vl1)->time) + if (vl0->time < vl1->time) return (-1); - else if ((*vl0)->time > (*vl1)->time) + else if (vl0->time > vl1->time) return (1); else - return (lcc_identifier_compare (&(*vl0)->identifier, /* Ouch, somebody */ - &(*vl1)->identifier)); /* is going to hate me for this. */ + return (0); } /* }}} int compare_time */ static int get_boundet_random (int min, int max) /* {{{ */ @@ -70,55 +138,6 @@ static int get_boundet_random (int min, int max) /* {{{ */ return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0)))); } /* }}} int get_boundet_random */ -#if 0 -static int dump_network_buffer (void) /* {{{ */ -{ - char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT]; - size_t buffer_size; - int status; - size_t offset; - - memset (buffer, 0, sizeof (buffer)); - buffer_size = sizeof (buffer); - - status = lcc_network_buffer_get (nb, buffer, &buffer_size); - if (status != 0) - { - fprintf (stderr, "lcc_network_buffer_get failed with status %i.\n", - status); - return (status); - } - - if (buffer_size > sizeof (buffer)) - buffer_size = sizeof (buffer); - - for (offset = 0; offset < buffer_size; offset += 16) - { - size_t i; - - for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++) - { - uint8_t v = (uint8_t) buffer[offset + i]; - printf ("%02"PRIx8" ", v); - } - for (; i < 16; i++) - printf (" "); - printf (" "); - for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++) - { - uint8_t v = (uint8_t) buffer[offset + i]; - if ((v >= 32) && (v < 128)) - printf ("%c", (int) buffer[offset + i]); - else - printf ("."); - } - printf ("\n"); - } - - return (0); -} /* }}} int dump_network_buffer */ -#endif - static lcc_value_list_t *create_value_list (void) /* {{{ */ { lcc_value_list_t *vl; @@ -153,8 +172,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 + time (NULL) + + (host_num % (1 + (int) vl->interval)); if (get_boundet_random (0, 2) == 0) vl->values_types[0] = LCC_TYPE_GAUGE; @@ -202,9 +222,121 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ return (0); } /* }}} int send_value */ +static int get_integer_opt (const char *str, int *ret_value) /* {{{ */ +{ + char *endptr; + int tmp; + + errno = 0; + endptr = NULL; + tmp = (int) strtol (str, &endptr, /* base = */ 0); + 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_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; + + while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1) + { + switch (opt) + { + case 'n': + get_integer_opt (optarg, &conf_num_values); + break; + + case 'H': + get_integer_opt (optarg, &conf_num_hosts); + break; + + case 'p': + get_integer_opt (optarg, &conf_num_plugins); + break; + + case 'i': + get_double_opt (optarg, &conf_interval); + break; + + case 'd': + conf_destination = optarg; + break; + + case 'D': + conf_service = optarg; + break; + + case 'h': + exit_usage (EXIT_SUCCESS); + + default: + exit_usage (EXIT_FAILURE); + } /* switch (opt) */ + } /* while (getopt) */ + + return (0); +} /* }}} int read_options */ + int main (int argc, char **argv) /* {{{ */ { int i; + time_t last_time; + int values_sent = 0; + + read_options (argc, argv); + + sigint_action.sa_handler = signal_handler; + sigaction (SIGINT, &sigint_action, /* old = */ NULL); + + sigterm_action.sa_handler = signal_handler; + sigaction (SIGTERM, &sigterm_action, /* old = */ NULL); + values_heap = c_heap_create (compare_time); if (values_heap == NULL) @@ -223,7 +355,7 @@ int main (int argc, char **argv) /* {{{ */ { lcc_server_t *srv; - srv = lcc_server_create (net, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT); + srv = lcc_server_create (net, conf_destination, conf_service); if (srv == NULL) { fprintf (stderr, "lcc_server_create failed.\n"); @@ -254,17 +386,50 @@ int main (int argc, char **argv) /* {{{ */ } fprintf (stdout, "done\n"); - while (42) + last_time = 0; + while (loop) { lcc_value_list_t *vl = c_heap_get_root (values_heap); if (vl == NULL) break; + if (vl->time != last_time) + { + printf ("%i values have been sent.\n", values_sent); + + /* Check if we need to sleep */ + time_t now = time (NULL); + + while (now < vl->time) + { + /* 1 / 100 second */ + struct timespec ts = { 0, 10000000 }; + nanosleep (&ts, /* remaining = */ NULL); + now = time (NULL); + + if (!loop) + break; + } + last_time = vl->time; + } + send_value (vl); - destroy_value_list (vl); + values_sent++; + + c_heap_insert (values_heap, vl); } + fprintf (stdout, "Shutting down.\n"); + fflush (stdout); + + while (42) + { + lcc_value_list_t *vl = c_heap_get_root (values_heap); + if (vl == NULL) + break; + destroy_value_list (vl); + } c_heap_destroy (values_heap); lcc_network_destroy (net);