2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2007 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 octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
32 * Private data structures
34 typedef struct threshold_s
36 char host[DATA_MAX_NAME_LEN];
37 char plugin[DATA_MAX_NAME_LEN];
38 char plugin_instance[DATA_MAX_NAME_LEN];
39 char type[DATA_MAX_NAME_LEN];
40 char type_instance[DATA_MAX_NAME_LEN];
47 * Private (static) variables
49 static avl_tree_t *threshold_tree = NULL;
50 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
53 * Threshold management
54 * ====================
55 * The following functions add, delete, search, etc. configured thresholds to
56 * the underlying AVL trees.
58 static int ut_threshold_add (const threshold_t *th)
60 char name[6 * DATA_MAX_NAME_LEN];
65 if (format_name (name, sizeof (name), th->host,
66 th->plugin, th->plugin_instance,
67 th->type, th->type_instance) != 0)
69 ERROR ("ut_threshold_add: format_name failed.");
73 name_copy = strdup (name);
74 if (name_copy == NULL)
76 ERROR ("ut_threshold_add: strdup failed.");
80 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
84 ERROR ("ut_threshold_add: malloc failed.");
87 memcpy (th_copy, th, sizeof (threshold_t));
89 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
91 pthread_mutex_lock (&threshold_lock);
92 status = avl_insert (threshold_tree, name_copy, th_copy);
93 pthread_mutex_unlock (&threshold_lock);
97 ERROR ("ut_threshold_add: avl_insert (%s) failed.", name);
103 } /* int ut_threshold_add */
105 * End of the threshold management functions
111 * The following approximately two hundred functions are used to convert the
114 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
116 if ((ci->values_num != 1)
117 || (ci->values[0].type != OCONFIG_TYPE_STRING))
119 WARNING ("threshold values: The `Instance' option needs exactly one "
124 strncpy (th->type_instance, ci->values[0].value.string,
125 sizeof (th->type_instance));
126 th->type_instance[sizeof (th->type_instance) - 1] = '\0';
129 } /* int ut_config_type_instance */
131 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
133 if ((ci->values_num != 1)
134 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
136 WARNING ("threshold values: The `Max' option needs exactly one "
141 th->max = ci->values[0].value.number;
144 } /* int ut_config_type_max */
146 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
148 if ((ci->values_num != 1)
149 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
151 WARNING ("threshold values: The `Min' option needs exactly one "
156 th->min = ci->values[0].value.number;
159 } /* int ut_config_type_min */
161 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
163 if ((ci->values_num != 1)
164 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
166 WARNING ("threshold values: The `Invert' option needs exactly one "
167 "boolean argument.");
171 th->invert = (ci->values[0].value.boolean) ? 1 : 0;
174 } /* int ut_config_type_invert */
176 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
182 if ((ci->values_num != 1)
183 || (ci->values[0].type != OCONFIG_TYPE_STRING))
185 WARNING ("threshold values: The `Type' block needs exactly one string "
190 if (ci->children_num < 1)
192 WARNING ("threshold values: The `Type' block needs at least one option.");
196 memcpy (&th, th_orig, sizeof (th));
197 strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
198 th.type[sizeof (th.type) - 1] = '\0';
200 for (i = 0; i < ci->children_num; i++)
202 oconfig_item_t *option = ci->children + i;
205 if (strcasecmp ("Instance", option->key) == 0)
206 status = ut_config_type_instance (&th, option);
207 else if (strcasecmp ("Max", option->key) == 0)
208 status = ut_config_type_max (&th, option);
209 else if (strcasecmp ("Min", option->key) == 0)
210 status = ut_config_type_min (&th, option);
211 else if (strcasecmp ("Invert", option->key) == 0)
212 status = ut_config_type_invert (&th, option);
215 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
216 "block.", option->key);
226 status = ut_threshold_add (&th);
230 } /* int ut_config_type */
232 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
234 if ((ci->values_num != 1)
235 || (ci->values[0].type != OCONFIG_TYPE_STRING))
237 WARNING ("threshold values: The `Instance' option needs exactly one "
242 strncpy (th->plugin_instance, ci->values[0].value.string,
243 sizeof (th->plugin_instance));
244 th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
247 } /* int ut_config_plugin_instance */
249 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
255 if ((ci->values_num != 1)
256 || (ci->values[0].type != OCONFIG_TYPE_STRING))
258 WARNING ("threshold values: The `Plugin' block needs exactly one string "
263 if (ci->children_num < 1)
265 WARNING ("threshold values: The `Plugin' block needs at least one nested "
270 memcpy (&th, th_orig, sizeof (th));
271 strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
272 th.plugin[sizeof (th.plugin) - 1] = '\0';
274 for (i = 0; i < ci->children_num; i++)
276 oconfig_item_t *option = ci->children + i;
279 if (strcasecmp ("Type", option->key) == 0)
280 status = ut_config_type (&th, option);
281 else if (strcasecmp ("Instance", option->key) == 0)
282 status = ut_config_plugin_instance (&th, option);
285 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
286 "block.", option->key);
295 } /* int ut_config_plugin */
297 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
303 if ((ci->values_num != 1)
304 || (ci->values[0].type != OCONFIG_TYPE_STRING))
306 WARNING ("threshold values: The `Host' block needs exactly one string "
311 if (ci->children_num < 1)
313 WARNING ("threshold values: The `Host' block needs at least one nested "
318 memcpy (&th, th_orig, sizeof (th));
319 strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
320 th.host[sizeof (th.host) - 1] = '\0';
322 for (i = 0; i < ci->children_num; i++)
324 oconfig_item_t *option = ci->children + i;
327 if (strcasecmp ("Type", option->key) == 0)
328 status = ut_config_type (&th, option);
329 else if (strcasecmp ("Plugin", option->key) == 0)
330 status = ut_config_plugin (&th, option);
333 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
334 "block.", option->key);
343 } /* int ut_config_host */
345 int ut_config (const oconfig_item_t *ci)
352 memset (&th, '\0', sizeof (th));
356 for (i = 0; i < ci->children_num; i++)
358 oconfig_item_t *option = ci->children + i;
361 if (strcasecmp ("Type", option->key) == 0)
362 status = ut_config_type (&th, option);
363 else if (strcasecmp ("Plugin", option->key) == 0)
364 status = ut_config_plugin (&th, option);
365 else if (strcasecmp ("Host", option->key) == 0)
366 status = ut_config_host (&th, option);
369 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
378 } /* int um_config */
380 * End of the functions used to configure threshold values.
383 /* vim: set sw=2 ts=8 sts=2 tw=78 : */