Code

c5e15194050d0201be8f295f80b54ecd04255903
[collectd.git] / src / collectd-tg.c
1 /**
2  * collectd - src/collectd-tg.c
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  **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
31 #include "libcollectdclient/collectd/client.h"
32 #include "libcollectdclient/collectd/network.h"
33 #include "libcollectdclient/collectd/network_buffer.h"
35 static int conf_num_hosts = 1000;
36 static int conf_num_plugins = 20;
37 static int conf_num_values = 100000;
39 static lcc_network_t *net;
41 static lcc_value_list_t **values;
42 static size_t values_num;
44 static int compare_time (const void *v0, const void *v1) /* {{{ */
45 {
46   lcc_value_list_t * const *vl0 = v0;
47   lcc_value_list_t * const *vl1 = v1;
49   if ((*vl0)->time < (*vl1)->time)
50     return (-1);
51   else if ((*vl0)->time > (*vl1)->time)
52     return (1);
53   else
54     return (lcc_identifier_compare (&(*vl0)->identifier, /* Ouch, somebody */
55           &(*vl1)->identifier));          /* is going to hate me for this. */
56 } /* }}} int compare_time */
58 static int get_boundet_random (int min, int max) /* {{{ */
59 {
60   int range;
62   if (min >= max)
63     return (-1);
64   if (min == (max - 1))
65     return (min);
67   range = max - min;
69   return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
70 } /* }}} int get_boundet_random */
72 #if 0
73 static int dump_network_buffer (void) /* {{{ */
74 {
75   char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT];
76   size_t buffer_size;
77   int status;
78   size_t offset;
80   memset (buffer, 0, sizeof (buffer));
81   buffer_size = sizeof (buffer);
83   status = lcc_network_buffer_get (nb, buffer, &buffer_size);
84   if (status != 0)
85   {
86     fprintf (stderr, "lcc_network_buffer_get failed with status %i.\n",
87         status);
88     return (status);
89   }
91   if (buffer_size > sizeof (buffer))
92     buffer_size = sizeof (buffer);
94   for (offset = 0; offset < buffer_size; offset += 16)
95   {
96     size_t i;
98     for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
99     {
100       uint8_t v = (uint8_t) buffer[offset + i];
101       printf ("%02"PRIx8" ", v);
102     }
103     for (; i < 16; i++)
104       printf ("   ");
105     printf ("   ");
106     for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
107     {
108       uint8_t v = (uint8_t) buffer[offset + i];
109       if ((v >= 32) && (v < 128))
110         printf ("%c", (int) buffer[offset + i]);
111       else
112         printf (".");
113     }
114     printf ("\n");
115   }
117   return (0);
118 } /* }}} int dump_network_buffer */
119 #endif
121 static lcc_value_list_t *create_value_list (void) /* {{{ */
123   lcc_value_list_t *vl;
124   int host_num;
126   vl = malloc (sizeof (*vl));
127   if (vl == NULL)
128   {
129     fprintf (stderr, "malloc failed.\n");
130     return (NULL);
131   }
132   memset (vl, 0, sizeof (*vl));
134   vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
135   if (vl->values == NULL)
136   {
137     fprintf (stderr, "calloc failed.\n");
138     free (vl);
139     return (NULL);
140   }
142   vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
143   if (vl->values_types == NULL)
144   {
145     fprintf (stderr, "calloc failed.\n");
146     free (vl->values);
147     free (vl);
148     return (NULL);
149   }
151   vl->values_len = 1;
153   host_num = get_boundet_random (0, conf_num_hosts);
155   vl->interval = 10;
156   vl->time = time (NULL) - (host_num % vl->interval);
158   if (get_boundet_random (0, 2) == 0)
159     vl->values_types[0] = LCC_TYPE_GAUGE;
160   else
161     vl->values_types[0] = LCC_TYPE_DERIVE;
163   snprintf (vl->identifier.host, sizeof (vl->identifier.host),
164       "host%04i", host_num);
165   snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
166       "plugin%03i", get_boundet_random (0, conf_num_plugins));
167   strncpy (vl->identifier.type,
168       (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
169       sizeof (vl->identifier.type));
170   snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
171       "ti%li", random ());
173   return (vl);
174 } /* }}} int create_value_list */
176 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
178   if (vl == NULL)
179     return;
181   free (vl->values);
182   free (vl->values_types);
183   free (vl);
184 } /* }}} void destroy_value_list */
186 static int send_value (lcc_value_list_t *vl) /* {{{ */
188   int status;
190   if (vl->values_types[0] == LCC_TYPE_GAUGE)
191     vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
192   else
193     vl->values[0].derive += get_boundet_random (0, 100);
195   status = lcc_network_values_send (net, vl);
196   if (status != 0)
197     fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
199   vl->time += vl->interval;
201   return (0);
202 } /* }}} int send_value */
204 int main (int argc, char **argv) /* {{{ */
206   size_t i;
208   net = lcc_network_create ();
209   if (net == NULL)
210   {
211     fprintf (stderr, "lcc_network_create failed.\n");
212     exit (EXIT_FAILURE);
213   }
214   else
215   {
216     lcc_server_t *srv;
217     
218     srv = lcc_server_create (net, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
219     if (srv == NULL)
220     {
221       fprintf (stderr, "lcc_server_create failed.\n");
222       exit (EXIT_FAILURE);
223     }
225     lcc_server_set_ttl (srv, 42);
226   }
228   values_num = (size_t) conf_num_values;
229   values = calloc (values_num, sizeof (*values));
230   if (values == NULL)
231   {
232     fprintf (stderr, "calloc failed.\n");
233     exit (EXIT_FAILURE);
234   }
236   fprintf (stdout, "Creating %i values ... ", conf_num_values);
237   fflush (stdout);
238   for (i = 0; i < values_num; i++)
239   {
240     values[i] = create_value_list ();
241     if (values[i] == NULL)
242     {
243       fprintf (stderr, "create_value_list failed.\n");
244       exit (EXIT_FAILURE);
245     }
246   }
247   fprintf (stdout, "done\n");
249   fprintf (stdout, "Sorting values by time ... ");
250   fflush (stdout);
251   qsort (values, values_num, sizeof (*values), compare_time);
252   fprintf (stdout, "done\n");
254   for (i = 0; i < values_num; i++)
255     send_value (values[i]);
257   for (i = 0; i < values_num; i++)
258     destroy_value_list (values[i]);
259   free (values);
261   lcc_network_destroy (net);
262   exit (EXIT_SUCCESS);
263   return (0);
264 } /* }}} int main */
266 /* vim: set sw=2 sts=2 et fdm=marker : */