501567da6ed2db0f4df82c0c60bc1cd8105f4386
[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 #ifndef _ISOC99_SOURCE
32 # define _ISOC99_SOURCE
33 #endif
34
35 #ifndef _POSIX_C_SOURCE
36 # define _POSIX_C_SOURCE 200809L
37 #endif
38
39 #ifndef _XOPEN_SOURCE
40 # define _XOPEN_SOURCE 700
41 #endif
42
43 #if !__GNUC__
44 # define __attribute__(x) /**/
45 #endif
46
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>
54 #include <math.h>
55
56 #include "utils_heap.h"
57
58 #include "libcollectdclient/collectd/client.h"
59 #include "libcollectdclient/collectd/network.h"
60 #include "libcollectdclient/collectd/network_buffer.h"
61
62 #define DEF_NUM_HOSTS    1000
63 #define DEF_NUM_PLUGINS    20
64 #define DEF_NUM_VALUES 100000
65 #define DEF_INTERVAL       10.0
66
67 static int conf_num_hosts = DEF_NUM_HOSTS;
68 static int conf_num_plugins = DEF_NUM_PLUGINS;
69 static int conf_num_values = DEF_NUM_VALUES;
70 static double conf_interval = DEF_INTERVAL;
71 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
72 static const char *conf_service = NET_DEFAULT_PORT;
73
74 static lcc_network_t *net;
75
76 static c_heap_t *values_heap = NULL;
77
78 static struct sigaction sigint_action;
79 static struct sigaction sigterm_action;
80
81 static _Bool loop = 1;
82
83 __attribute__((noreturn))
84 static void exit_usage (int exit_status) /* {{{ */
85 {
86   fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout,
87       "collectd-tg -- collectd traffic generator\n"
88       "\n"
89       "  Usage: collectd-ng [OPTION]\n"
90       "\n"
91       "  Valid options:\n"
92       "    -n <number>    Number of value lists. (Default: %i)\n"
93       "    -H <number>    Number of hosts to emulate. (Default: %i)\n"
94       "    -p <number>    Number of plugins to emulate. (Default: %i)\n"
95       "    -i <seconds>   Interval of each value in seconds. (Default: %.3f)\n"
96       "    -d <dest>      Destination address of the network packets.\n"
97       "                   (Default: %s)\n"
98       "    -D <port>      Destination port of the network packets.\n"
99       "                   (Default: %s)\n"
100       "    -h             Print usage information (this output).\n"
101       "\n"
102       "Copyright (C) 2010-2012  Florian Forster\n"
103       "Licensed under the MIT license.\n",
104       DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS,
105       DEF_INTERVAL,
106       NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
107   exit (exit_status);
108 } /* }}} void exit_usage */
109
110 static void signal_handler (int signal) /* {{{ */
111 {
112   loop = 0;
113 } /* }}} void signal_handler */
114
115 static double dtime (void) /* {{{ */
116 {
117   struct timespec ts = { 0 };
118
119   if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
120     return NAN;
121
122   return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9);
123 } /* }}} double dtime */
124
125 static int compare_time (const void *v0, const void *v1) /* {{{ */
126 {
127   const lcc_value_list_t *vl0 = v0;
128   const lcc_value_list_t *vl1 = v1;
129
130   if (vl0->time < vl1->time)
131     return (-1);
132   else if (vl0->time > vl1->time)
133     return (1);
134   else
135     return (0);
136 } /* }}} int compare_time */
137
138 static int get_boundet_random (int min, int max) /* {{{ */
139 {
140   int range;
141
142   if (min >= max)
143     return (-1);
144   if (min == (max - 1))
145     return (min);
146
147   range = max - min;
148
149   return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
150 } /* }}} int get_boundet_random */
151
152 static lcc_value_list_t *create_value_list (void) /* {{{ */
153 {
154   lcc_value_list_t *vl;
155   int host_num;
156
157   vl = malloc (sizeof (*vl));
158   if (vl == NULL)
159   {
160     fprintf (stderr, "malloc failed.\n");
161     return (NULL);
162   }
163   memset (vl, 0, sizeof (*vl));
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   snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
203       "ti%li", random ());
204
205   return (vl);
206 } /* }}} int create_value_list */
207
208 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
209 {
210   if (vl == NULL)
211     return;
212
213   free (vl->values);
214   free (vl->values_types);
215   free (vl);
216 } /* }}} void destroy_value_list */
217
218 static int send_value (lcc_value_list_t *vl) /* {{{ */
219 {
220   int status;
221
222   if (vl->values_types[0] == LCC_TYPE_GAUGE)
223     vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
224   else
225     vl->values[0].derive += (derive_t) get_boundet_random (0, 100);
226
227   status = lcc_network_values_send (net, vl);
228   if (status != 0)
229     fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
230
231   vl->time += vl->interval;
232
233   return (0);
234 } /* }}} int send_value */
235
236 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
237 {
238   char *endptr;
239   int tmp;
240
241   errno = 0;
242   endptr = NULL;
243   tmp = (int) strtol (str, &endptr, /* base = */ 0);
244   if (errno != 0)
245   {
246     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
247         str, strerror (errno));
248     exit (EXIT_FAILURE);
249   }
250   else if (endptr == str)
251   {
252     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
253     exit (EXIT_FAILURE);
254   }
255   else if (*endptr != 0)
256   {
257     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
258     exit (EXIT_FAILURE);
259   }
260
261   *ret_value = tmp;
262   return (0);
263 } /* }}} int get_integer_opt */
264
265 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
266 {
267   char *endptr;
268   double tmp;
269
270   errno = 0;
271   endptr = NULL;
272   tmp = strtod (str, &endptr);
273   if (errno != 0)
274   {
275     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
276         str, strerror (errno));
277     exit (EXIT_FAILURE);
278   }
279   else if (endptr == str)
280   {
281     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
282     exit (EXIT_FAILURE);
283   }
284   else if (*endptr != 0)
285   {
286     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
287     exit (EXIT_FAILURE);
288   }
289
290   *ret_value = tmp;
291   return (0);
292 } /* }}} int get_double_opt */
293
294 static int read_options (int argc, char **argv) /* {{{ */
295 {
296   int opt;
297
298   while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
299   {
300     switch (opt)
301     {
302       case 'n':
303         get_integer_opt (optarg, &conf_num_values);
304         break;
305
306       case 'H':
307         get_integer_opt (optarg, &conf_num_hosts);
308         break;
309
310       case 'p':
311         get_integer_opt (optarg, &conf_num_plugins);
312         break;
313
314       case 'i':
315         get_double_opt (optarg, &conf_interval);
316         break;
317
318       case 'd':
319         conf_destination = optarg;
320         break;
321
322       case 'D':
323         conf_service = optarg;
324         break;
325
326       case 'h':
327         exit_usage (EXIT_SUCCESS);
328
329       default:
330         exit_usage (EXIT_FAILURE);
331     } /* switch (opt) */
332   } /* while (getopt) */
333
334   return (0);
335 } /* }}} int read_options */
336
337 int main (int argc, char **argv) /* {{{ */
338 {
339   int i;
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 (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         /* 1 / 100 second */
418         struct timespec ts = { 0, 10000000 };
419
420         ts.tv_sec = (time_t) now;
421         ts.tv_nsec = (long) ((now - ((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   return (0);
453 } /* }}} int main */
454
455 /* vim: set sw=2 sts=2 et fdm=marker : */