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 <errno.h>
36 #include <math.h>
37 #include <signal.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/time.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "utils/heap/heap.h"
47
48 #include "collectd/client.h"
49 #include "collectd/network.h"
50
51 #define DEF_NUM_HOSTS 1000
52 #define DEF_NUM_PLUGINS 20
53 #define DEF_NUM_VALUES 100000
54 #define DEF_INTERVAL 10.0
55
56 static int conf_num_hosts = DEF_NUM_HOSTS;
57 static int conf_num_plugins = DEF_NUM_PLUGINS;
58 static int conf_num_values = DEF_NUM_VALUES;
59 static double conf_interval = DEF_INTERVAL;
60 static const char *conf_destination = NET_DEFAULT_V6_ADDR;
61 static const char *conf_service = NET_DEFAULT_PORT;
62
63 static lcc_network_t *net;
64
65 static c_heap_t *values_heap;
66
67 static struct sigaction sigint_action;
68 static struct sigaction sigterm_action;
69
70 static bool loop = true;
71
72 __attribute__((noreturn)) static void exit_usage(int exit_status) /* {{{ */
73 {
74   fprintf(
75       (exit_status == EXIT_FAILURE) ? stderr : stdout,
76       "collectd-tg -- collectd traffic generator\n"
77       "\n"
78       "  Usage: collectd-ng [OPTION]\n"
79       "\n"
80       "  Valid options:\n"
81       "    -n <number>    Number of value lists. (Default: %i)\n"
82       "    -H <number>    Number of hosts to emulate. (Default: %i)\n"
83       "    -p <number>    Number of plugins to emulate. (Default: %i)\n"
84       "    -i <seconds>   Interval of each value in seconds. (Default: %.3f)\n"
85       "    -d <dest>      Destination address of the network packets.\n"
86       "                   (Default: %s)\n"
87       "    -D <port>      Destination port of the network packets.\n"
88       "                   (Default: %s)\n"
89       "    -h             Print usage information (this output).\n"
90       "\n"
91       "Copyright (C) 2010-2012  Florian Forster\n"
92       "Licensed under the MIT license.\n",
93       DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS, 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 __attribute__((unused)) signal) /* {{{ */
99 {
100   loop = false;
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_REALTIME, &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()) /
151                       (((double)RAND_MAX) + 1.0)));
152 } /* }}} int get_boundet_random */
153
154 static lcc_value_list_t *create_value_list(void) /* {{{ */
155 {
156   lcc_value_list_t *vl;
157   int host_num;
158
159   vl = calloc(1, sizeof(*vl));
160   if (vl == NULL) {
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     fprintf(stderr, "calloc failed.\n");
168     free(vl);
169     return NULL;
170   }
171
172   vl->values_types = calloc(/* nmemb = */ 1, sizeof(*vl->values_types));
173   if (vl->values_types == NULL) {
174     fprintf(stderr, "calloc failed.\n");
175     free(vl->values);
176     free(vl);
177     return NULL;
178   }
179
180   vl->values_len = 1;
181
182   host_num = get_boundet_random(0, conf_num_hosts);
183
184   vl->interval = conf_interval;
185   vl->time = 1.0 + dtime() + (host_num % (1 + (int)vl->interval));
186
187   if (get_boundet_random(0, 2) == 0)
188     vl->values_types[0] = LCC_TYPE_GAUGE;
189   else
190     vl->values_types[0] = LCC_TYPE_DERIVE;
191
192   snprintf(vl->identifier.host, sizeof(vl->identifier.host), "host%04i",
193            host_num);
194   snprintf(vl->identifier.plugin, sizeof(vl->identifier.plugin), "plugin%03i",
195            get_boundet_random(0, conf_num_plugins));
196   strncpy(vl->identifier.type,
197           (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
198           sizeof(vl->identifier.type));
199   vl->identifier.type[sizeof(vl->identifier.type) - 1] = '\0';
200   snprintf(vl->identifier.type_instance, sizeof(vl->identifier.type_instance),
201            "ti%li", random());
202
203   return vl;
204 } /* }}} int create_value_list */
205
206 static void destroy_value_list(lcc_value_list_t *vl) /* {{{ */
207 {
208   if (vl == NULL)
209     return;
210
211   free(vl->values);
212   free(vl->values_types);
213   free(vl);
214 } /* }}} void destroy_value_list */
215
216 static int send_value(lcc_value_list_t *vl) /* {{{ */
217 {
218   int status;
219
220   if (vl->values_types[0] == LCC_TYPE_GAUGE)
221     vl->values[0].gauge =
222         100.0 * ((gauge_t)random()) / (((gauge_t)RAND_MAX) + 1.0);
223   else
224     vl->values[0].derive += (derive_t)get_boundet_random(0, 100);
225
226   status = lcc_network_values_send(net, vl);
227   if (status != 0)
228     fprintf(stderr, "lcc_network_values_send failed with status %i.\n", status);
229
230   vl->time += vl->interval;
231
232   return 0;
233 } /* }}} int send_value */
234
235 static int get_integer_opt(const char *str, int *ret_value) /* {{{ */
236 {
237   char *endptr;
238   int tmp;
239
240   errno = 0;
241   endptr = NULL;
242   tmp = (int)strtol(str, &endptr, /* base = */ 0);
243   if (errno != 0) {
244     fprintf(stderr, "Unable to parse option as a number: \"%s\": %s\n", str,
245             strerror(errno));
246     exit(EXIT_FAILURE);
247   } else if (endptr == str) {
248     fprintf(stderr, "Unable to parse option as a number: \"%s\"\n", str);
249     exit(EXIT_FAILURE);
250   } else if (*endptr != 0) {
251     fprintf(stderr, "Garbage after end of value: \"%s\"\n", str);
252     exit(EXIT_FAILURE);
253   }
254
255   *ret_value = tmp;
256   return 0;
257 } /* }}} int get_integer_opt */
258
259 static int get_double_opt(const char *str, double *ret_value) /* {{{ */
260 {
261   char *endptr;
262   double tmp;
263
264   errno = 0;
265   endptr = NULL;
266   tmp = strtod(str, &endptr);
267   if (errno != 0) {
268     fprintf(stderr, "Unable to parse option as a number: \"%s\": %s\n", str,
269             strerror(errno));
270     exit(EXIT_FAILURE);
271   } else if (endptr == str) {
272     fprintf(stderr, "Unable to parse option as a number: \"%s\"\n", str);
273     exit(EXIT_FAILURE);
274   } else if (*endptr != 0) {
275     fprintf(stderr, "Garbage after end of value: \"%s\"\n", str);
276     exit(EXIT_FAILURE);
277   }
278
279   *ret_value = tmp;
280   return 0;
281 } /* }}} int get_double_opt */
282
283 static int read_options(int argc, char **argv) /* {{{ */
284 {
285   int opt;
286
287   while ((opt = getopt(argc, argv, "n:H:p:i:d:D:h")) != -1) {
288     switch (opt) {
289     case 'n':
290       get_integer_opt(optarg, &conf_num_values);
291       break;
292
293     case 'H':
294       get_integer_opt(optarg, &conf_num_hosts);
295       break;
296
297     case 'p':
298       get_integer_opt(optarg, &conf_num_plugins);
299       break;
300
301     case 'i':
302       get_double_opt(optarg, &conf_interval);
303       break;
304
305     case 'd':
306       conf_destination = optarg;
307       break;
308
309     case 'D':
310       conf_service = optarg;
311       break;
312
313     case 'h':
314       exit_usage(EXIT_SUCCESS);
315
316     default:
317       exit_usage(EXIT_FAILURE);
318     } /* switch (opt) */
319   }   /* while (getopt) */
320
321   return 0;
322 } /* }}} int read_options */
323
324 int main(int argc, char **argv) /* {{{ */
325 {
326   double last_time;
327   int values_sent = 0;
328
329   read_options(argc, argv);
330
331   sigint_action.sa_handler = signal_handler;
332   sigaction(SIGINT, &sigint_action, /* old = */ NULL);
333
334   sigterm_action.sa_handler = signal_handler;
335   sigaction(SIGTERM, &sigterm_action, /* old = */ NULL);
336
337   values_heap = c_heap_create(compare_time);
338   if (values_heap == NULL) {
339     fprintf(stderr, "c_heap_create failed.\n");
340     exit(EXIT_FAILURE);
341   }
342
343   net = lcc_network_create();
344   if (net == NULL) {
345     fprintf(stderr, "lcc_network_create failed.\n");
346     exit(EXIT_FAILURE);
347   } else {
348     lcc_server_t *srv;
349
350     srv = lcc_server_create(net, conf_destination, conf_service);
351     if (srv == NULL) {
352       fprintf(stderr, "lcc_server_create failed.\n");
353       exit(EXIT_FAILURE);
354     }
355
356     lcc_server_set_ttl(srv, 42);
357 #if 0
358     lcc_server_set_security_level (srv, ENCRYPT,
359         "admin", "password1");
360 #endif
361   }
362
363   fprintf(stdout, "Creating %i values ... ", conf_num_values);
364   fflush(stdout);
365   for (int i = 0; i < conf_num_values; i++) {
366     lcc_value_list_t *vl;
367
368     vl = create_value_list();
369     if (vl == NULL) {
370       fprintf(stderr, "create_value_list failed.\n");
371       exit(EXIT_FAILURE);
372     }
373
374     c_heap_insert(values_heap, vl);
375   }
376   fprintf(stdout, "done\n");
377
378   last_time = 0;
379   while (loop) {
380     lcc_value_list_t *vl = c_heap_get_root(values_heap);
381
382     if (vl == NULL)
383       break;
384
385     if (vl->time != last_time) {
386       printf("%i values have been sent.\n", values_sent);
387
388       /* Check if we need to sleep */
389       double now = dtime();
390
391       while (now < vl->time) {
392         double diff = vl->time - now;
393         struct timespec ts = {
394             .tv_sec = (time_t)diff,
395         };
396         ts.tv_nsec = (long)((diff - ((double)ts.tv_sec)) * 1e9);
397
398         nanosleep(&ts, /* remaining = */ NULL);
399         now = dtime();
400
401         if (!loop)
402           break;
403       }
404       last_time = vl->time;
405     }
406
407     send_value(vl);
408     values_sent++;
409
410     c_heap_insert(values_heap, vl);
411   }
412
413   fprintf(stdout, "Shutting down.\n");
414   fflush(stdout);
415
416   while (42) {
417     lcc_value_list_t *vl = c_heap_get_root(values_heap);
418     if (vl == NULL)
419       break;
420     destroy_value_list(vl);
421   }
422   c_heap_destroy(values_heap);
423
424   lcc_network_destroy(net);
425   exit(EXIT_SUCCESS);
426 } /* }}} int main */