Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / collectd-tg.c
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  **/
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #if !__GNUC__
32 # define __attribute__(x) /**/
33 #endif
34
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <time.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <math.h>
43 #include <sys/time.h>
44
45 #include "utils_heap.h"
46
47 #include "libcollectdclient/collectd/client.h"
48 #include "libcollectdclient/collectd/network.h"
49
50 #define DEF_NUM_HOSTS    1000
51 #define DEF_NUM_PLUGINS    20
52 #define DEF_NUM_VALUES 100000
53 #define DEF_INTERVAL       10.0
54
55 static int conf_num_hosts = DEF_NUM_HOSTS;
56 static int conf_num_plugins = DEF_NUM_PLUGINS;
57 static int conf_num_values = DEF_NUM_VALUES;
58 static double conf_interval = DEF_INTERVAL;
59 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
60 static const char *conf_service = NET_DEFAULT_PORT;
61
62 static lcc_network_t *net;
63
64 static c_heap_t *values_heap = NULL;
65
66 static struct sigaction sigint_action;
67 static struct sigaction sigterm_action;
68
69 static _Bool loop = 1;
70
71 __attribute__((noreturn))
72 static void exit_usage (int exit_status) /* {{{ */
73 {
74   fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
75       "collectd-tg -- collectd traffic generator\n"
76       "\n"
77       "  Usage: collectd-ng [OPTION]\n"
78       "\n"
79       "  Valid options:\n"
80       "    -n <number>    Number of value lists. (Default: %i)\n"
81       "    -H <number>    Number of hosts to emulate. (Default: %i)\n"
82       "    -p <number>    Number of plugins to emulate. (Default: %i)\n"
83       "    -i <seconds>   Interval of each value in seconds. (Default: %.3f)\n"
84       "    -d <dest>      Destination address of the network packets.\n"
85       "                   (Default: %s)\n"
86       "    -D <port>      Destination port of the network packets.\n"
87       "                   (Default: %s)\n"
88       "    -h             Print usage information (this output).\n"
89       "\n"
90       "Copyright (C) 2010-2012  Florian Forster\n"
91       "Licensed under the MIT license.\n",
92       DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS,
93       DEF_INTERVAL,
94       NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
95   exit (exit_status);
96 } /* }}} void exit_usage */
97
98 static void signal_handler (int signal) /* {{{ */
99 {
100   loop = 0;
101 } /* }}} void signal_handler */
102
103 #if HAVE_CLOCK_GETTIME
104 static double dtime (void) /* {{{ */
105 {
106   struct timespec ts = { 0 };
107
108   if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
109     perror ("clock_gettime");
110
111   return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9);
112 } /* }}} double dtime */
113 #else
114 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
115 static double dtime (void) /* {{{ */
116 {
117   struct timeval tv = { 0 };
118
119   if (gettimeofday (&tv, /* timezone = */ NULL) != 0)
120     perror ("gettimeofday");
121
122   return ((double) tv.tv_sec) + (((double) tv.tv_usec) / 1e6);
123 } /* }}} double dtime */
124 #endif
125
126 static int compare_time (const void *v0, const void *v1) /* {{{ */
127 {
128   const lcc_value_list_t *vl0 = v0;
129   const lcc_value_list_t *vl1 = v1;
130
131   if (vl0->time < vl1->time)
132     return (-1);
133   else if (vl0->time > vl1->time)
134     return (1);
135   else
136     return (0);
137 } /* }}} int compare_time */
138
139 static int get_boundet_random (int min, int max) /* {{{ */
140 {
141   int range;
142
143   if (min >= max)
144     return (-1);
145   if (min == (max - 1))
146     return (min);
147
148   range = max - min;
149
150   return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
151 } /* }}} int get_boundet_random */
152
153 static lcc_value_list_t *create_value_list (void) /* {{{ */
154 {
155   lcc_value_list_t *vl;
156   int host_num;
157
158   vl = calloc (1, sizeof (*vl));
159   if (vl == NULL)
160   {
161     fprintf (stderr, "calloc failed.\n");
162     return (NULL);
163   }
164
165   vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
166   if (vl->values == NULL)
167   {
168     fprintf (stderr, "calloc failed.\n");
169     free (vl);
170     return (NULL);
171   }
172
173   vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
174   if (vl->values_types == NULL)
175   {
176     fprintf (stderr, "calloc failed.\n");
177     free (vl->values);
178     free (vl);
179     return (NULL);
180   }
181
182   vl->values_len = 1;
183
184   host_num = get_boundet_random (0, conf_num_hosts);
185
186   vl->interval = conf_interval;
187   vl->time = 1.0 + dtime ()
188     + (host_num % (1 + (int) vl->interval));
189
190   if (get_boundet_random (0, 2) == 0)
191     vl->values_types[0] = LCC_TYPE_GAUGE;
192   else
193     vl->values_types[0] = LCC_TYPE_DERIVE;
194
195   snprintf (vl->identifier.host, sizeof (vl->identifier.host),
196       "host%04i", host_num);
197   snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
198       "plugin%03i", get_boundet_random (0, conf_num_plugins));
199   strncpy (vl->identifier.type,
200       (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
201       sizeof (vl->identifier.type));
202   vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0;
203   snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
204       "ti%li", random ());
205
206   return (vl);
207 } /* }}} int create_value_list */
208
209 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
210 {
211   if (vl == NULL)
212     return;
213
214   free (vl->values);
215   free (vl->values_types);
216   free (vl);
217 } /* }}} void destroy_value_list */
218
219 static int send_value (lcc_value_list_t *vl) /* {{{ */
220 {
221   int status;
222
223   if (vl->values_types[0] == LCC_TYPE_GAUGE)
224     vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
225   else
226     vl->values[0].derive += (derive_t) get_boundet_random (0, 100);
227
228   status = lcc_network_values_send (net, vl);
229   if (status != 0)
230     fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
231
232   vl->time += vl->interval;
233
234   return (0);
235 } /* }}} int send_value */
236
237 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
238 {
239   char *endptr;
240   int tmp;
241
242   errno = 0;
243   endptr = NULL;
244   tmp = (int) strtol (str, &endptr, /* base = */ 0);
245   if (errno != 0)
246   {
247     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
248         str, strerror (errno));
249     exit (EXIT_FAILURE);
250   }
251   else if (endptr == str)
252   {
253     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
254     exit (EXIT_FAILURE);
255   }
256   else if (*endptr != 0)
257   {
258     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
259     exit (EXIT_FAILURE);
260   }
261
262   *ret_value = tmp;
263   return (0);
264 } /* }}} int get_integer_opt */
265
266 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
267 {
268   char *endptr;
269   double tmp;
270
271   errno = 0;
272   endptr = NULL;
273   tmp = strtod (str, &endptr);
274   if (errno != 0)
275   {
276     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
277         str, strerror (errno));
278     exit (EXIT_FAILURE);
279   }
280   else if (endptr == str)
281   {
282     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
283     exit (EXIT_FAILURE);
284   }
285   else if (*endptr != 0)
286   {
287     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
288     exit (EXIT_FAILURE);
289   }
290
291   *ret_value = tmp;
292   return (0);
293 } /* }}} int get_double_opt */
294
295 static int read_options (int argc, char **argv) /* {{{ */
296 {
297   int opt;
298
299   while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
300   {
301     switch (opt)
302     {
303       case 'n':
304         get_integer_opt (optarg, &conf_num_values);
305         break;
306
307       case 'H':
308         get_integer_opt (optarg, &conf_num_hosts);
309         break;
310
311       case 'p':
312         get_integer_opt (optarg, &conf_num_plugins);
313         break;
314
315       case 'i':
316         get_double_opt (optarg, &conf_interval);
317         break;
318
319       case 'd':
320         conf_destination = optarg;
321         break;
322
323       case 'D':
324         conf_service = optarg;
325         break;
326
327       case 'h':
328         exit_usage (EXIT_SUCCESS);
329
330       default:
331         exit_usage (EXIT_FAILURE);
332     } /* switch (opt) */
333   } /* while (getopt) */
334
335   return (0);
336 } /* }}} int read_options */
337
338 int main (int argc, char **argv) /* {{{ */
339 {
340   double last_time;
341   int values_sent = 0;
342
343   read_options (argc, argv);
344
345   sigint_action.sa_handler = signal_handler;
346   sigaction (SIGINT, &sigint_action, /* old = */ NULL);
347
348   sigterm_action.sa_handler = signal_handler;
349   sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
350
351
352   values_heap = c_heap_create (compare_time);
353   if (values_heap == NULL)
354   {
355     fprintf (stderr, "c_heap_create failed.\n");
356     exit (EXIT_FAILURE);
357   }
358
359   net = lcc_network_create ();
360   if (net == NULL)
361   {
362     fprintf (stderr, "lcc_network_create failed.\n");
363     exit (EXIT_FAILURE);
364   }
365   else
366   {
367     lcc_server_t *srv;
368
369     srv = lcc_server_create (net, conf_destination, conf_service);
370     if (srv == NULL)
371     {
372       fprintf (stderr, "lcc_server_create failed.\n");
373       exit (EXIT_FAILURE);
374     }
375
376     lcc_server_set_ttl (srv, 42);
377 #if 0
378     lcc_server_set_security_level (srv, ENCRYPT,
379         "admin", "password1");
380 #endif
381   }
382
383   fprintf (stdout, "Creating %i values ... ", conf_num_values);
384   fflush (stdout);
385   for (int i = 0; i < conf_num_values; i++)
386   {
387     lcc_value_list_t *vl;
388
389     vl = create_value_list ();
390     if (vl == NULL)
391     {
392       fprintf (stderr, "create_value_list failed.\n");
393       exit (EXIT_FAILURE);
394     }
395
396     c_heap_insert (values_heap, vl);
397   }
398   fprintf (stdout, "done\n");
399
400   last_time = 0;
401   while (loop)
402   {
403     lcc_value_list_t *vl = c_heap_get_root (values_heap);
404
405     if (vl == NULL)
406       break;
407
408     if (vl->time != last_time)
409     {
410       printf ("%i values have been sent.\n", values_sent);
411
412       /* Check if we need to sleep */
413       double now = dtime ();
414
415       while (now < vl->time)
416       {
417         double diff = vl->time - now;
418         struct timespec ts = {
419           .tv_sec = (time_t) diff,
420         };
421         ts.tv_nsec = (long) ((diff - ((double) ts.tv_sec)) * 1e9);
422
423         nanosleep (&ts, /* remaining = */ NULL);
424         now = dtime ();
425
426         if (!loop)
427           break;
428       }
429       last_time = vl->time;
430     }
431
432     send_value (vl);
433     values_sent++;
434
435     c_heap_insert (values_heap, vl);
436   }
437
438   fprintf (stdout, "Shutting down.\n");
439   fflush (stdout);
440
441   while (42)
442   {
443     lcc_value_list_t *vl = c_heap_get_root (values_heap);
444     if (vl == NULL)
445       break;
446     destroy_value_list (vl);
447   }
448   c_heap_destroy (values_heap);
449
450   lcc_network_destroy (net);
451   exit (EXIT_SUCCESS);
452 } /* }}} int main */
453
454 /* vim: set sw=2 sts=2 et fdm=marker : */