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)
174 /* FIXME: Not thread safe! */
175 sp = getpwnam (pl->user);
178 syslog (LOG_ERR, "exec plugin: getpwnam failed: %s", strerror (errno));
185 syslog (LOG_ERR, "exec plugin: Cowardly refusing to exec program as root.");
189 status = setuid (uid);
192 syslog (LOG_ERR, "exec plugin: setuid failed: %s", strerror (errno));
196 arg0 = strrchr (pl->exec, '/');
199 if ((arg0 == NULL) || (*arg0 == '\0'))
202 status = execlp (pl->exec, arg0, (char *) 0);
204 syslog (LOG_ERR, "exec plugin: exec failed: %s", strerror (errno));
206 } /* void exec_child */
208 static int fork_child (program_list_t *pl)
216 status = pipe (fd_pipe);
219 syslog (LOG_ERR, "exec plugin: pipe failed: %s", strerror (errno));
226 syslog (LOG_ERR, "exec plugin: fork failed: %s", strerror (errno));
229 else if (pl->pid == 0)
233 /* Connect the pipe to STDOUT and STDERR */
234 if (fd_pipe[1] != STDOUT_FILENO)
235 dup2 (fd_pipe[1], STDOUT_FILENO);
236 if (fd_pipe[1] != STDERR_FILENO)
237 dup2 (fd_pipe[1], STDERR_FILENO);
238 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
242 /* does not return */
247 } /* int fork_child */
249 static void *exec_read_one (void *arg)
251 program_list_t *pl = (program_list_t *) arg;
256 fd = fork_child (pl);
258 pthread_exit ((void *) 1);
260 assert (pl->pid != 0);
262 fh = fdopen (fd, "r");
265 syslog (LOG_ERR, "exec plugin: fdopen (%i) failed: %s", fd,
267 kill (pl->pid, SIGTERM);
269 pthread_exit ((void *) 1);
272 while (fgets (buffer, sizeof (buffer), fh) != NULL)
279 DBG ("buffer = %s", buffer);
281 len = strlen (buffer);
285 if (buffer[0] == '#')
290 type_instance = strchr (type, ',');
291 if (type_instance == NULL)
293 *type_instance = '\0';
296 if ((strcasecmp ("counter", type) != 0)
297 && (strcasecmp ("gauge", type) != 0))
299 syslog (LOG_WARNING, "exec plugin: Received invalid type: %s", type);
303 value = strchr (type_instance, ',');
309 DBG ("value = %s", value);
311 if (strcasecmp ("counter", type) == 0)
312 submit_counter (type_instance, atoll (value));
314 submit_gauge (type_instance, atof (value));
315 } /* while (fgets) */
320 pthread_exit ((void *) 0);
321 } /* void *exec_read_one */
323 static int exec_read (void)
327 for (pl = pl_head; pl != NULL; pl = pl->next)
335 pthread_attr_init (&attr);
336 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
337 pthread_create (&t, &attr, exec_read_one, (void *) pl);
341 } /* int exec_read */
343 void module_register (void)
345 plugin_register_data_set (&ds_counter);
346 plugin_register_data_set (&ds_gauge);
347 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
348 plugin_register_read ("exec", exec_read);
349 } /* void module_register */
352 * vim:shiftwidth=2:softtabstop=2:tabstop=8