Tue Mar  4 17:03:17 GMT 2008  Dave Love <fx@gnu.org>
  * Merge from svn to revision 496.

Mon Dec 10 15:46:45 GMT 2007  Dave Love <fx@gnu.org>
  * Tidy/fix SGE changes.

Fri Dec  7 10:57:55 GMT 2007  Dave Love <fx@gnu.org>
  * Add note about SGE tasks to overview template.

Fri Nov  9 15:39:41 GMT 2007  Dave Love <fx@gnu.org>
  * Replace previous SGE implementation with a different one.
  This provides the full set of monarch data.
  There's some associated restructuring involving the PBS code.

Fri Nov  9 15:27:01 GMT 2007  Dave Love <fx@gnu.org>
  * jobmond.conf comment fix.

Fri Nov  9 15:21:25 GMT 2007  Dave Love <fx@gnu.org>
  * Somewhat modify RPM spec files.

Wed Nov  7 17:49:37 GMT 2007  Dave Love <fx@gnu.org>
  * Modify jobmond for use with SGE.

Wed Nov  7 17:45:24 GMT 2007  Dave Love <fx@gnu.org>
  * Reformat and update INSTALL file.

Wed Nov  7 17:44:38 GMT 2007  Dave Love <fx@gnu.org>
  * Add PATH example to init file defaults.

Index: pkg/deb/default/jobmond
===================================================================
--- pkg/deb/default/jobmond	(revision 496)
+++ pkg/deb/default/jobmond	(working copy)
@@ -1 +1,6 @@
 OPTIONS=""
+# If you use SGE, modify this as appropriate to find qstat.
+case $(uname -m) in
+    x86_64) PATH="${PATH}:/opt/sge/bin/lx26-amd64";;
+    i*86) PATH="${PATH}:/opt/sge/bin/lx26-x86";;
+esac
Index: pkg/rpm/jobmonarch-jobmond.spec
===================================================================
--- pkg/rpm/jobmonarch-jobmond.spec	(revision 496)
+++ pkg/rpm/jobmonarch-jobmond.spec	(working copy)
@@ -3,9 +3,9 @@
 Version: 
 Release: 
 Summary: Job Monitoring Daemon
-License: see /usr/share/doc/jobmonarch-jobmond/copyright
-Distribution: Debian
-Group: Converted/misc
+License: GPL
+Distribution: Fedora
+Group: Applications/System
 
 %define _rpmdir ../
 %define _rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
@@ -19,18 +19,18 @@
 if [ -x /etc/init.d/jobmond ]
 	then
 
+		chkconfig --add jobmond
 		chkconfig jobmond on
+		/etc/init.d/jobmond restart
 
 fi
 
-/etc/init.d/jobmond restart
-
-
 %preun
 #!/bin/sh
 
 /etc/init.d/jobmond stop
 chkconfig jobmond off
+chkconfig --del jobmond
 
 
 %description
Index: pkg/rpm/sysconfig/jobmond
===================================================================
--- pkg/rpm/sysconfig/jobmond	(revision 496)
+++ pkg/rpm/sysconfig/jobmond	(working copy)
@@ -1 +1,6 @@
 OPTIONS=""
+# If you use SGE, modify this as appropriate to find qstat.
+case $(uname -m) in
+    x86_64) PATH="${PATH}:/opt/sge/bin/lx26-amd64";;
+    i*86) PATH="${PATH}:/opt/sge/bin/lx26-x86";;
+esac
Index: pkg/rpm/jobmonarch-webfrontend.spec
===================================================================
--- pkg/rpm/jobmonarch-webfrontend.spec	(revision 496)
+++ pkg/rpm/jobmonarch-webfrontend.spec	(working copy)
@@ -2,10 +2,10 @@
 Name: jobmonarch-webfrontend
 Version: 
 Release: 
-Summary: Job MonArch's Web Frontend
-License: see /usr/share/doc/jobmonarch-webfrontend/copyright
-Distribution: Debian
-Group: Converted/misc
+Summary: Job MonArch Web Frontend
+License: GPL
+Distribution: Fedora
+Group: Applications/Internet
 
 %define _rpmdir ../
 %define _rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
@@ -32,40 +32,40 @@
 Job Monarch's web frontend.
 
 %files
-%dir "/var/www/ganglia/templates/job_monarch/"
-%dir "/var/www/ganglia/templates/job_monarch/images/"
-"/var/www/ganglia/templates/job_monarch/cluster_extra.tpl"
-"/var/www/ganglia/templates/job_monarch/host_extra.tpl"
-%dir "/var/www/ganglia/addons/job_monarch/"
-%dir "/var/www/ganglia/addons/job_monarch/clusterconf/"
-"/var/www/ganglia/addons/job_monarch/clusterconf/example.php"
-%dir "/var/www/ganglia/addons/job_monarch/templates/"
-"/var/www/ganglia/addons/job_monarch/templates/overview.tpl"
-"/var/www/ganglia/addons/job_monarch/templates/search.tpl"
-"/var/www/ganglia/addons/job_monarch/templates/footer.tpl"
-"/var/www/ganglia/addons/job_monarch/templates/header.tpl"
-"/var/www/ganglia/addons/job_monarch/templates/host_view.tpl"
-"/var/www/ganglia/addons/job_monarch/templates/index.tpl"
-%config "/var/www/ganglia/addons/job_monarch/conf.php"
-"/var/www/ganglia/addons/job_monarch/search.php"
-"/var/www/ganglia/addons/job_monarch/libtoga.php"
-"/var/www/ganglia/addons/job_monarch/version.php"
-"/var/www/ganglia/addons/job_monarch/cal.gif"
-"/var/www/ganglia/addons/job_monarch/document_archive.jpg"
-"/var/www/ganglia/addons/job_monarch/graph.php"
-"/var/www/ganglia/addons/job_monarch/header.php"
-"/var/www/ganglia/addons/job_monarch/host_view.php"
-"/var/www/ganglia/addons/job_monarch/image.php"
-"/var/www/ganglia/addons/job_monarch/libtoga.js"
-"/var/www/ganglia/addons/job_monarch/logo_ned.gif"
-"/var/www/ganglia/addons/job_monarch/next.gif"
-"/var/www/ganglia/addons/job_monarch/prev.gif"
-"/var/www/ganglia/addons/job_monarch/redcross.jpg"
-"/var/www/ganglia/addons/job_monarch/ts_picker.js"
-"/var/www/ganglia/addons/job_monarch/ts_validatetime.js"
-"/var/www/ganglia/addons/job_monarch/footer.php"
-"/var/www/ganglia/addons/job_monarch/styles.css"
-"/var/www/ganglia/addons/job_monarch/index.php"
-"/var/www/ganglia/addons/job_monarch/overview.php"
-"/var/www/ganglia/addons/job_monarch/jobmonarch.gif"
-"/var/www/ganglia/templates/job_monarch/images/logo.jpg"
+%dir "/usr/share/ganglia/templates/job_monarch/"
+%dir "/usr/share/ganglia/templates/job_monarch/images/"
+"/usr/share/ganglia/templates/job_monarch/cluster_extra.tpl"
+"/usr/share/ganglia/templates/job_monarch/host_extra.tpl"
+%dir "/usr/share/ganglia/addons/job_monarch/"
+%dir "/usr/share/ganglia/addons/job_monarch/clusterconf/"
+"/usr/share/ganglia/addons/job_monarch/clusterconf/example.php"
+%dir "/usr/share/ganglia/addons/job_monarch/templates/"
+"/usr/share/ganglia/addons/job_monarch/templates/overview.tpl"
+"/usr/share/ganglia/addons/job_monarch/templates/search.tpl"
+"/usr/share/ganglia/addons/job_monarch/templates/footer.tpl"
+"/usr/share/ganglia/addons/job_monarch/templates/header.tpl"
+"/usr/share/ganglia/addons/job_monarch/templates/host_view.tpl"
+"/usr/share/ganglia/addons/job_monarch/templates/index.tpl"
+%config "/usr/share/ganglia/addons/job_monarch/conf.php"
+"/usr/share/ganglia/addons/job_monarch/search.php"
+"/usr/share/ganglia/addons/job_monarch/libtoga.php"
+"/usr/share/ganglia/addons/job_monarch/version.php"
+"/usr/share/ganglia/addons/job_monarch/cal.gif"
+"/usr/share/ganglia/addons/job_monarch/document_archive.jpg"
+"/usr/share/ganglia/addons/job_monarch/graph.php"
+"/usr/share/ganglia/addons/job_monarch/header.php"
+"/usr/share/ganglia/addons/job_monarch/host_view.php"
+"/usr/share/ganglia/addons/job_monarch/image.php"
+"/usr/share/ganglia/addons/job_monarch/libtoga.js"
+"/usr/share/ganglia/addons/job_monarch/logo_ned.gif"
+"/usr/share/ganglia/addons/job_monarch/next.gif"
+"/usr/share/ganglia/addons/job_monarch/prev.gif"
+"/usr/share/ganglia/addons/job_monarch/redcross.jpg"
+"/usr/share/ganglia/addons/job_monarch/ts_picker.js"
+"/usr/share/ganglia/addons/job_monarch/ts_validatetime.js"
+"/usr/share/ganglia/addons/job_monarch/footer.php"
+"/usr/share/ganglia/addons/job_monarch/styles.css"
+"/usr/share/ganglia/addons/job_monarch/index.php"
+"/usr/share/ganglia/addons/job_monarch/overview.php"
+"/usr/share/ganglia/addons/job_monarch/jobmonarch.gif"
+"/usr/share/ganglia/templates/job_monarch/images/logo.jpg"
Index: pkg/rpm/jobmonarch-jobarchived.spec
===================================================================
--- pkg/rpm/jobmonarch-jobarchived.spec	(revision 496)
+++ pkg/rpm/jobmonarch-jobarchived.spec	(working copy)
@@ -3,9 +3,9 @@
 Version: 
 Release: 
 Summary: Job Archiving Daemon
-License: see /usr/share/doc/jobmonarch-jobarchived/copyright
-Distribution: Debian
-Group: Converted/misc
+License: GPL
+Distribution: Fedora
+Group: Applications/System
 
 %define _rpmdir ../
 %define _rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
@@ -19,6 +19,7 @@
 if [ -x /etc/init.d/jobarchived ]
 	then
 
+                chkconfig --add jobarchived
 		chkconfig jobarchived on
 
 fi
@@ -40,6 +41,7 @@
 
 /etc/init.d/jobarchived stop
 chkconfig jobarchived off
+chkconfig --del jobarchived
 
 
 %description
Index: INSTALL
===================================================================
--- INSTALL	(revision 496)
+++ INSTALL	(working copy)
@@ -1,70 +1,90 @@
 DESCRIPTION
 ===========
 
-	Job Monarch is a set of tools to monitor and optionally archive (batch)job information.
+	Job Monarch is a set of tools to monitor and optionally archive
+	(batch)job information.
 
-	It is a addon for the Ganglia monitoring system and plugs in to a existing Ganglia setup.
+	It is a addon for the Ganglia monitoring system and plugs in to a
+	existing Ganglia setup.
 
-	To view a operational setup with Job Monarch, have a look here: http://ganglia.sara.nl/
+	To view a operational setup with Job Monarch, have a look here:
+	http://ganglia.sara.nl/
 
 
-	Job Monarch stands for 'Job Monitoring and Archiving' tool and consists of three (3) components:
+	Job Monarch stands for 'Job Monitoring and Archiving' tool and
+	consists of three (3) components:
 
 	* jobmond
 
 		The Job Monitoring Daemon.
 		  
-		Gathers PBS/Torque batch statistics on jobs/nodes and submits them into
-		Ganglia's XML stream.
+		Gathers PBS/Torque or SGE batch statistics on
+		jobs/nodes and submits them into Ganglia's XML stream.
 
-		Through this daemon, users are able to view the PBS/Torque batch system and the
-		jobs/nodes that are in it (be it either running or queued).
+		Through this daemon, users are able to view the batch
+		system and the jobs/nodes that are in it (be it either
+		running or queued).
 
 	* jobarchived (optionally)
 
 		The Job Archiving Daemon.
 
-		Listens to Ganglia's XML stream and archives the job and node statistics.
-		It stores the job statistics in a Postgres SQL database and the node statistics
-		in RRD files.
+		Listens to Ganglia's XML stream and archives the job and node
+		statistics.  It stores the job statistics in a Postgres SQL
+		database and the node statistics in RRD files.
 		
-		Through this daemon, users are able to lookup a old/finished job
-		and view all it's statistics.
+		Through this daemon, users are able to lookup a old/finished
+		job and view all it's statistics.
 
-		Optionally: You can either choose to use this daemon if your users have use for it.
-		As it can be a heavy application to run and not everyone may have a need for it.
+		Optionally: You can either choose to use this daemon if your
+		users have use for it.
+		As it can be a heavy application to run and not everyone may
+		have a need for it.
 
-		- Multithreaded:	Will not miss any data regardless of (slow) storage
+		- Multithreaded:	Will not miss any data regardless of
+					(slow) storage
 
 		- Staged writing:	Spread load over bigger time periods
 
-		- High precision RRDs:	Allow for zooming on old periods with large precision
+		- High precision RRDs:	Allow for zooming on old periods with
+					large precision
 
-		- Timeperiod RRDs:	Allow for smaller number of files while still keeping advantage
+		- Timeperiod RRDs:	Allow for smaller number of files
+					while still keeping advantage
 					of small disk space
 		
 	* web
 
 		The Job Monarch web interface.
 
-		This interfaces with the jobmond data and (optionally) the jobarchived and presents the
-		data and graphs.
+		This interfaces with the jobmond data and (optionally) the
+		jobarchived and presents the data and graphs.
 
-		It does this in a similar layout/setup as Ganglia itself, so the navigation and usage is intuitive.
+		It does this in a similar layout/setup as Ganglia itself, so
+		the navigation and usage is intuitive.
 
-		- Graphical usage:	Displays graphical cluster overview so you can see the cluster (job) state
-					in one view/image and additional pie chart with relevant information on your
-					current view
+		- Graphical usage:	Displays graphical cluster overview so
+					you can see the cluster (job) state
+					in one view/image and additional pie
+					chart with relevant information on
+					your current view
 
-		- Filters:		Ability to filter output to limit information displayed (usefull for those
-					clusters with 500+ jobs). This also filters the graphical overview images output
-					and pie chart so you only see the filter relevant data
+		- Filters:		Ability to filter output to limit
+					information displayed (usefull for
+					those clusters with 500+ jobs). This
+					also filters the graphical overview
+					images output and pie chart so you
+					only see the filter relevant data
 
-		- Archive:		When enabling jobarchived, users can go back as far as recorded in the database
-					or archived RRDs to find out what happened to a crashed or old job
+		- Archive:		When enabling jobarchived, users can
+					go back as far as recorded in the
+					database or archived RRDs to find out
+					what happened to a crashed or old job
 
-		- Zoom ability:		Users can zoom into a timepriod as small as the smallest grain of the RRDS
-					(typically up to 10 seconds) when a jobarchived is present
+		- Zoom ability:		Users can zoom into a timepriod as
+					small as the smallest grain of the
+					RRDS (typically up to 10 seconds) when
+					a jobarchived is present
 
 REQUIREMENTS
 ============
@@ -75,12 +95,15 @@
 
 	jobmond:
 
-		- pbs_python v2.8.2 or higher
+		- pbs_python v2.8.2 or higher (if using PBS)
 		  https://subtrac.sara.nl/oss/pbs_python/
 
 		- gmond v3.0.1 or higher
 		  http://www.ganglia.info/
 
+                - For SGE, this is only tested on version 6.0u8
+                  upwards.  It may need changes for 6.1
+
 	jobarchived:
 
 		- Postgres SQL v7.xx
@@ -116,10 +139,11 @@
 INSTALLATION
 ============
 
-	Prior to installing the software make sure you meet the necessary requirements as
-	mentioned above.
+	Prior to installing the software make sure you meet the necessary
+	requirements as mentioned above.
 
-	NOTE: You can choose to install to other path/directories if your setup is different.
+	NOTE: You can choose to install to other path/directories if your
+	setup is different.
 
 	* jobmond
 
@@ -195,15 +219,19 @@
 
 		   ( see config comments for syntax and explanation )
 
+		3. If using SGE, make sure that qstat(1) is on your
+                   path, possibly by editing /etc/sysconfig/jobmond
+                   (RedHat) or /etc/default/jobmond (Debian).
+
 START
 =====
 
 	* jobmond
 
 		The Job Monitor has to be run on a machine that is allowed to
-		query the PBS/Torque server.
-		Make sure that if you have 'acl_hosts' enabled on your PBS/Torque
-		server that jobmond's machine is in it.
+		query the batch system (PBS/Torque server or SGE qmaster).
+		Make sure that if you have 'acl_hosts' enabled on your
+		PBS/Torque server, jobmond's machine is in it.
 
 		1. Start the Job Monitor:
 
@@ -218,7 +246,8 @@
 	* web
 
 		Doesn't require you to (re)start anything.
-		( make sure the Postgres database is running though )
+		( make sure the Postgres database is running though,
+		if using jobarchived )
 
 CONTACT
 =======
Index: jobmond/jobmond.py
===================================================================
--- jobmond/jobmond.py	(revision 496)
+++ jobmond/jobmond.py	(working copy)
@@ -3,6 +3,7 @@
 # This file is part of Jobmonarch
 #
 # Copyright (C) 2006-2007  Ramon Bastiaans
+# Copyright (C) 2007  Dave Love  (SGE code)
 # 
 # Jobmonarch is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -22,9 +23,7 @@
 #
 
 import sys, getopt, ConfigParser, time, os, socket, string, re
-import xdrlib, socket, syslog, xml, xml.sax
-from xml.sax import saxutils, make_parser
-from xml.sax import make_parser
+import xdrlib, syslog, xml, xml.sax
 from xml.sax.handler import feature_namespaces
 
 VERSION='0.3'
@@ -251,10 +250,15 @@
 			sys.exit( 1 )
 		else:
 
-			debug_msg( 0, "ERROR: GMETRIC_TARGET not set: internel Gmetric handling aborted. Failing back to DEPRECATED use of gmond.conf/gmetric binary. This will slow down jobmond significantly!" )
+			debug_msg( 0, "ERROR: GMETRIC_TARGET not set: internal Gmetric handling aborted. Failing back to DEPRECATED use of gmond.conf/gmetric binary. This will slow down jobmond significantly!" )
 
 	return True
 
+def fqdn_parts (fqdn):
+	"""Return pair of host and domain for fully-qualified domain name arg."""
+	parts = fqdn.split (".")
+	return (parts[0], string.join(parts[1:], "."))
+
 METRIC_MAX_VAL_LEN = 900
 
 class DataProcessor:
@@ -382,7 +386,7 @@
 
 			except NameError:
 
-				debug_msg( 10, 'Assuming /etc/gmond.conf for gmetric cmd (ommitting)' )
+				debug_msg( 10, 'Assuming /etc/gmond.conf for gmetric cmd (omitting)' )
 
 			cmd = cmd + ' -n' + str( metricname )+ ' -v"' + str( metricval )+ '" -t' + str( valtype ) + ' -d' + str( self.dmax )
 
@@ -420,6 +424,171 @@
 
 			print '\t%s = %s' %( name, val )
 
+	def getAttr( self, attrs, name ):
+
+		"""Return certain attribute from dictionary, if exists"""
+
+		if attrs.has_key( name ):
+
+			return attrs[ name ]
+		else:
+			return ''
+
+	def jobDataChanged( self, jobs, job_id, attrs ):
+
+		"""Check if job with attrs and job_id in jobs has changed"""
+
+		if jobs.has_key( job_id ):
+
+			oldData = jobs[ job_id ]	
+		else:
+			return 1
+
+		for name, val in attrs.items():
+
+			if oldData.has_key( name ):
+
+				if oldData[ name ] != attrs[ name ]:
+
+					return 1
+
+			else:
+				return 1
+
+		return 0
+
+	def submitJobData( self ):
+
+		"""Submit job info list"""
+
+		self.dp.multicastGmetric( 'MONARCH-HEARTBEAT', str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) )
+
+		running_jobs	= 0
+		queued_jobs	= 0
+
+		# Count how many running/queued jobs we found
+		#
+		for jobid, jobattrs in self.jobs.items():
+
+			if jobattrs[ 'status' ] == 'Q':
+
+				queued_jobs += 1
+
+			elif jobattrs[ 'status' ] == 'R':
+
+				running_jobs += 1
+
+		# Report running/queued jobs as seperate metric for a nice RRD graph
+		#
+		self.dp.multicastGmetric( 'MONARCH-RJ', str( running_jobs ), 'uint32', 'jobs' )
+		self.dp.multicastGmetric( 'MONARCH-QJ', str( queued_jobs ), 'uint32', 'jobs' )
+
+		# Now let's spread the knowledge
+		#
+		for jobid, jobattrs in self.jobs.items():
+
+			# Make gmetric values for each job: respect max gmetric value length
+			#
+			gmetric_val		= self.compileGmetricVal( jobid, jobattrs )
+			metric_increment	= 0
+
+			# If we have more job info than max gmetric value length allows, split it up
+			# amongst multiple metrics
+			#
+			for val in gmetric_val:
+
+				self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), val )
+
+				# Increase follow number if this jobinfo is split up amongst more than 1 gmetric
+				#
+				metric_increment	= metric_increment + 1
+
+	def compileGmetricVal( self, jobid, jobattrs ):
+
+		"""Create a val string for gmetric of jobinfo"""
+
+		gval_lists	= [ ]
+		val_list	= { }
+
+		for val_name, val_value in jobattrs.items():
+
+			# These are our own metric names, i.e.: status, start_timestamp, etc
+			#
+			val_list_names_len	= len( string.join( val_list.keys() ) ) + len(val_list.keys())
+
+			# These are their corresponding values
+			#
+			val_list_vals_len	= len( string.join( val_list.values() ) ) + len(val_list.values())
+
+			if val_name == 'nodes' and jobattrs['status'] == 'R':
+
+				node_str = None
+
+				for node in val_value:
+
+					if node_str:
+
+						node_str = node_str + ';' + node
+					else:
+						node_str = node
+
+					# Make sure if we add this new info, that the total metric's value length does not exceed METRIC_MAX_VAL_LEN
+					#
+					if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(node_str) ) > METRIC_MAX_VAL_LEN:
+
+						# It's too big, we need to make a new gmetric for the additional info
+						#
+						val_list[ val_name ]	= node_str
+
+						gval_lists.append( val_list )
+
+						val_list		= { }
+						node_str		= None
+
+				val_list[ val_name ]	= node_str
+
+				gval_lists.append( val_list )
+
+				val_list		= { }
+
+			elif val_value != '':
+
+				# Make sure if we add this new info, that the total metric's value length does not exceed METRIC_MAX_VAL_LEN
+				#
+				if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(str(val_value)) ) > METRIC_MAX_VAL_LEN:
+
+					# It's too big, we need to make a new gmetric for the additional info
+					#
+					gval_lists.append( val_list )
+
+					val_list		= { }
+
+				val_list[ val_name ]	= val_value
+
+		if len( val_list ) > 0:
+
+			gval_lists.append( val_list )
+
+		str_list	= [ ]
+
+		# Now append the value names and values together, i.e.: stop_timestamp=value, etc
+		#
+		for val_list in gval_lists:
+
+			my_val_str	= None
+
+			for val_name, val_value in val_list.items():
+
+				if my_val_str:
+
+					my_val_str = my_val_str + ' ' + val_name + '=' + val_value
+				else:
+					my_val_str = val_name + '=' + val_value
+
+			str_list.append( my_val_str )
+
+		return str_list
+
         def daemon( self ):
 
                 """Run as daemon forever"""
@@ -467,125 +636,276 @@
 			self.submitJobData()
 			time.sleep( BATCH_POLL_INTERVAL )	
 
-class SgeQstatXMLParser(xml.sax.handler.ContentHandler):
+# SGE code by Dave Love <fx@gnu.org>.  Tested with SGE 6.0u8 and 6.0u11.
+# Probably needs modification for SGE 6.1.  See also the fixmes.
 
-	"""Babu Sundaram's experimental SGE qstat XML parser"""
+class NoJobs (Exception):
+	"""Exception raised by empty job list in qstat output."""
+	pass
 
-	def __init__(self, qstatinxml):
+class SgeQstatXMLParser(xml.sax.handler.ContentHandler):
+	"""SAX handler for XML output from Sun Grid Engine's `qstat'."""
 
-		self.qstatfile = qstatinxml
-		self.attribs = {}
-		self.value = ''
-		self.jobID = ''
-		self.currentJobInfo = ''
-		self.job_list = []
-		self.EOFFlag = 0
-		self.jobinfoCount = 0
+	def __init__(self):
+		self.value = ""
+		self.joblist = []
+		self.job = {}
+		self.queue = ""
+		self.in_joblist = False
+		self.lrequest = False
+		xml.sax.handler.ContentHandler.__init__(self)
 
+	# The structure of the output is as follows.  Unfortunately
+	# it's voluminous, and probably doesn't scale to large
+	# clusters/queues.
 
-	def startElement(self, name, attrs):
+	# <detailed_job_info  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+	#   <djob_info>
+	#     <qmaster_response>  <!-- job -->
+	#       ...
+	#       <JB_ja_template>  
+	#         <ulong_sublist>
+	#         ...             <!-- start_time, state ... -->
+	#         </ulong_sublist>
+	#       </JB_ja_template>  
+	#       <JB_ja_tasks>
+	#         <ulong_sublist>
+	#           ...           <!-- task info
+	#         </ulong_sublist>
+	#         ...
+	#       </JB_ja_tasks>
+	#       ...
+	#     </qmaster_response>
+	#   </djob_info>
+	#   <messages>
+	#   ...
 
-		if name == 'job_list':
-			self.currentJobInfo = 'Status=' + attrs.get('state', None) + ' '
-		elif name == 'job_info':
-			self.job_list = []
-			self.jobinfoCount += 1
+	# NB.  We might treat each task as a separate job, like
+	# straight qstat output, but the web interface expects jobs to
+	# be identified by integers, not, say, <job number>.<task>.
 
-	def characters(self, ch):
+	# So, I lied.  If the job list is empty, we get invalid XML
+	# like this, which we need to defend against:
 
-		self.value = self.value + ch
+	# <unknown_jobs  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+	#   <>
+	#     <ST_name>*</ST_name>
+	#   </>
+	# </unknown_jobs>
 
-	def endElement(self, name):
+	def startElement(self, name, attrs):
+		self.value = ""
+		if name == "djob_info":	# job list
+			self.in_joblist = True
+		elif name == "qmaster_response" and self.in_joblist: # job
+			self.job = {"job_state": "U", "slots": 0,
+				    "nodes": [], "queued_timestamp": "",
+				    "queued_timestamp": "", "queue": "",
+				    "ppn": "0", "RN_max": 0,
+				    # fixme in endElement
+				    "requested_memory": 0, "requested_time": 0
+				    }
+			self.joblist.append(self.job)
+		elif name == "qstat_l_requests": # resource request
+			self.lrequest = True
+		elif name == "unknown_jobs":
+			raise NoJobs
 
-		if len(self.value.strip()) > 0 :
+	def characters(self, ch):
+		self.value += ch
 
-			self.currentJobInfo += name + '=' + self.value.strip() + ' '         
-		elif name != 'job_list':
+	def endElement(self, name): 
+		"""Snarf job elements contents into job dictionary.
+		   Translate keys if appropriate."""
 
-			self.currentJobInfo += name + '=Unknown '
+		name_trans = {
+		  "JB_job_number": "number",
+		  "JB_job_name": "name", "JB_owner": "owner",
+		  "queue_name": "queue", "JAT_start_time": "start_timestamp",
+		  "JB_submission_time": "queued_timestamp"
+		  }
+		value = self.value
 
-		if name == 'JB_job_number':
+		if name == "djob_info":
+			self.in_joblist = False
+			self.job = {}
+		elif name == "JAT_master_queue":
+			self.job["queue"] = value.split("@")[0]
+		elif name == "JG_qhostname":
+			if not (value in self.job["nodes"]):
+				self.job["nodes"].append(value)
+		elif name == "JG_slots": # slots in use
+			self.job["slots"] += int(value)
+		elif name == "RN_max": # requested slots (tasks or parallel)
+			self.job["RN_max"] = max (self.job["RN_max"],
+						  int(value))
+		elif name == "JAT_state": # job state (bitwise or)
+			value = int (value)
+			# Status values from sge_jobL.h
+			#define JIDLE                   0x00000000
+			#define JHELD                   0x00000010
+			#define JMIGRATING              0x00000020
+			#define JQUEUED                 0x00000040
+			#define JRUNNING                0x00000080
+			#define JSUSPENDED              0x00000100
+			#define JTRANSFERING            0x00000200
+			#define JDELETED                0x00000400
+			#define JWAITING                0x00000800
+			#define JEXITING                0x00001000
+			#define JWRITTEN                0x00002000
+			#define JSUSPENDED_ON_THRESHOLD 0x00010000
+			#define JFINISHED               0x00010000
+			if value & 0x80:
+				self.job["status"] = "R"
+			elif value & 0x40:
+				self.job["status"] = "Q"
+			else:
+				self.job["status"] = "O" # `other'
+		elif name == "CE_name" and self.lrequest and self.value in \
+			    ("h_cpu", "s_cpu", "cpu", "h_core", "s_core"):
+			# We're in a container for an interesting resource
+			# request; record which type.
+			self.lrequest = self.value
+		elif name == "CE_doubleval" and self.lrequest:
+			# if we're in a container for an interesting
+			# resource request, use the maxmimum of the hard
+			# and soft requests to record the requested CPU
+			# or core.  Fixme:  I'm not sure if this logic is
+			# right.
+			if self.lrequest in ("h_core", "s_core"):
+				self.job["requested_memory"] = \
+				    max (float (value),
+					 self.job["requested_memory"])
+			# Fixme:  Check what cpu means, c.f [hs]_cpu.
+			elif self.lrequest in ("h_cpu", "s_cpu", "cpu"):
+				self.job["requested_time"] = \
+				    max (float (value),
+					 self.job["requested_time"])
+		elif name == "qstat_l_requests":
+			self.lrequest = False
+		elif self.job and self.in_joblist:
+			if name in name_trans:
+				name = name_trans[name]
+				self.job[name] = value
 
-			self.jobID = self.value.strip()
-			self.job_list.append(self.jobID)          
+# Abstracted from PBS original.
+# Fixme:  Is it worth (or appropriate for PBS) sorting the result?
+def do_nodelist (nodes):
+	"""Translate node list as appropriate."""
+	nodeslist		= [ ]
+	my_domain = fqdn_parts(socket.getfqdn())[1]
+	for node in nodes:
+		host		= node.split( '/' )[0] # not relevant for SGE
+		h, host_domain	= fqdn_parts(host)
+		if host_domain == my_domain:
+			host	= h
+		if nodeslist.count( host ) == 0:
+			for translate_pattern in BATCH_HOST_TRANSLATE:
+				if translate_pattern.find( '/' ) != -1:
+					translate_orig	= \
+					    translate_pattern.split( '/' )[1]
+					translate_new	= \
+					    translate_pattern.split( '/' )[2]
+					host = re.sub( translate_orig,
+						       translate_new, host )
+			if not host in nodeslist:
+				nodeslist.append( host )
+	return nodeslist
 
-		if name == 'job_list':
-
-			if self.attribs.has_key(self.jobID) == False:
-				self.attribs[self.jobID] = self.currentJobInfo
-			elif self.attribs.has_key(self.jobID) and self.attribs[self.jobID] != self.currentJobInfo:
-				self.attribs[self.jobID] = self.currentJobInfo
-			self.currentJobInfo = ''
-			self.jobID = ''
-
-		elif name == 'job_info' and self.jobinfoCount == 2:
-
-			deljobs = []
-			for id in self.attribs:
-				try:
-					self.job_list.index(str(id))
-				except ValueError:
-					deljobs.append(id)
-			for i in deljobs:
-				del self.attribs[i]
-			deljobs = []
-			self.jobinfoCount = 0
-
-		self.value = ''
-
 class SgeDataGatherer(DataGatherer):
 
-	jobs = { }
-	SGE_QSTAT_XML_FILE	= '/tmp/.jobmonarch.sge.qstat'
+	jobs = {}
 
 	def __init__( self ):
-		"""Setup appropriate variables"""
-
-		self.jobs = { }
+		self.jobs = {}
 		self.timeoffset = 0
 		self.dp = DataProcessor()
-		self.initSgeJobInfo()
 
-	def initSgeJobInfo( self ):
-		"""This is outside the scope of DRMAA; Get the current jobs in SGE"""
-		"""This is a hack because we cant get info about jobs beyond"""
-		"""those in the current DRMAA session"""
-
-		self.qstatparser = SgeQstatXMLParser( self.SGE_QSTAT_XML_FILE )
-
-		# Obtain the qstat information from SGE in XML format
-		# This would change to DRMAA-specific calls from 6.0u9
-
-	def getJobData(self):
+	def getJobData( self ):
 		"""Gather all data on current jobs in SGE"""
 
-		# Get the information about the current jobs in the SGE queue
-		info = os.popen("qstat -ext -xml").readlines()
-		f = open(self.SGE_QSTAT_XML_FILE,'w')
-		for lines in info:
-			f.write(lines)
-		f.close()
+		import popen2
 
-		# Parse the input
-		f = open(self.qstatparser.qstatfile, 'r')
-		xml.sax.parse(f, self.qstatparser)
-		f.close()
-
+		self.cur_time = 0
+		queues = ""
+		if QUEUE:	# only for specific queues
+			# Fixme:  assumes queue names don't contain single
+			# quote or comma.  Don't know what the SGE rules are.
+			queues = " -q '" + string.join (QUEUE, ",") + "'"
+		# Note the comment in SgeQstatXMLParser about scaling with
+		# this method of getting data.  I haven't found better one.
+		# Output with args `-xml -ext -f -r' is easier to parse
+		# in some ways, harder in others, but it doesn't provide
+		# the submission time, at least.
+		piping = popen2.Popen3("qstat -u '*' -j '*' -xml" + queues,
+				       True)
+		qstatparser = SgeQstatXMLParser()
+		parse_err = 0
+		try:
+			xml.sax.parse(piping.fromchild, qstatparser)
+		except NoJobs:
+			pass
+		except:
+			parse_err = 1
+	       	if piping.wait():
+			debug_msg(10,
+				  "qstat error, skipping until next polling interval: "
+				  + piping.childerr.readline())
+			return None
+		elif parse_err:
+			debug_msg(10, "Bad XML output from qstat"())
+			exit (1)
+		for f in piping.fromchild, piping.tochild, piping.childerr:
+			f.close()
 		self.cur_time = time.time()
+		jobs_processed = []
+		for job in qstatparser.joblist:
+			job_id = job["number"]
+			if job["status"] in [ 'Q', 'R' ]:
+				jobs_processed.append(job_id)
+			if job["status"] == "R":
+				job["nodes"] = do_nodelist (job["nodes"])
+				# Fixme: Is this right?
+				job["ppn"] = float(job["slots"]) / \
+				    len(job["nodes"])
+				if DETECT_TIME_DIFFS:
+					# If a job start is later than our
+					# current date, that must mean
+					# the SGE server's time is later
+					# than our local time.
+					start_timestamp = \
+					    int (job["start_timestamp"])
+					if start_timestamp > \
+						    int(self.cur_time) + \
+						    int(self.timeoffset):
 
-		return self.qstatparser.attribs
+						self.timeoffset	= \
+						    start_timestamp - \
+						    int(self.cur_time)
+			else:
+				# fixme: Note sure what this should be:
+				job["ppn"] = job["RN_max"]
+				job["nodes"] = "1"
 
-	def submitJobData(self):
-		"""Submit job info list"""
+			myAttrs = {}
+			for attr in ["name", "queue", "owner",
+				     "requested_time", "status",
+				     "requested_memory", "ppn",
+				     "start_timestamp", "queued_timestamp"]:
+				myAttrs[attr] = str(job[attr])
+			myAttrs["nodes"] = job["nodes"]
+			myAttrs["reported"] = str(int(self.cur_time) + \
+						  int(self.timeoffset))
+			myAttrs["domain"] = fqdn_parts(socket.getfqdn())[1]
+			myAttrs["poll_interval"] = str(BATCH_POLL_INTERVAL)
 
-		self.dp.multicastGmetric( 'MONARCH-HEARTBEAT', str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) )
-		# Now let's spread the knowledge
-		#
-		metric_increment = 0
-		for jobid, jobattrs in self.qstatparser.attribs.items():
+			if self.jobDataChanged(self.jobs, job_id, myAttrs) \
+				    and myAttrs["status"] in ["R", "Q"]:
+				self.jobs[job_id] = myAttrs
+		for id, attrs in self.jobs.items():
+			if id not in jobs_processed:
+				del self.jobs[id]
 
-			self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), jobattrs)
-
 class PbsDataGatherer( DataGatherer ):
 
 	"""This is the DataGatherer for PBS and Torque"""
@@ -612,39 +932,6 @@
 		else:
 			self.pq		= PBSQuery()
 
-	def getAttr( self, attrs, name ):
-
-		"""Return certain attribute from dictionary, if exists"""
-
-		if attrs.has_key( name ):
-
-			return attrs[ name ]
-		else:
-			return ''
-
-	def jobDataChanged( self, jobs, job_id, attrs ):
-
-		"""Check if job with attrs and job_id in jobs has changed"""
-
-		if jobs.has_key( job_id ):
-
-			oldData = jobs[ job_id ]	
-		else:
-			return 1
-
-		for name, val in attrs.items():
-
-			if oldData.has_key( name ):
-
-				if oldData[ name ] != attrs[ name ]:
-
-					return 1
-
-			else:
-				return 1
-
-		return 0
-
 	def getJobData( self ):
 
 		"""Gather all data on current jobs in Torque"""
@@ -663,8 +950,6 @@
 
 		jobs_processed	= [ ]
 
-		my_domain		= string.join( socket.getfqdn().split( '.' )[1:], '.' )
-
 		for name, attrs in joblist.items():
 
 			job_id			= name.split( '.' )[0]
@@ -709,33 +994,8 @@
 				start_timestamp		= self.getAttr( attrs, 'mtime' )
 				nodes			= self.getAttr( attrs, 'exec_host' ).split( '+' )
 
-				nodeslist		= [ ]
+				nodeslist		= do_nodelist( nodes )
 
-				for node in nodes:
-
-					host		= node.split( '/' )[0]
-
-					host_domain	= string.join( host.split( '.' )[1:], '.' )
-
-					if host_domain == my_domain:
-
-						host		= host.split( '.' )[0]
-
-					if nodeslist.count( host ) == 0:
-
-						for translate_pattern in BATCH_HOST_TRANSLATE:
-
-							if translate_pattern.find( '/' ) != -1:
-
-								translate_orig	= translate_pattern.split( '/' )[1]
-								translate_new	= translate_pattern.split( '/' )[2]
-
-								host		= re.sub( translate_orig, translate_new, host )
-				
-						if not host in nodeslist:
-				
-							nodeslist.append( host )
-
 				if DETECT_TIME_DIFFS:
 
 					# If a job start if later than our current date,
@@ -825,7 +1085,7 @@
 			myAttrs[ 'queued_timestamp' ]	= str( queued_timestamp )
 			myAttrs[ 'reported' ]		= str( int( int( self.cur_time ) + int( self.timeoffset ) ) )
 			myAttrs[ 'nodes' ]		= nodeslist
-			myAttrs[ 'domain' ]		= string.join( socket.getfqdn().split( '.' )[1:], '.' )
+			myAttrs[ 'domain' ]		= fqdn_parts( socket.getfqdn() )[1]
 			myAttrs[ 'poll_interval' ]	= str( BATCH_POLL_INTERVAL )
 
 			if self.jobDataChanged( self.jobs, job_id, myAttrs ) and myAttrs['status'] in [ 'R', 'Q' ]:
@@ -840,139 +1100,6 @@
 				#
 				del self.jobs[ id ]
 
-	def submitJobData( self ):
-
-		"""Submit job info list"""
-
-		self.dp.multicastGmetric( 'MONARCH-HEARTBEAT', str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) )
-
-		running_jobs	= 0
-		queued_jobs	= 0
-
-		# Count how many running/queued jobs we found
-		#
-		for jobid, jobattrs in self.jobs.items():
-
-			if jobattrs[ 'status' ] == 'Q':
-
-				queued_jobs += 1
-
-			elif jobattrs[ 'status' ] == 'R':
-
-				running_jobs += 1
-
-		# Report running/queued jobs as seperate metric for a nice RRD graph
-		#
-		self.dp.multicastGmetric( 'MONARCH-RJ', str( running_jobs ), 'uint32', 'jobs' )
-		self.dp.multicastGmetric( 'MONARCH-QJ', str( queued_jobs ), 'uint32', 'jobs' )
-
-		# Now let's spread the knowledge
-		#
-		for jobid, jobattrs in self.jobs.items():
-
-			# Make gmetric values for each job: respect max gmetric value length
-			#
-			gmetric_val		= self.compileGmetricVal( jobid, jobattrs )
-			metric_increment	= 0
-
-			# If we have more job info than max gmetric value length allows, split it up
-			# amongst multiple metrics
-			#
-			for val in gmetric_val:
-
-				self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), val )
-
-				# Increase follow number if this jobinfo is split up amongst more than 1 gmetric
-				#
-				metric_increment	= metric_increment + 1
-
-	def compileGmetricVal( self, jobid, jobattrs ):
-
-		"""Create a val string for gmetric of jobinfo"""
-
-		gval_lists	= [ ]
-		mystr		= None
-		val_list	= { }
-
-		for val_name, val_value in jobattrs.items():
-
-			# These are our own metric names, i.e.: status, start_timestamp, etc
-			#
-			val_list_names_len	= len( string.join( val_list.keys() ) ) + len(val_list.keys())
-
-			# These are their corresponding values
-			#
-			val_list_vals_len	= len( string.join( val_list.values() ) ) + len(val_list.values())
-
-			if val_name == 'nodes' and jobattrs['status'] == 'R':
-
-				node_str = None
-
-				for node in val_value:
-
-					if node_str:
-
-						node_str = node_str + ';' + node
-					else:
-						node_str = node
-
-					# Make sure if we add this new info, that the total metric's value length does not exceed METRIC_MAX_VAL_LEN
-					#
-					if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(node_str) ) > METRIC_MAX_VAL_LEN:
-
-						# It's too big, we need to make a new gmetric for the additional info
-						#
-						val_list[ val_name ]	= node_str
-
-						gval_lists.append( val_list )
-
-						val_list		= { }
-						node_str		= None
-
-				val_list[ val_name ]	= node_str
-
-				gval_lists.append( val_list )
-
-				val_list		= { }
-
-			elif val_value != '':
-
-				# Make sure if we add this new info, that the total metric's value length does not exceed METRIC_MAX_VAL_LEN
-				#
-				if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(str(val_value)) ) > METRIC_MAX_VAL_LEN:
-
-					# It's too big, we need to make a new gmetric for the additional info
-					#
-					gval_lists.append( val_list )
-
-					val_list		= { }
-
-				val_list[ val_name ]	= val_value
-
-		if len( val_list ) > 0:
-
-			gval_lists.append( val_list )
-
-		str_list	= [ ]
-
-		# Now append the value names and values together, i.e.: stop_timestamp=value, etc
-		#
-		for val_list in gval_lists:
-
-			my_val_str	= None
-
-			for val_name, val_value in val_list.items():
-
-				if my_val_str:
-
-					my_val_str = my_val_str + ' ' + val_name + '=' + val_value
-				else:
-					my_val_str = val_name + '=' + val_value
-
-			str_list.append( my_val_str )
-
-		return str_list
-
 #
 # Gmetric by Nick Galbreath - nickg(a.t)modp(d.o.t)com
 # Version 1.0 - 21-April2-2007
@@ -1134,9 +1261,10 @@
 
 	elif BATCH_API == 'sge':
 
-		debug_msg( 0, "FATAL ERROR: BATCH_API 'sge' implementation is currently broken, check future releases" )
+		# Tested with SGE 6.0u11.
+# 		debug_msg( 0, "FATAL ERROR: BATCH_API 'sge' implementation is currently broken, check future releases" )
 
-		sys.exit( 1 )
+# 		sys.exit( 1 )
 
 		gather = SgeDataGatherer()
 
Index: jobmond/jobmond.conf
===================================================================
--- jobmond/jobmond.conf	(revision 496)
+++ jobmond/jobmond.conf	(working copy)
@@ -20,7 +20,7 @@
 BATCH_SERVER		: localhost
 
 # Which queue(s) to report jobs of
-# (optional and only supported for pbs)
+# (optional)
 #
 #QUEUE			: long, short
 
Index: web/addons/job_monarch/templates/overview.tpl
===================================================================
--- web/addons/job_monarch/templates/overview.tpl	(revision 496)
+++ web/addons/job_monarch/templates/overview.tpl	(working copy)
@@ -1,4 +1,6 @@
-<BR><BR>
+<P>
+All tasks of parallel and array jobs appear as a single &lsquo;job&rsquo;.
+<BR></P>
 
 <CENTER>
 <TABLE cellpadding="15">
@@ -137,6 +139,7 @@
 <BR>
 
 <SCRIPT TYPE="text/javascript" SRC="libtoga.js"></SCRIPT>
+<NOSCRIPT><P>[Sorting by column header requires JavaScript]<BR><BR></P></NOSCRIPT>
 
 <INPUT TYPE="HIDDEN" NAME="sortby" VALUE="{sortby}">
 <INPUT TYPE="HIDDEN" NAME="sortorder" VALUE="{sortorder}">
