1 /**
2 * collectd - src/utils_cms_getthreshold.c
3 * Copyright (C) 2008,2009 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
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) { \
33 char errbuf[1024]; \
34 WARNING ("handle_getthreshold: failed to write to socket #%i: %s", \
35 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
36 return -1; \
37 }
39 int handle_getthreshold (FILE *fh, char *buffer)
40 {
41 char *command;
42 char *identifier;
43 char *identifier_copy;
45 char *host;
46 char *plugin;
47 char *plugin_instance;
48 char *type;
49 char *type_instance;
51 value_list_t vl;
52 threshold_t threshold;
54 int status;
55 size_t i;
57 if ((fh == NULL) || (buffer == NULL))
58 return (-1);
60 DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
61 (void *) fh, buffer);
63 command = NULL;
64 status = parse_string (&buffer, &command);
65 if (status != 0)
66 {
67 print_to_socket (fh, "-1 Cannot parse command.\n");
68 return (-1);
69 }
70 assert (command != NULL);
72 if (strcasecmp ("GETTHRESHOLD", command) != 0)
73 {
74 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
75 return (-1);
76 }
78 identifier = NULL;
79 status = parse_string (&buffer, &identifier);
80 if (status != 0)
81 {
82 print_to_socket (fh, "-1 Cannot parse identifier.\n");
83 return (-1);
84 }
85 assert (identifier != NULL);
87 if (*buffer != 0)
88 {
89 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
90 return (-1);
91 }
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);
100 if (status != 0)
101 {
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);
105 return (-1);
106 }
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)
121 {
122 print_to_socket (fh, "-1 No threshold found for identifier %s\n",
123 identifier);
124 return (0);
125 }
126 else if (status != 0)
127 {
128 print_to_socket (fh, "-1 Error while looking up threshold: %i\n",
129 status);
130 return (-1);
131 }
133 /* Lets count the number of lines we'll return. */
134 i = 0;
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)
176 return (0);
177 } /* int handle_getthreshold */
179 /* vim: set sw=2 sts=2 ts=8 et : */