[Commits] r667 - trunk/daemon/src

new-commit at lists.openvcp.org new-commit at lists.openvcp.org
Mon May 4 13:42:46 UTC 2009


Author: cryptronic
Date: 2009-05-04 13:42:45 +0000 (Mon, 04 May 2009)
New Revision: 667

Modified:
   trunk/daemon/src/traffic.c
Log:
ugly fix for problems with locked table


Modified: trunk/daemon/src/traffic.c
===================================================================
--- trunk/daemon/src/traffic.c	2009-05-03 17:26:29 UTC (rev 666)
+++ trunk/daemon/src/traffic.c	2009-05-04 13:42:45 UTC (rev 667)
@@ -50,17 +50,17 @@
 
 
 	char sql_query[BUF_SIZE];
-	char *sql_error; 
+	char *sql_error;
 	int nrow, ncolumn;
 	char **sql_result;
 	int  ret;
 
-	
+
 	*in = 0;
 	*out = 0;
 	*total = 0;
-	
-	snprintf(sql_query, BUF_SIZE, 
+
+	snprintf(sql_query, BUF_SIZE,
 		"SELECT \"in\",\"out\",\"total\" FROM node_traffic WHERE (day=%d and month=%d and year=%d)",  day, month, year);
 
 	ret = sqlite3_get_table(openvcp_db, sql_query, &sql_result, &nrow, &ncolumn, &sql_error);
@@ -72,7 +72,7 @@
 		sqlite3_close(openvcp_db);
 		exit(-1);
 	}
-	
+
 	ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
 
 	if(nrow < 1)
@@ -80,38 +80,38 @@
 		sqlite3_free_table(sql_result);
 		return -1;
 	}
-	
+
 	*in = strtoll(sql_result[(ncolumn)+0], NULL, 0);
 	*out = strtoll(sql_result[(ncolumn)+1], NULL, 0);
-	*total = strtoll(sql_result[(ncolumn)+2], NULL, 0);		
-	
+	*total = strtoll(sql_result[(ncolumn)+2], NULL, 0);
 
-	
+
+
 	ovcp_log(OVCP_DEBUG, "Node Traffic Result: %d, %d, %d, @ %lld, %lld, %lld",
 			day, month, year, *in, *out, *total);
-	
+
 	sqlite3_free_table(sql_result);
-	
+
 	return 1;
 }
 
 static int get_vserver_traffic(sqlite3 *openvcp_db, int day, int month, int year, char *ip, char *name, long long int *in, long long int *out, long long int *total)
 {
 	char sql_query[BUF_SIZE];
-	char *sql_error; 
+	char *sql_error;
 	int nrow, ncolumn;
 	char **sql_result;
 	int ret;
 
 
-	
+
 	*in = 0;
 	*out = 0;
 	*total = 0;
-	
 
-	
-	snprintf(sql_query, BUF_SIZE, 
+
+
+	snprintf(sql_query, BUF_SIZE,
 		"SELECT \"in\",\"out\",\"total\" FROM vserver_traffic WHERE (day=%d and month=%d and year=%d) and (ip = '%s' and name = '%s')", day, month, year, ip, name);
 
 
@@ -127,23 +127,23 @@
 
 	ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
 
-	
+
 	if(nrow < 1)
 	{
 		sqlite3_free_table(sql_result);
 		return -1;
 	}
-	
+
 	*in = strtoll(sql_result[(ncolumn)+0], NULL, 0);
 	*out = strtoll(sql_result[(ncolumn)+1], NULL, 0);
-	*total = strtoll(sql_result[(ncolumn)+2], NULL, 0);		
-	
+	*total = strtoll(sql_result[(ncolumn)+2], NULL, 0);
 
-	
+
+
 	ovcp_log(OVCP_DEBUG, "VServer Traffic Result: %s, %d, %d, %d, @ %lld, %lld, %lld", ip, day, month, year, *in, *out, *total);
-	
+
 	sqlite3_free_table(sql_result);
-	
+
 	return 1;
 }
 
@@ -151,27 +151,27 @@
 						long long int *node_in, long long int *node_out, long long int *node_total, struct vsip_entry *iplist)
 {
 	char sql_query[BUF_SIZE];
-	char *sql_error; 
-	int ret, i = 0;
+	char *sql_error;
+	int ret, i = 0, c = 0;
 	long long int day_in = 0, day_out = 0, day_total = 0;
 	char ipstr[40];
-	
+
 	if(root_node == NULL)
 		return;
-		
 
-		
-	
-	
+
+
+
+
 	#ifdef IPV6_SUPPORT
-	
+
 	if(root_node->ip.in6_u.u6_addr16[0] == 0 && root_node->ip.in6_u.u6_addr16[1] == 0 &&
 		root_node->ip.in6_u.u6_addr16[2] == 0 && root_node->ip.in6_u.u6_addr16[3] == 0 &&
 		root_node->ip.in6_u.u6_addr16[4] == 0 && root_node->ip.in6_u.u6_addr16[5] == 0xFFFF)
 	{
 		inet_ntop(AF_INET, &root_node->ip.in6_u.u6_addr16[6], ipstr, 40);
 		ovcp_log(OVCP_DEBUG, "IPV4: %s", ipstr);
-		
+
 		while(iplist != NULL && iplist[i].ip != NULL)
 		{
 
@@ -179,12 +179,12 @@
 				break;
 			i++;
 		}
-	
+
 		snprintf(ipstr, 40, "%u", str2ip(ipstr));
 
 	}
 	else
-	{	
+	{
 		inet_ntop(AF_INET6, &root_node->ip, ipstr, 40);
 
 		while(iplist != NULL && iplist[i].ip != NULL)
@@ -195,13 +195,13 @@
 			i++;
 		}
 	}
-	
-	
+
+
 	#else
 	inet_ntop(AF_INET, &root_node->ip, ipstr, 40);
-	
+
 	ovcp_log(OVCP_DEBUG, "IPV4: %s", ipstr);
-	
+
 	while(iplist != NULL && iplist[i].ip != NULL)
 	{
 
@@ -212,25 +212,32 @@
 
 	snprintf(ipstr, 40, "%u", str2ip(ipstr));
 	#endif
-	
 
-	
+
+
 	if(iplist != NULL && iplist[i].name != NULL)
 	{
-	
+
 		/* Hour Traffic */
-	
-		snprintf(sql_query, BUF_SIZE, 
+
+		c = 0;
+
+		snprintf(sql_query, BUF_SIZE,
 			"INSERT OR REPLACE INTO vserver_hour_traffic VALUES(%d, %d, %d, %d, '%s', %lld, %lld, %lld, '%s')",
 			timeinfo->tm_mon+1, timeinfo->tm_hour, timeinfo->tm_mday, timeinfo->tm_year+1900,
 				ipstr, root_node->in, root_node->out, root_node->total, iplist[i].name);
 
 
 		ovcp_log(OVCP_DEBUG, "IP: %s IN: %lld OUT: %lld TOTAL: %lld", ipstr, root_node->in, root_node->out, root_node->total);
-		ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
-		
-		ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
 
+		do {
+			c++;
+			ovcp_log(OVCP_DEBUG, "SQL QUERY[%i]: %s", c, sql_query);
+			ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+			if(ret != SQLITE_OK)
+				sleep(2);
+		} while (c <= 10 && ret != SQLITE_OK);
+
 		if(ret != SQLITE_OK)
 		{
 			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);
@@ -239,49 +246,51 @@
 			exit(-1);
 		}
 
-	
 		/* Day Traffic */
 
-	
+		c = 0;
+
 		if(timeinfo->tm_hour != 0)
 			get_vserver_traffic(openvcp_db, timeinfo->tm_mday, timeinfo->tm_mon+1, timeinfo->tm_year+1900, ipstr, iplist[i].name, &day_in, &day_out, &day_total);
-	
+
 		day_in += root_node->in;
 		day_out += root_node->out;
 		day_total += root_node->total;
-	
-	
-		snprintf(sql_query, BUF_SIZE, 
+
+
+		snprintf(sql_query, BUF_SIZE,
 				"INSERT OR REPLACE INTO vserver_traffic VALUES(%d, %d, %d, '%s', %lld, %lld, %.lld, '%s')",
 				timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900, ipstr, day_in, day_out, day_total, iplist[i].name);
-	
-		ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
 
-	
-		ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+		do {
+			c++;
+			ovcp_log(OVCP_DEBUG, "SQL QUERY[%i]: %s", c, sql_query);
+			ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+			if(ret != SQLITE_OK)
+				sleep(2);
+		} while (c <= 10 && ret != SQLITE_OK);
 
 		if(ret != SQLITE_OK)
 		{
-			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);	
+			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);
 			sqlite3_free(sql_error);
 			sqlite3_close(openvcp_db);
 			exit(-1);
 		}
 
-	
-	}	
+	}
 
 
-	
-	
-	/* Total Traffic */	
-	
-	
+
+
+	/* Total Traffic */
+
+
 	*node_in += root_node->in;
 	*node_out += root_node->out;
-	*node_total += root_node->total; 
-	
-	
+	*node_total += root_node->total;
+
+
 	if(root_node->right != NULL)
 	{
 		ovcp_log(OVCP_DEBUG, "Right:");
@@ -300,50 +309,50 @@
 {
 	char path[BUF_SIZE];
 	char sql_query[BUF_SIZE];
-	char *sql_error; 
-	
+	char *sql_error;
+
 	DIR *procdir;
 	struct dirent *vsentry;
 	char *limitfile;
 	char **lines, *word, *name;
 	unsigned int lcount;
-	int i, ret;
-	
+	int i, ret, c = 0;
 
-	
-	
+
+
+
 	int proc, vm, vml, rss, anon, files;
 	int ofd, locks, sock, msgq, shm;
-	
 
 
+
 	if((procdir = opendir("/proc/virtual/")) == NULL)
 		return -1;
-	
+
 	while((vsentry = readdir(procdir)) != NULL)
 	{
 		proc = 0; vm = 0; vml = 0; rss = 0; anon = 0; files = 0;
 		ofd = 0; locks = 0; sock = 0; msgq = 0; shm = 0;
-		
+
 		if(vsentry->d_name[0] == '.' || strcmp(vsentry->d_name, "info") == 0)
 			continue;
-			
+
 		sconcat(path, BUF_SIZE, "/proc/virtual/", vsentry->d_name, "/limit", NULL);
 		limitfile = file_read(path);
-		
+
 		if(limitfile == NULL)
 			continue;
-		
-	
+
+
 		lines = split_string(limitfile, '\n',  &lcount);
 		i = 0;
-		
+
 		while(i < lcount)
 		{
 			word = strtok(lines[i], " ");
 			trim_string(word);
-				
-				
+
+
 			if(strcmp(word, "PROC:") == 0)
 			{
 				word = strtok(NULL, " ");
@@ -419,33 +428,37 @@
 				trim_string(word);
 				shm = atoi(word);
 			}
-			
-		
+
+
 			i++;
 		}
-		
+
 		free_strings(lines, lcount);
 		free(limitfile);
-		
-		
-		
+
+
+
 		if(proc == 0) continue;
 
-		
+
 		name = vserver_name(atoi(vsentry->d_name));
 		if(name == NULL) continue;
 
-		
-		snprintf(sql_query, BUF_SIZE, 
+
+		snprintf(sql_query, BUF_SIZE,
 			"INSERT OR REPLACE INTO vserver_hour_resources VALUES(%d, %d, %d, %d, '%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)",
 			month, hour, day, year, name, proc, vm, vml, rss, anon, files, ofd, locks, sock, msgq, shm);
-		
+
 		free(name);
-		
-		ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
-		
-		ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
-	
+
+		do {
+			c++;
+			ovcp_log(OVCP_DEBUG, "SQL QUERY[%i]: %s", c, sql_query);
+			ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+			if(ret != SQLITE_OK)
+				sleep(2);
+		} while (c <= 10 && ret != SQLITE_OK);
+
 		if(ret != SQLITE_OK)
 		{
 			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);
@@ -457,9 +470,9 @@
 	}
 
 	closedir(procdir);
-	
 
-	return 0;	
+
+	return 0;
 }
 
 
@@ -467,70 +480,70 @@
 int traffic_daemon_processe(void)
 {
 	sqlite3 *openvcp_db;
-	char *sql_error; 
+	char *sql_error;
 	char sql_query[BUF_SIZE];
 	struct vsip_entry *iplist;
-	
+
 	time_t rawtime, current_time;
 	struct tm * timeinfo;
 
 
-	int i=1, ret;
+	int i=1, ret, c = 0;
 
 	int refresh_delay;
-	
 
 
 
+
 	char **ifaces;
 	unsigned int iface_count;
 	struct _packetlogger **loggerdata;
-	
-	long long int node_in, node_out, node_total;		
+
+	long long int node_in, node_out, node_total;
 	long long int day_total, day_in, day_out;
-	
-	
+
+
 	signal(SIGSEGV, segv_handle);
 
 	ret = sqlite3_open(OPENVCP_DATABASE, &openvcp_db);
-	
+
 	if(ret != 0)
 	{
 		ovcp_log(OVCP_ERROR, "Can't open database: (%s)", sqlite3_errmsg(openvcp_db));
 		sqlite3_close(openvcp_db);
 		exit(-1);
-	}        
+	}
 
-	sqlite3_exec(openvcp_db, 
+	sqlite3_exec(openvcp_db,
 		"CREATE TABLE vserver_hour_resources ('month', 'hour', 'day', 'year', 'name', 'proc', 'vm', 'vml', 'rss', 'anon',"
 					"'files', 'ofd', 'locks', 'sock', 'msgq', 'shm', UNIQUE(month, day, year, hour, name))", NULL, 0, NULL);
 
-	sqlite3_exec(openvcp_db, 
+	sqlite3_exec(openvcp_db,
 		"CREATE TABLE vserver_traffic ('month', 'day', 'year', 'ip', 'in', 'out', 'total', 'name', UNIQUE(month, day, year, ip))"
 																, NULL, 0, NULL);
 	sqlite3_exec(openvcp_db, "ALTER TABLE vserver_traffic ADD COLUMN 'name'", NULL, 0, NULL);
-								
-	sqlite3_exec(openvcp_db, 
+
+	sqlite3_exec(openvcp_db,
 		"CREATE TABLE node_traffic ('month', 'day', 'year', 'in', 'out', 'total', UNIQUE(month, day, year))"
 													, NULL, 0, NULL);
-	sqlite3_exec(openvcp_db, 
+	sqlite3_exec(openvcp_db,
 		"CREATE TABLE vserver_hour_traffic ('month', 'hour', 'day', 'year', 'ip', 'in', 'out', 'total', 'name', UNIQUE(month, hour, day, year, ip))"
 																		, NULL, 0, NULL);
 	sqlite3_exec(openvcp_db, "ALTER TABLE vserver_hour_traffic ADD COLUMN 'name'", NULL, 0, NULL);
-								
-	sqlite3_exec(openvcp_db, 
+
+	sqlite3_exec(openvcp_db,
 		"CREATE TABLE node_hour_traffic ('month', 'hour', 'day', 'year', 'in', 'out', 'total', UNIQUE(month, hour, day, year))"
-								, NULL, 0, NULL);	
-	sqlite3_exec(openvcp_db, 
-		"CREATE TABLE iptable_rules ('direction', 'proto', 'srcip', 'srcport', 'destip', 'destport', 'target', 'match',	 'match-value'," 
-		"'name', 'id' INTEGER PRIMARY KEY AUTOINCREMENT, UNIQUE('direction', 'proto', 'srcip', 'srcport', 'destip', 'destport', 'match', 'match-value'))" , NULL, 0, NULL);	
+								, NULL, 0, NULL);
+	sqlite3_exec(openvcp_db,
+		"CREATE TABLE iptable_rules ('direction', 'proto', 'srcip', 'srcport', 'destip', 'destport', 'target', 'match',	 'match-value',"
+		"'name', 'id' INTEGER PRIMARY KEY AUTOINCREMENT, UNIQUE('direction', 'proto', 'srcip', 'srcport', 'destip', 'destport', 'match', 'match-value'))" , NULL, 0, NULL);
 
 	sqlite3_exec(openvcp_db, "CREATE TABLE vserver_bandwidth ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name', 'direction', 'rate', 'ceil', 'burst', 'cburst', UNIQUE(name, direction))", NULL, 0, NULL);
-	
+
 	sqlite3_exec(openvcp_db, "CREATE TABLE vserver_trafficlimit ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name', 'total', 'notify', 'type', 'notified', UNIQUE(name, type))", NULL, 0, NULL);
 
-	execb_cmd("modprobe", "modprobe", "ifb", NULL);	
-	
+	execb_cmd("modprobe", "modprobe", "ifb", NULL);
+
 	execb_cmd("modprobe", "modprobe", "ip_tables", NULL);
 
 	ovcp_create_chains();
@@ -538,18 +551,18 @@
 
 	execb_cmd("modprobe", "modprobe", "nf_conntrack_ftp", NULL);
 
-	
+
 	ifaces = split_string(global_settings.ifaces, ' ', &iface_count);
-	
+
 	loggerdata = (struct _packetlogger **) malloc(sizeof(struct _packetlogger **) * iface_count);
-	
+
 	i = 0;
-	
+
 	while(i != iface_count)
 	{
 		loggerdata[i] = logtraffic_attach_iface(ifaces[i]);
-		
-		ovcp_log(OVCP_DEBUG, "Attaching to IFace: %s", ifaces[i]);	
+
+		ovcp_log(OVCP_DEBUG, "Attaching to IFace: %s", ifaces[i]);
 		i++;
 	}
 
@@ -557,65 +570,71 @@
 
 	while(1)
 	{
-	
+
 		time ( &current_time );
 		timeinfo = localtime ( &current_time );
-	
 
+
 		timeinfo->tm_sec = 0;
 		timeinfo->tm_min = 0;
-		
+
 		rawtime = current_time;
 		rawtime = mktime(timeinfo);
-		
-		refresh_delay = rawtime + 3600 - current_time;	
-		
+
+		refresh_delay = rawtime + 3600 - current_time;
+
 		day_in = 0;
 		day_out = 0;
 		day_total = 0;
-	
+
 		node_in = 0;
 		node_out = 0;
 		node_total = 0;
-	
+
 		i = 0;
-		
+
 		parse_resources(openvcp_db, timeinfo->tm_mon+1, timeinfo->tm_hour, timeinfo->tm_mday, timeinfo->tm_year+1900);
-	
-	
+
+
 		while(i != iface_count)
 		{
 			iplist = vserver_iplist();
-		
+
 			pthread_mutex_lock (&loggerdata[i]->mutex);
-					
-			parse_traffic_tree(loggerdata[i]->root_node, openvcp_db, timeinfo, &node_in, &node_out, &node_total, iplist);	
-			
+
+			parse_traffic_tree(loggerdata[i]->root_node, openvcp_db, timeinfo, &node_in, &node_out, &node_total, iplist);
+
 			free_tree(loggerdata[i]->root_node);
 			loggerdata[i]->root_node = NULL;
-			
+
 			pthread_mutex_unlock (&loggerdata[i]->mutex);
-			
+
 			free_iplist(iplist);
-			
+
 			i++;
-		}		
-		
+		}
+
 		if(ifb_kernel_support())
 			ovcp_setup_trafficthrottling(openvcp_db);
 		else  ovcp_log(OVCP_WARNING, "ifb Kernel Module not found");
-		
+
 		/* VServer Hour Traffic */
 
-		snprintf(sql_query, BUF_SIZE, 
+		c = 0;
+
+		snprintf(sql_query, BUF_SIZE,
 				"INSERT OR REPLACE INTO node_hour_traffic VALUES(%d, %d, %d, %d, %lld, %lld, %lld)",
 				timeinfo->tm_mon+1, timeinfo->tm_hour, timeinfo->tm_mday, timeinfo->tm_year+1900,
 				node_in, node_out, node_total);
-		
-		ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
-		
-		ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
 
+		do {
+			c++;
+			ovcp_log(OVCP_DEBUG, "SQL QUERY[%i]: %s", c, sql_query);
+			ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+			if(ret != SQLITE_OK)
+				sleep(2);
+		} while (c <= 10 && ret != SQLITE_OK);
+
 		if(ret != SQLITE_OK)
 		{
 			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);
@@ -629,23 +648,28 @@
 
 		/* Node Day Traffic */
 
+		c = 0;
+
 		if(timeinfo->tm_hour != 0)
 			get_node_traffic(openvcp_db, timeinfo->tm_mday, timeinfo->tm_mon+1, timeinfo->tm_year+1900, &day_in, &day_out, &day_total);
-		
-		day_in += node_in; 
-		day_out += node_out; 
-		day_total += node_total; 			
-		
-		snprintf(sql_query, BUF_SIZE, 
+
+		day_in += node_in;
+		day_out += node_out;
+		day_total += node_total;
+
+		snprintf(sql_query, BUF_SIZE,
 				"INSERT OR REPLACE INTO node_traffic VALUES(%d, %d, %d, %lld, %lld, %lld)",
 				timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900,
 				day_in, day_out, day_total);
 
-		ovcp_log(OVCP_DEBUG, "SQL QUERY: %s", sql_query);
+		do {
+			c++;
+			ovcp_log(OVCP_DEBUG, "SQL QUERY[%i]: %s", c, sql_query);
+			ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
+			if(ret != SQLITE_OK)
+				sleep(2);
+		} while (c <= 10 && ret != SQLITE_OK);
 
-		
-		ret = sqlite3_exec(openvcp_db, sql_query, NULL, 0, &sql_error);
-
 		if(ret != SQLITE_OK)
 		{
 			ovcp_log(OVCP_ERROR, "SQL error (%s)", sql_error);
@@ -653,22 +677,21 @@
 			sqlite3_close(openvcp_db);
 			exit(-1);
 		}
-		
-	
+
 		ovcp_check_trafficlimits(openvcp_db);
-		
-		
+
+
 		ovcp_log(OVCP_DEBUG, "Next Refresh: [%d s]\n", refresh_delay);
-		
+
 		sleep(refresh_delay);
 	}
-	
 
 
 
-	
-	sqlite3_close(openvcp_db);	
-	
+
+
+	sqlite3_close(openvcp_db);
+
 }
 
 



More information about the Commits mailing list