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