contrib/exec-nagios.px: Added a Perl script which handles Nagios plugins.
[collectd.git] / src / exec.c
index df912a6..d5a8d0f 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/exec.c
- * Copyright (C) 2007  Florian octo Forster
+ * Copyright (C) 2007,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
@@ -22,7 +22,9 @@
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+
 #include "utils_cmd_putval.h"
+#include "utils_cmd_putnotif.h"
 
 #include <sys/types.h>
 #include <pwd.h>
@@ -469,10 +471,15 @@ static int parse_line (char *buffer) /* {{{ */
   int fields_num;
 
   fields[0] = "PUTVAL";
-  fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
+  fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
 
-  handle_putval (stdout, fields, fields_num + 1);
-  return (0);
+  if (strcasecmp (fields[1], "putval") == 0)
+    return (handle_putval (stdout, fields + 1, fields_num));
+  else if (strcasecmp (fields[1], "putnotif") == 0)
+    return (handle_putnotif (stdout, fields + 1, fields_num));
+
+  /* compatibility code */
+  return (handle_putval (stdout, fields, fields_num + 1));
 } /* int parse_line }}} */
 
 static void *exec_read_one (void *arg) /* {{{ */
@@ -574,8 +581,10 @@ static void *exec_notification_one (void *arg) /* {{{ */
   const char *severity;
 
   pid = fork_child (pl, &fd, NULL);
-  if (pid < 0)
+  if (pid < 0) {
+    sfree (arg);
     pthread_exit ((void *) 1);
+  }
 
   fh = fdopen (fd, "w");
   if (fh == NULL)
@@ -586,6 +595,7 @@ static void *exec_notification_one (void *arg) /* {{{ */
     kill (pl->pid, SIGTERM);
     pl->pid = 0;
     close (fd);
+    sfree (arg);
     pthread_exit ((void *) 1);
   }
 
@@ -597,10 +607,24 @@ static void *exec_notification_one (void *arg) /* {{{ */
 
   fprintf (fh, "Severity: %s\n"
       "Time: %u\n"
-      "Host: %s\n"
-      "Message: %s\n"
-      "\n",
-      severity, (unsigned int) n->time, n->host, n->message);
+      "Message: %s\n",
+      severity, (unsigned int) n->time, n->message);
+
+  /* Print the optional fields */
+  if (strlen (n->host) > 0)
+    fprintf (fh, "Host: %s\n", n->host);
+  if (strlen (n->plugin) > 0)
+    fprintf (fh, "Plugin: %s\n", n->plugin);
+  if (strlen (n->plugin_instance) > 0)
+    fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
+  if (strlen (n->type) > 0)
+    fprintf (fh, "Type: %s\n", n->type);
+  if (strlen (n->type_instance) > 0)
+    fprintf (fh, "TypeInstance: %s\n", n->type_instance);
+
+  /* Newline signalling end of data */
+  fprintf (fh, "\n");
+
   fflush (fh);
   fclose (fh);