X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Futils_cmd_putnotif.c;h=cee5327f2c148a46251cc6ef1fce29e739092430;hp=18c1eceedff4f834d5dc196f906bc2426ce18b34;hb=633c3966f770e4d46651a2fe219a18d8a9907a9f;hpb=e8fc151794f5e3ca5c86259dbe201c41bae861d0 diff --git a/src/utils_cmd_putnotif.c b/src/utils_cmd_putnotif.c index 18c1ecee..cee5327f 100644 --- a/src/utils_cmd_putnotif.c +++ b/src/utils_cmd_putnotif.c @@ -1,29 +1,44 @@ /** - * collectd - src/utils_cms_putnotif.c - * Copyright (C) 2008 Florian octo Forster + * collectd - src/utils_cmd_putnotif.c + * Copyright (C) 2008 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * - * Author: - * Florian octo Forster + * Authors: + * Florian octo Forster **/ #include "collectd.h" #include "common.h" #include "plugin.h" -static int parse_option_severity (notification_t *n, char *value) +#include "utils_parse_option.h" + +#define print_to_socket(fh, ...) \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_putnotif: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } + +static int set_option_severity (notification_t *n, const char *value) { if (strcasecmp (value, "Failure") == 0) n->severity = NOTIF_FAILURE; @@ -35,38 +50,40 @@ static int parse_option_severity (notification_t *n, char *value) return (-1); return (0); -} /* int parse_option_severity */ +} /* int set_option_severity */ -static int parse_option_time (notification_t *n, char *value) +static int set_option_time (notification_t *n, const char *value) { - time_t tmp; - - tmp = (time_t) atoi (value); - if (tmp <= 0) + char *endptr = NULL; + double tmp; + + errno = 0; + tmp = strtod (value, &endptr); + if ((errno != 0) /* Overflow */ + || (endptr == value) /* Invalid string */ + || (endptr == NULL) /* This should not happen */ + || (*endptr != 0)) /* Trailing chars */ return (-1); - n->time = tmp; + n->time = DOUBLE_TO_CDTIME_T (tmp); return (0); -} /* int parse_option_time */ +} /* int set_option_time */ -static int parse_option (notification_t *n, char *buffer) +static int set_option (notification_t *n, const char *option, const char *value) { - char *option = buffer; - char *value; - - if ((n == NULL) || (option == NULL)) + if ((n == NULL) || (option == NULL) || (value == NULL)) return (-1); - value = strchr (option, '='); - if (value == NULL) - return (-1); - *value = '\0'; value++; + DEBUG ("utils_cmd_putnotif: set_option (option = %s, value = %s);", + option, value); if (strcasecmp ("severity", option) == 0) - return (parse_option_severity (n, value)); + return (set_option_severity (n, value)); else if (strcasecmp ("time", option) == 0) - return (parse_option_time (n, value)); + return (set_option_time (n, value)); + else if (strcasecmp ("message", option) == 0) + sstrncpy (n->message, value, sizeof (n->message)); else if (strcasecmp ("host", option) == 0) sstrncpy (n->host, value, sizeof (n->host)); else if (strcasecmp ("plugin", option) == 0) @@ -81,78 +98,72 @@ static int parse_option (notification_t *n, char *buffer) return (1); return (0); -} /* int parse_option */ +} /* int set_option */ -static int parse_message (notification_t *n, char **fields, int fields_num) +int handle_putnotif (FILE *fh, char *buffer) { + char *command; + notification_t n; int status; - /* Strip off the leading `message=' */ - fields[0] += strlen ("message="); - - status = strjoin (n->message, sizeof (n->message), fields, fields_num, " "); - if (status < 0) + if ((fh == NULL) || (buffer == NULL)) return (-1); - return (0); -} /* int parse_message */ + DEBUG ("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);", + (void *) fh, buffer); -int handle_putnotif (FILE *fh, char **fields, int fields_num) -{ - notification_t n; - int status; - int i; + command = NULL; + status = parse_string (&buffer, &command); + if (status != 0) + { + print_to_socket (fh, "-1 Cannot parse command.\n"); + return (-1); + } + assert (command != NULL); - /* Required fields: `PUTNOTIF', severity, time, message */ - if (fields_num < 4) + if (strcasecmp ("PUTNOTIF", command) != 0) { - DEBUG ("cmd putnotif: Wrong number of fields: %i", fields_num); - fprintf (fh, "-1 Wrong number of fields: Got %i, expected at least 4.\n", - fields_num); - fflush (fh); + print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); return (-1); } memset (&n, '\0', sizeof (n)); status = 0; - for (i = 1; i < fields_num; i++) + while (*buffer != 0) { - if (strncasecmp (fields[i], "message=", strlen ("message=")) == 0) + char *key; + char *value; + + status = parse_option (&buffer, &key, &value); + if (status != 0) { - status = parse_message (&n, fields + i, fields_num - i); - if (status != 0) - { - fprintf (fh, "-1 Error parsing the message. Have you hit the " - "limit of %u bytes?\n", (unsigned int) sizeof (n.message)); - } + print_to_socket (fh, "-1 Malformed option.\n"); break; } - else + + status = set_option (&n, key, value); + if (status != 0) { - status = parse_option (&n, fields[i]); - if (status != 0) - { - fprintf (fh, "-1 Error parsing option `%s'\n", fields[i]); - break; - } + print_to_socket (fh, "-1 Error parsing option `%s'\n", key); + break; } } /* for (i) */ /* Check for required fields and complain if anything is missing. */ if ((status == 0) && (n.severity == 0)) { - fprintf (fh, "-1 Option `severity' missing.\n"); + print_to_socket (fh, "-1 Option `severity' missing.\n"); status = -1; } if ((status == 0) && (n.time == 0)) { - fprintf (fh, "-1 Option `time' missing.\n"); + print_to_socket (fh, "-1 Option `time' missing.\n"); status = -1; } if ((status == 0) && (strlen (n.message) == 0)) { - fprintf (fh, "-1 No message or message of length 0 given.\n"); + print_to_socket (fh, "-1 No message or message of length 0 given.\n"); status = -1; } @@ -161,9 +172,8 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num) if (status == 0) { plugin_dispatch_notification (&n); - fprintf (fh, "0 Success\n"); + print_to_socket (fh, "0 Success\n"); } - fflush (fh); return (0); } /* int handle_putnotif */