[pve-devel] r5675 - in pve-cluster/trunk/data: . src

svn-commits at proxmox.com svn-commits at proxmox.com
Thu Mar 10 12:21:35 CET 2011


Author: dietmar
Date: 2011-03-10 12:21:35 +0100 (Thu, 10 Mar 2011)
New Revision: 5675

Modified:
   pve-cluster/trunk/data/ChangeLog
   pve-cluster/trunk/data/src/status.c
Log:
 use thread safe API, connect to rrdcached


Modified: pve-cluster/trunk/data/ChangeLog
===================================================================
--- pve-cluster/trunk/data/ChangeLog	2011-03-10 08:14:09 UTC (rev 5674)
+++ pve-cluster/trunk/data/ChangeLog	2011-03-10 11:21:35 UTC (rev 5675)
@@ -1,6 +1,7 @@
 2011-03-10  Proxmox Support Team  <support at proxmox.com>
 
 	* src/status.c (update_rrd_data): add RRD definition for VMs 
+	(update_rrd_data): use thread safe API, connect to rrdcached
 
 	* src/dfsm.c (dfsm_send_message_full): limit to 100 retries (max
 	10 seconds delay).

Modified: pve-cluster/trunk/data/src/status.c
===================================================================
--- pve-cluster/trunk/data/src/status.c	2011-03-10 08:14:09 UTC (rev 5674)
+++ pve-cluster/trunk/data/src/status.c	2011-03-10 11:21:35 UTC (rev 5675)
@@ -31,6 +31,7 @@
 #include <glib.h>
 #include <sys/syslog.h>
 #include <rrd.h>
+#include <rrd_client.h>
 #include <time.h>
 
 #include "cfs-utils.h"
@@ -779,12 +780,7 @@
 	return TRUE;
 }
 
-static char *rrd_def_node[] = {
-	"create",
-	NULL, /* filename */
-	"-b",
-	NULL, /* starttime */
-	"--step", "60",
+static const char *rrd_def_node[] = {
 	"DS:loadavg:GAUGE:120:0:U",
 	"DS:cpu:GAUGE:120:0:1",
 	"DS:iowait:GAUGE:120:0:1",
@@ -811,12 +807,7 @@
 	NULL,
 };
 
-static char *rrd_def_vm[] = {
-	"create",
-	NULL, /* filename */
-	"-b",
-	NULL, /* starttime */
-	"--step", "60",
+static const char *rrd_def_vm[] = {
 	"DS:maxcpu:GAUGE:120:0:U",
 	"DS:cpu:GAUGE:120:0:1",
 	"DS:maxmem:GAUGE:120:0:U",
@@ -840,14 +831,31 @@
 	NULL,
 };
 
+static const char *rrd_def_storage[] = {
+	"DS:total:GAUGE:120:0:U",
+	"DS:used:GAUGE:120:0:U",
 
+	"RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
+	"RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
+	"RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
+	"RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
+	"RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
+	
+	"RRA:MAX:0.5:1:70", // 1 min max - one hour
+	"RRA:MAX:0.5:30:70", // 30 min max - one day
+	"RRA:MAX:0.5:180:70",  // 3 hour max - one week
+	"RRA:MAX:0.5:720:70", // 12 hour max - one month
+	"RRA:MAX:0.5:10080:70", // 7 day max - ony year
+	NULL,
+};
+
 #define RRDDIR "/var/lib/rrdcached/db"
 
 static void
 create_rrd_file(
-	char *filename,
+	const char *filename,
 	int argcount,
-	char *rrddef[])
+	const char *rrddef[])
 {
 	/* start at day boundary */
 	time_t ctime;
@@ -857,16 +865,10 @@
 	ltm->tm_min = 0;
 	ltm->tm_hour = 0;
 
-	char *start = g_strdup_printf("%lu", timelocal(ltm));
-
-	rrddef[1] = filename;
-	rrddef[3] = start;
-
-	if (rrd_create(argcount, rrddef) != 0) {
+	rrd_clear_error();
+	if (rrd_create_r(filename, 60, timelocal(ltm), argcount, rrddef)) {
 		cfs_message("RRD create error %s: %s", filename, rrd_get_error());
 	}
-
-	g_free(start);
 }
 
 static void 
@@ -882,42 +884,102 @@
 
 	// fixme: when do we remove old entries ?
 
+	static const char *rrdcsock = "unix:/var/run/rrdcached.sock";
+
+	int use_daemon = 1;
+        if (rrdc_connect(rrdcsock) != 0)
+		use_daemon = 0;
+
 	char *filename = g_strdup_printf(RRDDIR "/%s", key);
 
-	if (strncmp(key, "pve-nodes/", 10) == 0) {
-		const char *node = key + 10;
-		g_return_if_fail(strchr(node, '/') == NULL);
+	if (strncmp(key, "pve-node/", 9) == 0) {
+		const char *node = key + 9;
+
+		if (strchr(node, '/') != NULL)
+			goto keyerror;
+
+		if (strlen(node) < 1)
+			goto keyerror;
 		
 		if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
 
-			mkdir(RRDDIR "/pve-nodes", 0755);
+			mkdir(RRDDIR "/pve-node", 0755);
 			int argcount = sizeof(rrd_def_node)/sizeof(void*) - 1;
 			create_rrd_file(filename, argcount, rrd_def_node);
 		}
 		
-	} else if (strncmp(key, "pve-vms/", 8) == 0) {
-		const char *vmid = key + 10;
-		g_return_if_fail(strchr(vmid, '/') == NULL);
-		
+	} else if (strncmp(key, "pve-vm/", 7) == 0) {
+		const char *vmid = key + 7;
+
+		if (strchr(vmid, '/') != NULL)
+			goto keyerror;
+
+		if (strlen(vmid) < 1)
+			goto keyerror;
+
 		if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
 
-			mkdir(RRDDIR "/pve-vms", 0755);
+			mkdir(RRDDIR "/pve-vm", 0755);
 			int argcount = sizeof(rrd_def_vm)/sizeof(void*) - 1;
 			create_rrd_file(filename, argcount, rrd_def_vm);
 		}
 		
+	} else if (strncmp(key, "pve-storage/", 12) == 0) {
+		const char *node = key + 12;
+
+		if (strchr(node, '/') != NULL)
+			goto keyerror;
+
+		const char *storage = node;
+		while (*storage && *storage != '_')
+			storage++;
+
+		if (*storage != '_' || ((storage - node) < 1))
+			goto keyerror;
+
+		storage++;
+
+		if (strlen(storage) < 1)
+			goto keyerror;
+
+		if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
+
+			mkdir(RRDDIR "/pve-storage", 0755);
+			int argcount = sizeof(rrd_def_storage)/sizeof(void*) - 1;
+			create_rrd_file(filename, argcount, rrd_def_storage);
+		}
+		
 	} else {
-		cfs_critical("RRD update error: unknown key %s", key);
-		return;
+		goto keyerror;
 	}
 
-	char *update_args[] = { "update", filename, (char*)data, NULL };
+	const char *update_args[] = { data, NULL };
 
-	if (rrd_update(3, update_args) != 0) {
-		cfs_message("RRD update error %s: %s", filename, rrd_get_error());
+	if (use_daemon) {
+		int status;
+		if ((status = rrdc_update(filename, 1, update_args)) != 0) {
+			cfs_message("RRDC update error %s: %d", filename, status);
+			rrdc_disconnect();
+			rrd_clear_error();
+			if (rrd_update_r(filename, NULL, 1, update_args) != 0) {
+				cfs_message("RRD update error %s: %s", filename, rrd_get_error());
+			}
+		}
+
+	} else {
+		rrd_clear_error();
+		if (rrd_update_r(filename, NULL, 1, update_args) != 0) {
+			cfs_message("RRD update error %s: %s", filename, rrd_get_error());
+		}
 	}
 
+ret:
 	g_free(filename);
+	return;
+
+keyerror:
+	cfs_critical("RRD update error: unknown/wrong key %s", key);
+	goto ret;
 }
 
 void



More information about the pve-devel mailing list