#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
-#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
{
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;
static long pagesize_g;
#endif /* KERNEL_LINUX */
-#if HAVE_THREAD_INFO | KERNEL_LINUX
static void ps_list_register (const char *name)
{
procstat_t *new;
{
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);
return (0);
}
-#endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
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;
#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 */
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));
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));
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,
if ((dh = opendir (dirname)) == NULL)
{
- DBG ("Failed to open directory `%s'", dirname);
+ DEBUG ("Failed to open directory `%s'", dirname);
return (NULL);
}
{
if (list != NULL)
free (list);
- syslog (LOG_ERR, "processes plugin: "
+ ERROR ("processes plugin: "
"Failed to allocate more memory.");
return (NULL);
}
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)
{
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;
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++)
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);
}
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 (...) */
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
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;
}
&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;
&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 */
}
&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 */
}
&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 */
}
* 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,
&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,
* 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) */
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) */
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;
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));
}
(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;
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) */
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);
}
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;
}
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
-#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 */