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 static int parse_option_severity (notification_t *n, char *value)
28 if (strcasecmp (value, "Failure") == 0)
29 n->severity = NOTIF_FAILURE;
30 else if (strcasecmp (value, "Warning") == 0)
31 n->severity = NOTIF_WARNING;
32 else if (strcasecmp (value, "Okay") == 0)
33 n->severity = NOTIF_OKAY;
38 } /* int parse_option_severity */
40 static int parse_option_time (notification_t *n, char *value)
44 tmp = (time_t) atoi (value);
51 } /* int parse_option_time */
53 static int parse_option (notification_t *n, char *buffer)
55 char *option = buffer;
58 if ((n == NULL) || (option == NULL))
61 value = strchr (option, '=');
64 *value = '\0'; value++;
66 if (strcasecmp ("severity", option) == 0)
67 return (parse_option_severity (n, value));
68 else if (strcasecmp ("time", option) == 0)
69 return (parse_option_time (n, value));
70 else if (strcasecmp ("host", option) == 0)
71 sstrncpy (n->host, value, sizeof (n->host));
72 else if (strcasecmp ("plugin", option) == 0)
73 sstrncpy (n->plugin, value, sizeof (n->plugin));
74 else if (strcasecmp ("plugin_instance", option) == 0)
75 sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
76 else if (strcasecmp ("type", option) == 0)
77 sstrncpy (n->type, value, sizeof (n->type));
78 else if (strcasecmp ("type_instance", option) == 0)
79 sstrncpy (n->type_instance, value, sizeof (n->type_instance));
84 } /* int parse_option */
86 static int parse_message (notification_t *n, char **fields, int fields_num)
90 /* Strip off the leading `message=' */
91 fields[0] += strlen ("message=");
93 status = strjoin (n->message, sizeof (n->message), fields, fields_num, " ");
98 } /* int parse_message */
100 int handle_putnotif (FILE *fh, char **fields, int fields_num)
106 /* Required fields: `PUTNOTIF', severity, time, message */
109 DEBUG ("cmd putnotif: Wrong number of fields: %i", fields_num);
110 fprintf (fh, "-1 Wrong number of fields: Got %i, expected at least 4.\n",
116 memset (&n, '\0', sizeof (n));
119 for (i = 1; i < fields_num; i++)
121 if (strncasecmp (fields[i], "message=", strlen ("message=")) == 0)
123 status = parse_message (&n, fields + i, fields_num - i);
126 fprintf (fh, "-1 Error parsing the message. Have you hit the "
127 "limit of %u bytes?\n", (unsigned int) sizeof (n.message));
133 status = parse_option (&n, fields[i]);
136 fprintf (fh, "-1 Error parsing option `%s'\n", fields[i]);
142 /* Check for required fields and complain if anything is missing. */
143 if ((status == 0) && (n.severity == 0))
145 fprintf (fh, "-1 Option `severity' missing.\n");
148 if ((status == 0) && (n.time == 0))
150 fprintf (fh, "-1 Option `time' missing.\n");
153 if ((status == 0) && (strlen (n.message) == 0))
155 fprintf (fh, "-1 No message or message of length 0 given.\n");
159 /* If status is still zero the notification is fine and we can finally
163 plugin_dispatch_notification (&n);
164 fprintf (fh, "0 Success\n");
169 } /* int handle_putnotif */
171 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */