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