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