Merge branch 'collectd-4.10' into collectd-5.0
[collectd.git] / src / processes.c
index 8853660..8f4eb88 100644 (file)
@@ -1,11 +1,12 @@
 /**
  * collectd - src/processes.c
  * Copyright (C) 2005       Lyonel Vincent
- * Copyright (C) 2006-2008  Florian octo Forster
+ * Copyright (C) 2006-2010  Florian octo Forster
  * Copyright (C) 2008       Oleg King
  * Copyright (C) 2009       Sebastian Harl
  * Copyright (C) 2009       Andrés J. Díaz
  * Copyright (C) 2009       Manuel Sanmartin
+ * Copyright (C) 2010       Clément Stenac
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -28,6 +29,7 @@
  *   Sebastian Harl <sh at tokkee.org>
  *   Andrés J. Díaz <ajdiaz at connectical.com>
  *   Manuel Sanmartin
+ *   Clément Stenac <clement.stenac at diwi.org>
  **/
 
 #include "collectd.h"
 #  define ARG_MAX 4096
 #endif
 
-#define BUFSIZE 256
-
-static const char *config_keys[] =
-{
-       "Process",
-       "ProcessMatch"
-};
-static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
 typedef struct procstat_entry_s
 {
        unsigned long id;
@@ -143,13 +136,13 @@ typedef struct procstat_entry_s
 
        unsigned long vmem_minflt;
        unsigned long vmem_majflt;
-       unsigned long vmem_minflt_counter;
-       unsigned long vmem_majflt_counter;
+       derive_t      vmem_minflt_counter;
+       derive_t      vmem_majflt_counter;
 
        unsigned long cpu_user;
        unsigned long cpu_system;
-       unsigned long cpu_user_counter;
-       unsigned long cpu_system_counter;
+       derive_t      cpu_user_counter;
+       derive_t      cpu_system_counter;
 
        /* io data */
        derive_t io_rchar;
@@ -176,11 +169,11 @@ typedef struct procstat
        unsigned long vmem_code;
        unsigned long stack_size;
 
-       unsigned long vmem_minflt_counter;
-       unsigned long vmem_majflt_counter;
+       derive_t vmem_minflt_counter;
+       derive_t vmem_majflt_counter;
 
-       unsigned long cpu_user_counter;
-       unsigned long cpu_system_counter;
+       derive_t cpu_user_counter;
+       derive_t cpu_system_counter;
 
        /* io data */
        derive_t io_rchar;
@@ -520,42 +513,61 @@ static void ps_list_reset (void)
 }
 
 /* put all pre-defined 'Process' names from config to list_head_g tree */
-static int ps_config (const char *key, const char *value)
+static int ps_config (oconfig_item_t *ci)
 {
-       if (strcasecmp (key, "Process") == 0)
-       {
-               ps_list_register (value, NULL);
-       }
-       else if (strcasecmp (key, "ProcessMatch") == 0)
-       {
-               char *new_val;
-               char *fields[3];
-               int fields_num;
-
-               new_val = strdup (value);
-               if (new_val == NULL) {
-                       ERROR ("processes plugin: strdup failed when processing "
-                                       "`ProcessMatch %s'.", value);
-                       return (1);
+       int i;
+
+       for (i = 0; i < ci->children_num; ++i) {
+               oconfig_item_t *c = ci->children + i;
+
+               if (strcasecmp (c->key, "Process") == 0)
+               {
+                       if ((c->values_num != 1)
+                                       || (OCONFIG_TYPE_STRING != c->values[0].type)) {
+                               ERROR ("processes plugin: `Process' expects exactly "
+                                               "one string argument (got %i).",
+                                               c->values_num);
+                               continue;
+                       }
+
+                       if (c->children_num != 0) {
+                               WARNING ("processes plugin: the `Process' config option "
+                                               "does not expect any child elements -- ignoring "
+                                               "content (%i elements) of the <Process '%s'> block.",
+                                               c->children_num, c->values[0].value.string);
+                       }
+
+                       ps_list_register (c->values[0].value.string, NULL);
                }
+               else if (strcasecmp (c->key, "ProcessMatch") == 0)
+               {
+                       if ((c->values_num != 2)
+                                       || (OCONFIG_TYPE_STRING != c->values[0].type)
+                                       || (OCONFIG_TYPE_STRING != c->values[1].type))
+                       {
+                               ERROR ("processes plugin: `ProcessMatch' needs exactly "
+                                               "two string arguments (got %i).",
+                                               c->values_num);
+                               continue;
+                       }
 
-               fields_num = strsplit (new_val, fields,
-                               STATIC_ARRAY_SIZE (fields));
-               if (fields_num != 2)
+                       if (c->children_num != 0) {
+                               WARNING ("processes plugin: the `ProcessMatch' config option "
+                                               "does not expect any child elements -- ignoring "
+                                               "content (%i elements) of the <ProcessMatch '%s' '%s'> "
+                                               "block.", c->children_num, c->values[0].value.string,
+                                               c->values[1].value.string);
+                       }
+
+                       ps_list_register (c->values[0].value.string,
+                                       c->values[1].value.string);
+               }
+               else
                {
-                       ERROR ("processes plugin: `ProcessMatch' needs exactly "
-                                       "two string arguments.");
-                       sfree (new_val);
-                       return (1);
+                       ERROR ("processes plugin: The `%s' configuration option is not "
+                                       "understood and will be ignored.", c->key);
+                       continue;
                }
-               ps_list_register (fields[0], fields[1]);
-               sfree (new_val);
-       }
-       else
-       {
-               ERROR ("processes plugin: The `%s' configuration option is not "
-                               "understood and will be ignored.", key);
-               return (-1);
        }
 
        return (0);
@@ -664,8 +676,8 @@ static void ps_submit_proc_list (procstat_t *ps)
        plugin_dispatch_values (&vl);
 
        sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
-       vl.values[0].counter = ps->cpu_user_counter;
-       vl.values[1].counter = ps->cpu_system_counter;
+       vl.values[0].derive = ps->cpu_user_counter;
+       vl.values[1].derive = ps->cpu_system_counter;
        vl.values_len = 2;
        plugin_dispatch_values (&vl);
 
@@ -676,8 +688,8 @@ static void ps_submit_proc_list (procstat_t *ps)
        plugin_dispatch_values (&vl);
 
        sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
-       vl.values[0].counter = ps->vmem_minflt_counter;
-       vl.values[1].counter = ps->vmem_majflt_counter;
+       vl.values[0].derive = ps->vmem_minflt_counter;
+       vl.values[1].derive = ps->vmem_majflt_counter;
        vl.values_len = 2;
        plugin_dispatch_values (&vl);
 
@@ -702,8 +714,8 @@ static void ps_submit_proc_list (procstat_t *ps)
        DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
                         "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
                        "vmem_code = %lu; "
-                       "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
-                       "cpu_user_counter = %lu; cpu_system_counter = %lu; "
+                       "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
+                       "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
                        "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
                        "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
                        ps->name, ps->num_proc, ps->num_lwp,
@@ -759,15 +771,16 @@ static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
        if ((fh = fopen (filename, "r")) == NULL)
                return (NULL);
 
-       while (fgets (buffer, 1024, fh) != NULL)
+       while (fgets (buffer, sizeof(buffer), fh) != NULL)
        {
                long long tmp;
                char *endptr;
 
-               if (strncasecmp (buffer, "Vm", 2) != 0)
+               if (strncmp (buffer, "Vm", 2) != 0)
                        continue;
 
-               numfields = strsplit (buffer, fields, 8);
+               numfields = strsplit (buffer, fields,
+                                      STATIC_ARRAY_SIZE (fields));
 
                if (numfields < 2)
                        continue;
@@ -818,7 +831,7 @@ static procstat_t *ps_read_io (int pid, procstat_t *ps)
        if ((fh = fopen (filename, "r")) == NULL)
                return (NULL);
 
-       while (fgets (buffer, 1024, fh) != NULL)
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
                derive_t *val = NULL;
                long long tmp;
@@ -835,7 +848,8 @@ static procstat_t *ps_read_io (int pid, procstat_t *ps)
                else
                        continue;
 
-               numfields = strsplit (buffer, fields, 8);
+               numfields = strsplit (buffer, fields,
+                               STATIC_ARRAY_SIZE (fields));
 
                if (numfields < 2)
                        continue;
@@ -869,11 +883,10 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
 
        int   i;
 
-       int   ppid;
        int   name_len;
 
-       long long unsigned cpu_user_counter;
-       long long unsigned cpu_system_counter;
+       derive_t cpu_user_counter;
+       derive_t cpu_system_counter;
        long long unsigned vmem_size;
        long long unsigned vmem_rss;
        long long unsigned stack_size;
@@ -887,7 +900,7 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
                return (-1);
        buffer[i] = 0;
 
-       fields_len = strsplit (buffer, fields, 64);
+       fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
        if (fields_len < 24)
        {
                DEBUG ("processes plugin: ps_read_process (pid = %i):"
@@ -907,7 +920,6 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        fields[1][name_len] = '\0';
        strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
 
-       ppid = atoi (fields[3]);
 
        *state = fields[2][0];
 
@@ -938,8 +950,8 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        cpu_system_counter = atoll (fields[14]);
        vmem_size          = atoll (fields[22]);
        vmem_rss           = atoll (fields[23]);
-       ps->vmem_minflt_counter = atol (fields[9]);
-       ps->vmem_majflt_counter = atol (fields[11]);
+       ps->vmem_minflt_counter = atoll (fields[9]);
+       ps->vmem_majflt_counter = atoll (fields[11]);
 
        {
                unsigned long long stack_start = atoll (fields[27]);
@@ -963,8 +975,8 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
                DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
        }
 
-       ps->cpu_user_counter = (unsigned long) cpu_user_counter;
-       ps->cpu_system_counter = (unsigned long) cpu_system_counter;
+       ps->cpu_user_counter = cpu_user_counter;
+       ps->cpu_system_counter = cpu_system_counter;
        ps->vmem_size = (unsigned long) vmem_size;
        ps->vmem_rss = (unsigned long) vmem_rss;
        ps->stack_size = (unsigned long) stack_size;
@@ -997,13 +1009,18 @@ static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
        if ((pid < 1) || (NULL == buf) || (buf_len < 2))
                return NULL;
 
-       ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
+       ssnprintf (file, sizeof (file), "/proc/%u/cmdline",
+                       (unsigned int) pid);
 
+       errno = 0;
        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)));
+               /* ENOENT means the process exited while we were handling it.
+                * Don't complain about this, it only fills the logs. */
+               if (errno != ENOENT)
+                       WARNING ("processes plugin: Failed to open `%s': %s.", file,
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                return NULL;
        }
 
@@ -1018,7 +1035,7 @@ static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
                status = read (fd, (void *)buf_ptr, len);
 
                if (status < 0) {
-                       char errbuf[4096];
+                       char errbuf[1024];
 
                        if ((EAGAIN == errno) || (EINTR == errno))
                                continue;
@@ -1103,7 +1120,7 @@ static unsigned long read_fork_rate ()
 
                errno = 0;
                endptr = NULL;
-               result = strtoul(fields[1], &endptr, 10);
+               result = strtoul(fields[1], &endptr, /* base = */ 10);
                if ((endptr == fields[1]) || (errno != 0)) {
                        ERROR ("processes plugin: Cannot parse fork rate: %s",
                                        fields[1]);
@@ -1587,9 +1604,9 @@ static int ps_read (void)
        procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
        if (procs == NULL)
        {
-               kvm_close (kd);
                ERROR ("processes plugin: Cannot get kvm processes list: %s",
                                kvm_geterr(kd));
+               kvm_close (kd);
                return (0);
        }
 
@@ -1795,6 +1812,11 @@ static int ps_read (void)
                        pse.vmem_code = 0;
                        pse.stack_size =  0;
 
+                       pse.io_rchar = -1;
+                       pse.io_wchar = -1;
+                       pse.io_syscr = -1;
+                       pse.io_syscw = -1;
+
                        ps_list_add (cmdline, cargs, &pse);
                } /* for (i = 0 .. nprocs) */
 
@@ -1817,8 +1839,7 @@ static int ps_read (void)
 
 void module_register (void)
 {
-       plugin_register_config ("processes", ps_config,
-                       config_keys, config_keys_num);
+       plugin_register_complex_config ("processes", ps_config);
        plugin_register_init ("processes", ps_init);
        plugin_register_read ("processes", ps_read);
 } /* void module_register */