Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / daemon / utils_threshold.c
1 /**
2  * collectd - src/utils_threshold.c
3  * Copyright (C) 2014       Pierre-Yves Ritschard
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Pierre-Yves Ritschard <pyr at spootnik.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "utils_avltree.h"
30 #include "utils_threshold.h"
31
32 #include <pthread.h>
33
34 /*
35  * Exported symbols
36  * {{{ */
37 c_avl_tree_t   *threshold_tree = NULL;
38 pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
39 /* }}} */
40
41 /*
42  * threshold_t *threshold_get
43  *
44  * Retrieve one specific threshold configuration. For looking up a threshold
45  * matching a value_list_t, see "threshold_search" below. Returns NULL if the
46  * specified threshold doesn't exist.
47  */
48 threshold_t *threshold_get (const char *hostname,
49     const char *plugin, const char *plugin_instance,
50     const char *type, const char *type_instance)
51 { /* {{{ */
52   char name[6 * DATA_MAX_NAME_LEN];
53   threshold_t *th = NULL;
54
55   format_name (name, sizeof (name),
56       (hostname == NULL) ? "" : hostname,
57       (plugin == NULL) ? "" : plugin, plugin_instance,
58       (type == NULL) ? "" : type, type_instance);
59   name[sizeof (name) - 1] = '\0';
60
61   if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
62     return (th);
63   else
64     return (NULL);
65 } /* }}} threshold_t *threshold_get */
66
67 /*
68  * threshold_t *threshold_search
69  *
70  * Searches for a threshold configuration using all the possible variations of
71  * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be
72  * found.
73  * XXX: This is likely the least efficient function in collectd.
74  */
75 threshold_t *threshold_search (const value_list_t *vl)
76 { /* {{{ */
77   threshold_t *th;
78
79   if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
80           vl->type, vl->type_instance)) != NULL)
81     return (th);
82   else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
83           vl->type, NULL)) != NULL)
84     return (th);
85   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
86           vl->type, vl->type_instance)) != NULL)
87     return (th);
88   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
89           vl->type, NULL)) != NULL)
90     return (th);
91   else if ((th = threshold_get (vl->host, "", NULL,
92           vl->type, vl->type_instance)) != NULL)
93     return (th);
94   else if ((th = threshold_get (vl->host, "", NULL,
95           vl->type, NULL)) != NULL)
96     return (th);
97   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
98           vl->type, vl->type_instance)) != NULL)
99     return (th);
100   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
101           vl->type, NULL)) != NULL)
102     return (th);
103   else if ((th = threshold_get ("", vl->plugin, NULL,
104           vl->type, vl->type_instance)) != NULL)
105     return (th);
106   else if ((th = threshold_get ("", vl->plugin, NULL,
107           vl->type, NULL)) != NULL)
108     return (th);
109   else if ((th = threshold_get ("", "", NULL,
110           vl->type, vl->type_instance)) != NULL)
111     return (th);
112   else if ((th = threshold_get ("", "", NULL,
113           vl->type, NULL)) != NULL)
114     return (th);
115
116   return (NULL);
117 } /* }}} threshold_t *threshold_search */
118
119 int ut_search_threshold (const value_list_t *vl, /* {{{ */
120     threshold_t *ret_threshold)
121 {
122   threshold_t *t;
123
124   if (vl == NULL)
125     return (EINVAL);
126
127         /* Is this lock really necessary? */
128         pthread_mutex_lock (&threshold_lock);
129   t = threshold_search (vl);
130   if (t == NULL) {
131                 pthread_mutex_unlock (&threshold_lock);
132     return (ENOENT);
133         }
134
135   memcpy (ret_threshold, t, sizeof (*ret_threshold));
136         pthread_mutex_unlock (&threshold_lock);
137
138   ret_threshold->next = NULL;
139
140   return (0);
141 } /* }}} int ut_search_threshold */
142
143