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