X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Futils_cmd_putnotif.c;h=eb7d60bc88657dfb5df94fec66940482c3d7b5c3;hb=66e0fe82631c8a8e44ffcad8ffd378fab83bc83f;hp=18c1eceedff4f834d5dc196f906bc2426ce18b34;hpb=79e2f5ddc8bffe7ba604530b6090b5d7f7ec9bb1;p=collectd.git diff --git a/src/utils_cmd_putnotif.c b/src/utils_cmd_putnotif.c index 18c1ecee..eb7d60bc 100644 --- a/src/utils_cmd_putnotif.c +++ b/src/utils_cmd_putnotif.c @@ -23,6 +23,14 @@ #include "common.h" #include "plugin.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 parse_option_severity (notification_t *n, char *value) { if (strcasecmp (value, "Failure") == 0) @@ -107,9 +115,9 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num) if (fields_num < 4) { DEBUG ("cmd putnotif: Wrong number of fields: %i", fields_num); - fprintf (fh, "-1 Wrong number of fields: Got %i, expected at least 4.\n", + print_to_socket (fh, "-1 Wrong number of fields: Got %i, " + "expected at least 4.\n", fields_num); - fflush (fh); return (-1); } @@ -123,7 +131,7 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num) status = parse_message (&n, fields + i, fields_num - i); if (status != 0) { - fprintf (fh, "-1 Error parsing the message. Have you hit the " + print_to_socket (fh, "-1 Error parsing the message. Have you hit the " "limit of %u bytes?\n", (unsigned int) sizeof (n.message)); } break; @@ -133,7 +141,7 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num) status = parse_option (&n, fields[i]); if (status != 0) { - fprintf (fh, "-1 Error parsing option `%s'\n", fields[i]); + print_to_socket (fh, "-1 Error parsing option `%s'\n", fields[i]); break; } } @@ -142,17 +150,17 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num) /* 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 +169,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 */