5b31cc9d13a423e4b66a2384a0581177e221943b
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 "utils_heap.h"
33 #include "libcollectdclient/collectd/client.h"
34 #include "libcollectdclient/collectd/network.h"
35 #include "libcollectdclient/collectd/network_buffer.h"
37 static int conf_num_hosts = 1000;
38 static int conf_num_plugins = 20;
39 static int conf_num_values = 100000;
41 static lcc_network_t *net;
43 static c_heap_t *values_heap = NULL;
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;
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 */
59 static int get_boundet_random (int min, int max) /* {{{ */
60 {
61 int range;
63 if (min >= max)
64 return (-1);
65 if (min == (max - 1))
66 return (min);
68 range = max - min;
70 return (min + ((int) (((double) range) * ((double) random ()) / (((double) RAND_MAX) + 1.0))));
71 } /* }}} int get_boundet_random */
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;
81 memset (buffer, 0, sizeof (buffer));
82 buffer_size = sizeof (buffer);
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 }
92 if (buffer_size > sizeof (buffer))
93 buffer_size = sizeof (buffer);
95 for (offset = 0; offset < buffer_size; offset += 16)
96 {
97 size_t i;
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 }
118 return (0);
119 } /* }}} int dump_network_buffer */
120 #endif
122 static lcc_value_list_t *create_value_list (void) /* {{{ */
123 {
124 lcc_value_list_t *vl;
125 int host_num;
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));
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 }
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 }
152 vl->values_len = 1;
154 host_num = get_boundet_random (0, conf_num_hosts);
156 vl->interval = 10;
157 vl->time = time (NULL) - (host_num % vl->interval);
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;
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 ());
174 return (vl);
175 } /* }}} int create_value_list */
177 static void destroy_value_list (lcc_value_list_t *vl) /* {{{ */
178 {
179 if (vl == NULL)
180 return;
182 free (vl->values);
183 free (vl->values_types);
184 free (vl);
185 } /* }}} void destroy_value_list */
187 static int send_value (lcc_value_list_t *vl) /* {{{ */
188 {
189 int status;
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);
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);
200 vl->time += vl->interval;
202 return (0);
203 } /* }}} int send_value */
205 int main (int argc, char **argv) /* {{{ */
206 {
207 int i;
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 }
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;
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 }
233 lcc_server_set_ttl (srv, 42);
234 #if 0
235 lcc_server_set_security_level (srv, ENCRYPT,
236 "admin", "password1");
237 #endif
238 }
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;
246 vl = create_value_list ();
247 if (vl == NULL)
248 {
249 fprintf (stderr, "create_value_list failed.\n");
250 exit (EXIT_FAILURE);
251 }
253 c_heap_insert (values_heap, vl);
254 }
255 fprintf (stdout, "done\n");
257 while (42)
258 {
259 lcc_value_list_t *vl = c_heap_get_root (values_heap);
261 if (vl == NULL)
262 break;
264 send_value (vl);
265 destroy_value_list (vl);
266 }
268 c_heap_destroy (values_heap);
270 lcc_network_destroy (net);
271 exit (EXIT_SUCCESS);
272 return (0);
273 } /* }}} int main */
275 /* vim: set sw=2 sts=2 et fdm=marker : */