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>
26 #include <sys/types.h>
34 struct program_list_s;
35 typedef struct program_list_s program_list_t;
47 static data_source_t dsrc_counter[1] =
49 {"value", DS_TYPE_COUNTER, NAN, NAN}
52 static data_set_t ds_counter =
54 "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
57 static data_source_t dsrc_gauge[1] =
59 {"value", DS_TYPE_GAUGE, NAN, NAN}
62 static data_set_t ds_gauge =
64 "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
67 static const char *config_keys[] =
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static program_list_t *pl_head = NULL;
78 static int exec_config (const char *key, const char *value)
80 if (strcasecmp ("Exec", key) == 0)
83 pl = (program_list_t *) malloc (sizeof (program_list_t));
86 memset (pl, '\0', sizeof (program_list_t));
88 pl->user = strdup (value);
95 pl->exec = strchr (pl->user, ' ');
102 while (*pl->exec == ' ')
108 if (*pl->exec == '\0')
124 } /* int exec_config */
126 static void submit_counter (const char *type_instance, counter_t value)
129 value_list_t vl = VALUE_LIST_INIT;
131 DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
133 values[0].counter = value;
137 vl.time = time (NULL);
138 strcpy (vl.host, hostname_g);
139 strcpy (vl.plugin, "exec");
140 strcpy (vl.plugin_instance, "");
141 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
143 plugin_dispatch_values ("counter", &vl);
144 } /* void submit_counter */
146 static void submit_gauge (const char *type_instance, gauge_t value)
149 value_list_t vl = VALUE_LIST_INIT;
151 DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
153 values[0].gauge = value;
157 vl.time = time (NULL);
158 strcpy (vl.host, hostname_g);
159 strcpy (vl.plugin, "exec");
160 strcpy (vl.plugin_instance, "");
161 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
163 plugin_dispatch_values ("gauge", &vl);
164 } /* void submit_counter */
166 static void exec_child (program_list_t *pl)
172 struct passwd *sp_ptr;
178 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
181 ERROR ("exec plugin: getpwnam_r failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
187 ERROR ("exec plugin: No such user: `%s'", pl->user);
194 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
198 status = setuid (uid);
201 ERROR ("exec plugin: setuid failed: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
206 arg0 = strrchr (pl->exec, '/');
209 if ((arg0 == NULL) || (*arg0 == '\0'))
212 status = execlp (pl->exec, arg0, (char *) 0);
214 ERROR ("exec plugin: exec failed: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
217 } /* void exec_child */
219 static int fork_child (program_list_t *pl)
227 status = pipe (fd_pipe);
231 ERROR ("exec plugin: pipe failed: %s",
232 sstrerror (errno, errbuf, sizeof (errbuf)));
240 ERROR ("exec plugin: fork failed: %s",
241 sstrerror (errno, errbuf, sizeof (errbuf)));
244 else if (pl->pid == 0)
248 /* Connect the pipe to STDOUT and STDERR */
249 if (fd_pipe[1] != STDOUT_FILENO)
250 dup2 (fd_pipe[1], STDOUT_FILENO);
251 if (fd_pipe[1] != STDERR_FILENO)
252 dup2 (fd_pipe[1], STDERR_FILENO);
253 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
257 /* does not return */
262 } /* int fork_child */
264 static void *exec_read_one (void *arg)
266 program_list_t *pl = (program_list_t *) arg;
271 fd = fork_child (pl);
273 pthread_exit ((void *) 1);
275 assert (pl->pid != 0);
277 fh = fdopen (fd, "r");
281 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
282 sstrerror (errno, errbuf, sizeof (errbuf)));
283 kill (pl->pid, SIGTERM);
285 pthread_exit ((void *) 1);
288 while (fgets (buffer, sizeof (buffer), fh) != NULL)
295 DEBUG ("buffer = %s", buffer);
297 len = strlen (buffer);
301 if (buffer[0] == '#')
306 type_instance = strchr (type, ',');
307 if (type_instance == NULL)
309 *type_instance = '\0';
312 if ((strcasecmp ("counter", type) != 0)
313 && (strcasecmp ("gauge", type) != 0))
315 WARNING ("exec plugin: Received invalid type: %s", type);
319 value = strchr (type_instance, ',');
325 DEBUG ("value = %s", value);
327 if (strcasecmp ("counter", type) == 0)
328 submit_counter (type_instance, atoll (value));
330 submit_gauge (type_instance, atof (value));
331 } /* while (fgets) */
336 pthread_exit ((void *) 0);
337 } /* void *exec_read_one */
339 static int exec_read (void)
343 for (pl = pl_head; pl != NULL; pl = pl->next)
351 pthread_attr_init (&attr);
352 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
353 pthread_create (&t, &attr, exec_read_one, (void *) pl);
357 } /* int exec_read */
359 void module_register (void)
361 plugin_register_data_set (&ds_counter);
362 plugin_register_data_set (&ds_gauge);
363 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
364 plugin_register_read ("exec", exec_read);
365 } /* void module_register */
368 * vim:shiftwidth=2:softtabstop=2:tabstop=8