Merge pull request #3339 from jkohen/patch-1
[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
44 #include "utils_heap.h"
45
46 #include "libcollectdclient/collectd/client.h"
47 #include "libcollectdclient/collectd/network.h"
48 #include "libcollectdclient/collectd/network_buffer.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 static double dtime (void) /* {{{ */
104 {
105   struct timespec ts = { 0 };
106
107   if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
108     perror ("clock_gettime");
109
110   return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9);
111 } /* }}} double dtime */
112
113 static int compare_time (const void *v0, const void *v1) /* {{{ */
114 {
115   const lcc_value_list_t *vl0 = v0;
116   const lcc_value_list_t *vl1 = v1;
117
118   if (vl0->time < vl1->time)
119     return (-1);
120   else if (vl0->time > vl1->time)
121     return (1);
122   else
123     return (0);
124 } /* }}} int compare_time */
125
126 static int get_boundet_random (int min, int max) /* {{{ */
127 {
128   int range;
129
130   if (min >= max)
131     return (-1);
132   if (min == (max - 1))
133     return (min);
134
135   range = max - min;
136
137   return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
138 } /* }}} int get_boundet_random */
139
140 static lcc_value_list_t *create_value_list (void) /* {{{ */
141 {
142   lcc_value_list_t *vl;
143   int host_num;
144
145   vl = malloc (sizeof (*vl));
146   if (vl == NULL)
147   {
148     fprintf (stderr, "malloc failed.\n");
149     return (NULL);
150   }
151   memset (vl, 0, sizeof (*vl));
152
153   vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
154   if (vl->values == NULL)
155   {
156     fprintf (stderr, "calloc failed.\n");
157     free (vl);
158     return (NULL);
159   }
160
161   vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
162   if (vl->values_types == NULL)
163   {
164     fprintf (stderr, "calloc failed.\n");
165     free (vl->values);
166     free (vl);
167     return (NULL);
168   }
169
170   vl->values_len = 1;
171
172   host_num = get_boundet_random (0, conf_num_hosts);
173
174   vl->interval = conf_interval;
175   vl->time = 1.0 + dtime ()
176     + (host_num % (1 + (int) vl->interval));
177
178   if (get_boundet_random (0, 2) == 0)
179     vl->values_types[0] = LCC_TYPE_GAUGE;
180   else
181     vl->values_types[0] = LCC_TYPE_DERIVE;
182
183   snprintf (vl->identifier.host, sizeof (vl->identifier.host),
184       "host%04i", host_num);
185   snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
186       "plugin%03i", get_boundet_random (0, conf_num_plugins));
187   strncpy (vl->identifier.type,
188       (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
189       sizeof (vl->identifier.type));
190   snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
191       "ti%li", random ());
192
193   return (vl);
194 } /* }}} int create_value_list */
195
196 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
197 {
198   if (vl == NULL)
199     return;
200
201   free (vl->values);
202   free (vl->values_types);
203   free (vl);
204 } /* }}} void destroy_value_list */
205
206 static int send_value (lcc_value_list_t *vl) /* {{{ */
207 {
208   int status;
209
210   if (vl->values_types[0] == LCC_TYPE_GAUGE)
211     vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
212   else
213     vl->values[0].derive += (derive_t) get_boundet_random (0, 100);
214
215   status = lcc_network_values_send (net, vl);
216   if (status != 0)
217     fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
218
219   vl->time += vl->interval;
220
221   return (0);
222 } /* }}} int send_value */
223
224 static int get_integer_opt (const char *str, int *ret_value) /* {{{ */
225 {
226   char *endptr;
227   int tmp;
228
229   errno = 0;
230   endptr = NULL;
231   tmp = (int) strtol (str, &endptr, /* base = */ 0);
232   if (errno != 0)
233   {
234     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
235         str, strerror (errno));
236     exit (EXIT_FAILURE);
237   }
238   else if (endptr == str)
239   {
240     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
241     exit (EXIT_FAILURE);
242   }
243   else if (*endptr != 0)
244   {
245     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
246     exit (EXIT_FAILURE);
247   }
248
249   *ret_value = tmp;
250   return (0);
251 } /* }}} int get_integer_opt */
252
253 static int get_double_opt (const char *str, double *ret_value) /* {{{ */
254 {
255   char *endptr;
256   double tmp;
257
258   errno = 0;
259   endptr = NULL;
260   tmp = strtod (str, &endptr);
261   if (errno != 0)
262   {
263     fprintf (stderr, "Unable to parse option as a number: \"%s\": %s\n",
264         str, strerror (errno));
265     exit (EXIT_FAILURE);
266   }
267   else if (endptr == str)
268   {
269     fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", str);
270     exit (EXIT_FAILURE);
271   }
272   else if (*endptr != 0)
273   {
274     fprintf (stderr, "Garbage after end of value: \"%s\"\n", str);
275     exit (EXIT_FAILURE);
276   }
277
278   *ret_value = tmp;
279   return (0);
280 } /* }}} int get_double_opt */
281
282 static int read_options (int argc, char **argv) /* {{{ */
283 {
284   int opt;
285
286   while ((opt = getopt (argc, argv, "n:H:p:i:d:D:h")) != -1)
287   {
288     switch (opt)
289     {
290       case 'n':
291         get_integer_opt (optarg, &conf_num_values);
292         break;
293
294       case 'H':
295         get_integer_opt (optarg, &conf_num_hosts);
296         break;
297
298       case 'p':
299         get_integer_opt (optarg, &conf_num_plugins);
300         break;
301
302       case 'i':
303         get_double_opt (optarg, &conf_interval);
304         break;
305
306       case 'd':
307         conf_destination = optarg;
308         break;
309
310       case 'D':
311         conf_service = optarg;
312         break;
313
314       case 'h':
315         exit_usage (EXIT_SUCCESS);
316
317       default:
318         exit_usage (EXIT_FAILURE);
319     } /* switch (opt) */
320   } /* while (getopt) */
321
322   return (0);
323 } /* }}} int read_options */
324
325 int main (int argc, char **argv) /* {{{ */
326 {
327   int i;
328   double last_time;
329   int values_sent = 0;
330
331   read_options (argc, argv);
332
333   sigint_action.sa_handler = signal_handler;
334   sigaction (SIGINT, &sigint_action, /* old = */ NULL);
335
336   sigterm_action.sa_handler = signal_handler;
337   sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
338
339
340   values_heap = c_heap_create (compare_time);
341   if (values_heap == NULL)
342   {
343     fprintf (stderr, "c_heap_create failed.\n");
344     exit (EXIT_FAILURE);
345   }
346
347   net = lcc_network_create ();
348   if (net == NULL)
349   {
350     fprintf (stderr, "lcc_network_create failed.\n");
351     exit (EXIT_FAILURE);
352   }
353   else
354   {
355     lcc_server_t *srv;
356     
357     srv = lcc_server_create (net, conf_destination, conf_service);
358     if (srv == NULL)
359     {
360       fprintf (stderr, "lcc_server_create failed.\n");
361       exit (EXIT_FAILURE);
362     }
363
364     lcc_server_set_ttl (srv, 42);
365 #if 0
366     lcc_server_set_security_level (srv, ENCRYPT,
367         "admin", "password1");
368 #endif
369   }
370
371   fprintf (stdout, "Creating %i values ... ", conf_num_values);
372   fflush (stdout);
373   for (i = 0; i < conf_num_values; i++)
374   {
375     lcc_value_list_t *vl;
376
377     vl = create_value_list ();
378     if (vl == NULL)
379     {
380       fprintf (stderr, "create_value_list failed.\n");
381       exit (EXIT_FAILURE);
382     }
383
384     c_heap_insert (values_heap, vl);
385   }
386   fprintf (stdout, "done\n");
387
388   last_time = 0;
389   while (loop)
390   {
391     lcc_value_list_t *vl = c_heap_get_root (values_heap);
392
393     if (vl == NULL)
394       break;
395
396     if (vl->time != last_time)
397     {
398       printf ("%i values have been sent.\n", values_sent);
399
400       /* Check if we need to sleep */
401       double now = dtime ();
402
403       while (now < vl->time)
404       {
405         /* 1 / 100 second */
406         struct timespec ts = { 0, 10000000 };
407
408         ts.tv_sec = (time_t) now;
409         ts.tv_nsec = (long) ((now - ((double) ts.tv_sec)) * 1e9);
410
411         nanosleep (&ts, /* remaining = */ NULL);
412         now = dtime ();
413
414         if (!loop)
415           break;
416       }
417       last_time = vl->time;
418     }
419
420     send_value (vl);
421     values_sent++;
422
423     c_heap_insert (values_heap, vl);
424   }
425
426   fprintf (stdout, "Shutting down.\n");
427   fflush (stdout);
428
429   while (42)
430   {
431     lcc_value_list_t *vl = c_heap_get_root (values_heap);
432     if (vl == NULL)
433       break;
434     destroy_value_list (vl);
435   }
436   c_heap_destroy (values_heap);
437
438   lcc_network_destroy (net);
439   exit (EXIT_SUCCESS);
440   return (0);
441 } /* }}} int main */
442
443 /* vim: set sw=2 sts=2 et fdm=marker : */