Code

Merge branch 'collectd-3.11' into collectd-4.0
[collectd.git] / src / processes.c
index b1f9fbbefea6356ed4f34995f0b1cf5793a572c6..954cd0df9d4a4b901a46a11bc8273bbd5ef0cc64 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.. */
 
 #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
+#if HAVE_THREAD_INFO | KERNEL_LINUX
 static const char *config_keys[] =
 {
        "Process",
        NULL
 };
 static int config_keys_num = 1;
+#endif
 
 typedef struct procstat_entry_s
 {
@@ -199,7 +143,9 @@ typedef struct procstat
        struct procstat_entry_s *instances;
 } procstat_t;
 
+#if HAVE_THREAD_INFO | KERNEL_LINUX
 static procstat_t *list_head_g = NULL;
+#endif
 
 #if HAVE_THREAD_INFO
 static mach_port_t port_host_self;
@@ -384,7 +330,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 +356,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)
 {
@@ -425,6 +370,7 @@ static int ps_config (const char *key, const char *value)
 
        return (0);
 }
+#endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
 
 static int ps_init (void)
 {
@@ -447,7 +393,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 +403,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 +420,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 +436,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 +459,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 +483,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 +503,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 +521,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 +568,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 +587,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,10 +611,10 @@ 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;",
+               DEBUG ("This is only a zombi: pid = %i; name = %s;",
                                pid, ps->name);
                return (0);
        }
@@ -684,8 +634,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 +665,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 +721,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 +730,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 +764,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 +776,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 +788,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 +812,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 +829,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 +861,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 +871,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 +881,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 +895,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 +907,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 +916,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 +955,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 +972,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;
                }
 
@@ -1076,17 +1025,12 @@ static int ps_read (void)
 
 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
+#if HAVE_THREAD_INFO | KERNEL_LINUX
        plugin_register_config ("processes", ps_config,
                        config_keys, config_keys_num);
+#endif
        plugin_register_init ("processes", ps_init);
        plugin_register_read ("processes", ps_read);
 #endif /* PROCESSES_HAVE_READ */
-}
-
+} /* void module_register */