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
41 struct program_list_s;
42 typedef struct program_list_s program_list_t;
58 static program_list_t *pl_head = NULL;
63 static void sigchld_handler (int signal) /* {{{ */
67 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
70 for (pl = pl_head; pl != NULL; pl = pl->next)
75 } /* while (waitpid) */
76 } /* void sigchld_handler }}} */
78 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
84 if (ci->children_num != 0)
86 WARNING ("exec plugin: The config option `%s' may not be a block.",
90 if (ci->values_num < 2)
92 WARNING ("exec plugin: The config option `%s' needs at least two "
93 "arguments.", ci->key);
96 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
97 || (ci->values[1].type != OCONFIG_TYPE_STRING))
99 WARNING ("exec plugin: The first two arguments to the `%s' option must "
100 "be string arguments.", ci->key);
104 pl = (program_list_t *) malloc (sizeof (program_list_t));
107 ERROR ("exec plugin: malloc failed.");
110 memset (pl, '\0', sizeof (program_list_t));
112 if (strcasecmp ("NagiosExec", ci->key) == 0)
113 pl->flags |= PL_NAGIOS_PLUGIN;
114 else if (strcasecmp ("NotificationExec", ci->key) == 0)
115 pl->flags |= PL_NOTIF_ACTION;
117 pl->flags |= PL_NORMAL;
119 pl->user = strdup (ci->values[0].value.string);
120 if (pl->user == NULL)
122 ERROR ("exec plugin: strdup failed.");
127 pl->group = strchr (pl->user, ':');
128 if (pl->group != NULL)
134 pl->exec = strdup (ci->values[1].value.string);
135 if (pl->exec == NULL)
137 ERROR ("exec plugin: strdup failed.");
143 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
144 if (pl->argv == NULL)
146 ERROR ("exec plugin: malloc failed.");
152 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
155 char *tmp = strrchr (ci->values[1].value.string, '/');
157 strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
159 strncpy (buffer, tmp + 1, sizeof (buffer));
160 buffer[sizeof (buffer) - 1] = '\0';
162 pl->argv[0] = strdup (buffer);
163 if (pl->argv[0] == NULL)
165 ERROR ("exec plugin: malloc failed.");
173 for (i = 1; i < (ci->values_num - 1); i++)
175 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
177 pl->argv[i] = strdup (ci->values[i + 1].value.string);
181 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
183 snprintf (buffer, sizeof (buffer), "%lf",
184 ci->values[i + 1].value.number);
188 if (ci->values[i + 1].value.boolean)
189 strncpy (buffer, "true", sizeof (buffer));
191 strncpy (buffer, "false", sizeof (buffer));
193 buffer[sizeof (buffer) - 1] = '\0';
195 pl->argv[i] = strdup (buffer);
198 if (pl->argv[i] == NULL)
200 ERROR ("exec plugin: strdup failed.");
205 if (i < (ci->values_num - 1))
218 for (i = 0; pl->argv[i] != NULL; i++)
220 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
227 } /* int exec_config_exec }}} */
229 static int exec_config (oconfig_item_t *ci) /* {{{ */
233 for (i = 0; i < ci->children_num; i++)
235 oconfig_item_t *child = ci->children + i;
236 if ((strcasecmp ("Exec", child->key) == 0)
237 || (strcasecmp ("NagiosExec", child->key) == 0)
238 || (strcasecmp ("NotificationExec", child->key) == 0))
239 exec_config_exec (child);
242 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
247 } /* int exec_config }}} */
249 static void exec_child (program_list_t *pl) /* {{{ */
256 struct passwd *sp_ptr;
262 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
265 ERROR ("exec plugin: getpwnam_r failed: %s",
266 sstrerror (errno, errbuf, sizeof (errbuf)));
271 ERROR ("exec plugin: No such user: `%s'", pl->user);
279 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
283 /* The group configured in the configfile is set as effective group, because
284 * this way the forked process can (re-)gain the user's primary group. */
286 if (NULL != pl->group)
288 if ('\0' != *pl->group) {
289 struct group *gr_ptr = NULL;
292 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
295 ERROR ("exec plugin: getgrnam_r failed: %s",
296 sstrerror (errno, errbuf, sizeof (errbuf)));
301 ERROR ("exec plugin: No such group: `%s'", pl->group);
311 } /* if (pl->group == NULL) */
313 status = setgid (gid);
316 ERROR ("exec plugin: setgid (%i) failed: %s",
317 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
323 status = setegid (egid);
326 ERROR ("exec plugin: setegid (%i) failed: %s",
327 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
332 status = setuid (uid);
335 ERROR ("exec plugin: setuid (%i) failed: %s",
336 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
340 status = execvp (pl->exec, pl->argv);
342 ERROR ("exec plugin: exec failed: %s",
343 sstrerror (errno, errbuf, sizeof (errbuf)));
345 } /* void exec_child }}} */
347 static int fork_child (program_list_t *pl) /* {{{ */
355 status = pipe (fd_pipe);
359 ERROR ("exec plugin: pipe failed: %s",
360 sstrerror (errno, errbuf, sizeof (errbuf)));
368 ERROR ("exec plugin: fork failed: %s",
369 sstrerror (errno, errbuf, sizeof (errbuf)));
372 else if (pl->pid == 0)
376 /* Connect the pipe to STDOUT and STDERR */
377 if (fd_pipe[1] != STDOUT_FILENO)
378 dup2 (fd_pipe[1], STDOUT_FILENO);
379 if (fd_pipe[1] != STDERR_FILENO)
380 dup2 (fd_pipe[1], STDERR_FILENO);
381 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
385 /* does not return */
390 } /* int fork_child }}} */
392 static int parse_line (char *buffer) /* {{{ */
397 fields[0] = "PUTVAL";
398 fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
400 handle_putval (stdout, fields, fields_num + 1);
402 } /* int parse_line }}} */
404 static void *exec_read_one (void *arg) /* {{{ */
406 program_list_t *pl = (program_list_t *) arg;
412 fd = fork_child (pl);
414 pthread_exit ((void *) 1);
416 assert (pl->pid != 0);
418 fh = fdopen (fd, "r");
422 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
423 sstrerror (errno, errbuf, sizeof (errbuf)));
424 kill (pl->pid, SIGTERM);
427 pthread_exit ((void *) 1);
431 while (fgets (buffer, sizeof (buffer), fh) != NULL)
435 len = strlen (buffer);
437 /* Remove newline from end. */
438 while ((len > 0) && ((buffer[len - 1] == '\n')
439 || (buffer[len - 1] == '\r')))
440 buffer[--len] = '\0';
442 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
444 if (pl->flags & PL_NAGIOS_PLUGIN)
448 } /* while (fgets) */
452 if (waitpid (pl->pid, &status, 0) > 0)
455 DEBUG ("exec plugin: Child %i exited with status %i.",
456 (int) pl->pid, pl->status);
458 if (pl->flags & PL_NAGIOS_PLUGIN)
462 memset (&n, '\0', sizeof (n));
464 n.severity = NOTIF_FAILURE;
466 n.severity = NOTIF_OKAY;
467 else if (pl->status == 1)
468 n.severity = NOTIF_WARNING;
470 strncpy (n.message, buffer, sizeof (n.message));
471 n.message[sizeof (n.message) - 1] = '\0';
473 n.time = time (NULL);
475 strncpy (n.host, hostname_g, sizeof (n.host));
476 n.host[sizeof (n.host) - 1] = '\0';
478 plugin_dispatch_notification (&n);
482 pthread_exit ((void *) 0);
484 } /* void *exec_read_one }}} */
486 static int exec_init (void) /* {{{ */
490 memset (&sa, '\0', sizeof (sa));
491 sa.sa_handler = sigchld_handler;
492 sigaction (SIGCHLD, &sa, NULL);
495 } /* int exec_init }}} */
497 static int exec_read (void) /* {{{ */
501 for (pl = pl_head; pl != NULL; pl = pl->next)
509 pthread_attr_init (&attr);
510 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
511 pthread_create (&t, &attr, exec_read_one, (void *) pl);
515 } /* int exec_read }}} */
517 static int exec_shutdown (void) /* {{{ */
520 program_list_t *next;
529 kill (pl->pid, SIGTERM);
530 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
541 } /* int exec_shutdown }}} */
543 void module_register (void)
545 plugin_register_complex_config ("exec", exec_config);
546 plugin_register_init ("exec", exec_init);
547 plugin_register_read ("exec", exec_read);
548 plugin_register_shutdown ("exec", exec_shutdown);
549 } /* void module_register */
552 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker