2 * collectd - src/exec.c
3 * Copyright (C) 2007 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>
25 #include "utils_cmd_putval.h"
27 #include <sys/types.h>
34 #define PL_NORMAL 0x01
35 #define PL_NOTIF_ACTION 0x02
36 #define PL_NAGIOS_PLUGIN 0x04
38 #define PL_RUNNING 0x10
44 * Access to this structure is serialized using the `pl_lock' lock and the
45 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
46 * all functions used to handle notifications MUST NOT write to this structure.
47 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
49 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
51 struct program_list_s;
52 typedef struct program_list_s program_list_t;
65 typedef struct program_list_and_notification_s
68 const notification_t *n;
69 } program_list_and_notification_t;
74 static program_list_t *pl_head = NULL;
75 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
80 static void sigchld_handler (int signal) /* {{{ */
84 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
87 for (pl = pl_head; pl != NULL; pl = pl->next)
92 } /* while (waitpid) */
93 } /* void sigchld_handler }}} */
95 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
101 if (ci->children_num != 0)
103 WARNING ("exec plugin: The config option `%s' may not be a block.",
107 if (ci->values_num < 2)
109 WARNING ("exec plugin: The config option `%s' needs at least two "
110 "arguments.", ci->key);
113 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
114 || (ci->values[1].type != OCONFIG_TYPE_STRING))
116 WARNING ("exec plugin: The first two arguments to the `%s' option must "
117 "be string arguments.", ci->key);
121 pl = (program_list_t *) malloc (sizeof (program_list_t));
124 ERROR ("exec plugin: malloc failed.");
127 memset (pl, '\0', sizeof (program_list_t));
129 if (strcasecmp ("NagiosExec", ci->key) == 0)
130 pl->flags |= PL_NAGIOS_PLUGIN;
131 else if (strcasecmp ("NotificationExec", ci->key) == 0)
132 pl->flags |= PL_NOTIF_ACTION;
134 pl->flags |= PL_NORMAL;
136 pl->user = strdup (ci->values[0].value.string);
137 if (pl->user == NULL)
139 ERROR ("exec plugin: strdup failed.");
144 pl->group = strchr (pl->user, ':');
145 if (pl->group != NULL)
151 pl->exec = strdup (ci->values[1].value.string);
152 if (pl->exec == NULL)
154 ERROR ("exec plugin: strdup failed.");
160 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
161 if (pl->argv == NULL)
163 ERROR ("exec plugin: malloc failed.");
169 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
172 char *tmp = strrchr (ci->values[1].value.string, '/');
174 strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
176 strncpy (buffer, tmp + 1, sizeof (buffer));
177 buffer[sizeof (buffer) - 1] = '\0';
179 pl->argv[0] = strdup (buffer);
180 if (pl->argv[0] == NULL)
182 ERROR ("exec plugin: malloc failed.");
190 for (i = 1; i < (ci->values_num - 1); i++)
192 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
194 pl->argv[i] = strdup (ci->values[i + 1].value.string);
198 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
200 snprintf (buffer, sizeof (buffer), "%lf",
201 ci->values[i + 1].value.number);
205 if (ci->values[i + 1].value.boolean)
206 strncpy (buffer, "true", sizeof (buffer));
208 strncpy (buffer, "false", sizeof (buffer));
210 buffer[sizeof (buffer) - 1] = '\0';
212 pl->argv[i] = strdup (buffer);
215 if (pl->argv[i] == NULL)
217 ERROR ("exec plugin: strdup failed.");
222 if (i < (ci->values_num - 1))
235 for (i = 0; pl->argv[i] != NULL; i++)
237 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
244 } /* int exec_config_exec }}} */
246 static int exec_config (oconfig_item_t *ci) /* {{{ */
250 for (i = 0; i < ci->children_num; i++)
252 oconfig_item_t *child = ci->children + i;
253 if ((strcasecmp ("Exec", child->key) == 0)
254 || (strcasecmp ("NagiosExec", child->key) == 0)
255 || (strcasecmp ("NotificationExec", child->key) == 0))
256 exec_config_exec (child);
259 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
264 } /* int exec_config }}} */
266 static void exec_child (program_list_t *pl) /* {{{ */
273 struct passwd *sp_ptr;
279 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
282 ERROR ("exec plugin: getpwnam_r failed: %s",
283 sstrerror (errno, errbuf, sizeof (errbuf)));
288 ERROR ("exec plugin: No such user: `%s'", pl->user);
296 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
300 /* The group configured in the configfile is set as effective group, because
301 * this way the forked process can (re-)gain the user's primary group. */
303 if (NULL != pl->group)
305 if ('\0' != *pl->group) {
306 struct group *gr_ptr = NULL;
309 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
312 ERROR ("exec plugin: getgrnam_r failed: %s",
313 sstrerror (errno, errbuf, sizeof (errbuf)));
318 ERROR ("exec plugin: No such group: `%s'", pl->group);
328 } /* if (pl->group == NULL) */
330 status = setgid (gid);
333 ERROR ("exec plugin: setgid (%i) failed: %s",
334 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
340 status = setegid (egid);
343 ERROR ("exec plugin: setegid (%i) failed: %s",
344 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
349 status = setuid (uid);
352 ERROR ("exec plugin: setuid (%i) failed: %s",
353 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
357 status = execvp (pl->exec, pl->argv);
359 ERROR ("exec plugin: exec failed: %s",
360 sstrerror (errno, errbuf, sizeof (errbuf)));
362 } /* void exec_child }}} */
365 * Creates two pipes (one for reading, ong for writing), forks a child, sets up
366 * the pipes so that fd_in is connected to STDIN of the child and fd_out is
367 * connected to STDOUT and STDERR of the child. Then is calls `exec_child'.
369 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
379 status = pipe (fd_pipe_in);
383 ERROR ("exec plugin: pipe failed: %s",
384 sstrerror (errno, errbuf, sizeof (errbuf)));
388 status = pipe (fd_pipe_out);
392 ERROR ("exec plugin: pipe failed: %s",
393 sstrerror (errno, errbuf, sizeof (errbuf)));
401 ERROR ("exec plugin: fork failed: %s",
402 sstrerror (errno, errbuf, sizeof (errbuf)));
407 close (fd_pipe_in[1]);
408 close (fd_pipe_out[0]);
410 /* If the `out' pipe has the filedescriptor STDIN we have to be careful
411 * with the `dup's below. So, if this is the case we have to handle the
412 * `out' pipe first. */
413 if (fd_pipe_out[1] == STDIN_FILENO)
415 int new_fileno = (fd_pipe_in[0] == STDOUT_FILENO)
416 ? STDERR_FILENO : STDOUT_FILENO;
417 dup2 (fd_pipe_out[1], new_fileno);
418 close (fd_pipe_out[1]);
419 fd_pipe_out[1] = new_fileno;
421 /* Now `fd_pipe_out[1]' is either `STDOUT' or `STDERR', but definitely not
424 /* Connect the `in' pipe to STDIN */
425 if (fd_pipe_in[0] != STDIN_FILENO)
427 dup2 (fd_pipe_in[0], STDIN_FILENO);
428 close (fd_pipe_in[0]);
429 fd_pipe_in[0] = STDIN_FILENO;
432 /* Now connect the `out' pipe to STDOUT and STDERR */
433 if (fd_pipe_out[1] != STDOUT_FILENO)
434 dup2 (fd_pipe_out[1], STDOUT_FILENO);
435 if (fd_pipe_out[1] != STDERR_FILENO)
436 dup2 (fd_pipe_out[1], STDERR_FILENO);
438 /* If the pipe has some FD that's something completely different, close it
440 if ((fd_pipe_out[1] != STDOUT_FILENO) && (fd_pipe_out[1] != STDERR_FILENO))
442 close (fd_pipe_out[1]);
443 fd_pipe_out[1] = STDOUT_FILENO;
447 /* does not return */
450 close (fd_pipe_in[0]);
451 close (fd_pipe_out[1]);
454 *fd_in = fd_pipe_in[1];
456 close (fd_pipe_in[1]);
459 *fd_out = fd_pipe_out[0];
461 close (fd_pipe_out[0]);
464 } /* int fork_child }}} */
466 static int parse_line (char *buffer) /* {{{ */
471 fields[0] = "PUTVAL";
472 fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
474 handle_putval (stdout, fields, fields_num + 1);
476 } /* int parse_line }}} */
478 static void *exec_read_one (void *arg) /* {{{ */
480 program_list_t *pl = (program_list_t *) arg;
486 status = fork_child (pl, NULL, &fd);
488 pthread_exit ((void *) 1);
491 assert (pl->pid != 0);
493 fh = fdopen (fd, "r");
497 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
498 sstrerror (errno, errbuf, sizeof (errbuf)));
499 kill (pl->pid, SIGTERM);
502 pthread_exit ((void *) 1);
506 while (fgets (buffer, sizeof (buffer), fh) != NULL)
510 len = strlen (buffer);
512 /* Remove newline from end. */
513 while ((len > 0) && ((buffer[len - 1] == '\n')
514 || (buffer[len - 1] == '\r')))
515 buffer[--len] = '\0';
517 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
519 if (pl->flags & PL_NAGIOS_PLUGIN)
523 } /* while (fgets) */
527 if (waitpid (pl->pid, &status, 0) > 0)
530 DEBUG ("exec plugin: Child %i exited with status %i.",
531 (int) pl->pid, pl->status);
533 if (pl->flags & PL_NAGIOS_PLUGIN)
537 memset (&n, '\0', sizeof (n));
539 n.severity = NOTIF_FAILURE;
541 n.severity = NOTIF_OKAY;
542 else if (pl->status == 1)
543 n.severity = NOTIF_WARNING;
545 strncpy (n.message, buffer, sizeof (n.message));
546 n.message[sizeof (n.message) - 1] = '\0';
548 n.time = time (NULL);
550 strncpy (n.host, hostname_g, sizeof (n.host));
551 n.host[sizeof (n.host) - 1] = '\0';
553 plugin_dispatch_notification (&n);
558 pthread_mutex_lock (&pl_lock);
559 pl->flags &= ~PL_RUNNING;
560 pthread_mutex_unlock (&pl_lock);
562 pthread_exit ((void *) 0);
564 } /* void *exec_read_one }}} */
566 static void *exec_notification_one (void *arg) /* {{{ */
568 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
569 const notification_t *n = ((program_list_and_notification_t *) arg)->n;
574 const char *severity;
576 pid = fork_child (pl, &fd, NULL);
578 pthread_exit ((void *) 1);
580 fh = fdopen (fd, "w");
584 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
585 sstrerror (errno, errbuf, sizeof (errbuf)));
586 kill (pl->pid, SIGTERM);
589 pthread_exit ((void *) 1);
592 severity = "FAILURE";
593 if (n->severity == NOTIF_WARNING)
594 severity = "WARNING";
595 else if (n->severity == NOTIF_OKAY)
598 fprintf (fh, "Severity: %s\n"
603 severity, (unsigned int) n->time, n->host, n->message);
607 waitpid (pid, &status, 0);
609 DEBUG ("exec plugin: Child %i exited with status %i.",
612 pthread_exit ((void *) 0);
614 } /* void *exec_notification_one }}} */
616 static int exec_init (void) /* {{{ */
620 memset (&sa, '\0', sizeof (sa));
621 sa.sa_handler = sigchld_handler;
622 sigaction (SIGCHLD, &sa, NULL);
625 } /* int exec_init }}} */
627 static int exec_read (void) /* {{{ */
631 for (pl = pl_head; pl != NULL; pl = pl->next)
636 /* Only execute `normal' and `nagios' style executables here. */
637 if ((pl->flags & (PL_NAGIOS_PLUGIN | PL_NORMAL)) == 0)
640 pthread_mutex_lock (&pl_lock);
641 /* Skip if a child is already running. */
642 if ((pl->flags & PL_RUNNING) != 0)
644 pthread_mutex_unlock (&pl_lock);
647 pl->flags |= PL_RUNNING;
648 pthread_mutex_unlock (&pl_lock);
650 pthread_attr_init (&attr);
651 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
652 pthread_create (&t, &attr, exec_read_one, (void *) pl);
656 } /* int exec_read }}} */
658 static int exec_notification (const notification_t *n)
662 for (pl = pl_head; pl != NULL; pl = pl->next)
667 /* Only execute `normal' and `nagios' style executables here. */
668 if ((pl->flags & PL_NOTIF_ACTION) == 0)
671 /* Skip if a child is already running. */
675 pthread_attr_init (&attr);
676 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
677 pthread_create (&t, &attr, exec_notification_one, (void *) pl);
681 } /* int exec_notification */
683 static int exec_shutdown (void) /* {{{ */
686 program_list_t *next;
695 kill (pl->pid, SIGTERM);
696 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
707 } /* int exec_shutdown }}} */
709 void module_register (void)
711 plugin_register_complex_config ("exec", exec_config);
712 plugin_register_init ("exec", exec_init);
713 plugin_register_read ("exec", exec_read);
714 plugin_register_notification ("exec", exec_notification);
715 plugin_register_shutdown ("exec", exec_shutdown);
716 } /* void module_register */
719 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker