Index: trunk/daemon/togad.py
===================================================================
--- trunk/daemon/togad.py	(revision 37)
+++ trunk/daemon/togad.py	(revision 38)
@@ -12,14 +12,14 @@
 import threading
 import mutex
+import random
 
 # Specify debugging level here;
 #
-# <=11 = metric XML
-# <=10 = host,cluster,grid,ganglia XML
-# <=9  = RRD activity,gmetad config parsing
-# <=8  = host processing
-# <=7  = daemon threading - NOTE: Daemon will 'halt on all errors' from this level
-#
-DEBUG_LEVEL = 10
+# 11 = XML: metrics
+# 10 = XML: host, cluster, grid, ganglia
+# 9  = RRD activity, gmetad config parsing
+# 8  = daemon threading
+#
+DEBUG_LEVEL = 8
 
 # Where is the gmetad.conf located
@@ -37,5 +37,5 @@
 # Amount of hours to store in one single archived .rrd
 #
-ARCHIVE_HOURS_PER_RRD = 12
+ARCHIVE_HOURS_PER_RRD = 1
 
 # Wether or not to run as a daemon in background
@@ -58,4 +58,5 @@
 
 class RRDMutator:
+	"A class for handling .rrd mutations"
 
 	binary = '/usr/bin/rrdtool'
@@ -84,5 +85,5 @@
 				arg_string = arg_string + ' ' + arg
 
-		debug_msg( 7, 'rrdm.perform(): ' + self.binary + ' ' + action + ' ' + arg_string  )
+		debug_msg( 9, 'rrdm.perform(): ' + self.binary + ' ' + action + ' ' + arg_string  )
 
 		for line in os.popen( self.binary + ' ' + action + ' ' + arg_string ).readlines():
@@ -91,5 +92,5 @@
 
 				error_msg = string.join( line.split( ' ' )[1:] )
-				debu_msg( 7, error_msg )
+				debug_msg( 9, error_msg )
 				return 1
 
@@ -158,6 +159,11 @@
 		for clustername, rrdh in self.clusters.items():
 
-			print 'storing metrics of cluster ' + clustername
-			rrdh.storeMetrics()
+			ret = rrdh.storeMetrics()
+
+			if ret:
+				debug_msg( 9, 'An error occured while storing metrics for cluster %s' %clustername )
+				return 1
+
+		return 0
 
 class GangliaXMLGatherer:
@@ -221,6 +227,10 @@
 		"Connect, and return a file object"
 
-		self.disconnect()
-		self.connect()
+		if self.s:
+			# Apearantly, only data is received when a connection is made
+			# therefor, disconnect and connect
+			#
+			self.disconnect()
+			self.connect()
 
 		return self.s.makefile( 'r' )
@@ -283,49 +293,6 @@
 		return time.strftime("%a %d %b %Y %H:%M:%S")
 
-	def grabXML( self ):
-
-		debug_msg( 7, self.printTime() + ' - mainthread() - xmlthread() started' )
-		pid = os.fork()
-
-		if pid == 0:
-			# Child - XML Thread
-			#
-			# Process XML and exit
-
-			debug_msg( 7, self.printTime() + ' - xmlthread()  - Start XML processing..' )
-			self.processXML()
-			debug_msg( 7, self.printTime() + ' - xmlthread()  - Done processing; exiting.' )
-			sys.exit( 0 )
-
-		elif pid > 0:
-			# Parent - Time/sleep Thread
-			#
-			# Make sure XML is processed on time and at regular intervals
-
-			debug_msg( 7, self.printTime() + ' - mainthread() - Sleep '+ str( self.config.getLowestInterval() ) +'s: zzzzz..' )
-			time.sleep( float( self.config.getLowestInterval() ) )
-			debug_msg( 7, self.printTime() + ' - mainthread() - Awoken: waiting for XML thread..' )
-
-			r = os.wait()
-			ret = r[1]
-			if ret != 0:
-				debug_msg( 7, self.printTime() + ' - mainthread() - Done waiting: ERROR! xmlthread() exited with status %d' %(ret) )
-				if DEBUG_LEVEL>=7: sys.exit( 1 )
-			else:
-
-				debug_msg( 7, self.printTime() + ' - mainthread() - Done waiting: xmlthread() finished succesfully' )
-
 	def run( self ):
 		"Main thread"
-
-		#self.processXML()
-		#self.storeMetrics()
-
-		#time.sleep( 20 )
-
-		#self.processXML()
-		#self.storeMetrics()
-
-		#sys.exit(1)
 
 		xmlthread = threading.Thread( None, self.processXML, 'xmlthread' )
@@ -340,5 +307,5 @@
 				#
 				xmlthread = threading.Thread( None, self.processXML, 'xmlthread' )
-				debug_msg( 7, self.printTime() + ' - mainthread() - xmlthread() started' )
+				debug_msg( 8, self.printTime() + ' - mainthread() - xmlthread() started' )
 				xmlthread.start()
 
@@ -349,5 +316,5 @@
 				#
 				storethread = threading.Thread( None, self.storeMetrics, 'storethread' )
-				debug_msg( 7, self.printTime() + ' - mainthread() - storethread() started' )
+				debug_msg( 8, self.printTime() + ' - mainthread() - storethread() started' )
 				storethread.start()
 		
@@ -358,7 +325,9 @@
 		"Store metrics retained in memory to disk"
 
-		STORE_INTERVAL = 5
-
-		debug_msg( 7, self.printTime() + ' - storethread() - Storing data..' )
+		# Store metrics somewhere between every 60 and 180 seconds
+		#
+		STORE_INTERVAL = random.randint( 60, 180 )
+
+		debug_msg( 8, self.printTime() + ' - storethread() - storemetricthread() started: Storing data..' )
 
 		# threaded call to: self.myHandler.storeMetrics()
@@ -367,15 +336,16 @@
 		storethread.start()
 
-		debug_msg( 7, self.printTime() + ' - storethread() - Sleeping.. (%ss)' %STORE_INTERVAL )
+		debug_msg( 8, self.printTime() + ' - storethread() - Sleeping.. (%ss)' %STORE_INTERVAL )
 		time.sleep( STORE_INTERVAL )
-		debug_msg( 7, self.printTime() + ' - storethread() - Done sleeping.' )
+		debug_msg( 8, self.printTime() + ' - storethread() - Done sleeping.' )
 
 		if storethread.isAlive():
 
-			debug_msg( 7, self.printTime() + ' - storethread() - storemetricthread() still running, waiting to finish..' )
+			debug_msg( 8, self.printTime() + ' - storethread() - storemetricthread() still running, waiting to finish..' )
 			parsethread.join( 180 ) # Maximum time is 3 minutes for storing thread to finish - more then enough
-			debug_msg( 7, self.printTime() + ' - storethread() - storemetricthread() finished.' )
-
-		debug_msg( 7, self.printTime() + ' - storethread() finished' )
+
+		debug_msg( 8, self.printTime() + ' - storethread() - storemetricthread() finished.' )
+
+		debug_msg( 8, self.printTime() + ' - storethread() finished' )
 
 		return 0
@@ -384,5 +354,5 @@
 		"Process XML"
 
-		debug_msg( 7, self.printTime() + ' - xmlthread() - Parsing..' )
+		debug_msg( 8, self.printTime() + ' - xmlthread() - parsethread() started: Parsing..' )
 
 		# threaded call to: self.myParser.parse( self.myXMLGatherer.getFileObject() )
@@ -391,15 +361,16 @@
 		parsethread.start()
 
-		debug_msg( 7, self.printTime() + ' - xmlthread() - Sleeping.. (%ss)' %self.config.getLowestInterval() )
+		debug_msg( 8, self.printTime() + ' - xmlthread() - Sleeping.. (%ss)' %self.config.getLowestInterval() )
 		time.sleep( float( self.config.getLowestInterval() ) )	
-		debug_msg( 7, self.printTime() + ' - xmlthread() - Done sleeping.' )
+		debug_msg( 8, self.printTime() + ' - xmlthread() - Done sleeping.' )
 
 		if parsethread.isAlive():
 
-			debug_msg( 7, self.printTime() + ' - xmlthread() - parsethread() still running, waiting to finish..' )
+			debug_msg( 8, self.printTime() + ' - xmlthread() - parsethread() still running, waiting to finish..' )
 			parsethread.join( 180 ) # Maximum time is 3 minutes for XML thread to finish - more then enough
-			debug_msg( 7, self.printTime() + ' - xmlthread() - parsethread() finished.' )
-
-		debug_msg( 7, self.printTime() + ' - xmlthread() finished.' )
+
+		debug_msg( 8, self.printTime() + ' - xmlthread() - parsethread() finished.' )
+
+		debug_msg( 8, self.printTime() + ' - xmlthread() finished.' )
 
 		return 0
@@ -513,21 +484,4 @@
 		self.slot.unlock()
 
-	def makeUpdateString( self, host, metricname ):
-
-		update_string = None
-
-		print self.myMetrics[ host ][ metricname ]
-
-		for m in self.myMetrics[ host ][ metricname ]:
-
-			print m
-
-			if not update_string:
-				update_string = '%s:%s' %( m['time'], m['val'] )
-			else:
-				update_string = update_string + ' %s:%s' %( m['time'], m['val'] )
-
-		return update_string
-
 	def makeUpdateList( self, host, metricname ):
 
@@ -558,6 +512,5 @@
 				else:
 					debug_msg( 9, 'metric update failed' )
-
-				return 1
+					return 1
 
 	def makeTimeSerial( self ):
@@ -693,21 +646,10 @@
 		rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial )
 
-		#timestamp = metric['time']
-		#val = metric['val']
-
-		#update_string = '%s:%s' %(timestamp, val)
 		update_list = self.makeUpdateList( host, metricname )
 
-		#try:
-
-		self.rrdm.update( str(rrd_file), update_list )
-
-		#except rrdtool.error, detail:
-
-		#	debug_msg( 0, 'EXCEPTION! While trying to update rrd:' )
-		#	debug_msg( 0, '\trrd %s with %s' %( str(rrd_file), update_string ) )
-		#	debug_msg( 0, str(detail) )
-
-		#	return 1
+		ret = self.rrdm.update( str(rrd_file), update_list )
+
+		if ret:
+			return 1
 		
 		debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), string.join( update_list ) ) )
