2 * collectd-tg - src/collectd-tg.c
3 * Copyright (C) 2010-2012 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
31 #ifndef _ISOC99_SOURCE
32 # define _ISOC99_SOURCE
35 #ifndef _POSIX_C_SOURCE
36 # define _POSIX_C_SOURCE 200809L
40 # define _XOPEN_SOURCE 700
44 # define __attribute__(x) /**/
55 #include "utils_heap.h"
57 #include "libcollectdclient/collectd/client.h"
58 #include "libcollectdclient/collectd/network.h"
59 #include "libcollectdclient/collectd/network_buffer.h"
61 #define DEF_NUM_HOSTS 1000
62 #define DEF_NUM_PLUGINS 20
63 #define DEF_NUM_VALUES 100000
64 #define DEF_INTERVAL 10.0
66 static int conf_num_hosts = DEF_NUM_HOSTS;
67 static int conf_num_plugins = DEF_NUM_PLUGINS;
68 static int conf_num_values = DEF_NUM_VALUES;
69 static double conf_interval = DEF_INTERVAL;
70 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
71 static const char *conf_service = NET_DEFAULT_PORT;
73 static lcc_network_t *net;
75 static c_heap_t *values_heap = NULL;
77 static struct sigaction sigint_action;
78 static struct sigaction sigterm_action;
80 static _Bool loop = 1;
82 __attribute__((noreturn))
83 static void exit_usage (int exit_status) /* {{{ */
85 fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
86 "collectd-tg -- collectd traffic generator\n"
88 " Usage: collectd-ng [OPTION]\n"
91 " -n <number> Number of value lists. (Default: %i)\n"
92 " -H <number> Number of hosts to emulate. (Default: %i)\n"
93 " -p <number> Number of plugins to emulate. (Default: %i)\n"
94 " -i <seconds> Interval of each value in seconds. (Default: %.3f)\n"
95 " -d <dest> Destination address of the network packets.\n"
97 " -D <port> Destination port of the network packets.\n"
99 " -h Print usage information (this output).\n"
101 "Copyright (C) 2010-2012 Florian Forster\n"
102 "Licensed under the MIT license.\n",
103 DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS,
105 NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
107 } /* }}} void exit_usage */
109 static void signal_handler (int signal) /* {{{ */
112 } /* }}} void signal_handler */
114 static int compare_time (const void *v0, const void *v1) /* {{{ */
116 const lcc_value_list_t *vl0 = v0;
117 const lcc_value_list_t *vl1 = v1;
119 if (vl0->time < vl1->time)
121 else if (vl0->time > vl1->time)
125 } /* }}} int compare_time */
127 static int get_boundet_random (int min, int max) /* {{{ */
133 if (min == (max - 1))
138 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
139 } /* }}} int get_boundet_random */
141 static lcc_value_list_t *create_value_list (void) /* {{{ */
143 lcc_value_list_t *vl;
146 vl = malloc (sizeof (*vl));
149 fprintf (stderr, "malloc failed.\n");
152 memset (vl, 0, sizeof (*vl));
154 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
155 if (vl->values == NULL)
157 fprintf (stderr, "calloc failed.\n");
162 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
163 if (vl->values_types == NULL)
165 fprintf (stderr, "calloc failed.\n");
173 host_num = get_boundet_random (0, conf_num_hosts);
175 vl->interval = conf_interval;
176 vl->time = 1.0 + time (NULL)
177 + (host_num % (1 + (int) vl->interval));
179 if (get_boundet_random (0, 2) == 0)
180 vl->values_types[0] = LCC_TYPE_GAUGE;
182 vl->values_types[0] = LCC_TYPE_DERIVE;
184 snprintf (vl->identifier.host, sizeof (vl->identifier.host),
185 "host%04i", host_num);
186 snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
187 "plugin%03i", get_boundet_random (0, conf_num_plugins));
188 strncpy (vl->identifier.type,
189 (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
190 sizeof (vl->identifier.type));
191 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
195 } /* }}} int create_value_list */
197 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
203 free (vl->values_types);
205 } /* }}} void destroy_value_list */
207 static int send_value (lcc_value_list_t *vl) /* {{{ */
211 if (vl->values_types[0] == LCC_TYPE_GAUGE)
212 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
214 vl->values[0].derive += get_boundet_random (0, 100);
216 status = lcc_network_values_send (net, vl);
218 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
220 vl->time += vl->interval;
223 } /* }}} int send_value */
225 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
232 tmp = (int) strtol (str, &endptr, /* base = */ 0);
235 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
236 str, strerror (errno));
239 else if (endptr == str)
241 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
244 else if (*endptr != 0)
246 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
252 } /* }}} int get_integer_opt */
254 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
261 tmp = strtod (str, &endptr);
264 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
265 str, strerror (errno));
268 else if (endptr == str)
270 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
273 else if (*endptr != 0)
275 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
281 } /* }}} int get_double_opt */
283 static int read_options (int argc, char **argv) /* {{{ */
287 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
292 get_integer_opt (optarg, &conf_num_values);
296 get_integer_opt (optarg, &conf_num_hosts);
300 get_integer_opt (optarg, &conf_num_plugins);
304 get_double_opt (optarg, &conf_interval);
308 conf_destination = optarg;
312 conf_service = optarg;
316 exit_usage (EXIT_SUCCESS);
319 exit_usage (EXIT_FAILURE);
321 } /* while (getopt) */
324 } /* }}} int read_options */
326 int main (int argc, char **argv) /* {{{ */
332 read_options (argc, argv);
334 sigint_action.sa_handler = signal_handler;
335 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
337 sigterm_action.sa_handler = signal_handler;
338 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
341 values_heap = c_heap_create (compare_time);
342 if (values_heap == NULL)
344 fprintf (stderr, "c_heap_create failed.\n");
348 net = lcc_network_create ();
351 fprintf (stderr, "lcc_network_create failed.\n");
358 srv = lcc_server_create (net, conf_destination, conf_service);
361 fprintf (stderr, "lcc_server_create failed.\n");
365 lcc_server_set_ttl (srv, 42);
367 lcc_server_set_security_level (srv, ENCRYPT,
368 "admin", "password1");
372 fprintf (stdout, "Creating %i values ... ", conf_num_values);
374 for (i = 0; i < conf_num_values; i++)
376 lcc_value_list_t *vl;
378 vl = create_value_list ();
381 fprintf (stderr, "create_value_list failed.\n");
385 c_heap_insert (values_heap, vl);
387 fprintf (stdout, "done\n");
392 lcc_value_list_t *vl = c_heap_get_root (values_heap);
397 if (vl->time != last_time)
399 printf ("%i values have been sent.\n", values_sent);
401 /* Check if we need to sleep */
402 time_t now = time (NULL);
404 while (now < vl->time)
407 struct timespec ts = { 0, 10000000 };
408 nanosleep (&ts, /* remaining = */ NULL);
414 last_time = vl->time;
420 c_heap_insert (values_heap, vl);
423 fprintf (stdout, "Shutting down.\n");
428 lcc_value_list_t *vl = c_heap_get_root (values_heap);
431 destroy_value_list (vl);
433 c_heap_destroy (values_heap);
435 lcc_network_destroy (net);
440 /* vim: set sw=2 sts=2 et fdm=marker : */