| 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 |
|
| 29 |
#include "utils/avltree/avltree.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
#include "utils_threshold.h" |
| 32 |
|
| 33 |
#include <pthread.h> |
| 34 |
|
| 35 |
/* |
| 36 |
* Exported symbols |
| 37 |
* {{{ */ |
| 38 |
c_avl_tree_t *threshold_tree = NULL; |
| 39 |
pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER; |
| 40 |
/* }}} */ |
| 41 |
|
| 42 |
/* |
| 43 |
* threshold_t *threshold_get |
| 44 |
* |
| 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. |
| 48 |
*/ |
| 49 |
threshold_t *threshold_get(const char *hostname, const char *plugin, |
| 50 |
const char *plugin_instance, const char *type, |
| 51 |
const char *type_instance) { /* {{{ */ |
| 52 |
char name[6 * DATA_MAX_NAME_LEN]; |
| 53 |
threshold_t *th = NULL; |
| 54 |
|
| 55 |
format_name(name, sizeof(name), (hostname == NULL) ? "" : hostname, |
| 56 |
(plugin == NULL) ? "" : plugin, plugin_instance, |
| 57 |
(type == NULL) ? "" : type, type_instance); |
| 58 |
name[sizeof(name) - 1] = '\0'; |
| 59 |
|
| 60 |
if (c_avl_get(threshold_tree, name, (void *)&th) == 0) |
| 61 |
return th; |
| 62 |
else |
| 63 |
return NULL; |
| 64 |
} /* }}} threshold_t *threshold_get */ |
| 65 |
|
| 66 |
/* |
| 67 |
* threshold_t *threshold_search |
| 68 |
* |
| 69 |
* Searches for a threshold configuration using all the possible variations of |
| 70 |
* "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be |
| 71 |
* found. |
| 72 |
* XXX: This is likely the least efficient function in collectd. |
| 73 |
*/ |
| 74 |
threshold_t *threshold_search(const value_list_t *vl) { /* {{{ */ |
| 75 |
threshold_t *th; |
| 76 |
|
| 77 |
if ((th = threshold_get(vl->host, vl->plugin, vl->plugin_instance, vl->type, |
| 78 |
vl->type_instance)) != NULL) |
| 79 |
return th; |
| 80 |
else if ((th = threshold_get(vl->host, vl->plugin, vl->plugin_instance, |
| 81 |
vl->type, NULL)) != NULL) |
| 82 |
return th; |
| 83 |
else if ((th = threshold_get(vl->host, vl->plugin, NULL, vl->type, |
| 84 |
vl->type_instance)) != NULL) |
| 85 |
return th; |
| 86 |
else if ((th = threshold_get(vl->host, vl->plugin, NULL, vl->type, NULL)) != |
| 87 |
NULL) |
| 88 |
return th; |
| 89 |
else if ((th = threshold_get(vl->host, "", NULL, vl->type, |
| 90 |
vl->type_instance)) != NULL) |
| 91 |
return th; |
| 92 |
else if ((th = threshold_get(vl->host, "", NULL, vl->type, NULL)) != NULL) |
| 93 |
return th; |
| 94 |
else if ((th = threshold_get("", vl->plugin, vl->plugin_instance, vl->type, |
| 95 |
vl->type_instance)) != NULL) |
| 96 |
return th; |
| 97 |
else if ((th = threshold_get("", vl->plugin, vl->plugin_instance, vl->type, |
| 98 |
NULL)) != NULL) |
| 99 |
return th; |
| 100 |
else if ((th = threshold_get("", vl->plugin, NULL, vl->type, |
| 101 |
vl->type_instance)) != NULL) |
| 102 |
return th; |
| 103 |
else if ((th = threshold_get("", vl->plugin, NULL, vl->type, NULL)) != NULL) |
| 104 |
return th; |
| 105 |
else if ((th = threshold_get("", "", NULL, vl->type, vl->type_instance)) != |
| 106 |
NULL) |
| 107 |
return th; |
| 108 |
else if ((th = threshold_get("", "", NULL, vl->type, NULL)) != NULL) |
| 109 |
return th; |
| 110 |
|
| 111 |
return NULL; |
| 112 |
} /* }}} threshold_t *threshold_search */ |
| 113 |
|
| 114 |
int ut_search_threshold(const value_list_t *vl, /* {{{ */ |
| 115 |
threshold_t *ret_threshold) { |
| 116 |
threshold_t *t; |
| 117 |
|
| 118 |
if (vl == NULL) |
| 119 |
return EINVAL; |
| 120 |
|
| 121 |
/* Is this lock really necessary? */ |
| 122 |
pthread_mutex_lock(&threshold_lock); |
| 123 |
t = threshold_search(vl); |
| 124 |
if (t == NULL) { |
| 125 |
pthread_mutex_unlock(&threshold_lock); |
| 126 |
return ENOENT; |
| 127 |
} |
| 128 |
|
| 129 |
memcpy(ret_threshold, t, sizeof(*ret_threshold)); |
| 130 |
pthread_mutex_unlock(&threshold_lock); |
| 131 |
|
| 132 |
ret_threshold->next = NULL; |
| 133 |
|
| 134 |
return 0; |
| 135 |
} /* }}} int ut_search_threshold */ |