2 * collectd - src/utils_cms_getthreshold.c
3 * Copyright (C) 2008,2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include "utils_avltree.h"
27 #include "utils_threshold.h"
28 #include "utils_parse_option.h" /* for `parse_string' */
29 #include "utils_cmd_getthreshold.h"
31 #define print_to_socket(fh, ...) \
32 if (fprintf (fh, __VA_ARGS__) < 0) { \
34 WARNING ("handle_getthreshold: failed to write to socket #%i: %s", \
35 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
39 int handle_getthreshold (FILE *fh, char *buffer)
43 char *identifier_copy;
47 char *plugin_instance;
52 threshold_t threshold;
57 if ((fh == NULL) || (buffer == NULL))
60 DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
64 status = parse_string (&buffer, &command);
67 print_to_socket (fh, "-1 Cannot parse command.\n");
70 assert (command != NULL);
72 if (strcasecmp ("GETTHRESHOLD", command) != 0)
74 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
79 status = parse_string (&buffer, &identifier);
82 print_to_socket (fh, "-1 Cannot parse identifier.\n");
85 assert (identifier != NULL);
89 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
93 /* parse_identifier() modifies its first argument,
94 * returning pointers into it */
95 identifier_copy = sstrdup (identifier);
97 status = parse_identifier (identifier_copy, &host,
98 &plugin, &plugin_instance,
99 &type, &type_instance);
102 DEBUG ("handle_getthreshold: Cannot parse identifier `%s'.", identifier);
103 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
104 sfree (identifier_copy);
108 memset (&vl, 0, sizeof (vl));
109 sstrncpy (vl.host, host, sizeof (vl.host));
110 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
111 if (plugin_instance != NULL)
112 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
113 sstrncpy (vl.type, type, sizeof (vl.type));
114 if (type_instance != NULL)
115 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
116 sfree (identifier_copy);
118 memset (&threshold, 0, sizeof (threshold));
119 status = ut_search_threshold (&vl, &threshold);
120 if (status == ENOENT)
122 print_to_socket (fh, "-1 No threshold found for identifier %s\n",
126 else if (status != 0)
128 print_to_socket (fh, "-1 Error while looking up threshold: %i\n",
133 /* Lets count the number of lines we'll return. */
135 if (threshold.host[0] != 0) i++;
136 if (threshold.plugin[0] != 0) i++;
137 if (threshold.plugin_instance[0] != 0) i++;
138 if (threshold.type[0] != 0) i++;
139 if (threshold.type_instance[0] != 0) i++;
140 if (threshold.data_source[0] != 0) i++;
141 if (!isnan (threshold.warning_min)) i++;
142 if (!isnan (threshold.warning_max)) i++;
143 if (!isnan (threshold.failure_min)) i++;
144 if (!isnan (threshold.failure_max)) i++;
145 if (threshold.hysteresis > 0.0) i++;
146 if (threshold.hits > 1) i++;
148 /* Print the response */
149 print_to_socket (fh, "%zu Threshold found\n", i);
151 if (threshold.host[0] != 0)
152 print_to_socket (fh, "Host: %s\n", threshold.host)
153 if (threshold.plugin[0] != 0)
154 print_to_socket (fh, "Plugin: %s\n", threshold.plugin)
155 if (threshold.plugin_instance[0] != 0)
156 print_to_socket (fh, "Plugin Instance: %s\n", threshold.plugin_instance)
157 if (threshold.type[0] != 0)
158 print_to_socket (fh, "Type: %s\n", threshold.type)
159 if (threshold.type_instance[0] != 0)
160 print_to_socket (fh, "Type Instance: %s\n", threshold.type_instance)
161 if (threshold.data_source[0] != 0)
162 print_to_socket (fh, "Data Source: %s\n", threshold.data_source)
163 if (!isnan (threshold.warning_min))
164 print_to_socket (fh, "Warning Min: %g\n", threshold.warning_min)
165 if (!isnan (threshold.warning_max))
166 print_to_socket (fh, "Warning Max: %g\n", threshold.warning_max)
167 if (!isnan (threshold.failure_min))
168 print_to_socket (fh, "Failure Min: %g\n", threshold.failure_min)
169 if (!isnan (threshold.failure_max))
170 print_to_socket (fh, "Failure Max: %g\n", threshold.failure_max)
171 if (threshold.hysteresis > 0.0)
172 print_to_socket (fh, "Hysteresis: %g\n", threshold.hysteresis)
173 if (threshold.hits > 1)
174 print_to_socket (fh, "Hits: %i\n", threshold.hits)
177 } /* int handle_getthreshold */
179 /* vim: set sw=2 sts=2 ts=8 et : */