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 #define print_to_socket(fh, ...) \
27 if (fprintf (fh, __VA_ARGS__) < 0) { \
29 WARNING ("handle_putnotif: failed to write to socket #%i: %s", \
30 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
34 static int parse_option_severity (notification_t *n, char *value)
36 if (strcasecmp (value, "Failure") == 0)
37 n->severity = NOTIF_FAILURE;
38 else if (strcasecmp (value, "Warning") == 0)
39 n->severity = NOTIF_WARNING;
40 else if (strcasecmp (value, "Okay") == 0)
41 n->severity = NOTIF_OKAY;
46 } /* int parse_option_severity */
48 static int parse_option_time (notification_t *n, char *value)
52 tmp = (time_t) atoi (value);
59 } /* int parse_option_time */
61 static int parse_option (notification_t *n, char *buffer)
63 char *option = buffer;
66 if ((n == NULL) || (option == NULL))
69 value = strchr (option, '=');
72 *value = '\0'; value++;
74 if (strcasecmp ("severity", option) == 0)
75 return (parse_option_severity (n, value));
76 else if (strcasecmp ("time", option) == 0)
77 return (parse_option_time (n, value));
78 else if (strcasecmp ("host", option) == 0)
79 sstrncpy (n->host, value, sizeof (n->host));
80 else if (strcasecmp ("plugin", option) == 0)
81 sstrncpy (n->plugin, value, sizeof (n->plugin));
82 else if (strcasecmp ("plugin_instance", option) == 0)
83 sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
84 else if (strcasecmp ("type", option) == 0)
85 sstrncpy (n->type, value, sizeof (n->type));
86 else if (strcasecmp ("type_instance", option) == 0)
87 sstrncpy (n->type_instance, value, sizeof (n->type_instance));
92 } /* int parse_option */
94 static int parse_message (notification_t *n, char **fields, int fields_num)
98 /* Strip off the leading `message=' */
99 fields[0] += strlen ("message=");
101 status = strjoin (n->message, sizeof (n->message), fields, fields_num, " ");
106 } /* int parse_message */
108 int handle_putnotif (FILE *fh, char **fields, int fields_num)
114 /* Required fields: `PUTNOTIF', severity, time, message */
117 DEBUG ("cmd putnotif: Wrong number of fields: %i", fields_num);
118 print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
119 "expected at least 4.\n",
124 memset (&n, '\0', sizeof (n));
127 for (i = 1; i < fields_num; i++)
129 if (strncasecmp (fields[i], "message=", strlen ("message=")) == 0)
131 status = parse_message (&n, fields + i, fields_num - i);
134 print_to_socket (fh, "-1 Error parsing the message. Have you hit the "
135 "limit of %u bytes?\n", (unsigned int) sizeof (n.message));
141 status = parse_option (&n, fields[i]);
144 print_to_socket (fh, "-1 Error parsing option `%s'\n", fields[i]);
150 /* Check for required fields and complain if anything is missing. */
151 if ((status == 0) && (n.severity == 0))
153 print_to_socket (fh, "-1 Option `severity' missing.\n");
156 if ((status == 0) && (n.time == 0))
158 print_to_socket (fh, "-1 Option `time' missing.\n");
161 if ((status == 0) && (strlen (n.message) == 0))
163 print_to_socket (fh, "-1 No message or message of length 0 given.\n");
167 /* If status is still zero the notification is fine and we can finally
171 plugin_dispatch_notification (&n);
172 print_to_socket (fh, "0 Success\n");
176 } /* int handle_putnotif */
178 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */