2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Pierre-Yves Ritschard <pyr at spootnik.org>
30 #include "utils_avltree.h"
31 #include "utils_threshold.h"
38 c_avl_tree_t *threshold_tree = NULL;
39 pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
43 * threshold_t *threshold_get
45 * Retrieve one specific threshold configuration. For looking up a threshold
46 * matching a value_list_t, see "threshold_search" below. Returns NULL if the
47 * specified threshold doesn't exist.
49 threshold_t *threshold_get (const char *hostname,
50 const char *plugin, const char *plugin_instance,
51 const char *type, const char *type_instance)
53 char name[6 * DATA_MAX_NAME_LEN];
54 threshold_t *th = NULL;
56 format_name (name, sizeof (name),
57 (hostname == NULL) ? "" : hostname,
58 (plugin == NULL) ? "" : plugin, plugin_instance,
59 (type == NULL) ? "" : type, type_instance);
60 name[sizeof (name) - 1] = '\0';
62 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
66 } /* }}} threshold_t *threshold_get */
69 * threshold_t *threshold_search
71 * Searches for a threshold configuration using all the possible variations of
72 * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be
74 * XXX: This is likely the least efficient function in collectd.
76 threshold_t *threshold_search (const value_list_t *vl)
80 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
81 vl->type, vl->type_instance)) != NULL)
83 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
84 vl->type, NULL)) != NULL)
86 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
87 vl->type, vl->type_instance)) != NULL)
89 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
90 vl->type, NULL)) != NULL)
92 else if ((th = threshold_get (vl->host, "", NULL,
93 vl->type, vl->type_instance)) != NULL)
95 else if ((th = threshold_get (vl->host, "", NULL,
96 vl->type, NULL)) != NULL)
98 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
99 vl->type, vl->type_instance)) != NULL)
101 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
102 vl->type, NULL)) != NULL)
104 else if ((th = threshold_get ("", vl->plugin, NULL,
105 vl->type, vl->type_instance)) != NULL)
107 else if ((th = threshold_get ("", vl->plugin, NULL,
108 vl->type, NULL)) != NULL)
110 else if ((th = threshold_get ("", "", NULL,
111 vl->type, vl->type_instance)) != NULL)
113 else if ((th = threshold_get ("", "", NULL,
114 vl->type, NULL)) != NULL)
118 } /* }}} threshold_t *threshold_search */
120 int ut_search_threshold (const value_list_t *vl, /* {{{ */
121 threshold_t *ret_threshold)
128 /* Is this lock really necessary? */
129 pthread_mutex_lock (&threshold_lock);
130 t = threshold_search (vl);
132 pthread_mutex_unlock (&threshold_lock);
136 memcpy (ret_threshold, t, sizeof (*ret_threshold));
137 pthread_mutex_unlock (&threshold_lock);
139 ret_threshold->next = NULL;
142 } /* }}} int ut_search_threshold */