#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_cmd_putval.h"
#include <sys/types.h>
#include <pwd.h>
+#include <grp.h>
#include <signal.h>
#include <pthread.h>
struct program_list_s
{
char *user;
+ char *group;
char *exec;
int pid;
program_list_t *next;
/*
* Private variables
*/
-static data_source_t dsrc_counter[1] =
-{
- {"value", DS_TYPE_COUNTER, NAN, NAN}
-};
-
-static data_set_t ds_counter =
-{
- "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
-};
-
-static data_source_t dsrc_gauge[1] =
-{
- {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t ds_gauge =
-{
- "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
-};
-
static const char *config_keys[] =
{
"Exec"
pl->next = pl_head;
pl_head = pl;
+
+ pl->group = strchr (pl->user, ':');
+ if (NULL != pl->group) {
+ *pl->group = '\0';
+ pl->group++;
+ }
}
else
{
return (0);
} /* int exec_config */
-static void submit_counter (const char *type_instance, counter_t value)
-{
- value_t values[1];
- value_list_t vl = VALUE_LIST_INIT;
-
- DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
-
- values[0].counter = value;
-
- vl.values = values;
- vl.values_len = 1;
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "exec");
- strcpy (vl.plugin_instance, "");
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-
- plugin_dispatch_values ("counter", &vl);
-} /* void submit_counter */
-
-static void submit_gauge (const char *type_instance, gauge_t value)
-{
- value_t values[1];
- value_list_t vl = VALUE_LIST_INIT;
-
- DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
-
- values[0].gauge = value;
-
- vl.values = values;
- vl.values_len = 1;
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "exec");
- strcpy (vl.plugin_instance, "");
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-
- plugin_dispatch_values ("gauge", &vl);
-} /* void submit_counter */
-
static void exec_child (program_list_t *pl)
{
int status;
int uid;
+ int gid;
char *arg0;
struct passwd *sp_ptr;
struct passwd sp;
- char pwnambuf[2048];
+ char nambuf[2048];
char errbuf[1024];
sp_ptr = NULL;
- status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
+ status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
if (status != 0)
{
ERROR ("exec plugin: getpwnam_r failed: %s",
exit (-1);
}
+ if (NULL != pl->group)
+ {
+ if ('\0' != *pl->group) {
+ struct group *gr_ptr = NULL;
+ struct group gr;
+
+ status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
+ if (0 != status)
+ {
+ ERROR ("exec plugin: getgrnam_r failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ exit (-1);
+ }
+ if (NULL == gr_ptr)
+ {
+ ERROR ("exec plugin: No such group: `%s'", pl->group);
+ exit (-1);
+ }
+
+ gid = gr.gr_gid;
+ }
+ else
+ {
+ gid = sp.pw_gid;
+ }
+
+ status = setgid (gid);
+ if (0 != status)
+ {
+ ERROR ("exec plugin: setgid failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ exit (-1);
+ }
+ }
+
arg0 = strrchr (pl->exec, '/');
if (arg0 != NULL)
arg0++;
return (fd_pipe[0]);
} /* int fork_child */
+static int parse_line (char *buffer)
+{
+ char *fields[256];
+ int fields_num;
+
+ fields[0] = "PUTVAL";
+ fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
+
+ handle_putval (stdout, fields, fields_num + 1);
+ return (0);
+} /* int parse_line */
+
static void *exec_read_one (void *arg)
{
program_list_t *pl = (program_list_t *) arg;
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
int len;
- char *type;
- char *type_instance;
- char *value;
-
- DEBUG ("buffer = %s", buffer);
len = strlen (buffer);
- if (len < 5)
- continue;
- if (buffer[0] == '#')
- continue;
+ /* Remove newline from end. */
+ while ((len > 0) && ((buffer[len - 1] == '\n')
+ || (buffer[len - 1] == '\r')))
+ buffer[--len] = '\0';
- type = buffer;
+ DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
- type_instance = strchr (type, ',');
- if (type_instance == NULL)
- continue;
- *type_instance = '\0';
- type_instance++;
-
- if ((strcasecmp ("counter", type) != 0)
- && (strcasecmp ("gauge", type) != 0))
- {
- WARNING ("exec plugin: Received invalid type: %s", type);
- continue;
- }
-
- value = strchr (type_instance, ',');
- if (value == NULL)
- continue;
- *value = '\0';
- value++;
-
- DEBUG ("value = %s", value);
-
- if (strcasecmp ("counter", type) == 0)
- submit_counter (type_instance, atoll (value));
- else
- submit_gauge (type_instance, atof (value));
+ parse_line (buffer);
} /* while (fgets) */
fclose (fh);
return (0);
} /* int exec_shutdown */
-void module_register (modreg_e load)
+void module_register (void)
{
- if (load & MR_DATASETS)
- {
- plugin_register_data_set (&ds_counter);
- plugin_register_data_set (&ds_gauge);
- }
-
- if (load & MR_READ)
- {
- plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
- plugin_register_read ("exec", exec_read);
- plugin_register_shutdown ("exec", exec_shutdown);
- }
+ plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
+ plugin_register_read ("exec", exec_read);
+ plugin_register_shutdown ("exec", exec_shutdown);
} /* void module_register */
/*