2 * collectd - src/exec.c
3 * Copyright (C) 2007,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 #include "utils_cmd_putval.h"
27 #include "utils_cmd_putnotif.h"
29 #include <sys/types.h>
36 #define PL_NORMAL 0x01
37 #define PL_NOTIF_ACTION 0x02
38 #define PL_NAGIOS_PLUGIN 0x04
40 #define PL_RUNNING 0x10
46 * Access to this structure is serialized using the `pl_lock' lock and the
47 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
48 * all functions used to handle notifications MUST NOT write to this structure.
49 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
51 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
53 struct program_list_s;
54 typedef struct program_list_s program_list_t;
67 typedef struct program_list_and_notification_s
71 } program_list_and_notification_t;
76 static program_list_t *pl_head = NULL;
77 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
82 static void sigchld_handler (int signal) /* {{{ */
86 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
89 for (pl = pl_head; pl != NULL; pl = pl->next)
94 } /* while (waitpid) */
95 } /* void sigchld_handler }}} */
97 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
103 if (ci->children_num != 0)
105 WARNING ("exec plugin: The config option `%s' may not be a block.",
109 if (ci->values_num < 2)
111 WARNING ("exec plugin: The config option `%s' needs at least two "
112 "arguments.", ci->key);
115 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
116 || (ci->values[1].type != OCONFIG_TYPE_STRING))
118 WARNING ("exec plugin: The first two arguments to the `%s' option must "
119 "be string arguments.", ci->key);
123 pl = (program_list_t *) malloc (sizeof (program_list_t));
126 ERROR ("exec plugin: malloc failed.");
129 memset (pl, '\0', sizeof (program_list_t));
132 if (strcasecmp ("NagiosExec", ci->key) == 0)
133 pl->flags |= PL_NAGIOS_PLUGIN;
136 if (strcasecmp ("NotificationExec", ci->key) == 0)
137 pl->flags |= PL_NOTIF_ACTION;
139 pl->flags |= PL_NORMAL;
141 pl->user = strdup (ci->values[0].value.string);
142 if (pl->user == NULL)
144 ERROR ("exec plugin: strdup failed.");
149 pl->group = strchr (pl->user, ':');
150 if (pl->group != NULL)
156 pl->exec = strdup (ci->values[1].value.string);
157 if (pl->exec == NULL)
159 ERROR ("exec plugin: strdup failed.");
165 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
166 if (pl->argv == NULL)
168 ERROR ("exec plugin: malloc failed.");
174 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
177 char *tmp = strrchr (ci->values[1].value.string, '/');
179 strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
181 strncpy (buffer, tmp + 1, sizeof (buffer));
182 buffer[sizeof (buffer) - 1] = '\0';
184 pl->argv[0] = strdup (buffer);
185 if (pl->argv[0] == NULL)
187 ERROR ("exec plugin: malloc failed.");
195 for (i = 1; i < (ci->values_num - 1); i++)
197 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
199 pl->argv[i] = strdup (ci->values[i + 1].value.string);
203 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
205 snprintf (buffer, sizeof (buffer), "%lf",
206 ci->values[i + 1].value.number);
210 if (ci->values[i + 1].value.boolean)
211 strncpy (buffer, "true", sizeof (buffer));
213 strncpy (buffer, "false", sizeof (buffer));
215 buffer[sizeof (buffer) - 1] = '\0';
217 pl->argv[i] = strdup (buffer);
220 if (pl->argv[i] == NULL)
222 ERROR ("exec plugin: strdup failed.");
227 if (i < (ci->values_num - 1))
240 for (i = 0; pl->argv[i] != NULL; i++)
242 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
249 } /* int exec_config_exec }}} */
251 static int exec_config (oconfig_item_t *ci) /* {{{ */
255 for (i = 0; i < ci->children_num; i++)
257 oconfig_item_t *child = ci->children + i;
258 if ((strcasecmp ("Exec", child->key) == 0)
260 || (strcasecmp ("NagiosExec", child->key) == 0)
262 || (strcasecmp ("NotificationExec", child->key) == 0))
263 exec_config_exec (child);
266 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
271 } /* int exec_config }}} */
273 static void exec_child (program_list_t *pl) /* {{{ */
280 struct passwd *sp_ptr;
286 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
289 ERROR ("exec plugin: getpwnam_r failed: %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
295 ERROR ("exec plugin: No such user: `%s'", pl->user);
303 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
307 /* The group configured in the configfile is set as effective group, because
308 * this way the forked process can (re-)gain the user's primary group. */
310 if (NULL != pl->group)
312 if ('\0' != *pl->group) {
313 struct group *gr_ptr = NULL;
316 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
319 ERROR ("exec plugin: getgrnam_r failed: %s",
320 sstrerror (errno, errbuf, sizeof (errbuf)));
325 ERROR ("exec plugin: No such group: `%s'", pl->group);
335 } /* if (pl->group == NULL) */
337 status = setgid (gid);
340 ERROR ("exec plugin: setgid (%i) failed: %s",
341 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
347 status = setegid (egid);
350 ERROR ("exec plugin: setegid (%i) failed: %s",
351 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
356 status = setuid (uid);
359 ERROR ("exec plugin: setuid (%i) failed: %s",
360 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
364 status = execvp (pl->exec, pl->argv);
366 ERROR ("exec plugin: exec failed: %s",
367 sstrerror (errno, errbuf, sizeof (errbuf)));
369 } /* void exec_child }}} */
372 * Creates two pipes (one for reading, ong for writing), forks a child, sets up
373 * the pipes so that fd_in is connected to STDIN of the child and fd_out is
374 * connected to STDOUT and STDERR of the child. Then is calls `exec_child'.
376 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
386 status = pipe (fd_pipe_in);
390 ERROR ("exec plugin: pipe failed: %s",
391 sstrerror (errno, errbuf, sizeof (errbuf)));
395 status = pipe (fd_pipe_out);
399 ERROR ("exec plugin: pipe failed: %s",
400 sstrerror (errno, errbuf, sizeof (errbuf)));
408 ERROR ("exec plugin: fork failed: %s",
409 sstrerror (errno, errbuf, sizeof (errbuf)));
414 close (fd_pipe_in[1]);
415 close (fd_pipe_out[0]);
417 /* If the `out' pipe has the filedescriptor STDIN we have to be careful
418 * with the `dup's below. So, if this is the case we have to handle the
419 * `out' pipe first. */
420 if (fd_pipe_out[1] == STDIN_FILENO)
422 int new_fileno = (fd_pipe_in[0] == STDOUT_FILENO)
423 ? STDERR_FILENO : STDOUT_FILENO;
424 dup2 (fd_pipe_out[1], new_fileno);
425 close (fd_pipe_out[1]);
426 fd_pipe_out[1] = new_fileno;
428 /* Now `fd_pipe_out[1]' is either `STDOUT' or `STDERR', but definitely not
431 /* Connect the `in' pipe to STDIN */
432 if (fd_pipe_in[0] != STDIN_FILENO)
434 dup2 (fd_pipe_in[0], STDIN_FILENO);
435 close (fd_pipe_in[0]);
436 fd_pipe_in[0] = STDIN_FILENO;
439 /* Now connect the `out' pipe to STDOUT and STDERR */
440 if (fd_pipe_out[1] != STDOUT_FILENO)
441 dup2 (fd_pipe_out[1], STDOUT_FILENO);
442 if (fd_pipe_out[1] != STDERR_FILENO)
443 dup2 (fd_pipe_out[1], STDERR_FILENO);
445 /* If the pipe has some FD that's something completely different, close it
447 if ((fd_pipe_out[1] != STDOUT_FILENO) && (fd_pipe_out[1] != STDERR_FILENO))
449 close (fd_pipe_out[1]);
450 fd_pipe_out[1] = STDOUT_FILENO;
454 /* does not return */
457 close (fd_pipe_in[0]);
458 close (fd_pipe_out[1]);
461 *fd_in = fd_pipe_in[1];
463 close (fd_pipe_in[1]);
466 *fd_out = fd_pipe_out[0];
468 close (fd_pipe_out[0]);
471 } /* int fork_child }}} */
473 static int parse_line (char *buffer) /* {{{ */
478 fields[0] = "PUTVAL";
479 fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
481 if (strcasecmp (fields[1], "putval") == 0)
482 return (handle_putval (stdout, fields + 1, fields_num));
483 else if (strcasecmp (fields[1], "putnotif") == 0)
484 return (handle_putnotif (stdout, fields + 1, fields_num));
486 /* compatibility code */
487 return (handle_putval (stdout, fields, fields_num + 1));
488 } /* int parse_line }}} */
490 static void *exec_read_one (void *arg) /* {{{ */
492 program_list_t *pl = (program_list_t *) arg;
498 status = fork_child (pl, NULL, &fd);
500 pthread_exit ((void *) 1);
503 assert (pl->pid != 0);
505 fh = fdopen (fd, "r");
509 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
510 sstrerror (errno, errbuf, sizeof (errbuf)));
511 kill (pl->pid, SIGTERM);
514 pthread_exit ((void *) 1);
518 while (fgets (buffer, sizeof (buffer), fh) != NULL)
522 len = strlen (buffer);
524 /* Remove newline from end. */
525 while ((len > 0) && ((buffer[len - 1] == '\n')
526 || (buffer[len - 1] == '\r')))
527 buffer[--len] = '\0';
529 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
531 if (pl->flags & PL_NAGIOS_PLUGIN)
535 } /* while (fgets) */
539 if (waitpid (pl->pid, &status, 0) > 0)
542 DEBUG ("exec plugin: Child %i exited with status %i.",
543 (int) pl->pid, pl->status);
545 if (pl->flags & PL_NAGIOS_PLUGIN)
549 memset (&n, '\0', sizeof (n));
551 n.severity = NOTIF_FAILURE;
553 n.severity = NOTIF_OKAY;
554 else if (pl->status == 1)
555 n.severity = NOTIF_WARNING;
557 strncpy (n.message, buffer, sizeof (n.message));
558 n.message[sizeof (n.message) - 1] = '\0';
560 n.time = time (NULL);
562 strncpy (n.host, hostname_g, sizeof (n.host));
563 n.host[sizeof (n.host) - 1] = '\0';
565 plugin_dispatch_notification (&n);
570 pthread_mutex_lock (&pl_lock);
571 pl->flags &= ~PL_RUNNING;
572 pthread_mutex_unlock (&pl_lock);
574 pthread_exit ((void *) 0);
576 } /* void *exec_read_one }}} */
578 static void *exec_notification_one (void *arg) /* {{{ */
580 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
581 const notification_t *n = &((program_list_and_notification_t *) arg)->n;
586 const char *severity;
588 pid = fork_child (pl, &fd, NULL);
591 pthread_exit ((void *) 1);
594 fh = fdopen (fd, "w");
598 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
599 sstrerror (errno, errbuf, sizeof (errbuf)));
600 kill (pl->pid, SIGTERM);
604 pthread_exit ((void *) 1);
607 severity = "FAILURE";
608 if (n->severity == NOTIF_WARNING)
609 severity = "WARNING";
610 else if (n->severity == NOTIF_OKAY)
616 severity, (unsigned int) n->time);
618 /* Print the optional fields */
619 if (strlen (n->host) > 0)
620 fprintf (fh, "Host: %s\n", n->host);
621 if (strlen (n->plugin) > 0)
622 fprintf (fh, "Plugin: %s\n", n->plugin);
623 if (strlen (n->plugin_instance) > 0)
624 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
625 if (strlen (n->type) > 0)
626 fprintf (fh, "Type: %s\n", n->type);
627 if (strlen (n->type_instance) > 0)
628 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
630 fprintf (fh, "\n%s\n", n->message);
635 waitpid (pid, &status, 0);
637 DEBUG ("exec plugin: Child %i exited with status %i.",
641 pthread_exit ((void *) 0);
643 } /* void *exec_notification_one }}} */
645 static int exec_init (void) /* {{{ */
649 memset (&sa, '\0', sizeof (sa));
650 sa.sa_handler = sigchld_handler;
651 sigaction (SIGCHLD, &sa, NULL);
654 } /* int exec_init }}} */
656 static int exec_read (void) /* {{{ */
660 for (pl = pl_head; pl != NULL; pl = pl->next)
665 /* Only execute `normal' and `nagios' style executables here. */
666 if ((pl->flags & (PL_NAGIOS_PLUGIN | PL_NORMAL)) == 0)
669 pthread_mutex_lock (&pl_lock);
670 /* Skip if a child is already running. */
671 if ((pl->flags & PL_RUNNING) != 0)
673 pthread_mutex_unlock (&pl_lock);
676 pl->flags |= PL_RUNNING;
677 pthread_mutex_unlock (&pl_lock);
679 pthread_attr_init (&attr);
680 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
681 pthread_create (&t, &attr, exec_read_one, (void *) pl);
685 } /* int exec_read }}} */
687 static int exec_notification (const notification_t *n)
690 program_list_and_notification_t *pln;
692 for (pl = pl_head; pl != NULL; pl = pl->next)
697 /* Only execute `notification' style executables here. */
698 if ((pl->flags & PL_NOTIF_ACTION) == 0)
701 /* Skip if a child is already running. */
705 pln = (program_list_and_notification_t *) malloc (sizeof
706 (program_list_and_notification_t));
709 ERROR ("exec plugin: malloc failed.");
714 memcpy (&pln->n, n, sizeof (notification_t));
716 pthread_attr_init (&attr);
717 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
718 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
722 } /* int exec_notification */
724 static int exec_shutdown (void) /* {{{ */
727 program_list_t *next;
736 kill (pl->pid, SIGTERM);
737 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
748 } /* int exec_shutdown }}} */
750 void module_register (void)
752 plugin_register_complex_config ("exec", exec_config);
753 plugin_register_init ("exec", exec_init);
754 plugin_register_read ("exec", exec_read);
755 plugin_register_notification ("exec", exec_notification);
756 plugin_register_shutdown ("exec", exec_shutdown);
757 } /* void module_register */
760 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker