From: Sebastian Harl Date: Tue, 25 Nov 2008 16:53:53 +0000 (+0100) Subject: processes plugin: Get complete command line for processes on Linux as well. X-Git-Tag: collectd-4.6.0~132 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=f560c943439ba28e74eb576eb58a965d049588f9;p=collectd.git processes plugin: Get complete command line for processes on Linux as well. On Linux, /proc//cmdline is now used to determine the complete command line of a process which may then be used with the "ProcessMatch" configuration option. --- diff --git a/src/processes.c b/src/processes.c index 3b219906..4fb70ade 100644 --- a/src/processes.c +++ b/src/processes.c @@ -22,6 +22,7 @@ * Lyonel Vincent * Florian octo Forster * Oleg King + * Sebastian Harl **/ #include "collectd.h" @@ -755,6 +756,96 @@ int ps_read_process (int pid, procstat_t *ps, char *state) /* success */ return (0); } /* int ps_read_process (...) */ + +static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len) +{ + char *buf_ptr; + size_t len; + + char file[PATH_MAX]; + int fd; + + size_t n; + + if ((pid < 1) || (NULL == buf) || (buf_len < 2)) + return NULL; + + ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid); + + fd = open (file, O_RDONLY); + if (fd < 0) { + char errbuf[4096]; + WARNING ("processes plugin: Failed to open `%s': %s.", file, + sstrerror (errno, errbuf, sizeof (errbuf))); + return NULL; + } + + buf_ptr = buf; + len = buf_len; + + n = 0; + + while (42) { + size_t status; + + status = read (fd, (void *)buf_ptr, len); + + if (status < 0) { + char errbuf[4096]; + + if ((EAGAIN == errno) || (EINTR == errno)) + continue; + + WARNING ("processes plugin: Failed to read from `%s': %s.", file, + sstrerror (errno, errbuf, sizeof (errbuf))); + close (fd); + return NULL; + } + + n += status; + + if (status == 0) + break; + + buf_ptr += status; + len -= status; + + if (len <= 0) + break; + } + + close (fd); + + if (0 == n) { + /* cmdline not available; e.g. kernel thread, zombie */ + if (NULL == name) + return NULL; + + ssnprintf (buf, buf_len, "[%s]", name); + return buf; + } + + assert (n <= buf_len); + + if (n == buf_len) + --n; + buf[n] = '\0'; + + --n; + /* remove trailing whitespace */ + while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) { + buf[n] = '\0'; + --n; + } + + /* arguments are separated by '\0' in /proc//cmdline */ + while (n > 0) { + if ('\0' == buf[n]) + buf[n] = ' '; + --n; + } + return buf; +} /* char *ps_get_cmdline (...) */ #endif /* KERNEL_LINUX */ #if HAVE_THREAD_INFO @@ -1070,6 +1161,8 @@ static int ps_read (void) DIR *proc; int pid; + char cmdline[ARG_MAX]; + int status; procstat_t ps; procstat_entry_t pse; @@ -1130,8 +1223,9 @@ static int ps_read (void) case 'W': paging++; break; } - /* FIXME: cmdline should be here instead of NULL */ - ps_list_add (ps.name, NULL, &pse); + ps_list_add (ps.name, + ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)), + &pse); } closedir (proc);