2 * collectd-td - collectd traffic generator
3 * Copyright (C) 2010-2012 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian Forster <octo at collectd.org>
26 #ifndef _ISOC99_SOURCE
27 # define _ISOC99_SOURCE
30 #ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200809L
35 # define _XOPEN_SOURCE 700
39 # define __attribute__(x) /**/
50 #include "utils_heap.h"
52 #include "libcollectdclient/collectd/client.h"
53 #include "libcollectdclient/collectd/network.h"
54 #include "libcollectdclient/collectd/network_buffer.h"
56 #define DEF_NUM_HOSTS 1000
57 #define DEF_NUM_PLUGINS 20
58 #define DEF_NUM_VALUES 100000
59 #define DEF_INTERVAL 10.0
61 static int conf_num_hosts = DEF_NUM_HOSTS;
62 static int conf_num_plugins = DEF_NUM_PLUGINS;
63 static int conf_num_values = DEF_NUM_VALUES;
64 static double conf_interval = DEF_INTERVAL;
65 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
66 static const char *conf_service = NET_DEFAULT_PORT;
68 static lcc_network_t *net;
70 static c_heap_t *values_heap = NULL;
72 static struct sigaction sigint_action;
73 static struct sigaction sigterm_action;
75 static _Bool loop = 1;
77 __attribute__((noreturn))
78 static void exit_usage (int exit_status) /* {{{ */
80 fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
81 "collectd-tg -- collectd traffic generator\n"
83 " Usage: collectd-ng [OPTION]\n"
86 " -n <number> Number of value lists. (Default: %i)\n"
87 " -H <number> Number of hosts to emulate. (Default: %i)\n"
88 " -p <number> Number of plugins to emulate. (Default: %i)\n"
89 " -i <seconds> Interval of each value in seconds. (Default: %.3f)\n"
90 " -d <dest> Destination address of the network packets.\n"
92 " -D <port> Destination port of the network packets.\n"
94 " -h Print usage information (this output).\n"
96 "Copyright (C) 2010-2012 Florian Forster\n"
97 "Licensed under the GNU General Public License, version 2 (GPLv2)\n",
98 DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS,
100 NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
102 } /* }}} void exit_usage */
104 static void signal_handler (int signal) /* {{{ */
107 } /* }}} void signal_handler */
109 static int compare_time (const void *v0, const void *v1) /* {{{ */
111 const lcc_value_list_t *vl0 = v0;
112 const lcc_value_list_t *vl1 = v1;
114 if (vl0->time < vl1->time)
116 else if (vl0->time > vl1->time)
120 } /* }}} int compare_time */
122 static int get_boundet_random (int min, int max) /* {{{ */
128 if (min == (max - 1))
133 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
134 } /* }}} int get_boundet_random */
136 static lcc_value_list_t *create_value_list (void) /* {{{ */
138 lcc_value_list_t *vl;
141 vl = malloc (sizeof (*vl));
144 fprintf (stderr, "malloc failed.\n");
147 memset (vl, 0, sizeof (*vl));
149 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
150 if (vl->values == NULL)
152 fprintf (stderr, "calloc failed.\n");
157 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
158 if (vl->values_types == NULL)
160 fprintf (stderr, "calloc failed.\n");
168 host_num = get_boundet_random (0, conf_num_hosts);
170 vl->interval = conf_interval;
171 vl->time = 1.0 + time (NULL)
172 + (host_num % (1 + (int) vl->interval));
174 if (get_boundet_random (0, 2) == 0)
175 vl->values_types[0] = LCC_TYPE_GAUGE;
177 vl->values_types[0] = LCC_TYPE_DERIVE;
179 snprintf (vl->identifier.host, sizeof (vl->identifier.host),
180 "host%04i", host_num);
181 snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
182 "plugin%03i", get_boundet_random (0, conf_num_plugins));
183 strncpy (vl->identifier.type,
184 (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
185 sizeof (vl->identifier.type));
186 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
190 } /* }}} int create_value_list */
192 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
198 free (vl->values_types);
200 } /* }}} void destroy_value_list */
202 static int send_value (lcc_value_list_t *vl) /* {{{ */
206 if (vl->values_types[0] == LCC_TYPE_GAUGE)
207 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
209 vl->values[0].derive += get_boundet_random (0, 100);
211 status = lcc_network_values_send (net, vl);
213 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
215 vl->time += vl->interval;
218 } /* }}} int send_value */
220 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
227 tmp = (int) strtol (str, &endptr, /* base = */ 0);
230 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
231 str, strerror (errno));
234 else if (endptr == str)
236 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
239 else if (*endptr != 0)
241 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
247 } /* }}} int get_integer_opt */
249 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
256 tmp = strtod (str, &endptr);
259 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
260 str, strerror (errno));
263 else if (endptr == str)
265 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
268 else if (*endptr != 0)
270 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
276 } /* }}} int get_double_opt */
278 static int read_options (int argc, char **argv) /* {{{ */
282 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
287 get_integer_opt (optarg, &conf_num_values);
291 get_integer_opt (optarg, &conf_num_hosts);
295 get_integer_opt (optarg, &conf_num_plugins);
299 get_double_opt (optarg, &conf_interval);
303 conf_destination = optarg;
307 conf_service = optarg;
311 exit_usage (EXIT_SUCCESS);
314 exit_usage (EXIT_FAILURE);
316 } /* while (getopt) */
319 } /* }}} int read_options */
321 int main (int argc, char **argv) /* {{{ */
327 read_options (argc, argv);
329 sigint_action.sa_handler = signal_handler;
330 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
332 sigterm_action.sa_handler = signal_handler;
333 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
336 values_heap = c_heap_create (compare_time);
337 if (values_heap == NULL)
339 fprintf (stderr, "c_heap_create failed.\n");
343 net = lcc_network_create ();
346 fprintf (stderr, "lcc_network_create failed.\n");
353 srv = lcc_server_create (net, conf_destination, conf_service);
356 fprintf (stderr, "lcc_server_create failed.\n");
360 lcc_server_set_ttl (srv, 42);
362 lcc_server_set_security_level (srv, ENCRYPT,
363 "admin", "password1");
367 fprintf (stdout, "Creating %i values ... ", conf_num_values);
369 for (i = 0; i < conf_num_values; i++)
371 lcc_value_list_t *vl;
373 vl = create_value_list ();
376 fprintf (stderr, "create_value_list failed.\n");
380 c_heap_insert (values_heap, vl);
382 fprintf (stdout, "done\n");
387 lcc_value_list_t *vl = c_heap_get_root (values_heap);
392 if (vl->time != last_time)
394 printf ("%i values have been sent.\n", values_sent);
396 /* Check if we need to sleep */
397 time_t now = time (NULL);
399 while (now < vl->time)
402 struct timespec ts = { 0, 10000000 };
403 nanosleep (&ts, /* remaining = */ NULL);
409 last_time = vl->time;
415 c_heap_insert (values_heap, vl);
418 fprintf (stdout, "Shutting down.\n");
423 lcc_value_list_t *vl = c_heap_get_root (values_heap);
426 destroy_value_list (vl);
428 c_heap_destroy (values_heap);
430 lcc_network_destroy (net);
435 /* vim: set sw=2 sts=2 et fdm=marker : */