{GPL, other}: Relicense to MIT license.
[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   if (strcasecmp ("severity", option) == 0)
82     return (set_option_severity (n, value));
83   else if (strcasecmp ("time", option) == 0)
84     return (set_option_time (n, value));
85   else if (strcasecmp ("message", option) == 0)
86     sstrncpy (n->message, value, sizeof (n->message));
87   else if (strcasecmp ("host", option) == 0)
88     sstrncpy (n->host, value, sizeof (n->host));
89   else if (strcasecmp ("plugin", option) == 0)
90     sstrncpy (n->plugin, value, sizeof (n->plugin));
91   else if (strcasecmp ("plugin_instance", option) == 0)
92     sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
93   else if (strcasecmp ("type", option) == 0)
94     sstrncpy (n->type, value, sizeof (n->type));
95   else if (strcasecmp ("type_instance", option) == 0)
96     sstrncpy (n->type_instance, value, sizeof (n->type_instance));
97   else
98     return (1);
99
100   return (0);
101 } /* int set_option */
102
103 int handle_putnotif (FILE *fh, char *buffer)
104 {
105   char *command;
106   notification_t n;
107   int status;
108
109   if ((fh == NULL) || (buffer == NULL))
110     return (-1);
111
112   DEBUG ("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);",
113       (void *) fh, buffer);
114
115   command = NULL;
116   status = parse_string (&buffer, &command);
117   if (status != 0)
118   {
119     print_to_socket (fh, "-1 Cannot parse command.\n");
120     return (-1);
121   }
122   assert (command != NULL);
123
124   if (strcasecmp ("PUTNOTIF", command) != 0)
125   {
126     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
127     return (-1);
128   }
129
130   memset (&n, '\0', sizeof (n));
131
132   status = 0;
133   while (*buffer != 0)
134   {
135     char *key;
136     char *value;
137
138     status = parse_option (&buffer, &key, &value);
139     if (status != 0)
140     {
141       print_to_socket (fh, "-1 Malformed option.\n");
142       break;
143     }
144
145     status = set_option (&n, key, value);
146     if (status != 0)
147     {
148       print_to_socket (fh, "-1 Error parsing option `%s'\n", key);
149       break;
150     }
151   } /* for (i) */
152
153   /* Check for required fields and complain if anything is missing. */
154   if ((status == 0) && (n.severity == 0))
155   {
156     print_to_socket (fh, "-1 Option `severity' missing.\n");
157     status = -1;
158   }
159   if ((status == 0) && (n.time == 0))
160   {
161     print_to_socket (fh, "-1 Option `time' missing.\n");
162     status = -1;
163   }
164   if ((status == 0) && (strlen (n.message) == 0))
165   {
166     print_to_socket (fh, "-1 No message or message of length 0 given.\n");
167     status = -1;
168   }
169
170   /* If status is still zero the notification is fine and we can finally
171    * dispatch it. */
172   if (status == 0)
173   {
174     plugin_dispatch_notification (&n);
175     print_to_socket (fh, "0 Success\n");
176   }
177
178   return (0);
179 } /* int handle_putnotif */
180
181 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */