2 * collectd - src/utils_cms_putnotif.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include "utils_parse_option.h"
28 #define print_to_socket(fh, ...) \
30 if (fprintf (fh, __VA_ARGS__) < 0) { \
32 WARNING ("handle_putnotif: failed to write to socket #%i: %s", \
33 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
39 static int set_option_severity (notification_t *n, const char *value)
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;
51 } /* int set_option_severity */
53 static int set_option_time (notification_t *n, const char *value)
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 */
66 n->time = DOUBLE_TO_CDTIME_T (tmp);
69 } /* int set_option_time */
71 static int set_option (notification_t *n, const char *option, const char *value)
73 if ((n == NULL) || (option == NULL) || (value == NULL))
76 DEBUG ("utils_cmd_putnotif: set_option (option = %s, value = %s);",
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));
99 } /* int set_option */
101 int handle_putnotif (FILE *fh, char *buffer)
107 if ((fh == NULL) || (buffer == NULL))
110 DEBUG ("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);",
111 (void *) fh, buffer);
114 status = parse_string (&buffer, &command);
117 print_to_socket (fh, "-1 Cannot parse command.\n");
120 assert (command != NULL);
122 if (strcasecmp ("PUTNOTIF", command) != 0)
124 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
128 memset (&n, '\0', sizeof (n));
136 status = parse_option (&buffer, &key, &value);
139 print_to_socket (fh, "-1 Malformed option.\n");
143 status = set_option (&n, key, value);
146 print_to_socket (fh, "-1 Error parsing option `%s'\n", key);
151 /* Check for required fields and complain if anything is missing. */
152 if ((status == 0) && (n.severity == 0))
154 print_to_socket (fh, "-1 Option `severity' missing.\n");
157 if ((status == 0) && (n.time == 0))
159 print_to_socket (fh, "-1 Option `time' missing.\n");
162 if ((status == 0) && (strlen (n.message) == 0))
164 print_to_socket (fh, "-1 No message or message of length 0 given.\n");
168 /* If status is still zero the notification is fine and we can finally
172 plugin_dispatch_notification (&n);
173 print_to_socket (fh, "0 Success\n");
177 } /* int handle_putnotif */
179 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */