d09eeb3ee75eba0eabb554f784f287968b4239c9
[collectd.git] / src / utils_cmd_putnotif.c
1 /**
2  * collectd - src/utils_cms_putnotif.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include "utils_parse_option.h"
27
28 #define print_to_socket(fh, ...) \
29   if (fprintf (fh, __VA_ARGS__) < 0) { \
30     char errbuf[1024]; \
31     WARNING ("handle_putnotif: failed to write to socket #%i: %s", \
32         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33     return -1; \
34   } \
35   fflush(fh);
36
37 static int set_option_severity (notification_t *n, const char *value)
38 {
39   if (strcasecmp (value, "Failure") == 0)
40     n->severity = NOTIF_FAILURE;
41   else if (strcasecmp (value, "Warning") == 0)
42     n->severity = NOTIF_WARNING;
43   else if (strcasecmp (value, "Okay") == 0)
44     n->severity = NOTIF_OKAY;
45   else
46     return (-1);
47
48   return (0);
49 } /* int set_option_severity */
50
51 static int set_option_time (notification_t *n, const char *value)
52 {
53   char *endptr = NULL;
54   double tmp;
55
56   errno = 0;
57   tmp = strtod (value, &endptr);
58   if ((errno != 0)         /* Overflow */
59       || (endptr == value) /* Invalid string */
60       || (endptr == NULL)  /* This should not happen */
61       || (*endptr != 0))   /* Trailing chars */
62     return (-1);
63
64   n->time = DOUBLE_TO_CDTIME_T (tmp);
65
66   return (0);
67 } /* int set_option_time */
68
69 static int set_option (notification_t *n, const char *option, const char *value)
70 {
71   if ((n == NULL) || (option == NULL) || (value == NULL))
72     return (-1);
73
74   DEBUG ("utils_cmd_putnotif: set_option (option = %s, value = %s);",
75       option, value);
76
77   if (strcasecmp ("severity", option) == 0)
78     return (set_option_severity (n, value));
79   else if (strcasecmp ("time", option) == 0)
80     return (set_option_time (n, value));
81   else if (strcasecmp ("message", option) == 0)
82     sstrncpy (n->message, value, sizeof (n->message));
83   else if (strcasecmp ("host", option) == 0)
84     sstrncpy (n->host, value, sizeof (n->host));
85   else if (strcasecmp ("plugin", option) == 0)
86     sstrncpy (n->plugin, value, sizeof (n->plugin));
87   else if (strcasecmp ("plugin_instance", option) == 0)
88     sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
89   else if (strcasecmp ("type", option) == 0)
90     sstrncpy (n->type, value, sizeof (n->type));
91   else if (strcasecmp ("type_instance", option) == 0)
92     sstrncpy (n->type_instance, value, sizeof (n->type_instance));
93   else
94     return (1);
95
96   return (0);
97 } /* int set_option */
98
99 int handle_putnotif (FILE *fh, char *buffer)
100 {
101   char *command;
102   notification_t n;
103   int status;
104
105   if ((fh == NULL) || (buffer == NULL))
106     return (-1);
107
108   DEBUG ("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);",
109       (void *) fh, buffer);
110
111   command = NULL;
112   status = parse_string (&buffer, &command);
113   if (status != 0)
114   {
115     print_to_socket (fh, "-1 Cannot parse command.\n");
116     return (-1);
117   }
118   assert (command != NULL);
119
120   if (strcasecmp ("PUTNOTIF", command) != 0)
121   {
122     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
123     return (-1);
124   }
125
126   memset (&n, '\0', sizeof (n));
127
128   status = 0;
129   while (*buffer != 0)
130   {
131     char *key;
132     char *value;
133
134     status = parse_option (&buffer, &key, &value);
135     if (status != 0)
136     {
137       print_to_socket (fh, "-1 Malformed option.\n");
138       break;
139     }
140
141     status = set_option (&n, key, value);
142     if (status != 0)
143     {
144       print_to_socket (fh, "-1 Error parsing option `%s'\n", key);
145       break;
146     }
147   } /* for (i) */
148
149   /* Check for required fields and complain if anything is missing. */
150   if ((status == 0) && (n.severity == 0))
151   {
152     print_to_socket (fh, "-1 Option `severity' missing.\n");
153     status = -1;
154   }
155   if ((status == 0) && (n.time == 0))
156   {
157     print_to_socket (fh, "-1 Option `time' missing.\n");
158     status = -1;
159   }
160   if ((status == 0) && (strlen (n.message) == 0))
161   {
162     print_to_socket (fh, "-1 No message or message of length 0 given.\n");
163     status = -1;
164   }
165
166   /* If status is still zero the notification is fine and we can finally
167    * dispatch it. */
168   if (status == 0)
169   {
170     plugin_dispatch_notification (&n);
171     print_to_socket (fh, "0 Success\n");
172   }
173
174   return (0);
175 } /* int handle_putnotif */
176
177 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */