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 vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0;
187 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
188 "ti%li", random ());
190 return (vl);
191 } /* }}} int create_value_list */
193 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
194 {
195 if (vl == NULL)
196 return;
198 free (vl->values);
199 free (vl->values_types);
200 free (vl);
201 } /* }}} void destroy_value_list */
203 static int send_value (lcc_value_list_t *vl) /* {{{ */
204 {
205 int status;
207 if (vl->values_types[0] == LCC_TYPE_GAUGE)
208 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
209 else
210 vl->values[0].derive += get_boundet_random (0, 100);
212 status = lcc_network_values_send (net, vl);
213 if (status != 0)
214 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
216 vl->time += vl->interval;
218 return (0);
219 } /* }}} int send_value */
221 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
222 {
223 char *endptr;
224 int tmp;
226 errno = 0;
227 endptr = NULL;
228 tmp = (int) strtol (str, &endptr, /* base = */ 0);
229 if (errno != 0)
230 {
231 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
232 str, strerror (errno));
233 exit (EXIT_FAILURE);
234 }
235 else if (endptr == str)
236 {
237 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
238 exit (EXIT_FAILURE);
239 }
240 else if (*endptr != 0)
241 {
242 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
243 exit (EXIT_FAILURE);
244 }
246 *ret_value = tmp;
247 return (0);
248 } /* }}} int get_integer_opt */
250 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
251 {
252 char *endptr;
253 double tmp;
255 errno = 0;
256 endptr = NULL;
257 tmp = strtod (str, &endptr);
258 if (errno != 0)
259 {
260 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
261 str, strerror (errno));
262 exit (EXIT_FAILURE);
263 }
264 else if (endptr == str)
265 {
266 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
267 exit (EXIT_FAILURE);
268 }
269 else if (*endptr != 0)
270 {
271 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
272 exit (EXIT_FAILURE);
273 }
275 *ret_value = tmp;
276 return (0);
277 } /* }}} int get_double_opt */
279 static int read_options (int argc, char **argv) /* {{{ */
280 {
281 int opt;
283 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
284 {
285 switch (opt)
286 {
287 case 'n':
288 get_integer_opt (optarg, &conf_num_values);
289 break;
291 case 'H':
292 get_integer_opt (optarg, &conf_num_hosts);
293 break;
295 case 'p':
296 get_integer_opt (optarg, &conf_num_plugins);
297 break;
299 case 'i':
300 get_double_opt (optarg, &conf_interval);
301 break;
303 case 'd':
304 conf_destination = optarg;
305 break;
307 case 'D':
308 conf_service = optarg;
309 break;
311 case 'h':
312 exit_usage (EXIT_SUCCESS);
314 default:
315 exit_usage (EXIT_FAILURE);
316 } /* switch (opt) */
317 } /* while (getopt) */
319 return (0);
320 } /* }}} int read_options */
322 int main (int argc, char **argv) /* {{{ */
323 {
324 int i;
325 time_t last_time;
326 int values_sent = 0;
328 read_options (argc, argv);
330 sigint_action.sa_handler = signal_handler;
331 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
333 sigterm_action.sa_handler = signal_handler;
334 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
337 values_heap = c_heap_create (compare_time);
338 if (values_heap == NULL)
339 {
340 fprintf (stderr, "c_heap_create failed.\n");
341 exit (EXIT_FAILURE);
342 }
344 net = lcc_network_create ();
345 if (net == NULL)
346 {
347 fprintf (stderr, "lcc_network_create failed.\n");
348 exit (EXIT_FAILURE);
349 }
350 else
351 {
352 lcc_server_t *srv;
354 srv = lcc_server_create (net, conf_destination, conf_service);
355 if (srv == NULL)
356 {
357 fprintf (stderr, "lcc_server_create failed.\n");
358 exit (EXIT_FAILURE);
359 }
361 lcc_server_set_ttl (srv, 42);
362 #if 0
363 lcc_server_set_security_level (srv, ENCRYPT,
364 "admin", "password1");
365 #endif
366 }
368 fprintf (stdout, "Creating %i values ... ", conf_num_values);
369 fflush (stdout);
370 for (i = 0; i < conf_num_values; i++)
371 {
372 lcc_value_list_t *vl;
374 vl = create_value_list ();
375 if (vl == NULL)
376 {
377 fprintf (stderr, "create_value_list failed.\n");
378 exit (EXIT_FAILURE);
379 }
381 c_heap_insert (values_heap, vl);
382 }
383 fprintf (stdout, "done\n");
385 last_time = 0;
386 while (loop)
387 {
388 lcc_value_list_t *vl = c_heap_get_root (values_heap);
390 if (vl == NULL)
391 break;
393 if (vl->time != last_time)
394 {
395 printf ("%i values have been sent.\n", values_sent);
397 /* Check if we need to sleep */
398 time_t now = time (NULL);
400 while (now < vl->time)
401 {
402 /* 1 / 100 second */
403 struct timespec ts = { 0, 10000000 };
404 nanosleep (&ts, /* remaining = */ NULL);
405 now = time (NULL);
407 if (!loop)
408 break;
409 }
410 last_time = vl->time;
411 }
413 send_value (vl);
414 values_sent++;
416 c_heap_insert (values_heap, vl);
417 }
419 fprintf (stdout, "Shutting down.\n");
420 fflush (stdout);
422 while (42)
423 {
424 lcc_value_list_t *vl = c_heap_get_root (values_heap);
425 if (vl == NULL)
426 break;
427 destroy_value_list (vl);
428 }
429 c_heap_destroy (values_heap);
431 lcc_network_destroy (net);
432 exit (EXIT_SUCCESS);
433 return (0);
434 } /* }}} int main */
436 /* vim: set sw=2 sts=2 et fdm=marker : */