| 1 |
/** |
| 2 |
* collectd - src/utils_cmd_getthreshold.c |
| 3 |
* Copyright (C) 2008,2009 Florian octo Forster |
| 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 |
* Florian octo Forster <octo at collectd.org> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
|
| 32 |
#include "utils/avltree/avltree.h" |
| 33 |
#include "utils/cmds/getthreshold.h" |
| 34 |
#include "utils/cmds/parse_option.h" /* for `parse_string' */ |
| 35 |
#include "utils_threshold.h" |
| 36 |
|
| 37 |
#define print_to_socket(fh, ...) \ |
| 38 |
if (fprintf(fh, __VA_ARGS__) < 0) { \ |
| 39 |
WARNING("handle_getthreshold: failed to write to socket #%i: %s", \ |
| 40 |
fileno(fh), STRERRNO); \ |
| 41 |
return -1; \ |
| 42 |
} |
| 43 |
|
| 44 |
int handle_getthreshold(FILE *fh, char *buffer) { |
| 45 |
char *command; |
| 46 |
char *identifier; |
| 47 |
char *identifier_copy; |
| 48 |
|
| 49 |
char *host; |
| 50 |
char *plugin; |
| 51 |
char *plugin_instance; |
| 52 |
char *type; |
| 53 |
char *type_instance; |
| 54 |
|
| 55 |
threshold_t threshold; |
| 56 |
|
| 57 |
int status; |
| 58 |
size_t i; |
| 59 |
|
| 60 |
if ((fh == NULL) || (buffer == NULL)) |
| 61 |
return -1; |
| 62 |
|
| 63 |
DEBUG("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);", |
| 64 |
(void *)fh, buffer); |
| 65 |
|
| 66 |
command = NULL; |
| 67 |
status = parse_string(&buffer, &command); |
| 68 |
if (status != 0) { |
| 69 |
print_to_socket(fh, "-1 Cannot parse command.\n"); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
assert(command != NULL); |
| 73 |
|
| 74 |
if (strcasecmp("GETTHRESHOLD", command) != 0) { |
| 75 |
print_to_socket(fh, "-1 Unexpected command: `%s'.\n", command); |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
identifier = NULL; |
| 80 |
status = parse_string(&buffer, &identifier); |
| 81 |
if (status != 0) { |
| 82 |
print_to_socket(fh, "-1 Cannot parse identifier.\n"); |
| 83 |
return -1; |
| 84 |
} |
| 85 |
assert(identifier != NULL); |
| 86 |
|
| 87 |
if (*buffer != 0) { |
| 88 |
print_to_socket(fh, "-1 Garbage after end of command: %s\n", buffer); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
/* parse_identifier() modifies its first argument, |
| 93 |
* returning pointers into it */ |
| 94 |
identifier_copy = sstrdup(identifier); |
| 95 |
|
| 96 |
status = parse_identifier(identifier_copy, &host, &plugin, &plugin_instance, |
| 97 |
&type, &type_instance, |
| 98 |
/* default_host = */ NULL); |
| 99 |
if (status != 0) { |
| 100 |
DEBUG("handle_getthreshold: Cannot parse identifier `%s'.", identifier); |
| 101 |
print_to_socket(fh, "-1 Cannot parse identifier `%s'.\n", identifier); |
| 102 |
sfree(identifier_copy); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
value_list_t vl = {.values = NULL}; |
| 107 |
sstrncpy(vl.host, host, sizeof(vl.host)); |
| 108 |
sstrncpy(vl.plugin, plugin, sizeof(vl.plugin)); |
| 109 |
if (plugin_instance != NULL) |
| 110 |
sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance)); |
| 111 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 112 |
if (type_instance != NULL) |
| 113 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 114 |
sfree(identifier_copy); |
| 115 |
|
| 116 |
status = ut_search_threshold(&vl, &threshold); |
| 117 |
if (status == ENOENT) { |
| 118 |
print_to_socket(fh, "-1 No threshold found for identifier %s\n", |
| 119 |
identifier); |
| 120 |
return 0; |
| 121 |
} else if (status != 0) { |
| 122 |
print_to_socket(fh, "-1 Error while looking up threshold: %i\n", status); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
|
| 126 |
/* Lets count the number of lines we'll return. */ |
| 127 |
i = 0; |
| 128 |
if (threshold.host[0] != 0) |
| 129 |
i++; |
| 130 |
if (threshold.plugin[0] != 0) |
| 131 |
i++; |
| 132 |
if (threshold.plugin_instance[0] != 0) |
| 133 |
i++; |
| 134 |
if (threshold.type[0] != 0) |
| 135 |
i++; |
| 136 |
if (threshold.type_instance[0] != 0) |
| 137 |
i++; |
| 138 |
if (threshold.data_source[0] != 0) |
| 139 |
i++; |
| 140 |
if (!isnan(threshold.warning_min)) |
| 141 |
i++; |
| 142 |
if (!isnan(threshold.warning_max)) |
| 143 |
i++; |
| 144 |
if (!isnan(threshold.failure_min)) |
| 145 |
i++; |
| 146 |
if (!isnan(threshold.failure_max)) |
| 147 |
i++; |
| 148 |
if (threshold.hysteresis > 0.0) |
| 149 |
i++; |
| 150 |
if (threshold.hits > 1) |
| 151 |
i++; |
| 152 |
|
| 153 |
/* Print the response */ |
| 154 |
print_to_socket(fh, "%" PRIsz " Threshold found\n", i); |
| 155 |
|
| 156 |
if (threshold.host[0] != 0) |
| 157 |
print_to_socket(fh, "Host: %s\n", threshold.host); |
| 158 |
if (threshold.plugin[0] != 0) |
| 159 |
print_to_socket(fh, "Plugin: %s\n", threshold.plugin); |
| 160 |
if (threshold.plugin_instance[0] != 0) |
| 161 |
print_to_socket(fh, "Plugin Instance: %s\n", threshold.plugin_instance); |
| 162 |
if (threshold.type[0] != 0) |
| 163 |
print_to_socket(fh, "Type: %s\n", threshold.type); |
| 164 |
if (threshold.type_instance[0] != 0) |
| 165 |
print_to_socket(fh, "Type Instance: %s\n", threshold.type_instance); |
| 166 |
if (threshold.data_source[0] != 0) |
| 167 |
print_to_socket(fh, "Data Source: %s\n", threshold.data_source); |
| 168 |
if (!isnan(threshold.warning_min)) |
| 169 |
print_to_socket(fh, "Warning Min: %g\n", threshold.warning_min); |
| 170 |
if (!isnan(threshold.warning_max)) |
| 171 |
print_to_socket(fh, "Warning Max: %g\n", threshold.warning_max); |
| 172 |
if (!isnan(threshold.failure_min)) |
| 173 |
print_to_socket(fh, "Failure Min: %g\n", threshold.failure_min); |
| 174 |
if (!isnan(threshold.failure_max)) |
| 175 |
print_to_socket(fh, "Failure Max: %g\n", threshold.failure_max); |
| 176 |
if (threshold.hysteresis > 0.0) |
| 177 |
print_to_socket(fh, "Hysteresis: %g\n", threshold.hysteresis); |
| 178 |
if (threshold.hits > 1) |
| 179 |
print_to_socket(fh, "Hits: %i\n", threshold.hits); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} /* int handle_getthreshold */ |