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_threshold.h"
27 #include "utils_parse_option.h" /* for `parse_string' */
28 #include "utils_cmd_getthreshold.h"
30 #define print_to_socket(fh, ...) \
31 if (fprintf (fh, __VA_ARGS__) < 0) { \
33 WARNING ("handle_getthreshold: failed to write to socket #%i: %s", \
34 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
38 int handle_getthreshold (FILE *fh, char *buffer)
42 char *identifier_copy;
46 char *plugin_instance;
51 threshold_t threshold;
56 if ((fh == NULL) || (buffer == NULL))
59 DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
63 status = parse_string (&buffer, &command);
66 print_to_socket (fh, "-1 Cannot parse command.\n");
69 assert (command != NULL);
71 if (strcasecmp ("GETTHRESHOLD", command) != 0)
73 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
78 status = parse_string (&buffer, &identifier);
81 print_to_socket (fh, "-1 Cannot parse identifier.\n");
84 assert (identifier != NULL);
88 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
92 /* parse_identifier() modifies its first argument,
93 * returning pointers into it */
94 identifier_copy = sstrdup (identifier);
96 status = parse_identifier (identifier_copy, &host,
97 &plugin, &plugin_instance,
98 &type, &type_instance);
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);
107 memset (&vl, 0, sizeof (vl));
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);
117 memset (&threshold, 0, sizeof (threshold));
118 status = ut_search_threshold (&vl, &threshold);
119 if (status == ENOENT)
121 print_to_socket (fh, "-1 No threshold found for identifier %s\n",
125 else if (status != 0)
127 print_to_socket (fh, "-1 Error while looking up threshold: %i\n",
132 /* Lets count the number of lines we'll return. */
134 if (threshold.host[0] != 0) i++;
135 if (threshold.plugin[0] != 0) i++;
136 if (threshold.plugin_instance[0] != 0) i++;
137 if (threshold.type[0] != 0) i++;
138 if (threshold.type_instance[0] != 0) i++;
139 if (threshold.data_source[0] != 0) i++;
140 if (!isnan (threshold.warning_min)) i++;
141 if (!isnan (threshold.warning_max)) i++;
142 if (!isnan (threshold.failure_min)) i++;
143 if (!isnan (threshold.failure_max)) i++;
144 if (threshold.hysteresis > 0.0) i++;
145 if (threshold.hits > 1) i++;
147 /* Print the response */
148 print_to_socket (fh, "%zu Threshold found\n", i);
150 if (threshold.host[0] != 0)
151 print_to_socket (fh, "Host: %s\n", threshold.host)
152 if (threshold.plugin[0] != 0)
153 print_to_socket (fh, "Plugin: %s\n", threshold.plugin)
154 if (threshold.plugin_instance[0] != 0)
155 print_to_socket (fh, "Plugin Instance: %s\n", threshold.plugin_instance)
156 if (threshold.type[0] != 0)
157 print_to_socket (fh, "Type: %s\n", threshold.type)
158 if (threshold.type_instance[0] != 0)
159 print_to_socket (fh, "Type Instance: %s\n", threshold.type_instance)
160 if (threshold.data_source[0] != 0)
161 print_to_socket (fh, "Data Source: %s\n", threshold.data_source)
162 if (!isnan (threshold.warning_min))
163 print_to_socket (fh, "Warning Min: %g\n", threshold.warning_min)
164 if (!isnan (threshold.warning_max))
165 print_to_socket (fh, "Warning Max: %g\n", threshold.warning_max)
166 if (!isnan (threshold.failure_min))
167 print_to_socket (fh, "Failure Min: %g\n", threshold.failure_min)
168 if (!isnan (threshold.failure_max))
169 print_to_socket (fh, "Failure Max: %g\n", threshold.failure_max)
170 if (threshold.hysteresis > 0.0)
171 print_to_socket (fh, "Hysteresis: %g\n", threshold.hysteresis)
172 if (threshold.hits > 1)
173 print_to_socket (fh, "Hits: %i\n", threshold.hits)
176 } /* int handle_getthreshold */
178 /* vim: set sw=2 sts=2 ts=8 et : */