Merge branch 'collectd-5.5' into collectd-5.6
[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_threshold.h"
34 #include "utils_parse_option.h" /* for `parse_string' */
35 #include "utils_cmd_getthreshold.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 {
47   char *command;
48   char *identifier;
49   char *identifier_copy;
50
51   char *host;
52   char *plugin;
53   char *plugin_instance;
54   char *type;
55   char *type_instance;
56
57   threshold_t threshold;
58
59   int   status;
60   size_t i;
61
62   if ((fh == NULL) || (buffer == NULL))
63     return (-1);
64
65   DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
66       (void *) fh, buffer);
67
68   command = NULL;
69   status = parse_string (&buffer, &command);
70   if (status != 0)
71   {
72     print_to_socket (fh, "-1 Cannot parse command.\n");
73     return (-1);
74   }
75   assert (command != NULL);
76
77   if (strcasecmp ("GETTHRESHOLD", command) != 0)
78   {
79     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
80     return (-1);
81   }
82
83   identifier = NULL;
84   status = parse_string (&buffer, &identifier);
85   if (status != 0)
86   {
87     print_to_socket (fh, "-1 Cannot parse identifier.\n");
88     return (-1);
89   }
90   assert (identifier != NULL);
91
92   if (*buffer != 0)
93   {
94     print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
95     return (-1);
96   }
97
98   /* parse_identifier() modifies its first argument,
99    * returning pointers into it */
100   identifier_copy = sstrdup (identifier);
101
102   status = parse_identifier (identifier_copy, &host,
103       &plugin, &plugin_instance,
104       &type, &type_instance);
105   if (status != 0)
106   {
107     DEBUG ("handle_getthreshold: Cannot parse identifier `%s'.", identifier);
108     print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
109     sfree (identifier_copy);
110     return (-1);
111   }
112
113   value_list_t vl = {
114     .values = NULL
115   };
116   sstrncpy (vl.host, host, sizeof (vl.host));
117   sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
118   if (plugin_instance != NULL)
119     sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
120   sstrncpy (vl.type, type, sizeof (vl.type));
121   if (type_instance != NULL)
122     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
123   sfree (identifier_copy);
124
125   status = ut_search_threshold (&vl, &threshold);
126   if (status == ENOENT)
127   {
128     print_to_socket (fh, "-1 No threshold found for identifier %s\n",
129         identifier);
130     return (0);
131   }
132   else if (status != 0)
133   {
134     print_to_socket (fh, "-1 Error while looking up threshold: %i\n",
135         status);
136     return (-1);
137   }
138
139   /* Lets count the number of lines we'll return. */
140   i = 0;
141   if (threshold.host[0] != 0)            i++;
142   if (threshold.plugin[0] != 0)          i++;
143   if (threshold.plugin_instance[0] != 0) i++;
144   if (threshold.type[0] != 0)            i++;
145   if (threshold.type_instance[0] != 0)   i++;
146   if (threshold.data_source[0] != 0)     i++;
147   if (!isnan (threshold.warning_min))    i++;
148   if (!isnan (threshold.warning_max))    i++;
149   if (!isnan (threshold.failure_min))    i++;
150   if (!isnan (threshold.failure_max))    i++;
151   if (threshold.hysteresis > 0.0)        i++;
152   if (threshold.hits > 1)                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)
159   if (threshold.plugin[0] != 0)
160     print_to_socket (fh, "Plugin: %s\n", threshold.plugin)
161   if (threshold.plugin_instance[0] != 0)
162     print_to_socket (fh, "Plugin Instance: %s\n", threshold.plugin_instance)
163   if (threshold.type[0] != 0)
164     print_to_socket (fh, "Type: %s\n", threshold.type)
165   if (threshold.type_instance[0] != 0)
166     print_to_socket (fh, "Type Instance: %s\n", threshold.type_instance)
167   if (threshold.data_source[0] != 0)
168     print_to_socket (fh, "Data Source: %s\n", threshold.data_source)
169   if (!isnan (threshold.warning_min))
170     print_to_socket (fh, "Warning Min: %g\n", threshold.warning_min)
171   if (!isnan (threshold.warning_max))
172     print_to_socket (fh, "Warning Max: %g\n", threshold.warning_max)
173   if (!isnan (threshold.failure_min))
174     print_to_socket (fh, "Failure Min: %g\n", threshold.failure_min)
175   if (!isnan (threshold.failure_max))
176     print_to_socket (fh, "Failure Max: %g\n", threshold.failure_max)
177   if (threshold.hysteresis > 0.0)
178     print_to_socket (fh, "Hysteresis: %g\n", threshold.hysteresis)
179   if (threshold.hits > 1)
180     print_to_socket (fh, "Hits: %i\n", threshold.hits)
181
182   return (0);
183 } /* int handle_getthreshold */
184
185 /* vim: set sw=2 sts=2 ts=8 et : */