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