1 /**
2 * collectd-td - collectd traffic generator
3 * Copyright (C) 2010 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 <ff at octo.it>
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
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 int 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: %i)\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 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 = time (NULL) + (host_num % vl->interval) + 1;
173 if (get_boundet_random (0, 2) == 0)
174 vl->values_types[0] = LCC_TYPE_GAUGE;
175 else
176 vl->values_types[0] = LCC_TYPE_DERIVE;
178 snprintf (vl->identifier.host, sizeof (vl->identifier.host),
179 "host%04i", host_num);
180 snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
181 "plugin%03i", get_boundet_random (0, conf_num_plugins));
182 strncpy (vl->identifier.type,
183 (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
184 sizeof (vl->identifier.type));
185 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
186 "ti%li", random ());
188 return (vl);
189 } /* }}} int create_value_list */
191 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
192 {
193 if (vl == NULL)
194 return;
196 free (vl->values);
197 free (vl->values_types);
198 free (vl);
199 } /* }}} void destroy_value_list */
201 static int send_value (lcc_value_list_t *vl) /* {{{ */
202 {
203 int status;
205 if (vl->values_types[0] == LCC_TYPE_GAUGE)
206 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
207 else
208 vl->values[0].derive += get_boundet_random (0, 100);
210 status = lcc_network_values_send (net, vl);
211 if (status != 0)
212 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
214 vl->time += vl->interval;
216 return (0);
217 } /* }}} int send_value */
219 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
220 {
221 char *endptr;
222 int tmp;
224 errno = 0;
225 endptr = NULL;
226 tmp = (int) strtol (str, &endptr, /* base = */ 0);
227 if (errno != 0)
228 {
229 fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
230 str, strerror (errno));
231 exit (EXIT_FAILURE);
232 }
233 else if (endptr == str)
234 {
235 fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
236 exit (EXIT_FAILURE);
237 }
238 else if (*endptr != 0)
239 {
240 fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
241 exit (EXIT_FAILURE);
242 }
244 *ret_value = tmp;
245 return (0);
246 } /* }}} int get_integer_opt */
248 static int read_options (int argc, char **argv) /* {{{ */
249 {
250 int opt;
252 while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
253 {
254 switch (opt)
255 {
256 case 'n':
257 get_integer_opt (optarg, &conf_num_values);
258 break;
260 case 'H':
261 get_integer_opt (optarg, &conf_num_hosts);
262 break;
264 case 'p':
265 get_integer_opt (optarg, &conf_num_plugins);
266 break;
268 case 'i':
269 get_integer_opt (optarg, &conf_interval);
270 break;
272 case 'd':
273 conf_destination = optarg;
274 break;
276 case 'D':
277 conf_service = optarg;
278 break;
280 case 'h':
281 exit_usage (EXIT_SUCCESS);
283 default:
284 exit_usage (EXIT_FAILURE);
285 } /* switch (opt) */
286 } /* while (getopt) */
288 return (0);
289 } /* }}} int read_options */
291 int main (int argc, char **argv) /* {{{ */
292 {
293 int i;
294 time_t last_time;
295 int values_sent = 0;
297 read_options (argc, argv);
299 sigint_action.sa_handler = signal_handler;
300 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
302 sigterm_action.sa_handler = signal_handler;
303 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
306 values_heap = c_heap_create (compare_time);
307 if (values_heap == NULL)
308 {
309 fprintf (stderr, "c_heap_create failed.\n");
310 exit (EXIT_FAILURE);
311 }
313 net = lcc_network_create ();
314 if (net == NULL)
315 {
316 fprintf (stderr, "lcc_network_create failed.\n");
317 exit (EXIT_FAILURE);
318 }
319 else
320 {
321 lcc_server_t *srv;
323 srv = lcc_server_create (net, conf_destination, conf_service);
324 if (srv == NULL)
325 {
326 fprintf (stderr, "lcc_server_create failed.\n");
327 exit (EXIT_FAILURE);
328 }
330 lcc_server_set_ttl (srv, 42);
331 #if 0
332 lcc_server_set_security_level (srv, ENCRYPT,
333 "admin", "password1");
334 #endif
335 }
337 fprintf (stdout, "Creating %i values ... ", conf_num_values);
338 fflush (stdout);
339 for (i = 0; i < conf_num_values; i++)
340 {
341 lcc_value_list_t *vl;
343 vl = create_value_list ();
344 if (vl == NULL)
345 {
346 fprintf (stderr, "create_value_list failed.\n");
347 exit (EXIT_FAILURE);
348 }
350 c_heap_insert (values_heap, vl);
351 }
352 fprintf (stdout, "done\n");
354 last_time = 0;
355 while (loop)
356 {
357 lcc_value_list_t *vl = c_heap_get_root (values_heap);
359 if (vl == NULL)
360 break;
362 if (vl->time != last_time)
363 {
364 printf ("%i values have been sent.\n", values_sent);
366 /* Check if we need to sleep */
367 time_t now = time (NULL);
369 while (now < vl->time)
370 {
371 /* 1 / 100 second */
372 struct timespec ts = { 0, 10000000 };
373 nanosleep (&ts, /* remaining = */ NULL);
374 now = time (NULL);
376 if (!loop)
377 break;
378 }
379 last_time = vl->time;
380 }
382 send_value (vl);
383 values_sent++;
385 c_heap_insert (values_heap, vl);
386 }
388 fprintf (stdout, "Shutting down.\n");
389 fflush (stdout);
391 while (42)
392 {
393 lcc_value_list_t *vl = c_heap_get_root (values_heap);
394 if (vl == NULL)
395 break;
396 destroy_value_list (vl);
397 }
398 c_heap_destroy (values_heap);
400 lcc_network_destroy (net);
401 exit (EXIT_SUCCESS);
402 return (0);
403 } /* }}} int main */
405 /* vim: set sw=2 sts=2 et fdm=marker : */