Merge branch 'collectd-4.7' into collectd-4.8
[collectd.git] / src / utils_cmd_getthreshold.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include "utils_threshold.h"
27 #include "utils_parse_option.h" /* for `parse_string' */
28 #include "utils_cmd_getthreshold.h"
29
30 #define print_to_socket(fh, ...) \
31   if (fprintf (fh, __VA_ARGS__) < 0) { \
32     char errbuf[1024]; \
33     WARNING ("handle_getthreshold: failed to write to socket #%i: %s", \
34         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
35     return -1; \
36   }
37
38 int handle_getthreshold (FILE *fh, char *buffer)
39 {
40   char *command;
41   char *identifier;
42   char *identifier_copy;
43
44   char *host;
45   char *plugin;
46   char *plugin_instance;
47   char *type;
48   char *type_instance;
49
50   value_list_t vl;
51   threshold_t threshold;
52
53   int   status;
54   size_t i;
55
56   if ((fh == NULL) || (buffer == NULL))
57     return (-1);
58
59   DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
60       (void *) fh, buffer);
61
62   command = NULL;
63   status = parse_string (&buffer, &command);
64   if (status != 0)
65   {
66     print_to_socket (fh, "-1 Cannot parse command.\n");
67     return (-1);
68   }
69   assert (command != NULL);
70
71   if (strcasecmp ("GETTHRESHOLD", command) != 0)
72   {
73     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
74     return (-1);
75   }
76
77   identifier = NULL;
78   status = parse_string (&buffer, &identifier);
79   if (status != 0)
80   {
81     print_to_socket (fh, "-1 Cannot parse identifier.\n");
82     return (-1);
83   }
84   assert (identifier != NULL);
85
86   if (*buffer != 0)
87   {
88     print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
89     return (-1);
90   }
91
92   /* parse_identifier() modifies its first argument,
93    * returning pointers into it */
94   identifier_copy = sstrdup (identifier);
95
96   status = parse_identifier (identifier_copy, &host,
97       &plugin, &plugin_instance,
98       &type, &type_instance);
99   if (status != 0)
100   {
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   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);
116
117   memset (&threshold, 0, sizeof (threshold));
118   status = ut_search_threshold (&vl, &threshold);
119   if (status == ENOENT)
120   {
121     print_to_socket (fh, "-1 No threshold found for identifier %s\n",
122         identifier);
123     return (0);
124   }
125   else if (status != 0)
126   {
127     print_to_socket (fh, "-1 Error while looking up threshold: %i\n",
128         status);
129     return (-1);
130   }
131
132   /* Lets count the number of lines we'll return. */
133   i = 0;
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++;
146
147   /* Print the response */
148   print_to_socket (fh, "%zu Threshold found\n", i);
149
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)
174
175   return (0);
176 } /* int handle_getthreshold */
177
178 /* vim: set sw=2 sts=2 ts=8 et : */