#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)
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);
}
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;
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;
}
}
/* 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;
}
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 */