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>
32 #include "utils_heap.h"
34 #include "libcollectdclient/collectd/client.h"
35 #include "libcollectdclient/collectd/network.h"
36 #include "libcollectdclient/collectd/network_buffer.h"
38 static int conf_num_hosts = 1000;
39 static int conf_num_plugins = 20;
40 static int conf_num_values = 100000;
42 static lcc_network_t *net;
44 static c_heap_t *values_heap = NULL;
46 static struct sigaction sigint_action;
47 static struct sigaction sigterm_action;
49 static _Bool loop = 1;
51 static void signal_handler (int signal) /* {{{ */
54 } /* }}} void signal_handler */
56 static int compare_time (const void *v0, const void *v1) /* {{{ */
58 const lcc_value_list_t *vl0 = v0;
59 const lcc_value_list_t *vl1 = v1;
61 if (vl0->time < vl1->time)
63 else if (vl0->time > vl1->time)
67 } /* }}} int compare_time */
69 static int get_boundet_random (int min, int max) /* {{{ */
80 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
81 } /* }}} int get_boundet_random */
84 static int dump_network_buffer (void) /* {{{ */
86 char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT];
91 memset (buffer, 0, sizeof (buffer));
92 buffer_size = sizeof (buffer);
94 status = lcc_network_buffer_get (nb, buffer, &buffer_size);
97 fprintf (stderr, "lcc_network_buffer_get failed with status %i.\n",
102 if (buffer_size > sizeof (buffer))
103 buffer_size = sizeof (buffer);
105 for (offset = 0; offset < buffer_size; offset += 16)
109 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
111 uint8_t v = (uint8_t) buffer[offset + i];
112 printf ("%02"PRIx8" ", v);
117 for (i = 0; (i < 16) && ((offset + i) < buffer_size); i++)
119 uint8_t v = (uint8_t) buffer[offset + i];
120 if ((v >= 32) && (v < 128))
121 printf ("%c", (int) buffer[offset + i]);
129 } /* }}} int dump_network_buffer */
132 static lcc_value_list_t *create_value_list (void) /* {{{ */
134 lcc_value_list_t *vl;
137 vl = malloc (sizeof (*vl));
140 fprintf (stderr, "malloc failed.\n");
143 memset (vl, 0, sizeof (*vl));
145 vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values));
146 if (vl->values == NULL)
148 fprintf (stderr, "calloc failed.\n");
153 vl->values_types = calloc (/* nmemb = */ 1, sizeof (*vl->values_types));
154 if (vl->values_types == NULL)
156 fprintf (stderr, "calloc failed.\n");
164 host_num = get_boundet_random (0, conf_num_hosts);
167 vl->time = time (NULL) - (host_num % vl->interval);
169 if (get_boundet_random (0, 2) == 0)
170 vl->values_types[0] = LCC_TYPE_GAUGE;
172 vl->values_types[0] = LCC_TYPE_DERIVE;
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),
185 } /* }}} int create_value_list */
187 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
193 free (vl->values_types);
195 } /* }}} void destroy_value_list */
197 static int send_value (lcc_value_list_t *vl) /* {{{ */
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);
204 vl->values[0].derive += get_boundet_random (0, 100);
206 status = lcc_network_values_send (net, vl);
208 fprintf (stderr, "lcc_network_values_send failed with status %i.\n", status);
210 vl->time += vl->interval;
213 } /* }}} int send_value */
215 int main (int argc, char **argv) /* {{{ */
221 sigint_action.sa_handler = signal_handler;
222 sigaction (SIGINT, &sigint_action, /* old = */ NULL);
224 sigterm_action.sa_handler = signal_handler;
225 sigaction (SIGTERM, &sigterm_action, /* old = */ NULL);
228 values_heap = c_heap_create (compare_time);
229 if (values_heap == NULL)
231 fprintf (stderr, "c_heap_create failed.\n");
235 net = lcc_network_create ();
238 fprintf (stderr, "lcc_network_create failed.\n");
245 srv = lcc_server_create (net, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT);
248 fprintf (stderr, "lcc_server_create failed.\n");
252 lcc_server_set_ttl (srv, 42);
254 lcc_server_set_security_level (srv, ENCRYPT,
255 "admin", "password1");
259 fprintf (stdout, "Creating %i values ... ", conf_num_values);
261 for (i = 0; i < conf_num_values; i++)
263 lcc_value_list_t *vl;
265 vl = create_value_list ();
268 fprintf (stderr, "create_value_list failed.\n");
272 c_heap_insert (values_heap, vl);
274 fprintf (stdout, "done\n");
279 lcc_value_list_t *vl = c_heap_get_root (values_heap);
284 if (vl->time != last_time)
286 printf ("%i values have been sent.\n", values_sent);
288 /* Check if we need to sleep */
289 time_t now = time (NULL);
291 while (now < vl->time)
294 struct timespec ts = { 0, 10000000 };
295 nanosleep (&ts, /* remaining = */ NULL);
298 last_time = vl->time;
304 c_heap_insert (values_heap, vl);
307 fprintf (stdout, "Shutting down.\n");
312 lcc_value_list_t *vl = c_heap_get_root (values_heap);
315 destroy_value_list (vl);
317 c_heap_destroy (values_heap);
319 lcc_network_destroy (net);
324 /* vim: set sw=2 sts=2 et fdm=marker : */