summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 5b123e4)
raw | patch | inline | side by side (parent: 5b123e4)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 25 Nov 2008 16:53:53 +0000 (17:53 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sat, 6 Dec 2008 09:21:03 +0000 (10:21 +0100) |
On Linux, /proc/<pid>/cmdline is now used to determine the complete command
line of a process which may then be used with the "ProcessMatch" configuration
option.
line of a process which may then be used with the "ProcessMatch" configuration
option.
src/processes.c | patch | blob | history |
diff --git a/src/processes.c b/src/processes.c
index 3b219906436bcce8caffa5b8370e38ad09309cb8..4fb70ade91636a2b62db708b25c5e5424c3041d5 100644 (file)
--- a/src/processes.c
+++ b/src/processes.c
* Lyonel Vincent <lyonel at ezix.org>
* Florian octo Forster <octo at verplant.org>
* Oleg King <king2 at kaluga.ru>
+ * Sebastian Harl <sh at tokkee.org>
**/
#include "collectd.h"
/* 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/<pid>/cmdline */
+ while (n > 0) {
+ if ('\0' == buf[n])
+ buf[n] = ' ';
+ --n;
+ }
+ return buf;
+} /* char *ps_get_cmdline (...) */
#endif /* KERNEL_LINUX */
#if HAVE_THREAD_INFO
DIR *proc;
int pid;
+ char cmdline[ARG_MAX];
+
int status;
procstat_t ps;
procstat_entry_t pse;
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);