Merge branch 'collectd-5.8'
[collectd.git] / src / utils_cmd_putnotif.c
1 /**
2  * collectd - src/utils_cmd_putnotif.c
3  * Copyright (C) 2008       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_cmd_putnotif.h"
33 #include "utils_parse_option.h"
34
35 #define print_to_socket(fh, ...)                                               \
36   do {                                                                         \
37     if (fprintf(fh, __VA_ARGS__) < 0) {                                        \
38       WARNING("handle_putnotif: failed to write to socket #%i: %s",            \
39               fileno(fh), STRERRNO);                                           \
40       return -1;                                                               \
41     }                                                                          \
42     fflush(fh);                                                                \
43   } while (0)
44
45 static int set_option_severity(notification_t *n, const char *value) {
46   if (strcasecmp(value, "Failure") == 0)
47     n->severity = NOTIF_FAILURE;
48   else if (strcasecmp(value, "Warning") == 0)
49     n->severity = NOTIF_WARNING;
50   else if (strcasecmp(value, "Okay") == 0)
51     n->severity = NOTIF_OKAY;
52   else
53     return -1;
54
55   return 0;
56 } /* int set_option_severity */
57
58 static int set_option_time(notification_t *n, const char *value) {
59   char *endptr = NULL;
60   double tmp;
61
62   errno = 0;
63   tmp = strtod(value, &endptr);
64   if ((errno != 0)         /* Overflow */
65       || (endptr == value) /* Invalid string */
66       || (endptr == NULL)  /* This should not happen */
67       || (*endptr != 0))   /* Trailing chars */
68     return -1;
69
70   n->time = DOUBLE_TO_CDTIME_T(tmp);
71
72   return 0;
73 } /* int set_option_time */
74
75 static int set_option(notification_t *n, const char *option,
76                       const char *value) {
77   if ((n == NULL) || (option == NULL) || (value == NULL))
78     return -1;
79
80   DEBUG("utils_cmd_putnotif: set_option (option = %s, value = %s);", option,
81         value);
82
83   /* Add a meta option in the form: <type>:<key> */
84   if (option[0] != '\0' && option[1] == ':') {
85     /* Refuse empty key */
86     if (option[2] == '\0')
87       return 1;
88
89     if (option[0] == 's')
90       return plugin_notification_meta_add_string(n, option + 2, value);
91     else
92       return 1;
93   }
94
95   if (strcasecmp("severity", option) == 0)
96     return set_option_severity(n, value);
97   else if (strcasecmp("time", option) == 0)
98     return set_option_time(n, value);
99   else if (strcasecmp("message", option) == 0)
100     sstrncpy(n->message, value, sizeof(n->message));
101   else if (strcasecmp("host", option) == 0)
102     sstrncpy(n->host, value, sizeof(n->host));
103   else if (strcasecmp("plugin", option) == 0)
104     sstrncpy(n->plugin, value, sizeof(n->plugin));
105   else if (strcasecmp("plugin_instance", option) == 0)
106     sstrncpy(n->plugin_instance, value, sizeof(n->plugin_instance));
107   else if (strcasecmp("type", option) == 0)
108     sstrncpy(n->type, value, sizeof(n->type));
109   else if (strcasecmp("type_instance", option) == 0)
110     sstrncpy(n->type_instance, value, sizeof(n->type_instance));
111   else
112     return 1;
113
114   return 0;
115 } /* int set_option */
116
117 int handle_putnotif(FILE *fh, char *buffer) {
118   char *command;
119   notification_t n = {0};
120   int status;
121
122   if ((fh == NULL) || (buffer == NULL))
123     return -1;
124
125   DEBUG("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);",
126         (void *)fh, buffer);
127
128   command = NULL;
129   status = parse_string(&buffer, &command);
130   if (status != 0) {
131     print_to_socket(fh, "-1 Cannot parse command.\n");
132     return -1;
133   }
134   assert(command != NULL);
135
136   if (strcasecmp("PUTNOTIF", command) != 0) {
137     print_to_socket(fh, "-1 Unexpected command: `%s'.\n", command);
138     return -1;
139   }
140
141   status = 0;
142   while (*buffer != 0) {
143     char *key;
144     char *value;
145
146     status = parse_option(&buffer, &key, &value);
147     if (status != 0) {
148       print_to_socket(fh, "-1 Malformed option.\n");
149       break;
150     }
151
152     status = set_option(&n, key, value);
153     if (status != 0) {
154       print_to_socket(fh, "-1 Error parsing option `%s'\n", key);
155       break;
156     }
157   } /* for (i) */
158
159   /* Check for required fields and complain if anything is missing. */
160   if ((status == 0) && (n.severity == 0)) {
161     print_to_socket(fh, "-1 Option `severity' missing.\n");
162     status = -1;
163   }
164   if ((status == 0) && (n.time == 0)) {
165     print_to_socket(fh, "-1 Option `time' missing.\n");
166     status = -1;
167   }
168   if ((status == 0) && (strlen(n.message) == 0)) {
169     print_to_socket(fh, "-1 No message or message of length 0 given.\n");
170     status = -1;
171   }
172
173   /* If status is still zero the notification is fine and we can finally
174    * dispatch it. */
175   if (status == 0) {
176     plugin_dispatch_notification(&n);
177     print_to_socket(fh, "0 Success\n");
178   }
179
180   return 0;
181 } /* int handle_putnotif */