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 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 DEBUG ("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 DEBUG ("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;
179 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
182 ERROR ("exec plugin: getpwnam_r failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
188 ERROR ("exec plugin: No such user: `%s'", pl->user);
195 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
199 status = setuid (uid);
202 ERROR ("exec plugin: setuid failed: %s",
203 sstrerror (errno, errbuf, sizeof (errbuf)));
207 arg0 = strrchr (pl->exec, '/');
210 if ((arg0 == NULL) || (*arg0 == '\0'))
213 status = execlp (pl->exec, arg0, (char *) 0);
215 ERROR ("exec plugin: exec failed: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
218 } /* void exec_child */
220 static int fork_child (program_list_t *pl)
228 status = pipe (fd_pipe);
232 ERROR ("exec plugin: pipe failed: %s",
233 sstrerror (errno, errbuf, sizeof (errbuf)));
241 ERROR ("exec plugin: fork failed: %s",
242 sstrerror (errno, errbuf, sizeof (errbuf)));
245 else if (pl->pid == 0)
249 /* Connect the pipe to STDOUT and STDERR */
250 if (fd_pipe[1] != STDOUT_FILENO)
251 dup2 (fd_pipe[1], STDOUT_FILENO);
252 if (fd_pipe[1] != STDERR_FILENO)
253 dup2 (fd_pipe[1], STDERR_FILENO);
254 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
258 /* does not return */
263 } /* int fork_child */
265 static void *exec_read_one (void *arg)
267 program_list_t *pl = (program_list_t *) arg;
272 fd = fork_child (pl);
274 pthread_exit ((void *) 1);
276 assert (pl->pid != 0);
278 fh = fdopen (fd, "r");
282 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
283 sstrerror (errno, errbuf, sizeof (errbuf)));
284 kill (pl->pid, SIGTERM);
286 pthread_exit ((void *) 1);
289 while (fgets (buffer, sizeof (buffer), fh) != NULL)
296 DEBUG ("buffer = %s", buffer);
298 len = strlen (buffer);
302 if (buffer[0] == '#')
307 type_instance = strchr (type, ',');
308 if (type_instance == NULL)
310 *type_instance = '\0';
313 if ((strcasecmp ("counter", type) != 0)
314 && (strcasecmp ("gauge", type) != 0))
316 WARNING ("exec plugin: Received invalid type: %s", type);
320 value = strchr (type_instance, ',');
326 DEBUG ("value = %s", value);
328 if (strcasecmp ("counter", type) == 0)
329 submit_counter (type_instance, atoll (value));
331 submit_gauge (type_instance, atof (value));
332 } /* while (fgets) */
337 pthread_exit ((void *) 0);
339 } /* void *exec_read_one */
341 static int exec_read (void)
345 for (pl = pl_head; pl != NULL; pl = pl->next)
353 pthread_attr_init (&attr);
354 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
355 pthread_create (&t, &attr, exec_read_one, (void *) pl);
359 } /* int exec_read */
361 static int exec_shutdown (void)
364 program_list_t *next;
373 kill (pl->pid, SIGTERM);
374 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
385 } /* int exec_shutdown */
387 void module_register (modreg_e load)
389 if (load & MR_DATASETS)
391 plugin_register_data_set (&ds_counter);
392 plugin_register_data_set (&ds_gauge);
397 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
398 plugin_register_read ("exec", exec_read);
399 plugin_register_shutdown ("exec", exec_shutdown);
401 } /* void module_register */
404 * vim:shiftwidth=2:softtabstop=2:tabstop=8