Merge branch 'collectd-4.0' into collectd-4.1
[collectd.git] / src / processes.c
index b1f9fbb..22d369d 100644 (file)
@@ -25,7 +25,6 @@
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
-#include "utils_debug.h"
 #include "configfile.h"
 
 /* Include header files for the mach system, if they exist.. */
 #  ifndef CONFIG_HZ
 #    define CONFIG_HZ 100
 #  endif
-#endif /* KERNEL_LINUX */
-
-#define MODULE_NAME "processes"
+/* #endif KERNEL_LINUX */
 
-#if HAVE_THREAD_INFO || KERNEL_LINUX
-# define PROCESSES_HAVE_READ 1
 #else
-# define PROCESSES_HAVE_READ 0
+# error "No applicable input method."
 #endif
 
 #define BUFSIZE 256
 
-static data_source_t state_dsrc[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
-};
-
-static data_set_t state_ds =
-{
-       "ps_state", 1, state_dsrc
-};
-
-static data_source_t rss_dsrc[1] =
-{
-       /* max = 2^63 - 1 */
-       {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
-};
-
-static data_set_t rss_ds =
-{
-       "ps_rss", 1, rss_dsrc
-};
-
-static data_source_t time_dsrc[2] =
-{
-       /* 1 second in user-mode per second ought to be enough.. */
-       {"user", DS_TYPE_COUNTER, 0.0, 1000000.0},
-       {"syst", DS_TYPE_COUNTER, 0.0, 1000000.0}
-};
-
-static data_set_t time_ds =
-{
-       "ps_cputime", 2, time_dsrc
-};
-
-static data_source_t count_dsrc[2] =
-{
-       /* 1 second in user-mode per second ought to be enough.. */
-       {"processes", DS_TYPE_GAUGE, 0.0, 1000000.0},
-       {"threads",   DS_TYPE_GAUGE, 0.0, 1000000.0}
-};
-
-static data_set_t count_ds =
-{
-       "ps_count", 2, count_dsrc
-};
-
-static data_source_t pagefaults_dsrc[2] =
-{
-       /* max = 2^63 - 1 */
-       {"minflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0},
-       {"majflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0}
-};
-
-static data_set_t pagefaults_ds =
-{
-       "ps_pagefaults", 2, pagefaults_dsrc
-};
-
-#if PROCESSES_HAVE_READ
 static const char *config_keys[] =
 {
        "Process",
@@ -213,7 +150,6 @@ static mach_msg_type_number_t     pset_list_len;
 static long pagesize_g;
 #endif /* KERNEL_LINUX */
 
-#if HAVE_THREAD_INFO | KERNEL_LINUX
 static void ps_list_register (const char *name)
 {
        procstat_t *new;
@@ -384,7 +320,7 @@ static void ps_list_reset (void)
                {
                        if (pse->age > 10)
                        {
-                               DBG ("Removing this procstat entry cause it's too old: "
+                               DEBUG ("Removing this procstat entry cause it's too old: "
                                                "id = %lu; name = %s;",
                                                pse->id, ps->name);
 
@@ -410,7 +346,6 @@ static void ps_list_reset (void)
                } /* while (pse != NULL) */
        } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
 }
-#endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
 
 static int ps_config (const char *key, const char *value)
 {
@@ -447,7 +382,7 @@ static int ps_init (void)
                                        &pset_list,
                                        &pset_list_len)) != KERN_SUCCESS)
        {
-               syslog (LOG_ERR, "host_processor_sets failed: %s\n",
+               ERROR ("host_processor_sets failed: %s\n",
                                mach_error_string (status));
                pset_list = NULL;
                pset_list_len = 0;
@@ -457,7 +392,7 @@ static int ps_init (void)
 
 #elif KERNEL_LINUX
        pagesize_g = sysconf(_SC_PAGESIZE);
-       DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
+       DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
                        pagesize_g, CONFIG_HZ);
 #endif /* KERNEL_LINUX */
 
@@ -474,7 +409,7 @@ static void ps_submit_state (const char *state, double value)
        vl.values = values;
        vl.values_len = 1;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname);
+       strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "processes");
        strcpy (vl.plugin_instance, "");
        strncpy (vl.type_instance, state, sizeof (vl.type_instance));
@@ -490,7 +425,7 @@ static void ps_submit_proc_list (procstat_t *ps)
        vl.values = values;
        vl.values_len = 2;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname);
+       strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "processes");
        strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
 
@@ -513,7 +448,7 @@ static void ps_submit_proc_list (procstat_t *ps)
        vl.values_len = 2;
        plugin_dispatch_values ("ps_pagefaults", &vl);
 
-       DBG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
+       DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
                        "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
                        "cpu_user_counter = %lu; cpu_system_counter = %lu;",
                        ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
@@ -537,7 +472,7 @@ static int *ps_read_tasks (int pid)
 
        if ((dh = opendir (dirname)) == NULL)
        {
-               DBG ("Failed to open directory `%s'", dirname);
+               DEBUG ("Failed to open directory `%s'", dirname);
                return (NULL);
        }
 
@@ -557,7 +492,7 @@ static int *ps_read_tasks (int pid)
                        {
                                if (list != NULL)
                                        free (list);
-                               syslog (LOG_ERR, "processes plugin: "
+                               ERROR ("processes plugin: "
                                                "Failed to allocate more memory.");
                                return (NULL);
                        }
@@ -575,11 +510,14 @@ static int *ps_read_tasks (int pid)
 
        closedir (dh);
 
+       if (list_len == 0)
+               return (NULL);
+
        assert (list_len < list_size);
        assert (list[list_len] == 0);
 
        return (list);
-}
+} /* int *ps_read_tasks */
 
 int ps_read_process (int pid, procstat_t *ps, char *state)
 {
@@ -619,20 +557,17 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        fields_len = strsplit (buffer, fields, 64);
        if (fields_len < 24)
        {
-               DBG ("`%s' has only %i fields..",
-                               filename, fields_len);
+               DEBUG ("processes plugin: ps_read_process (pid = %i):"
+                               " `%s' has only %i fields..",
+                               (int) pid, filename, fields_len);
                return (-1);
        }
-       else if (fields_len != 41)
-       {
-               DBG ("WARNING: (fields_len = %i) != 41", fields_len);
-       }
 
        /* copy the name, strip brackets in the process */
        name_len = strlen (fields[1]) - 2;
        if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
        {
-               DBG ("No brackets found in process name: `%s'", fields[1]);
+               DEBUG ("No brackets found in process name: `%s'", fields[1]);
                return (-1);
        }
        fields[1] = fields[1] + 1;
@@ -641,17 +576,21 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
 
        ppid = atoi (fields[3]);
 
-       if ((tasks = ps_read_tasks (pid)) == NULL)
+       *state = fields[2][0];
+
+       if (*state == 'Z')
        {
-               /* This happends for zombied, e.g. */
-               DBG ("ps_read_tasks (%i) failed.", pid);
-               *state = 'Z';
                ps->num_lwp  = 0;
                ps->num_proc = 0;
        }
+       else if ((tasks = ps_read_tasks (pid)) == NULL)
+       {
+               /* Kernel 2.4 or so */
+               ps->num_lwp  = 1;
+               ps->num_proc = 1;
+       }
        else
        {
-               *state = '\0';
                ps->num_lwp  = 0;
                ps->num_proc = 1;
                for (i = 0; tasks[i] != 0; i++)
@@ -661,11 +600,11 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
                tasks = NULL;
        }
 
-       /* Leave the rest at zero if this is only an LWP */
+       /* Leave the rest at zero if this is only a zombi */
        if (ps->num_proc == 0)
        {
-               DBG ("This is only an LWP: pid = %i; name = %s;",
-                               pid, ps->name);
+               DEBUG ("processes plugin: This is only a zombi: pid = %i; "
+                               "name = %s;", pid, ps->name);
                return (0);
        }
 
@@ -684,8 +623,6 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        ps->cpu_system_counter = (unsigned long) cpu_system_counter;
        ps->vmem_rss = (unsigned long) vmem_rss;
 
-       *state = fields[2][0];
-
        /* success */
        return (0);
 } /* int ps_read_process (...) */
@@ -717,7 +654,7 @@ static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_l
        strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
        name[name_max_len - 1] = '\0';
 
-       DBG ("pid = %i; name = %s;", *pid, name);
+       DEBUG ("pid = %i; name = %s;", *pid, name);
 
        /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
         * `top' does it, because it is a lot of work and only used when
@@ -773,7 +710,7 @@ static int ps_read (void)
                                                pset_list[pset],
                                                &port_pset_priv)) != KERN_SUCCESS)
                {
-                       syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
+                       ERROR ("host_processor_set_priv failed: %s\n",
                                        mach_error_string (status));
                        continue;
                }
@@ -782,7 +719,7 @@ static int ps_read (void)
                                                &task_list,
                                                &task_list_len)) != KERN_SUCCESS)
                {
-                       syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
+                       ERROR ("processor_set_tasks failed: %s\n",
                                        mach_error_string (status));
                        mach_port_deallocate (port_task_self, port_pset_priv);
                        continue;
@@ -816,7 +753,7 @@ static int ps_read (void)
                                                &task_basic_info_len);
                                if (status != KERN_SUCCESS)
                                {
-                                       syslog (LOG_ERR, "task_info failed: %s",
+                                       ERROR ("task_info failed: %s",
                                                        mach_error_string (status));
                                        continue; /* with next thread_list */
                                }
@@ -828,7 +765,7 @@ static int ps_read (void)
                                                &task_events_info_len);
                                if (status != KERN_SUCCESS)
                                {
-                                       syslog (LOG_ERR, "task_info failed: %s",
+                                       ERROR ("task_info failed: %s",
                                                        mach_error_string (status));
                                        continue; /* with next thread_list */
                                }
@@ -840,7 +777,7 @@ static int ps_read (void)
                                                &task_absolutetime_info_len);
                                if (status != KERN_SUCCESS)
                                {
-                                       syslog (LOG_ERR, "task_info failed: %s",
+                                       ERROR ("task_info failed: %s",
                                                        mach_error_string (status));
                                        continue; /* with next thread_list */
                                }
@@ -864,7 +801,7 @@ static int ps_read (void)
                                 * thread is nonsense, since the task/process
                                 * is dead. */
                                zombies++;
-                               DBG ("task_threads failed: %s",
+                               DEBUG ("task_threads failed: %s",
                                                mach_error_string (status));
                                if (task_list[task] != port_task_self)
                                        mach_port_deallocate (port_task_self,
@@ -881,7 +818,7 @@ static int ps_read (void)
                                                &thread_data_len);
                                if (status != KERN_SUCCESS)
                                {
-                                       syslog (LOG_ERR, "thread_info failed: %s",
+                                       ERROR ("thread_info failed: %s",
                                                        mach_error_string (status));
                                        if (task_list[task] != port_task_self)
                                                mach_port_deallocate (port_task_self,
@@ -913,8 +850,7 @@ static int ps_read (void)
                                         * There's only zombie tasks, which are
                                         * handled above. */
                                        default:
-                                               syslog (LOG_WARNING,
-                                                               "Unknown thread status: %s",
+                                               WARNING ("Unknown thread status: %s",
                                                                thread_data.run_state);
                                                break;
                                } /* switch (thread_data.run_state) */
@@ -924,7 +860,7 @@ static int ps_read (void)
                                        status = mach_port_deallocate (port_task_self,
                                                        thread_list[thread]);
                                        if (status != KERN_SUCCESS)
-                                               syslog (LOG_ERR, "mach_port_deallocate failed: %s",
+                                               ERROR ("mach_port_deallocate failed: %s",
                                                                mach_error_string (status));
                                }
                        } /* for (thread_list) */
@@ -934,7 +870,7 @@ static int ps_read (void)
                                                        thread_list_len * sizeof (thread_act_t)))
                                        != KERN_SUCCESS)
                        {
-                               syslog (LOG_ERR, "vm_deallocate failed: %s",
+                               ERROR ("vm_deallocate failed: %s",
                                                mach_error_string (status));
                        }
                        thread_list = NULL;
@@ -948,7 +884,7 @@ static int ps_read (void)
                                status = mach_port_deallocate (port_task_self,
                                                task_list[task]);
                                if (status != KERN_SUCCESS)
-                                       syslog (LOG_ERR, "mach_port_deallocate failed: %s",
+                                       ERROR ("mach_port_deallocate failed: %s",
                                                        mach_error_string (status));
                        }
 
@@ -960,7 +896,7 @@ static int ps_read (void)
                                (vm_address_t) task_list,
                                task_list_len * sizeof (task_t))) != KERN_SUCCESS)
                {
-                       syslog (LOG_ERR, "vm_deallocate failed: %s",
+                       ERROR ("vm_deallocate failed: %s",
                                        mach_error_string (status));
                }
                task_list = NULL;
@@ -969,7 +905,7 @@ static int ps_read (void)
                if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
                                != KERN_SUCCESS)
                {
-                       syslog (LOG_ERR, "mach_port_deallocate failed: %s",
+                       ERROR ("mach_port_deallocate failed: %s",
                                        mach_error_string (status));
                }
        } /* for (pset_list) */
@@ -1008,7 +944,9 @@ static int ps_read (void)
 
        if ((proc = opendir ("/proc")) == NULL)
        {
-               syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
+               char errbuf[1024];
+               ERROR ("Cannot open `/proc': %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
 
@@ -1023,7 +961,7 @@ static int ps_read (void)
                status = ps_read_process (pid, &ps, &state);
                if (status != 0)
                {
-                       DBG ("ps_read_process failed: %i", status);
+                       DEBUG ("ps_read_process failed: %i", status);
                        continue;
                }
 
@@ -1072,21 +1010,11 @@ static int ps_read (void)
 
        return (0);
 } /* int ps_read */
-#endif /* PROCESSES_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register_data_set (&state_ds);
-       plugin_register_data_set (&rss_ds);
-       plugin_register_data_set (&time_ds);
-       plugin_register_data_set (&count_ds );
-       plugin_register_data_set (&pagefaults_ds );
-
-#if PROCESSES_HAVE_READ
        plugin_register_config ("processes", ps_config,
                        config_keys, config_keys_num);
        plugin_register_init ("processes", ps_init);
        plugin_register_read ("processes", ps_read);
-#endif /* PROCESSES_HAVE_READ */
-}
-
+} /* void module_register */