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