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