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>
32 # define __attribute__(x) /**/
45 #include "utils_heap.h"
47 #include "libcollectdclient/collectd/client.h"
48 #include "libcollectdclient/collectd/network.h"
49 #include "libcollectdclient/collectd/network_buffer.h"
51 #define DEF_NUM_HOSTS 1000
52 #define DEF_NUM_PLUGINS 20
53 #define DEF_NUM_VALUES 100000
54 #define DEF_INTERVAL 10.0
56 static int conf_num_hosts = DEF_NUM_HOSTS;
57 static int conf_num_plugins = DEF_NUM_PLUGINS;
58 static int conf_num_values = DEF_NUM_VALUES;
59 static double conf_interval = DEF_INTERVAL;
60 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
61 static const char *conf_service = NET_DEFAULT_PORT;
63 static lcc_network_t *net;
65 static c_heap_t *values_heap = NULL;
67 static struct sigaction sigint_action;
68 static struct sigaction sigterm_action;
70 static _Bool loop = 1;
72 __attribute__((noreturn))
73 static void exit_usage (int exit_status) /* {{{ */
75 fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
76 "collectd-tg -- collectd traffic generator\n"
78 " Usage: collectd-ng [OPTION]\n"
81 " -n <number> Number of value lists. (Default: %i)\n"
82 " -H <number> Number of hosts to emulate. (Default: %i)\n"
83 " -p <number> Number of plugins to emulate. (Default: %i)\n"
84 " -i <seconds> Interval of each value in seconds. (Default: %.3f)\n"
85 " -d <dest> Destination address of the network packets.\n"
87 " -D <port> Destination port of the network packets.\n"
89 " -h Print usage information (this output).\n"
91 "Copyright (C) 2010-2012 Florian Forster\n"
92 "Licensed under the MIT license.\n",
93 DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS,
95 NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
97 } /* }}} void exit_usage */
99 static void signal_handler (int signal) /* {{{ */
102 } /* }}} void signal_handler */
104 #if HAVE_CLOCK_GETTIME
105 static double dtime (void) /* {{{ */
107 struct timespec ts = { 0 };
109 if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
110 perror ("clock_gettime");
112 return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9);
113 } /* }}} double dtime */
115 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
116 static double dtime (void) /* {{{ */
118 struct timeval tv = { 0 };
120 if (gettimeofday (&tv, /* timezone = */ NULL) != 0)
121 perror ("gettimeofday");
123 return ((double) tv.tv_sec) + (((double) tv.tv_usec) / 1e6);
124 } /* }}} double dtime */
127 static int compare_time (const void *v0, const void *v1) /* {{{ */
129 const lcc_value_list_t *vl0 = v0;
130 const lcc_value_list_t *vl1 = v1;
132 if (vl0->time < vl1->time)
134 else if (vl0->time > vl1->time)
138 } /* }}} int compare_time */
140 static int get_boundet_random (int min, int max) /* {{{ */
146 if (min == (max - 1))
151 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
152 } /* }}} int get_boundet_random */
154 static lcc_value_list_t *create_value_list (void) /* {{{ */
156 lcc_value_list_t *vl;
159 vl = calloc (1, sizeof (*vl));
162 fprintf (stderr, "calloc failed.\n");
166 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
167 if (vl->values == NULL)
169 fprintf (stderr, "calloc failed.\n");
174 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
175 if (vl->values_types == NULL)
177 fprintf (stderr, "calloc failed.\n");
185 host_num = get_boundet_random (0, conf_num_hosts);
187 vl->interval = conf_interval;
188 vl->time = 1.0 + dtime ()
189 + (host_num % (1 + (int) vl->interval));
191 if (get_boundet_random (0, 2) == 0)
192 vl->values_types[0] = LCC_TYPE_GAUGE;
194 vl->values_types[0] = LCC_TYPE_DERIVE;
196 snprintf (vl->identifier.host, sizeof (vl->identifier.host),
197 "host%04i", host_num);
198 snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
199 "plugin%03i", get_boundet_random (0, conf_num_plugins));
200 strncpy (vl->identifier.type,
201 (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
202 sizeof (vl->identifier.type));
203 vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0;
204 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
208 } /* }}} int create_value_list */
210 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
216 free (vl->values_types);
218 } /* }}} void destroy_value_list */
220 static int send_value (lcc_value_list_t *vl) /* {{{ */
224 if (vl->values_types[0] == LCC_TYPE_GAUGE)
225 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
227 vl->values[0].derive += (derive_t) get_boundet_random (0, 100);
229 status = lcc_network_values_send (net, vl);
231 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
233 vl->time += vl->interval;
236 } /* }}} int send_value */
238 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
245 tmp = (int) strtol (str, &endptr, /* base = */ 0);
248 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
249 str, strerror (errno));
252 else if (endptr == str)
254 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
257 else if (*endptr != 0)
259 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
265 } /* }}} int get_integer_opt */
267 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
274 tmp = strtod (str, &endptr);
277 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
278 str, strerror (errno));
281 else if (endptr == str)
283 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
286 else if (*endptr != 0)
288 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
294 } /* }}} int get_double_opt */
296 static int read_options (int argc, char **argv) /* {{{ */
300 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
305 get_integer_opt (optarg, &conf_num_values);
309 get_integer_opt (optarg, &conf_num_hosts);
313 get_integer_opt (optarg, &conf_num_plugins);
317 get_double_opt (optarg, &conf_interval);
321 conf_destination = optarg;
325 conf_service = optarg;
329 exit_usage (EXIT_SUCCESS);
332 exit_usage (EXIT_FAILURE);
334 } /* while (getopt) */
337 } /* }}} int read_options */
339 int main (int argc, char **argv) /* {{{ */
345 read_options (argc, argv);
347 sigint_action.sa_handler = signal_handler;
348 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
350 sigterm_action.sa_handler = signal_handler;
351 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
354 values_heap = c_heap_create (compare_time);
355 if (values_heap == NULL)
357 fprintf (stderr, "c_heap_create failed.\n");
361 net = lcc_network_create ();
364 fprintf (stderr, "lcc_network_create failed.\n");
371 srv = lcc_server_create (net, conf_destination, conf_service);
374 fprintf (stderr, "lcc_server_create failed.\n");
378 lcc_server_set_ttl (srv, 42);
380 lcc_server_set_security_level (srv, ENCRYPT,
381 "admin", "password1");
385 fprintf (stdout, "Creating %i values ... ", conf_num_values);
387 for (i = 0; i < conf_num_values; i++)
389 lcc_value_list_t *vl;
391 vl = create_value_list ();
394 fprintf (stderr, "create_value_list failed.\n");
398 c_heap_insert (values_heap, vl);
400 fprintf (stdout, "done\n");
405 lcc_value_list_t *vl = c_heap_get_root (values_heap);
410 if (vl->time != last_time)
412 printf ("%i values have been sent.\n", values_sent);
414 /* Check if we need to sleep */
415 double now = dtime ();
417 while (now < vl->time)
420 struct timespec ts = { 0, 10000000 };
422 ts.tv_sec = (time_t) now;
423 ts.tv_nsec = (long) ((now - ((double) ts.tv_sec)) * 1e9);
425 nanosleep (&ts, /* remaining = */ NULL);
431 last_time = vl->time;
437 c_heap_insert (values_heap, vl);
440 fprintf (stdout, "Shutting down.\n");
445 lcc_value_list_t *vl = c_heap_get_root (values_heap);
448 destroy_value_list (vl);
450 c_heap_destroy (values_heap);
452 lcc_network_destroy (net);
457 /* vim: set sw=2 sts=2 et fdm=marker : */