[collectd] [PATCH] Added fork rate collecting to processes plugin.

Patrik Weiskircher weiskircher at inqnet.at
Thu Oct 1 13:57:11 CEST 2009


---
 src/processes.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/types.db    |    1 +
 2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/src/processes.c b/src/processes.c
index fd06b21..ceefb48 100644
--- a/src/processes.c
+++ b/src/processes.c
@@ -836,6 +836,65 @@ static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
 	}
 	return buf;
 } /* char *ps_get_cmdline (...) */
+
+static unsigned long read_fork_rate ()
+{
+	FILE *proc_stat;
+	char buf[1024];
+	unsigned long result = 0;
+	int numfields;
+	char *fields[2];
+
+	proc_stat = fopen("/proc/stat", "r");
+	if (proc_stat == NULL) {
+		char errbuf[1024];
+		ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
+				sstrerror (errno, errbuf, sizeof (errbuf)));
+		return 0;
+	}
+
+	while (fgets (buf, sizeof(buf), proc_stat) != NULL)
+	{
+		if (strncmp (buf, "processes", 9))
+			continue;
+		numfields = strsplit(buf, fields, 2);
+		if (numfields != 2) {
+			ERROR ("processes plugin: processes in /proc/stat contain more than 2 fields.");
+			break;
+		}
+
+		result = strtoul(fields[1], NULL, 10);
+		if (errno == ERANGE && result == ULONG_MAX) {
+			ERROR ("processes plugin: processes value in /proc/stat overflows.");
+			break;
+		}
+
+		break;
+	}
+
+	fclose(proc_stat);
+
+	return result;
+}
+
+static void ps_submit_fork_rate (unsigned long value)
+{
+	value_t values[1];
+	value_list_t vl = VALUE_LIST_INIT;
+
+	values[0].derive = value;
+
+	vl.values = values;
+	vl.values_len = 1;
+	sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+	sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
+	sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+	sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
+	sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
+
+	plugin_dispatch_values (&vl);
+}
+
 #endif /* KERNEL_LINUX */
 
 #if HAVE_THREAD_INFO
@@ -1158,6 +1217,8 @@ static int ps_read (void)
 	procstat_entry_t pse;
 	char       state;
 
+	unsigned long fork_rate;
+
 	procstat_t *ps_ptr;
 
 	running = sleeping = zombies = stopped = paging = blocked = 0;
@@ -1231,6 +1292,9 @@ static int ps_read (void)
 
 	for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
 		ps_submit_proc_list (ps_ptr);
+
+	fork_rate = read_fork_rate();
+	ps_submit_fork_rate(fork_rate);
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
diff --git a/src/types.db b/src/types.db
index 1028b9c..177c57b 100644
--- a/src/types.db
+++ b/src/types.db
@@ -56,6 +56,7 @@ files			value:GAUGE:0:U
 frequency		frequency:GAUGE:0:U
 frequency_offset	ppm:GAUGE:-1000000:1000000
 fscache_stat		value:COUNTER:0:4294967295
+fork_rate		value:DERIVE:0:U
 gauge			value:GAUGE:U:U
 http_request_methods	count:COUNTER:0:134217728
 http_requests		count:COUNTER:0:134217728
-- 
1.5.6.5




More information about the collectd mailing list