2 * collectd - src/collectd-tg.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
19 * Florian Forster <ff at octo.it>
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_buffer_t *nb;
41 static lcc_value_list_t **values;
42 static size_t values_num;
44 static int compare_time (const void *v0, const void *v1) /* {{{ */
46 lcc_value_list_t * const *vl0 = v0;
47 lcc_value_list_t * const *vl1 = v1;
49 if ((*vl0)->time < (*vl1)->time)
51 else if ((*vl0)->time > (*vl1)->time)
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) /* {{{ */
69 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
70 } /* }}} int get_boundet_random */
72 static int dump_network_buffer (void) /* {{{ */
74 char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT];
79 memset (buffer, 0, sizeof (buffer));
80 buffer_size = sizeof (buffer);
82 status = lcc_network_buffer_get (nb, buffer, &buffer_size);
85 fprintf (stderr, "lcc_network_buffer_get failed with status %i.\n",
90 if (buffer_size > sizeof (buffer))
91 buffer_size = sizeof (buffer);
93 for (offset = 0; offset < buffer_size; offset += 16)
97 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
99 uint8_t v = (uint8_t) buffer[offset + i];
100 printf ("%02"PRIx8" ", v);
105 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
107 uint8_t v = (uint8_t) buffer[offset + i];
108 if ((v >= 32) && (v < 128))
109 printf ("%c", (int) buffer[offset + i]);
117 } /* }}} int dump_network_buffer */
119 static lcc_value_list_t *create_value_list (void) /* {{{ */
121 lcc_value_list_t *vl;
124 vl = malloc (sizeof (*vl));
127 fprintf (stderr, "malloc failed.\n");
130 memset (vl, 0, sizeof (*vl));
132 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
133 if (vl->values == NULL)
135 fprintf (stderr, "calloc failed.\n");
140 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
141 if (vl->values_types == NULL)
143 fprintf (stderr, "calloc failed.\n");
151 host_num = get_boundet_random (0, conf_num_hosts);
154 vl->time = time (NULL) - (host_num % vl->interval);
156 if (get_boundet_random (0, 2) == 0)
157 vl->values_types[0] = LCC_TYPE_GAUGE;
159 vl->values_types[0] = LCC_TYPE_DERIVE;
161 snprintf (vl->identifier.host, sizeof (vl->identifier.host),
162 "host%04i", host_num);
163 snprintf (vl->identifier.plugin, sizeof (vl->identifier.plugin),
164 "plugin%03i", get_boundet_random (0, conf_num_plugins));
165 strncpy (vl->identifier.type,
166 (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
167 sizeof (vl->identifier.type));
168 snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
172 } /* }}} int create_value_list */
174 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
180 free (vl->values_types);
182 } /* }}} void destroy_value_list */
184 static int send_value (lcc_value_list_t *vl) /* {{{ */
188 if (vl->values_types[0] == LCC_TYPE_GAUGE)
189 vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0);
191 vl->values[0].derive += get_boundet_random (0, 100);
193 status = lcc_network_buffer_add_value (nb, vl);
196 lcc_network_buffer_finalize (nb);
197 dump_network_buffer ();
198 lcc_network_buffer_initialize (nb);
200 status = lcc_network_buffer_add_value (nb, vl);
203 vl->time += vl->interval;
206 } /* }}} int send_value */
208 int main (int argc, char **argv) /* {{{ */
212 nb = lcc_network_buffer_create (/* size = */ 0);
215 fprintf (stderr, "lcc_network_buffer_create failed.\n");
219 values_num = (size_t) conf_num_values;
220 values = calloc (values_num, sizeof (*values));
223 fprintf (stderr, "calloc failed.\n");
227 fprintf (stdout, "Creating %i values ... ", conf_num_values);
229 for (i = 0; i < values_num; i++)
231 values[i] = create_value_list ();
232 if (values[i] == NULL)
234 fprintf (stderr, "create_value_list failed.\n");
238 fprintf (stdout, "done\n");
240 fprintf (stdout, "Sorting values by time ... ");
242 qsort (values, values_num, sizeof (*values), compare_time);
243 fprintf (stdout, "done\n");
245 lcc_network_buffer_initialize (nb);
246 for (i = 0; i < values_num; i++)
247 send_value (values[i]);
248 lcc_network_buffer_finalize (nb);
249 dump_network_buffer ();
251 for (i = 0; i < values_num; i++)
252 destroy_value_list (values[i]);
255 lcc_network_buffer_destroy (nb);
260 /* vim: set sw=2 sts=2 et fdm=marker : */