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>
35 struct program_list_s;
36 typedef struct program_list_s program_list_t;
48 static const char *config_keys[] =
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
54 static program_list_t *pl_head = NULL;
59 static int exec_config (const char *key, const char *value)
61 if (strcasecmp ("Exec", key) == 0)
64 pl = (program_list_t *) malloc (sizeof (program_list_t));
67 memset (pl, '\0', sizeof (program_list_t));
69 pl->user = strdup (value);
76 pl->exec = strchr (pl->user, ' ');
83 while (*pl->exec == ' ')
89 if (*pl->exec == '\0')
105 } /* int exec_config */
107 static void submit_counter (const char *type_instance, counter_t value)
110 value_list_t vl = VALUE_LIST_INIT;
112 DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
114 values[0].counter = value;
118 vl.time = time (NULL);
119 strcpy (vl.host, hostname_g);
120 strcpy (vl.plugin, "exec");
121 strcpy (vl.plugin_instance, "");
122 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
124 plugin_dispatch_values ("counter", &vl);
125 } /* void submit_counter */
127 static void submit_gauge (const char *type_instance, gauge_t value)
130 value_list_t vl = VALUE_LIST_INIT;
132 DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
134 values[0].gauge = 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 ("gauge", &vl);
145 } /* void submit_counter */
147 static void exec_child (program_list_t *pl)
153 struct passwd *sp_ptr;
159 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
162 ERROR ("exec plugin: getpwnam_r failed: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
168 ERROR ("exec plugin: No such user: `%s'", pl->user);
175 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
179 status = setuid (uid);
182 ERROR ("exec plugin: setuid failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
187 arg0 = strrchr (pl->exec, '/');
190 if ((arg0 == NULL) || (*arg0 == '\0'))
193 status = execlp (pl->exec, arg0, (char *) 0);
195 ERROR ("exec plugin: exec failed: %s",
196 sstrerror (errno, errbuf, sizeof (errbuf)));
198 } /* void exec_child */
200 static int fork_child (program_list_t *pl)
208 status = pipe (fd_pipe);
212 ERROR ("exec plugin: pipe failed: %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
221 ERROR ("exec plugin: fork failed: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
225 else if (pl->pid == 0)
229 /* Connect the pipe to STDOUT and STDERR */
230 if (fd_pipe[1] != STDOUT_FILENO)
231 dup2 (fd_pipe[1], STDOUT_FILENO);
232 if (fd_pipe[1] != STDERR_FILENO)
233 dup2 (fd_pipe[1], STDERR_FILENO);
234 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
238 /* does not return */
243 } /* int fork_child */
245 static void *exec_read_one (void *arg)
247 program_list_t *pl = (program_list_t *) arg;
252 fd = fork_child (pl);
254 pthread_exit ((void *) 1);
256 assert (pl->pid != 0);
258 fh = fdopen (fd, "r");
262 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
263 sstrerror (errno, errbuf, sizeof (errbuf)));
264 kill (pl->pid, SIGTERM);
266 pthread_exit ((void *) 1);
269 while (fgets (buffer, sizeof (buffer), fh) != NULL)
276 len = strlen (buffer);
278 /* Remove newline from end. */
279 while ((len > 0) && ((buffer[len - 1] == '\n')
280 || (buffer[len - 1] == '\r')))
281 buffer[--len] = '\0';
283 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
288 if (buffer[0] == '#')
293 type_instance = strchr (type, ',');
294 if (type_instance == NULL)
296 *type_instance = '\0';
299 if ((strcasecmp ("counter", type) != 0)
300 && (strcasecmp ("gauge", type) != 0))
302 WARNING ("exec plugin: Received invalid type: %s", type);
306 value = strchr (type_instance, ',');
309 WARNING ("exec plugin: type-instance is missing.");
315 DEBUG ("exec plugin: exec_read_one: type = %s; type_instance = %s; "
316 "value = %s;", type, type_instance, 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);
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 static int exec_shutdown (void)
354 program_list_t *next;
363 kill (pl->pid, SIGTERM);
364 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
375 } /* int exec_shutdown */
377 void module_register (void)
379 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
380 plugin_register_read ("exec", exec_read);
381 plugin_register_shutdown ("exec", exec_shutdown);
382 } /* void module_register */
385 * vim:shiftwidth=2:softtabstop=2:tabstop=8