1 /**
2 * collectd-td - collectd traffic generator
3 * Copyright (C) 2010-2012 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian Forster <octo at collectd.org>
20 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #ifndef _ISOC99_SOURCE
27 # define _ISOC99_SOURCE
28 #endif
30 #ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200809L
32 #endif
34 #ifndef _XOPEN_SOURCE
35 # define _XOPEN_SOURCE 700
36 #endif
38 #if !__GNUC__
39 # define __attribute__(x) /**/
40 #endif
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <time.h>
47 #include <signal.h>
48 #include <errno.h>
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) /* {{{ */
79 {
80 fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
81 "collectd-tg -- collectd traffic generator\n"
82 "\n"
83 " Usage: collectd-ng [OPTION]\n"
84 "\n"
85 " Valid options:\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"
91 " (Default: %s)\n"
92 " -D <port> Destination port of the network packets.\n"
93 " (Default: %s)\n"
94 " -h Print usage information (this output).\n"
95 "\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,
99 DEF_INTERVAL,
100 NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
101 exit (exit_status);
102 } /* }}} void exit_usage */
104 static void signal_handler (int signal) /* {{{ */
105 {
106 loop = 0;
107 } /* }}} void signal_handler */
109 static int compare_time (const void *v0, const void *v1) /* {{{ */
110 {
111 const lcc_value_list_t *vl0 = v0;
112 const lcc_value_list_t *vl1 = v1;
114 if (vl0->time < vl1->time)
115 return (-1);
116 else if (vl0->time > vl1->time)
117 return (1);
118 else
119 return (0);
120 } /* }}} int compare_time */
122 static int get_boundet_random (int min, int max) /* {{{ */
123 {
124 int range;
126 if (min >= max)
127 return (-1);
128 if (min == (max - 1))
129 return (min);
131 range = max - min;
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) /* {{{ */
137 {
138 lcc_value_list_t *vl;
139 int host_num;
141 vl = malloc (sizeof (*vl));
142 if (vl == NULL)
143 {
144 fprintf (stderr, "malloc failed.\n");
145 return (NULL);
146 }
147 memset (vl, 0, sizeof (*vl));
149 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
150 if (vl->values == NULL)
151 {
152 fprintf (stderr, "calloc failed.\n");
153 free (vl);
154 return (NULL);
155 }
157 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
158 if (vl->values_types == NULL)
159 {
160 fprintf (stderr, "calloc failed.\n");
161 free (vl->values);
162 free (vl);
163 return (NULL);
164 }
166 vl->values_len = 1;
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;
176 else
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),
187 "ti%li", random ());
189 return (vl);
190 } /* }}} int create_value_list */
192 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
193 {
194 if (vl == NULL)
195 return;
197 free (vl->values);
198 free (vl->values_types);
199 free (vl);
200 } /* }}} void destroy_value_list */
202 static int send_value (lcc_value_list_t *vl) /* {{{ */
203 {
204 int status;
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);
208 else
209 vl->values[0].derive += get_boundet_random (0, 100);
211 status = lcc_network_values_send (net, vl);
212 if (status != 0)
213 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
215 vl->time += vl->interval;
217 return (0);
218 } /* }}} int send_value */
220 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
221 {
222 char *endptr;
223 int tmp;
225 errno = 0;
226 endptr = NULL;
227 tmp = (int) strtol (str, &endptr, /* base = */ 0);
228 if (errno != 0)
229 {
230 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
231 str, strerror (errno));
232 exit (EXIT_FAILURE);
233 }
234 else if (endptr == str)
235 {
236 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
237 exit (EXIT_FAILURE);
238 }
239 else if (*endptr != 0)
240 {
241 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
242 exit (EXIT_FAILURE);
243 }
245 *ret_value = tmp;
246 return (0);
247 } /* }}} int get_integer_opt */
249 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
250 {
251 char *endptr;
252 double tmp;
254 errno = 0;
255 endptr = NULL;
256 tmp = strtod (str, &endptr);
257 if (errno != 0)
258 {
259 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
260 str, strerror (errno));
261 exit (EXIT_FAILURE);
262 }
263 else if (endptr == str)
264 {
265 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
266 exit (EXIT_FAILURE);
267 }
268 else if (*endptr != 0)
269 {
270 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
271 exit (EXIT_FAILURE);
272 }
274 *ret_value = tmp;
275 return (0);
276 } /* }}} int get_double_opt */
278 static int read_options (int argc, char **argv) /* {{{ */
279 {
280 int opt;
282 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
283 {
284 switch (opt)
285 {
286 case 'n':
287 get_integer_opt (optarg, &conf_num_values);
288 break;
290 case 'H':
291 get_integer_opt (optarg, &conf_num_hosts);
292 break;
294 case 'p':
295 get_integer_opt (optarg, &conf_num_plugins);
296 break;
298 case 'i':
299 get_double_opt (optarg, &conf_interval);
300 break;
302 case 'd':
303 conf_destination = optarg;
304 break;
306 case 'D':
307 conf_service = optarg;
308 break;
310 case 'h':
311 exit_usage (EXIT_SUCCESS);
313 default:
314 exit_usage (EXIT_FAILURE);
315 } /* switch (opt) */
316 } /* while (getopt) */
318 return (0);
319 } /* }}} int read_options */
321 int main (int argc, char **argv) /* {{{ */
322 {
323 int i;
324 time_t last_time;
325 int values_sent = 0;
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)
338 {
339 fprintf (stderr, "c_heap_create failed.\n");
340 exit (EXIT_FAILURE);
341 }
343 net = lcc_network_create ();
344 if (net == NULL)
345 {
346 fprintf (stderr, "lcc_network_create failed.\n");
347 exit (EXIT_FAILURE);
348 }
349 else
350 {
351 lcc_server_t *srv;
353 srv = lcc_server_create (net, conf_destination, conf_service);
354 if (srv == NULL)
355 {
356 fprintf (stderr, "lcc_server_create failed.\n");
357 exit (EXIT_FAILURE);
358 }
360 lcc_server_set_ttl (srv, 42);
361 #if 0
362 lcc_server_set_security_level (srv, ENCRYPT,
363 "admin", "password1");
364 #endif
365 }
367 fprintf (stdout, "Creating %i values ... ", conf_num_values);
368 fflush (stdout);
369 for (i = 0; i < conf_num_values; i++)
370 {
371 lcc_value_list_t *vl;
373 vl = create_value_list ();
374 if (vl == NULL)
375 {
376 fprintf (stderr, "create_value_list failed.\n");
377 exit (EXIT_FAILURE);
378 }
380 c_heap_insert (values_heap, vl);
381 }
382 fprintf (stdout, "done\n");
384 last_time = 0;
385 while (loop)
386 {
387 lcc_value_list_t *vl = c_heap_get_root (values_heap);
389 if (vl == NULL)
390 break;
392 if (vl->time != last_time)
393 {
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)
400 {
401 /* 1 / 100 second */
402 struct timespec ts = { 0, 10000000 };
403 nanosleep (&ts, /* remaining = */ NULL);
404 now = time (NULL);
406 if (!loop)
407 break;
408 }
409 last_time = vl->time;
410 }
412 send_value (vl);
413 values_sent++;
415 c_heap_insert (values_heap, vl);
416 }
418 fprintf (stdout, "Shutting down.\n");
419 fflush (stdout);
421 while (42)
422 {
423 lcc_value_list_t *vl = c_heap_get_root (values_heap);
424 if (vl == NULL)
425 break;
426 destroy_value_list (vl);
427 }
428 c_heap_destroy (values_heap);
430 lcc_network_destroy (net);
431 exit (EXIT_SUCCESS);
432 return (0);
433 } /* }}} int main */
435 /* vim: set sw=2 sts=2 et fdm=marker : */