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;
177 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
180 ERROR ("exec plugin: getpwnam_r failed: %s", strerror (status));
185 ERROR ("exec plugin: No such user: `%s'", pl->user);
192 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
196 status = setuid (uid);
199 ERROR ("exec plugin: setuid failed: %s", strerror (errno));
203 arg0 = strrchr (pl->exec, '/');
206 if ((arg0 == NULL) || (*arg0 == '\0'))
209 status = execlp (pl->exec, arg0, (char *) 0);
211 ERROR ("exec plugin: exec failed: %s", strerror (errno));
213 } /* void exec_child */
215 static int fork_child (program_list_t *pl)
223 status = pipe (fd_pipe);
226 ERROR ("exec plugin: pipe failed: %s", strerror (errno));
233 ERROR ("exec plugin: fork failed: %s", strerror (errno));
236 else if (pl->pid == 0)
240 /* Connect the pipe to STDOUT and STDERR */
241 if (fd_pipe[1] != STDOUT_FILENO)
242 dup2 (fd_pipe[1], STDOUT_FILENO);
243 if (fd_pipe[1] != STDERR_FILENO)
244 dup2 (fd_pipe[1], STDERR_FILENO);
245 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
249 /* does not return */
254 } /* int fork_child */
256 static void *exec_read_one (void *arg)
258 program_list_t *pl = (program_list_t *) arg;
263 fd = fork_child (pl);
265 pthread_exit ((void *) 1);
267 assert (pl->pid != 0);
269 fh = fdopen (fd, "r");
272 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
274 kill (pl->pid, SIGTERM);
276 pthread_exit ((void *) 1);
279 while (fgets (buffer, sizeof (buffer), fh) != NULL)
286 DEBUG ("buffer = %s", buffer);
288 len = strlen (buffer);
292 if (buffer[0] == '#')
297 type_instance = strchr (type, ',');
298 if (type_instance == NULL)
300 *type_instance = '\0';
303 if ((strcasecmp ("counter", type) != 0)
304 && (strcasecmp ("gauge", type) != 0))
306 WARNING ("exec plugin: Received invalid type: %s", type);
310 value = strchr (type_instance, ',');
316 DEBUG ("value = %s", value);
318 if (strcasecmp ("counter", type) == 0)
319 submit_counter (type_instance, atoll (value));
321 submit_gauge (type_instance, atof (value));
322 } /* while (fgets) */
327 pthread_exit ((void *) 0);
328 } /* void *exec_read_one */
330 static int exec_read (void)
334 for (pl = pl_head; pl != NULL; pl = pl->next)
342 pthread_attr_init (&attr);
343 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
344 pthread_create (&t, &attr, exec_read_one, (void *) pl);
348 } /* int exec_read */
350 void module_register (void)
352 plugin_register_data_set (&ds_counter);
353 plugin_register_data_set (&ds_gauge);
354 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
355 plugin_register_read ("exec", exec_read);
356 } /* void module_register */
359 * vim:shiftwidth=2:softtabstop=2:tabstop=8