2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
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 * Pierre-Yves Ritschard <pyr at spootnik.org>
24 #include "utils_avltree.h"
25 #include "utils_threshold.h"
32 c_avl_tree_t *threshold_tree = NULL;
33 pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
37 * threshold_t *threshold_get
39 * Retrieve one specific threshold configuration. For looking up a threshold
40 * matching a value_list_t, see "threshold_search" below. Returns NULL if the
41 * specified threshold doesn't exist.
43 threshold_t *threshold_get (const char *hostname,
44 const char *plugin, const char *plugin_instance,
45 const char *type, const char *type_instance)
47 char name[6 * DATA_MAX_NAME_LEN];
48 threshold_t *th = NULL;
50 format_name (name, sizeof (name),
51 (hostname == NULL) ? "" : hostname,
52 (plugin == NULL) ? "" : plugin, plugin_instance,
53 (type == NULL) ? "" : type, type_instance);
54 name[sizeof (name) - 1] = '\0';
56 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
60 } /* }}} threshold_t *threshold_get */
63 * threshold_t *threshold_search
65 * Searches for a threshold configuration using all the possible variations of
66 * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be
68 * XXX: This is likely the least efficient function in collectd.
70 threshold_t *threshold_search (const value_list_t *vl)
74 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
75 vl->type, vl->type_instance)) != NULL)
77 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
78 vl->type, NULL)) != NULL)
80 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
81 vl->type, vl->type_instance)) != NULL)
83 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
84 vl->type, NULL)) != NULL)
86 else if ((th = threshold_get (vl->host, "", NULL,
87 vl->type, vl->type_instance)) != NULL)
89 else if ((th = threshold_get (vl->host, "", NULL,
90 vl->type, NULL)) != NULL)
92 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
93 vl->type, vl->type_instance)) != NULL)
95 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
96 vl->type, NULL)) != NULL)
98 else if ((th = threshold_get ("", vl->plugin, NULL,
99 vl->type, vl->type_instance)) != NULL)
101 else if ((th = threshold_get ("", vl->plugin, NULL,
102 vl->type, NULL)) != NULL)
104 else if ((th = threshold_get ("", "", NULL,
105 vl->type, vl->type_instance)) != NULL)
107 else if ((th = threshold_get ("", "", NULL,
108 vl->type, NULL)) != NULL)
112 } /* }}} threshold_t *threshold_search */
114 int ut_search_threshold (const value_list_t *vl, /* {{{ */
115 threshold_t *ret_threshold)
122 /* Is this lock really necessary? */
123 pthread_mutex_lock (&threshold_lock);
124 t = threshold_search (vl);
126 pthread_mutex_unlock (&threshold_lock);
130 memcpy (ret_threshold, t, sizeof (*ret_threshold));
131 pthread_mutex_unlock (&threshold_lock);
133 ret_threshold->next = NULL;
136 } /* }}} int ut_search_threshold */