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_debug.h"
27 #include <sys/types.h>
35 struct program_list_s;
36 typedef struct program_list_s program_list_t;
48 static data_source_t dsrc_counter[1] =
50 {"value", DS_TYPE_COUNTER, NAN, NAN}
53 static data_set_t ds_counter =
55 "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
58 static data_source_t dsrc_gauge[1] =
60 {"value", DS_TYPE_GAUGE, NAN, NAN}
63 static data_set_t ds_gauge =
65 "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
68 static const char *config_keys[] =
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
74 static program_list_t *pl_head = NULL;
79 static int exec_config (const char *key, const char *value)
81 if (strcasecmp ("Exec", key) == 0)
84 pl = (program_list_t *) malloc (sizeof (program_list_t));
87 memset (pl, '\0', sizeof (program_list_t));
89 pl->user = strdup (value);
96 pl->exec = strchr (pl->user, ' ');
103 while (*pl->exec == ' ')
109 if (*pl->exec == '\0')
125 } /* int exec_config */
127 static void submit_counter (const char *type_instance, counter_t value)
130 value_list_t vl = VALUE_LIST_INIT;
132 DBG ("type_instance = %s; value = %llu;", type_instance, value);
134 values[0].counter = value;
138 vl.time = time (NULL);
139 strcpy (vl.host, hostname_g);
140 strcpy (vl.plugin, "exec");
141 strcpy (vl.plugin_instance, "");
142 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144 plugin_dispatch_values ("counter", &vl);
145 } /* void submit_counter */
147 static void submit_gauge (const char *type_instance, gauge_t value)
150 value_list_t vl = VALUE_LIST_INIT;
152 DBG ("type_instance = %s; value = %lf;", type_instance, value);
154 values[0].gauge = value;
158 vl.time = time (NULL);
159 strcpy (vl.host, hostname_g);
160 strcpy (vl.plugin, "exec");
161 strcpy (vl.plugin_instance, "");
162 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
164 plugin_dispatch_values ("gauge", &vl);
165 } /* void submit_counter */
167 static void exec_child (program_list_t *pl)
173 struct passwd *sp_ptr;
178 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
181 syslog (LOG_ERR, "exec plugin: getpwnam_r failed: %s", strerror (status));
186 syslog (LOG_ERR, "exec plugin: No such user: `%s'", pl->user);
193 syslog (LOG_ERR, "exec plugin: Cowardly refusing to exec program as root.");
197 status = setuid (uid);
200 syslog (LOG_ERR, "exec plugin: setuid failed: %s", strerror (errno));
204 arg0 = strrchr (pl->exec, '/');
207 if ((arg0 == NULL) || (*arg0 == '\0'))
210 status = execlp (pl->exec, arg0, (char *) 0);
212 syslog (LOG_ERR, "exec plugin: exec failed: %s", strerror (errno));
214 } /* void exec_child */
216 static int fork_child (program_list_t *pl)
224 status = pipe (fd_pipe);
227 syslog (LOG_ERR, "exec plugin: pipe failed: %s", strerror (errno));
234 syslog (LOG_ERR, "exec plugin: fork failed: %s", strerror (errno));
237 else if (pl->pid == 0)
241 /* Connect the pipe to STDOUT and STDERR */
242 if (fd_pipe[1] != STDOUT_FILENO)
243 dup2 (fd_pipe[1], STDOUT_FILENO);
244 if (fd_pipe[1] != STDERR_FILENO)
245 dup2 (fd_pipe[1], STDERR_FILENO);
246 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
250 /* does not return */
255 } /* int fork_child */
257 static void *exec_read_one (void *arg)
259 program_list_t *pl = (program_list_t *) arg;
264 fd = fork_child (pl);
266 pthread_exit ((void *) 1);
268 assert (pl->pid != 0);
270 fh = fdopen (fd, "r");
273 syslog (LOG_ERR, "exec plugin: fdopen (%i) failed: %s", fd,
275 kill (pl->pid, SIGTERM);
277 pthread_exit ((void *) 1);
280 while (fgets (buffer, sizeof (buffer), fh) != NULL)
287 DBG ("buffer = %s", buffer);
289 len = strlen (buffer);
293 if (buffer[0] == '#')
298 type_instance = strchr (type, ',');
299 if (type_instance == NULL)
301 *type_instance = '\0';
304 if ((strcasecmp ("counter", type) != 0)
305 && (strcasecmp ("gauge", type) != 0))
307 syslog (LOG_WARNING, "exec plugin: Received invalid type: %s", type);
311 value = strchr (type_instance, ',');
317 DBG ("value = %s", value);
319 if (strcasecmp ("counter", type) == 0)
320 submit_counter (type_instance, atoll (value));
322 submit_gauge (type_instance, atof (value));
323 } /* while (fgets) */
328 pthread_exit ((void *) 0);
329 } /* void *exec_read_one */
331 static int exec_read (void)
335 for (pl = pl_head; pl != NULL; pl = pl->next)
343 pthread_attr_init (&attr);
344 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
345 pthread_create (&t, &attr, exec_read_one, (void *) pl);
349 } /* int exec_read */
351 void module_register (void)
353 plugin_register_data_set (&ds_counter);
354 plugin_register_data_set (&ds_gauge);
355 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
356 plugin_register_read ("exec", exec_read);
357 } /* void module_register */
360 * vim:shiftwidth=2:softtabstop=2:tabstop=8