Index: trunk/daemon/togad.py
===================================================================
--- trunk/daemon/togad.py	(revision 46)
+++ trunk/daemon/togad.py	(revision 47)
@@ -55,4 +55,12 @@
 UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ]
 
+# Maximum time (in seconds) a parsethread may run
+#
+PARSE_TIMEOUT = 60
+
+# Maximum time (in seconds) a storethread may run
+#
+STORE_TIMEOUT = 360
+
 """
 This is TOrque-GAnglia's data Daemon
@@ -382,5 +390,5 @@
 
 			debug_msg( 7, self.printTime() + ' - storethread(): storemetricthread() still running, waiting to finish..' )
-			storethread.join( 180 ) # Maximum time is for storing thread to finish
+			storethread.join( STORE_TIMEOUT ) # Maximum time is for storing thread to finish
 			debug_msg( 7, self.printTime() + ' - storethread(): Done waiting.' )
 
@@ -414,5 +422,5 @@
 
 			debug_msg( 7, self.printTime() + ' - xmlthread(): parsethread() still running, waiting to finish..' )
-			parsethread.join( 180 ) # Maximum time for XML thread to finish
+			parsethread.join( PARSE_TIMEOUT ) # Maximum time for XML thread to finish
 			debug_msg( 7, self.printTime() + ' - xmlthread(): Done waiting.' )
 
@@ -507,4 +515,5 @@
 	myMetrics = { }
 	lastStored = { }
+	timeserials = { }
 	slot = None
 
@@ -540,4 +549,15 @@
 		for host in hosts:
 
+			host_dir = cluster_dir + '/' + host
+			dirlist = os.listdir( host_dir )
+
+			for dir in dirlist:
+
+				if not self.timeserials.has_key( host ):
+
+					self.timeserials[ host ] = [ ]
+
+				self.timeserials[ host ].append( dir )
+
 			last_serial = self.getLastRrdTimeSerial( host )
 			if last_serial:
@@ -589,29 +609,27 @@
 		# </atomic>
 
-	def makeUpdateList( self, host, metricname ):
+	def makeUpdateList( self, host, metriclist ):
 
 		update_list = [ ]
 		metric = None
 
-		while len( self.myMetrics[ host ][ metricname ] ) > 0:
+		while len( metriclist ) > 0:
 
 			# Kabouter pop
 			#
 			# <atomic>	
-			self.slot.acquire()
+			#self.slot.acquire()
 
 			# len might have changed since loop start
 			#
-			if len( self.myMetrics[ host ][ metricname ] ) > 0:
-				metric = self.myMetrics[ host ][ metricname ].pop( 0 )
-
-			self.slot.release()
+			if len( metriclist ) > 0:
+				metric = metriclist.pop( 0 )
+
+			#self.slot.release()
 			# </atomic>
 
 			if metric:
-				if self.checkStoreMetric( host, metricname, metric ):
+				if self.checkStoreMetric( host, metric ):
 					update_list.append( '%s:%s' %( metric['time'], metric['val'] ) )
-				#else:
-					#print 'allready wrote metric %s with timestamp %s' %( metric['name'], metric['time'] )
 
 		return update_list
@@ -621,7 +639,7 @@
 		if self.lastStored.has_key( host ):
 
-			if self.lastStored[ host ].has_key( metricname ):
-
-				if metric['time'] <= self.lastStored[ host ][ metricname ]:
+			if self.lastStored[ host ].has_key( metric['name'] ):
+
+				if metric['time'] <= self.lastStored[ host ][ metric['name'] ]:
 
 					# Allready wrote a value with this timestamp, skip tnx
@@ -631,5 +649,5 @@
 			self.lastStored[ host ] = { }
 
-		self.lastStored[ host ][ metricname ] = metric['time']
+		self.lastStored[ host ][ metric['name'] ] = metric['time']
 
 		return 1
@@ -641,16 +659,30 @@
 			for metricname, mymetric in mymetrics.items():
 
-				mytime = self.makeTimeSerial()
-				correct_serial = self.checkNewRrdPeriod( hostname, mytime )
-				self.createCheck( hostname, metricname, correct_serial )	
-				update_ret = self.update( hostname, metricname, correct_serial )
-
-				if update_ret == 0:
-
-					debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) )
-				else:
-					debug_msg( 9, 'metric update failed' )
-
-				#sys.exit(1)
+				#mytime = self.makeTimeSerial()
+				#serial = mymetric['time']
+				#correct_serial = self.checkNewRrdPeriod( hostname, mytime )
+
+				self.slot.acquire() 
+
+				# Create a mapping table, each metric to the period where it should be stored
+				#
+				metric_serial_table = self.determineSerials( hostname, metricname, mymetric )
+				self.myMetrics[ hostname ][ metricname ] = [ ]
+
+				self.slot.release()
+
+				for period, pmetric in metric_serial_table.items():
+
+					self.createCheck( hostname, metricname, period )	
+
+					update_ret = self.update( hostname, metricname, period, pmetric )
+
+					if update_ret == 0:
+
+						debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) )
+					else:
+						debug_msg( 9, 'metric update failed' )
+
+				sys.exit(1)
 
 	def makeTimeSerial( self ):
@@ -686,24 +718,18 @@
 		"""
 
-		rrd_dir, rrd_file = self.makeRrdPath( host )
-
 		newest_timeserial = 0
 
-		if os.path.exists( rrd_dir ):
-
-			dirlist = os.listdir( rrd_dir )
-
-			for dir in dirlist:
-
-				valid_dir = 1
-
-				for letter in dir:
-					if letter not in string.digits:
-						valid_dir = 0
-
-				if valid_dir:
-					timeserial = dir
-					if timeserial > newest_timeserial:
-						newest_timeserial = timeserial
+		for dir in self.timeserials[ host ]:
+
+			valid_dir = 1
+
+			for letter in dir:
+				if letter not in string.digits:
+					valid_dir = 0
+
+			if valid_dir:
+				timeserial = dir
+				if timeserial > newest_timeserial:
+					newest_timeserial = timeserial
 
 		if newest_timeserial:
@@ -712,4 +738,47 @@
 			return 0
 
+	def determinePeriod( self, host, check_serial ):
+
+		period_serial = 0
+
+		for serial in self.timeserials[ host ]:
+
+			if check_serial >= serial and period_serial < serial:
+
+				period_serial = serial
+
+		return period_serial
+
+	def determineSerials( self, host, metricname, metriclist ):
+		"""
+		Determine the correct serial and corresponding rrd to store
+		for a list of metrics
+		"""
+
+		metric_serial_table = { }
+
+		for metric in metriclist:
+
+			if metric['name'] == metricname:
+
+				period = self.determinePeriod( host, metric['time'] )	
+
+				archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60)
+
+				if (metric['time'] - period) > archive_secs:
+
+					# This one should get it's own new period
+					period = metric['time']
+
+				if not metric_serial_table.has_key( period ):
+
+					metric_serial_table = [ ]
+
+				metric_serial_table[ period ].append( metric )
+
+		print metric_serial_table
+
+		return metric_serial_table
+
 	def checkNewRrdPeriod( self, host, current_timeserial ):
 		"""
@@ -729,5 +798,5 @@
 			archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60)
 
-			if (current_timeserial - last_timeserial) >= archive_secs:
+			if (current_timeserial - last_timeserial) > archive_secs:
 				serial = current_timeserial
 			else:
@@ -753,5 +822,5 @@
 
 		debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) )
-
+		
 		rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial )
 
@@ -763,5 +832,5 @@
 
 			interval = self.config.getInterval( self.cluster )
-			heartbeat = 8 * int(interval)
+			heartbeat = 8 * int( interval )
 
 			params = [ ]
@@ -771,5 +840,5 @@
 
 			params.append( '--start' )
-			params.append( str( int( self.getFirstTime( host, metricname ) ) - 1 ) )
+			params.append( str( int( timeserial ) - 1 ) )
 
 			params.append( 'DS:sum:GAUGE:%d:U:U' %heartbeat )
@@ -780,5 +849,5 @@
 			debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) )
 
-	def update( self, host, metricname, timeserial ):
+	def update( self, host, metricname, timeserial, metriclist ):
 
 		debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) )
@@ -786,5 +855,5 @@
 		rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial )
 
-		update_list = self.makeUpdateList( host, metricname )
+		update_list = self.makeUpdateList( host, metriclist )
 
 		if len( update_list ) > 0:
