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