1 /**
2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
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 * Author:
19 * Pierre-Yves Ritschard <pyr at spootnik.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "utils_avltree.h"
25 #include "utils_threshold.h"
27 #include <pthread.h>
29 /*
30 * Exported symbols
31 * {{{ */
32 c_avl_tree_t *threshold_tree = NULL;
33 pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
34 /* }}} */
36 /*
37 * threshold_t *threshold_get
38 *
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.
42 */
43 threshold_t *threshold_get (const char *hostname,
44 const char *plugin, const char *plugin_instance,
45 const char *type, const char *type_instance)
46 { /* {{{ */
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)
57 return (th);
58 else
59 return (NULL);
60 } /* }}} threshold_t *threshold_get */
62 /*
63 * threshold_t *threshold_search
64 *
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
67 * found.
68 * XXX: This is likely the least efficient function in collectd.
69 */
70 threshold_t *threshold_search (const value_list_t *vl)
71 { /* {{{ */
72 threshold_t *th;
74 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
75 vl->type, vl->type_instance)) != NULL)
76 return (th);
77 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
78 vl->type, NULL)) != NULL)
79 return (th);
80 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
81 vl->type, vl->type_instance)) != NULL)
82 return (th);
83 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
84 vl->type, NULL)) != NULL)
85 return (th);
86 else if ((th = threshold_get (vl->host, "", NULL,
87 vl->type, vl->type_instance)) != NULL)
88 return (th);
89 else if ((th = threshold_get (vl->host, "", NULL,
90 vl->type, NULL)) != NULL)
91 return (th);
92 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
93 vl->type, vl->type_instance)) != NULL)
94 return (th);
95 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
96 vl->type, NULL)) != NULL)
97 return (th);
98 else if ((th = threshold_get ("", vl->plugin, NULL,
99 vl->type, vl->type_instance)) != NULL)
100 return (th);
101 else if ((th = threshold_get ("", vl->plugin, NULL,
102 vl->type, NULL)) != NULL)
103 return (th);
104 else if ((th = threshold_get ("", "", NULL,
105 vl->type, vl->type_instance)) != NULL)
106 return (th);
107 else if ((th = threshold_get ("", "", NULL,
108 vl->type, NULL)) != NULL)
109 return (th);
111 return (NULL);
112 } /* }}} threshold_t *threshold_search */
114 int ut_search_threshold (const value_list_t *vl, /* {{{ */
115 threshold_t *ret_threshold)
116 {
117 threshold_t *t;
119 if (vl == NULL)
120 return (EINVAL);
122 /* Is this lock really necessary? */
123 pthread_mutex_lock (&threshold_lock);
124 t = threshold_search (vl);
125 if (t == NULL) {
126 pthread_mutex_unlock (&threshold_lock);
127 return (ENOENT);
128 }
130 memcpy (ret_threshold, t, sizeof (*ret_threshold));
131 pthread_mutex_unlock (&threshold_lock);
133 ret_threshold->next = NULL;
135 return (0);
136 } /* }}} int ut_search_threshold */