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 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
192 "ti%li", random ());
194 return (vl);
195 } /* }}} int create_value_list */
197 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
198 {
199 if (vl == NULL)
200 return;
202 free (vl->values);
203 free (vl->values_types);
204 free (vl);
205 } /* }}} void destroy_value_list */
207 static int send_value (lcc_value_list_t *vl) /* {{{ */
208 {
209 int status;
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);
213 else
214 vl->values[0].derive += get_boundet_random (0, 100);
216 status = lcc_network_values_send (net, vl);
217 if (status != 0)
218 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
220 vl->time += vl->interval;
222 return (0);
223 } /* }}} int send_value */
225 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
226 {
227 char *endptr;
228 int tmp;
230 errno = 0;
231 endptr = NULL;
232 tmp = (int) strtol (str, &endptr, /* base = */ 0);
233 if (errno != 0)
234 {
235 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
236 str, strerror (errno));
237 exit (EXIT_FAILURE);
238 }
239 else if (endptr == str)
240 {
241 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
242 exit (EXIT_FAILURE);
243 }
244 else if (*endptr != 0)
245 {
246 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
247 exit (EXIT_FAILURE);
248 }
250 *ret_value = tmp;
251 return (0);
252 } /* }}} int get_integer_opt */
254 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
255 {
256 char *endptr;
257 double tmp;
259 errno = 0;
260 endptr = NULL;
261 tmp = strtod (str, &endptr);
262 if (errno != 0)
263 {
264 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
265 str, strerror (errno));
266 exit (EXIT_FAILURE);
267 }
268 else if (endptr == str)
269 {
270 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
271 exit (EXIT_FAILURE);
272 }
273 else if (*endptr != 0)
274 {
275 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
276 exit (EXIT_FAILURE);
277 }
279 *ret_value = tmp;
280 return (0);
281 } /* }}} int get_double_opt */
283 static int read_options (int argc, char **argv) /* {{{ */
284 {
285 int opt;
287 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
288 {
289 switch (opt)
290 {
291 case 'n':
292 get_integer_opt (optarg, &conf_num_values);
293 break;
295 case 'H':
296 get_integer_opt (optarg, &conf_num_hosts);
297 break;
299 case 'p':
300 get_integer_opt (optarg, &conf_num_plugins);
301 break;
303 case 'i':
304 get_double_opt (optarg, &conf_interval);
305 break;
307 case 'd':
308 conf_destination = optarg;
309 break;
311 case 'D':
312 conf_service = optarg;
313 break;
315 case 'h':
316 exit_usage (EXIT_SUCCESS);
318 default:
319 exit_usage (EXIT_FAILURE);
320 } /* switch (opt) */
321 } /* while (getopt) */
323 return (0);
324 } /* }}} int read_options */
326 int main (int argc, char **argv) /* {{{ */
327 {
328 int i;
329 time_t last_time;
330 int values_sent = 0;
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)
343 {
344 fprintf (stderr, "c_heap_create failed.\n");
345 exit (EXIT_FAILURE);
346 }
348 net = lcc_network_create ();
349 if (net == NULL)
350 {
351 fprintf (stderr, "lcc_network_create failed.\n");
352 exit (EXIT_FAILURE);
353 }
354 else
355 {
356 lcc_server_t *srv;
358 srv = lcc_server_create (net, conf_destination, conf_service);
359 if (srv == NULL)
360 {
361 fprintf (stderr, "lcc_server_create failed.\n");
362 exit (EXIT_FAILURE);
363 }
365 lcc_server_set_ttl (srv, 42);
366 #if 0
367 lcc_server_set_security_level (srv, ENCRYPT,
368 "admin", "password1");
369 #endif
370 }
372 fprintf (stdout, "Creating %i values ... ", conf_num_values);
373 fflush (stdout);
374 for (i = 0; i < conf_num_values; i++)
375 {
376 lcc_value_list_t *vl;
378 vl = create_value_list ();
379 if (vl == NULL)
380 {
381 fprintf (stderr, "create_value_list failed.\n");
382 exit (EXIT_FAILURE);
383 }
385 c_heap_insert (values_heap, vl);
386 }
387 fprintf (stdout, "done\n");
389 last_time = 0;
390 while (loop)
391 {
392 lcc_value_list_t *vl = c_heap_get_root (values_heap);
394 if (vl == NULL)
395 break;
397 if (vl->time != last_time)
398 {
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)
405 {
406 /* 1 / 100 second */
407 struct timespec ts = { 0, 10000000 };
408 nanosleep (&ts, /* remaining = */ NULL);
409 now = time (NULL);
411 if (!loop)
412 break;
413 }
414 last_time = vl->time;
415 }
417 send_value (vl);
418 values_sent++;
420 c_heap_insert (values_heap, vl);
421 }
423 fprintf (stdout, "Shutting down.\n");
424 fflush (stdout);
426 while (42)
427 {
428 lcc_value_list_t *vl = c_heap_get_root (values_heap);
429 if (vl == NULL)
430 break;
431 destroy_value_list (vl);
432 }
433 c_heap_destroy (values_heap);
435 lcc_network_destroy (net);
436 exit (EXIT_SUCCESS);
437 return (0);
438 } /* }}} int main */
440 /* vim: set sw=2 sts=2 et fdm=marker : */