1 /**
2 * collectd-tg - src/collectd-tg.c
3 * Copyright (C) 2010-2012 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #ifndef _ISOC99_SOURCE
32 # define _ISOC99_SOURCE
33 #endif
35 #ifndef _POSIX_C_SOURCE
36 # define _POSIX_C_SOURCE 200809L
37 #endif
39 #ifndef _XOPEN_SOURCE
40 # define _XOPEN_SOURCE 700
41 #endif
43 #if !__GNUC__
44 # define __attribute__(x) /**/
45 #endif
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <time.h>
52 #include <signal.h>
53 #include <errno.h>
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) /* {{{ */
84 {
85 fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
86 "collectd-tg -- collectd traffic generator\n"
87 "\n"
88 " Usage: collectd-ng [OPTION]\n"
89 "\n"
90 " Valid options:\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"
96 " (Default: %s)\n"
97 " -D <port> Destination port of the network packets.\n"
98 " (Default: %s)\n"
99 " -h Print usage information (this output).\n"
100 "\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,
104 DEF_INTERVAL,
105 NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
106 exit (exit_status);
107 } /* }}} void exit_usage */
109 static void signal_handler (int signal) /* {{{ */
110 {
111 loop = 0;
112 } /* }}} void signal_handler */
114 static int compare_time (const void *v0, const void *v1) /* {{{ */
115 {
116 const lcc_value_list_t *vl0 = v0;
117 const lcc_value_list_t *vl1 = v1;
119 if (vl0->time < vl1->time)
120 return (-1);
121 else if (vl0->time > vl1->time)
122 return (1);
123 else
124 return (0);
125 } /* }}} int compare_time */
127 static int get_boundet_random (int min, int max) /* {{{ */
128 {
129 int range;
131 if (min >= max)
132 return (-1);
133 if (min == (max - 1))
134 return (min);
136 range = max - min;
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) /* {{{ */
142 {
143 lcc_value_list_t *vl;
144 int host_num;
146 vl = malloc (sizeof (*vl));
147 if (vl == NULL)
148 {
149 fprintf (stderr, "malloc failed.\n");
150 return (NULL);
151 }
152 memset (vl, 0, sizeof (*vl));
154 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
155 if (vl->values == NULL)
156 {
157 fprintf (stderr, "calloc failed.\n");
158 free (vl);
159 return (NULL);
160 }
162 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
163 if (vl->values_types == NULL)
164 {
165 fprintf (stderr, "calloc failed.\n");
166 free (vl->values);
167 free (vl);
168 return (NULL);
169 }
171 vl->values_len = 1;
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;
181 else
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 vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0;
192 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
193 "ti%li", random ());
195 return (vl);
196 } /* }}} int create_value_list */
198 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
199 {
200 if (vl == NULL)
201 return;
203 free (vl->values);
204 free (vl->values_types);
205 free (vl);
206 } /* }}} void destroy_value_list */
208 static int send_value (lcc_value_list_t *vl) /* {{{ */
209 {
210 int status;
212 if (vl->values_types[0] == LCC_TYPE_GAUGE)
213 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
214 else
215 vl->values[0].derive += get_boundet_random (0, 100);
217 status = lcc_network_values_send (net, vl);
218 if (status != 0)
219 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
221 vl->time += vl->interval;
223 return (0);
224 } /* }}} int send_value */
226 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
227 {
228 char *endptr;
229 int tmp;
231 errno = 0;
232 endptr = NULL;
233 tmp = (int) strtol (str, &endptr, /* base = */ 0);
234 if (errno != 0)
235 {
236 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
237 str, strerror (errno));
238 exit (EXIT_FAILURE);
239 }
240 else if (endptr == str)
241 {
242 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
243 exit (EXIT_FAILURE);
244 }
245 else if (*endptr != 0)
246 {
247 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
248 exit (EXIT_FAILURE);
249 }
251 *ret_value = tmp;
252 return (0);
253 } /* }}} int get_integer_opt */
255 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
256 {
257 char *endptr;
258 double tmp;
260 errno = 0;
261 endptr = NULL;
262 tmp = strtod (str, &endptr);
263 if (errno != 0)
264 {
265 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
266 str, strerror (errno));
267 exit (EXIT_FAILURE);
268 }
269 else if (endptr == str)
270 {
271 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
272 exit (EXIT_FAILURE);
273 }
274 else if (*endptr != 0)
275 {
276 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
277 exit (EXIT_FAILURE);
278 }
280 *ret_value = tmp;
281 return (0);
282 } /* }}} int get_double_opt */
284 static int read_options (int argc, char **argv) /* {{{ */
285 {
286 int opt;
288 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
289 {
290 switch (opt)
291 {
292 case 'n':
293 get_integer_opt (optarg, &conf_num_values);
294 break;
296 case 'H':
297 get_integer_opt (optarg, &conf_num_hosts);
298 break;
300 case 'p':
301 get_integer_opt (optarg, &conf_num_plugins);
302 break;
304 case 'i':
305 get_double_opt (optarg, &conf_interval);
306 break;
308 case 'd':
309 conf_destination = optarg;
310 break;
312 case 'D':
313 conf_service = optarg;
314 break;
316 case 'h':
317 exit_usage (EXIT_SUCCESS);
319 default:
320 exit_usage (EXIT_FAILURE);
321 } /* switch (opt) */
322 } /* while (getopt) */
324 return (0);
325 } /* }}} int read_options */
327 int main (int argc, char **argv) /* {{{ */
328 {
329 int i;
330 time_t last_time;
331 int values_sent = 0;
333 read_options (argc, argv);
335 sigint_action.sa_handler = signal_handler;
336 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
338 sigterm_action.sa_handler = signal_handler;
339 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
342 values_heap = c_heap_create (compare_time);
343 if (values_heap == NULL)
344 {
345 fprintf (stderr, "c_heap_create failed.\n");
346 exit (EXIT_FAILURE);
347 }
349 net = lcc_network_create ();
350 if (net == NULL)
351 {
352 fprintf (stderr, "lcc_network_create failed.\n");
353 exit (EXIT_FAILURE);
354 }
355 else
356 {
357 lcc_server_t *srv;
359 srv = lcc_server_create (net, conf_destination, conf_service);
360 if (srv == NULL)
361 {
362 fprintf (stderr, "lcc_server_create failed.\n");
363 exit (EXIT_FAILURE);
364 }
366 lcc_server_set_ttl (srv, 42);
367 #if 0
368 lcc_server_set_security_level (srv, ENCRYPT,
369 "admin", "password1");
370 #endif
371 }
373 fprintf (stdout, "Creating %i values ... ", conf_num_values);
374 fflush (stdout);
375 for (i = 0; i < conf_num_values; i++)
376 {
377 lcc_value_list_t *vl;
379 vl = create_value_list ();
380 if (vl == NULL)
381 {
382 fprintf (stderr, "create_value_list failed.\n");
383 exit (EXIT_FAILURE);
384 }
386 c_heap_insert (values_heap, vl);
387 }
388 fprintf (stdout, "done\n");
390 last_time = 0;
391 while (loop)
392 {
393 lcc_value_list_t *vl = c_heap_get_root (values_heap);
395 if (vl == NULL)
396 break;
398 if (vl->time != last_time)
399 {
400 printf ("%i values have been sent.\n", values_sent);
402 /* Check if we need to sleep */
403 time_t now = time (NULL);
405 while (now < vl->time)
406 {
407 /* 1 / 100 second */
408 struct timespec ts = { 0, 10000000 };
409 nanosleep (&ts, /* remaining = */ NULL);
410 now = time (NULL);
412 if (!loop)
413 break;
414 }
415 last_time = vl->time;
416 }
418 send_value (vl);
419 values_sent++;
421 c_heap_insert (values_heap, vl);
422 }
424 fprintf (stdout, "Shutting down.\n");
425 fflush (stdout);
427 while (42)
428 {
429 lcc_value_list_t *vl = c_heap_get_root (values_heap);
430 if (vl == NULL)
431 break;
432 destroy_value_list (vl);
433 }
434 c_heap_destroy (values_heap);
436 lcc_network_destroy (net);
437 exit (EXIT_SUCCESS);
438 return (0);
439 } /* }}} int main */
441 /* vim: set sw=2 sts=2 et fdm=marker : */