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_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) /* {{{ */
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 */
73 static int dump_network_buffer (void) /* {{{ */
75 char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT];
80 memset (buffer, 0, sizeof (buffer));
81 buffer_size = sizeof (buffer);
83 status = lcc_network_buffer_get (nb, buffer, &buffer_size);
86 fprintf (stderr, "lcc_network_buffer_get failed with status %i.\n",
91 if (buffer_size > sizeof (buffer))
92 buffer_size = sizeof (buffer);
94 for (offset = 0; offset < buffer_size; offset += 16)
98 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
100 uint8_t v = (uint8_t) buffer[offset + i];
101 printf ("%02"PRIx8" ", v);
106 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
108 uint8_t v = (uint8_t) buffer[offset + i];
109 if ((v >= 32) && (v < 128))
110 printf ("%c", (int) buffer[offset + i]);
118 } /* }}} int dump_network_buffer */
121 static lcc_value_list_t *create_value_list (void) /* {{{ */
123 lcc_value_list_t *vl;
126 vl = malloc (sizeof (*vl));
129 fprintf (stderr, "malloc failed.\n");
132 memset (vl, 0, sizeof (*vl));
134 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
135 if (vl->values == NULL)
137 fprintf (stderr, "calloc failed.\n");
142 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
143 if (vl->values_types == NULL)
145 fprintf (stderr, "calloc failed.\n");
153 host_num = get_boundet_random (0, conf_num_hosts);
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;
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),
174 } /* }}} int create_value_list */
176 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
182 free (vl->values_types);
184 } /* }}} void destroy_value_list */
186 static int send_value (lcc_value_list_t *vl) /* {{{ */
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);
193 vl->values[0].derive += get_boundet_random (0, 100);
195 status = lcc_network_values_send (net, vl);
197 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
199 vl->time += vl->interval;
202 } /* }}} int send_value */
204 int main (int argc, char **argv) /* {{{ */
208 net = lcc_network_create ();
211 fprintf (stderr, "lcc_network_create failed.\n");
218 srv = lcc_server_create (net, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
221 fprintf (stderr, "lcc_server_create failed.\n");
225 lcc_server_set_ttl (srv, 42);
228 values_num = (size_t) conf_num_values;
229 values = calloc (values_num, sizeof (*values));
232 fprintf (stderr, "calloc failed.\n");
236 fprintf (stdout, "Creating %i values ... ", conf_num_values);
238 for (i = 0; i < values_num; i++)
240 values[i] = create_value_list ();
241 if (values[i] == NULL)
243 fprintf (stderr, "create_value_list failed.\n");
247 fprintf (stdout, "done\n");
249 fprintf (stdout, "Sorting values by time ... ");
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]);
261 lcc_network_destroy (net);
266 /* vim: set sw=2 sts=2 et fdm=marker : */