[23] | 1 | #!/usr/bin/env python |
---|
[225] | 2 | # |
---|
| 3 | # This file is part of Jobmonarch |
---|
| 4 | # |
---|
| 5 | # Copyright (C) 2006 Ramon Bastiaans |
---|
| 6 | # |
---|
| 7 | # Jobmonarch is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # Jobmonarch is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | # |
---|
[228] | 21 | # SVN $Id: jobmond.py 362 2007-06-08 14:25:07Z bastiaans $ |
---|
[227] | 22 | # |
---|
[23] | 23 | |
---|
[212] | 24 | import sys, getopt, ConfigParser |
---|
[354] | 25 | import time, os, socket, string, re |
---|
[362] | 26 | import xdrlib, socket |
---|
[318] | 27 | import xml, xml.sax |
---|
| 28 | from xml.sax import saxutils, make_parser |
---|
| 29 | from xml.sax import make_parser |
---|
| 30 | from xml.sax.handler import feature_namespaces |
---|
| 31 | |
---|
[307] | 32 | def usage(): |
---|
| 33 | |
---|
| 34 | print |
---|
| 35 | print 'usage: jobmond [options]' |
---|
| 36 | print 'options:' |
---|
| 37 | print ' --config, -c configuration file' |
---|
| 38 | print ' --pidfile, -p pid file' |
---|
| 39 | print ' --help, -h help' |
---|
| 40 | print |
---|
| 41 | |
---|
[212] | 42 | def processArgs( args ): |
---|
[26] | 43 | |
---|
[354] | 44 | SHORT_L = 'hc:' |
---|
| 45 | LONG_L = [ 'help', 'config=' ] |
---|
[165] | 46 | |
---|
[307] | 47 | global PIDFILE |
---|
[354] | 48 | PIDFILE = None |
---|
[61] | 49 | |
---|
[354] | 50 | config_filename = '/etc/jobmond.conf' |
---|
| 51 | |
---|
[212] | 52 | try: |
---|
[68] | 53 | |
---|
[354] | 54 | opts, args = getopt.getopt( args, SHORT_L, LONG_L ) |
---|
[185] | 55 | |
---|
[307] | 56 | except getopt.GetoptError, detail: |
---|
[212] | 57 | |
---|
| 58 | print detail |
---|
[307] | 59 | usage() |
---|
[354] | 60 | sys.exit( 1 ) |
---|
[212] | 61 | |
---|
| 62 | for opt, value in opts: |
---|
| 63 | |
---|
| 64 | if opt in [ '--config', '-c' ]: |
---|
| 65 | |
---|
[354] | 66 | config_filename = value |
---|
[212] | 67 | |
---|
[307] | 68 | if opt in [ '--pidfile', '-p' ]: |
---|
[212] | 69 | |
---|
[354] | 70 | PIDFILE = value |
---|
[307] | 71 | |
---|
| 72 | if opt in [ '--help', '-h' ]: |
---|
| 73 | |
---|
| 74 | usage() |
---|
[354] | 75 | sys.exit( 0 ) |
---|
[212] | 76 | |
---|
| 77 | return loadConfig( config_filename ) |
---|
| 78 | |
---|
| 79 | def loadConfig( filename ): |
---|
| 80 | |
---|
[215] | 81 | def getlist( cfg_string ): |
---|
| 82 | |
---|
| 83 | my_list = [ ] |
---|
| 84 | |
---|
| 85 | for item_txt in cfg_string.split( ',' ): |
---|
| 86 | |
---|
| 87 | sep_char = None |
---|
| 88 | |
---|
| 89 | item_txt = item_txt.strip() |
---|
| 90 | |
---|
| 91 | for s_char in [ "'", '"' ]: |
---|
| 92 | |
---|
| 93 | if item_txt.find( s_char ) != -1: |
---|
| 94 | |
---|
| 95 | if item_txt.count( s_char ) != 2: |
---|
| 96 | |
---|
| 97 | print 'Missing quote: %s' %item_txt |
---|
| 98 | sys.exit( 1 ) |
---|
| 99 | |
---|
| 100 | else: |
---|
| 101 | |
---|
| 102 | sep_char = s_char |
---|
| 103 | break |
---|
| 104 | |
---|
| 105 | if sep_char: |
---|
| 106 | |
---|
| 107 | item_txt = item_txt.split( sep_char )[1] |
---|
| 108 | |
---|
| 109 | my_list.append( item_txt ) |
---|
| 110 | |
---|
| 111 | return my_list |
---|
| 112 | |
---|
[354] | 113 | cfg = ConfigParser.ConfigParser() |
---|
[212] | 114 | |
---|
| 115 | cfg.read( filename ) |
---|
| 116 | |
---|
[354] | 117 | global DEBUG_LEVEL, DAEMONIZE, BATCH_SERVER, BATCH_POLL_INTERVAL |
---|
| 118 | global GMOND_CONF, DETECT_TIME_DIFFS, BATCH_HOST_TRANSLATE |
---|
| 119 | global BATCH_API, QUEUE, GMETRIC_TARGET |
---|
[212] | 120 | |
---|
[354] | 121 | DEBUG_LEVEL = cfg.getint( 'DEFAULT', 'DEBUG_LEVEL' ) |
---|
[212] | 122 | |
---|
[354] | 123 | DAEMONIZE = cfg.getboolean( 'DEFAULT', 'DAEMONIZE' ) |
---|
[212] | 124 | |
---|
[265] | 125 | try: |
---|
[212] | 126 | |
---|
[354] | 127 | BATCH_SERVER = cfg.get( 'DEFAULT', 'BATCH_SERVER' ) |
---|
[212] | 128 | |
---|
[265] | 129 | except ConfigParser.NoOptionError: |
---|
| 130 | |
---|
| 131 | # Backwards compatibility for old configs |
---|
| 132 | # |
---|
| 133 | |
---|
[354] | 134 | BATCH_SERVER = cfg.get( 'DEFAULT', 'TORQUE_SERVER' ) |
---|
| 135 | api_guess = 'pbs' |
---|
[265] | 136 | |
---|
| 137 | try: |
---|
| 138 | |
---|
[354] | 139 | BATCH_POLL_INTERVAL = cfg.getint( 'DEFAULT', 'BATCH_POLL_INTERVAL' ) |
---|
[265] | 140 | |
---|
| 141 | except ConfigParser.NoOptionError: |
---|
| 142 | |
---|
| 143 | # Backwards compatibility for old configs |
---|
| 144 | # |
---|
| 145 | |
---|
[354] | 146 | BATCH_POLL_INTERVAL = cfg.getint( 'DEFAULT', 'TORQUE_POLL_INTERVAL' ) |
---|
| 147 | api_guess = 'pbs' |
---|
[353] | 148 | |
---|
| 149 | try: |
---|
[212] | 150 | |
---|
[354] | 151 | GMOND_CONF = cfg.get( 'DEFAULT', 'GMOND_CONF' ) |
---|
[353] | 152 | |
---|
| 153 | except ConfigParser.NoOptionError: |
---|
| 154 | |
---|
[354] | 155 | GMOND_CONF = None |
---|
[353] | 156 | |
---|
[354] | 157 | DETECT_TIME_DIFFS = cfg.getboolean( 'DEFAULT', 'DETECT_TIME_DIFFS' ) |
---|
[212] | 158 | |
---|
[354] | 159 | BATCH_HOST_TRANSLATE = getlist( cfg.get( 'DEFAULT', 'BATCH_HOST_TRANSLATE' ) ) |
---|
[215] | 160 | |
---|
[266] | 161 | try: |
---|
[256] | 162 | |
---|
[354] | 163 | BATCH_API = cfg.get( 'DEFAULT', 'BATCH_API' ) |
---|
[266] | 164 | |
---|
| 165 | except ConfigParser.NoOptionError, detail: |
---|
| 166 | |
---|
| 167 | if BATCH_SERVER and api_guess: |
---|
[354] | 168 | |
---|
| 169 | BATCH_API = api_guess |
---|
[266] | 170 | else: |
---|
| 171 | debug_msg( 0, "fatal error: BATCH_API not set and can't make guess" ) |
---|
| 172 | sys.exit( 1 ) |
---|
[317] | 173 | |
---|
| 174 | try: |
---|
| 175 | |
---|
[354] | 176 | QUEUE = getlist( cfg.get( 'DEFAULT', 'QUEUE' ) ) |
---|
[317] | 177 | |
---|
| 178 | except ConfigParser.NoOptionError, detail: |
---|
| 179 | |
---|
[354] | 180 | QUEUE = None |
---|
[353] | 181 | |
---|
| 182 | try: |
---|
| 183 | |
---|
[354] | 184 | GMETRIC_TARGET = cfg.get( 'DEFAULT', 'GMETRIC_TARGET' ) |
---|
[353] | 185 | |
---|
| 186 | except ConfigParser.NoOptionError: |
---|
| 187 | |
---|
[354] | 188 | GMETRIC_TARGET = None |
---|
[353] | 189 | |
---|
| 190 | if not GMOND_CONF: |
---|
| 191 | |
---|
| 192 | debug_msg( 0, "fatal error: GMETRIC_TARGET or GMOND_CONF both not set!" ) |
---|
| 193 | sys.exit( 1 ) |
---|
| 194 | else: |
---|
| 195 | |
---|
| 196 | 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!" ) |
---|
| 197 | |
---|
[212] | 198 | return True |
---|
| 199 | |
---|
[253] | 200 | METRIC_MAX_VAL_LEN = 900 |
---|
| 201 | |
---|
[61] | 202 | class DataProcessor: |
---|
[355] | 203 | |
---|
[68] | 204 | """Class for processing of data""" |
---|
[61] | 205 | |
---|
| 206 | binary = '/usr/bin/gmetric' |
---|
| 207 | |
---|
| 208 | def __init__( self, binary=None ): |
---|
[355] | 209 | |
---|
[68] | 210 | """Remember alternate binary location if supplied""" |
---|
[61] | 211 | |
---|
| 212 | if binary: |
---|
| 213 | self.binary = binary |
---|
| 214 | |
---|
[80] | 215 | # Timeout for XML |
---|
| 216 | # |
---|
| 217 | # From ganglia's documentation: |
---|
| 218 | # |
---|
| 219 | # 'A metric will be deleted DMAX seconds after it is received, and |
---|
| 220 | # DMAX=0 means eternal life.' |
---|
[61] | 221 | |
---|
[256] | 222 | self.dmax = str( int( int( BATCH_POLL_INTERVAL ) * 2 ) ) |
---|
[80] | 223 | |
---|
[353] | 224 | if GMOND_CONF: |
---|
[354] | 225 | |
---|
[353] | 226 | try: |
---|
| 227 | gmond_file = GMOND_CONF |
---|
[68] | 228 | |
---|
[353] | 229 | except NameError: |
---|
| 230 | gmond_file = '/etc/gmond.conf' |
---|
[68] | 231 | |
---|
[353] | 232 | if not os.path.exists( gmond_file ): |
---|
| 233 | debug_msg( 0, 'fatal error: ' + gmond_file + ' does not exist' ) |
---|
| 234 | sys.exit( 1 ) |
---|
[68] | 235 | |
---|
[353] | 236 | incompatible = self.checkGmetricVersion() |
---|
[61] | 237 | |
---|
[353] | 238 | if incompatible: |
---|
[355] | 239 | |
---|
| 240 | debug_msg( 0, 'Gmetric version not compatible, please upgrade to at least 3.0.1' ) |
---|
[353] | 241 | sys.exit( 1 ) |
---|
[65] | 242 | |
---|
| 243 | def checkGmetricVersion( self ): |
---|
[355] | 244 | |
---|
[68] | 245 | """ |
---|
| 246 | Check version of gmetric is at least 3.0.1 |
---|
| 247 | for the syntax we use |
---|
| 248 | """ |
---|
[65] | 249 | |
---|
[255] | 250 | global METRIC_MAX_VAL_LEN |
---|
| 251 | |
---|
[341] | 252 | incompatible = 0 |
---|
| 253 | |
---|
[355] | 254 | gfp = os.popen( self.binary + ' --version' ) |
---|
| 255 | lines = gfp.readlines() |
---|
[65] | 256 | |
---|
[355] | 257 | gfp.close() |
---|
| 258 | |
---|
| 259 | for line in lines: |
---|
| 260 | |
---|
[65] | 261 | line = line.split( ' ' ) |
---|
| 262 | |
---|
[355] | 263 | if len( line ) == 2 and str( line ).find( 'gmetric' ) != -1: |
---|
[65] | 264 | |
---|
[355] | 265 | gmetric_version = line[1].split( '\n' )[0] |
---|
[65] | 266 | |
---|
[355] | 267 | version_major = int( gmetric_version.split( '.' )[0] ) |
---|
| 268 | version_minor = int( gmetric_version.split( '.' )[1] ) |
---|
| 269 | version_patch = int( gmetric_version.split( '.' )[2] ) |
---|
[65] | 270 | |
---|
[355] | 271 | incompatible = 0 |
---|
[65] | 272 | |
---|
| 273 | if version_major < 3: |
---|
| 274 | |
---|
| 275 | incompatible = 1 |
---|
| 276 | |
---|
| 277 | elif version_major == 3: |
---|
| 278 | |
---|
| 279 | if version_minor == 0: |
---|
| 280 | |
---|
| 281 | if version_patch < 1: |
---|
| 282 | |
---|
[91] | 283 | incompatible = 1 |
---|
[65] | 284 | |
---|
[255] | 285 | if version_patch < 3: |
---|
| 286 | |
---|
| 287 | METRIC_MAX_VAL_LEN = 900 |
---|
| 288 | |
---|
| 289 | elif version_patch >= 3: |
---|
| 290 | |
---|
| 291 | METRIC_MAX_VAL_LEN = 1400 |
---|
| 292 | |
---|
[65] | 293 | return incompatible |
---|
| 294 | |
---|
[75] | 295 | def multicastGmetric( self, metricname, metricval, valtype='string' ): |
---|
[355] | 296 | |
---|
[68] | 297 | """Call gmetric binary and multicast""" |
---|
[65] | 298 | |
---|
| 299 | cmd = self.binary |
---|
| 300 | |
---|
[353] | 301 | if GMETRIC_TARGET: |
---|
[61] | 302 | |
---|
[353] | 303 | from gmetric import Gmetric |
---|
[61] | 304 | |
---|
[353] | 305 | if GMETRIC_TARGET: |
---|
[61] | 306 | |
---|
[353] | 307 | GMETRIC_TARGET_HOST = GMETRIC_TARGET.split( ':' )[0] |
---|
| 308 | GMETRIC_TARGET_PORT = GMETRIC_TARGET.split( ':' )[1] |
---|
| 309 | |
---|
| 310 | metric_debug = "[gmetric] name: %s - val: %s - dmax: %s" %( str( metricname ), str( metricval ), str( self.dmax ) ) |
---|
| 311 | |
---|
| 312 | debug_msg( 10, printTime() + ' ' + metric_debug) |
---|
| 313 | |
---|
| 314 | gm = Gmetric( GMETRIC_TARGET_HOST, GMETRIC_TARGET_PORT ) |
---|
| 315 | |
---|
| 316 | gm.send( str( metricname ), str( metricval ), str( self.dmax ) ) |
---|
| 317 | |
---|
| 318 | else: |
---|
| 319 | try: |
---|
| 320 | cmd = cmd + ' -c' + GMOND_CONF |
---|
| 321 | |
---|
| 322 | except NameError: |
---|
| 323 | |
---|
| 324 | debug_msg( 10, 'Assuming /etc/gmond.conf for gmetric cmd (ommitting)' ) |
---|
| 325 | |
---|
| 326 | cmd = cmd + ' -n' + str( metricname )+ ' -v"' + str( metricval )+ '" -t' + str( valtype ) + ' -d' + str( self.dmax ) |
---|
| 327 | |
---|
| 328 | debug_msg( 10, printTime() + ' ' + cmd ) |
---|
| 329 | |
---|
| 330 | os.system( cmd ) |
---|
| 331 | |
---|
[318] | 332 | class DataGatherer: |
---|
[23] | 333 | |
---|
[318] | 334 | """Skeleton class for batch system DataGatherer""" |
---|
[256] | 335 | |
---|
[318] | 336 | def printJobs( self, jobs ): |
---|
[355] | 337 | |
---|
[318] | 338 | """Print a jobinfo overview""" |
---|
| 339 | |
---|
| 340 | for name, attrs in self.jobs.items(): |
---|
| 341 | |
---|
| 342 | print 'job %s' %(name) |
---|
| 343 | |
---|
| 344 | for name, val in attrs.items(): |
---|
| 345 | |
---|
| 346 | print '\t%s = %s' %( name, val ) |
---|
| 347 | |
---|
| 348 | def printJob( self, jobs, job_id ): |
---|
[355] | 349 | |
---|
[318] | 350 | """Print job with job_id from jobs""" |
---|
| 351 | |
---|
| 352 | print 'job %s' %(job_id) |
---|
| 353 | |
---|
| 354 | for name, val in jobs[ job_id ].items(): |
---|
| 355 | |
---|
| 356 | print '\t%s = %s' %( name, val ) |
---|
| 357 | |
---|
[256] | 358 | def daemon( self ): |
---|
[355] | 359 | |
---|
[318] | 360 | """Run as daemon forever""" |
---|
[256] | 361 | |
---|
[318] | 362 | # Fork the first child |
---|
| 363 | # |
---|
| 364 | pid = os.fork() |
---|
| 365 | if pid > 0: |
---|
| 366 | sys.exit(0) # end parent |
---|
[256] | 367 | |
---|
[318] | 368 | # creates a session and sets the process group ID |
---|
| 369 | # |
---|
| 370 | os.setsid() |
---|
| 371 | |
---|
| 372 | # Fork the second child |
---|
| 373 | # |
---|
| 374 | pid = os.fork() |
---|
| 375 | if pid > 0: |
---|
| 376 | sys.exit(0) # end parent |
---|
| 377 | |
---|
| 378 | write_pidfile() |
---|
| 379 | |
---|
| 380 | # Go to the root directory and set the umask |
---|
| 381 | # |
---|
| 382 | os.chdir('/') |
---|
| 383 | os.umask(0) |
---|
| 384 | |
---|
| 385 | sys.stdin.close() |
---|
| 386 | sys.stdout.close() |
---|
| 387 | sys.stderr.close() |
---|
| 388 | |
---|
| 389 | os.open('/dev/null', os.O_RDWR) |
---|
| 390 | os.dup2(0, 1) |
---|
| 391 | os.dup2(0, 2) |
---|
| 392 | |
---|
| 393 | self.run() |
---|
| 394 | |
---|
[256] | 395 | def run( self ): |
---|
[355] | 396 | |
---|
[318] | 397 | """Main thread""" |
---|
[256] | 398 | |
---|
[318] | 399 | while ( 1 ): |
---|
| 400 | |
---|
[348] | 401 | self.getJobData() |
---|
| 402 | self.submitJobData() |
---|
[318] | 403 | time.sleep( BATCH_POLL_INTERVAL ) |
---|
[256] | 404 | |
---|
[318] | 405 | class SgeQstatXMLParser(xml.sax.handler.ContentHandler): |
---|
[256] | 406 | |
---|
[318] | 407 | """Babu Sundaram's experimental SGE qstat XML parser""" |
---|
[256] | 408 | |
---|
[318] | 409 | def __init__(self, qstatinxml): |
---|
| 410 | |
---|
| 411 | self.qstatfile = qstatinxml |
---|
| 412 | self.attribs = {} |
---|
| 413 | self.value = '' |
---|
| 414 | self.jobID = '' |
---|
| 415 | self.currentJobInfo = '' |
---|
| 416 | self.job_list = [] |
---|
| 417 | self.EOFFlag = 0 |
---|
| 418 | self.jobinfoCount = 0 |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | def startElement(self, name, attrs): |
---|
| 422 | |
---|
| 423 | if name == 'job_list': |
---|
| 424 | self.currentJobInfo = 'Status=' + attrs.get('state', None) + ' ' |
---|
| 425 | elif name == 'job_info': |
---|
| 426 | self.job_list = [] |
---|
| 427 | self.jobinfoCount += 1 |
---|
| 428 | |
---|
| 429 | def characters(self, ch): |
---|
| 430 | |
---|
| 431 | self.value = self.value + ch |
---|
| 432 | |
---|
| 433 | def endElement(self, name): |
---|
| 434 | |
---|
| 435 | if len(self.value.strip()) > 0 : |
---|
| 436 | |
---|
| 437 | self.currentJobInfo += name + '=' + self.value.strip() + ' ' |
---|
| 438 | elif name != 'job_list': |
---|
| 439 | |
---|
| 440 | self.currentJobInfo += name + '=Unknown ' |
---|
| 441 | |
---|
| 442 | if name == 'JB_job_number': |
---|
| 443 | |
---|
| 444 | self.jobID = self.value.strip() |
---|
| 445 | self.job_list.append(self.jobID) |
---|
| 446 | |
---|
| 447 | if name == 'job_list': |
---|
| 448 | |
---|
| 449 | if self.attribs.has_key(self.jobID) == False: |
---|
| 450 | self.attribs[self.jobID] = self.currentJobInfo |
---|
| 451 | elif self.attribs.has_key(self.jobID) and self.attribs[self.jobID] != self.currentJobInfo: |
---|
| 452 | self.attribs[self.jobID] = self.currentJobInfo |
---|
| 453 | self.currentJobInfo = '' |
---|
| 454 | self.jobID = '' |
---|
| 455 | |
---|
| 456 | elif name == 'job_info' and self.jobinfoCount == 2: |
---|
| 457 | |
---|
| 458 | deljobs = [] |
---|
| 459 | for id in self.attribs: |
---|
| 460 | try: |
---|
| 461 | self.job_list.index(str(id)) |
---|
| 462 | except ValueError: |
---|
| 463 | deljobs.append(id) |
---|
| 464 | for i in deljobs: |
---|
| 465 | del self.attribs[i] |
---|
| 466 | deljobs = [] |
---|
| 467 | self.jobinfoCount = 0 |
---|
| 468 | |
---|
| 469 | self.value = '' |
---|
| 470 | |
---|
| 471 | class SgeDataGatherer(DataGatherer): |
---|
| 472 | |
---|
[61] | 473 | jobs = { } |
---|
[347] | 474 | SGE_QSTAT_XML_FILE = '/tmp/.jobmonarch.sge.qstat' |
---|
[61] | 475 | |
---|
[318] | 476 | def __init__( self ): |
---|
| 477 | """Setup appropriate variables""" |
---|
| 478 | |
---|
| 479 | self.jobs = { } |
---|
| 480 | self.timeoffset = 0 |
---|
| 481 | self.dp = DataProcessor() |
---|
| 482 | self.initSgeJobInfo() |
---|
| 483 | |
---|
| 484 | def initSgeJobInfo( self ): |
---|
| 485 | """This is outside the scope of DRMAA; Get the current jobs in SGE""" |
---|
| 486 | """This is a hack because we cant get info about jobs beyond""" |
---|
| 487 | """those in the current DRMAA session""" |
---|
| 488 | |
---|
[347] | 489 | self.qstatparser = SgeQstatXMLParser( self.SGE_QSTAT_XML_FILE ) |
---|
[318] | 490 | |
---|
| 491 | # Obtain the qstat information from SGE in XML format |
---|
| 492 | # This would change to DRMAA-specific calls from 6.0u9 |
---|
| 493 | |
---|
| 494 | def getJobData(self): |
---|
| 495 | """Gather all data on current jobs in SGE""" |
---|
| 496 | |
---|
| 497 | # Get the information about the current jobs in the SGE queue |
---|
| 498 | info = os.popen("qstat -ext -xml").readlines() |
---|
[347] | 499 | f = open(self.SGE_QSTAT_XML_FILE,'w') |
---|
[318] | 500 | for lines in info: |
---|
| 501 | f.write(lines) |
---|
| 502 | f.close() |
---|
| 503 | |
---|
| 504 | # Parse the input |
---|
| 505 | f = open(self.qstatparser.qstatfile, 'r') |
---|
| 506 | xml.sax.parse(f, self.qstatparser) |
---|
| 507 | f.close() |
---|
| 508 | |
---|
| 509 | self.cur_time = time.time() |
---|
| 510 | |
---|
| 511 | return self.qstatparser.attribs |
---|
| 512 | |
---|
| 513 | def submitJobData(self): |
---|
| 514 | """Submit job info list""" |
---|
| 515 | |
---|
| 516 | self.dp.multicastGmetric( 'MONARCH-HEARTBEAT', str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) ) |
---|
| 517 | # Now let's spread the knowledge |
---|
| 518 | # |
---|
| 519 | metric_increment = 0 |
---|
| 520 | for jobid, jobattrs in self.qstatparser.attribs.items(): |
---|
| 521 | |
---|
| 522 | self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), jobattrs) |
---|
| 523 | |
---|
[355] | 524 | class PbsDataGatherer( DataGatherer ): |
---|
[318] | 525 | |
---|
| 526 | """This is the DataGatherer for PBS and Torque""" |
---|
| 527 | |
---|
[256] | 528 | global PBSQuery |
---|
| 529 | |
---|
[23] | 530 | def __init__( self ): |
---|
[354] | 531 | |
---|
[68] | 532 | """Setup appropriate variables""" |
---|
[23] | 533 | |
---|
[354] | 534 | self.jobs = { } |
---|
| 535 | self.timeoffset = 0 |
---|
| 536 | self.dp = DataProcessor() |
---|
| 537 | |
---|
[91] | 538 | self.initPbsQuery() |
---|
[23] | 539 | |
---|
[91] | 540 | def initPbsQuery( self ): |
---|
| 541 | |
---|
[354] | 542 | self.pq = None |
---|
| 543 | |
---|
[256] | 544 | if( BATCH_SERVER ): |
---|
[354] | 545 | |
---|
| 546 | self.pq = PBSQuery( BATCH_SERVER ) |
---|
[174] | 547 | else: |
---|
[354] | 548 | self.pq = PBSQuery() |
---|
[91] | 549 | |
---|
[26] | 550 | def getAttr( self, attrs, name ): |
---|
[354] | 551 | |
---|
[68] | 552 | """Return certain attribute from dictionary, if exists""" |
---|
[26] | 553 | |
---|
| 554 | if attrs.has_key( name ): |
---|
[354] | 555 | |
---|
| 556 | return attrs[ name ] |
---|
[26] | 557 | else: |
---|
| 558 | return '' |
---|
| 559 | |
---|
| 560 | def jobDataChanged( self, jobs, job_id, attrs ): |
---|
[354] | 561 | |
---|
[68] | 562 | """Check if job with attrs and job_id in jobs has changed""" |
---|
[26] | 563 | |
---|
| 564 | if jobs.has_key( job_id ): |
---|
[354] | 565 | |
---|
[26] | 566 | oldData = jobs[ job_id ] |
---|
| 567 | else: |
---|
| 568 | return 1 |
---|
| 569 | |
---|
| 570 | for name, val in attrs.items(): |
---|
| 571 | |
---|
| 572 | if oldData.has_key( name ): |
---|
| 573 | |
---|
| 574 | if oldData[ name ] != attrs[ name ]: |
---|
| 575 | |
---|
| 576 | return 1 |
---|
| 577 | |
---|
| 578 | else: |
---|
| 579 | return 1 |
---|
| 580 | |
---|
| 581 | return 0 |
---|
| 582 | |
---|
[348] | 583 | def getJobData( self ): |
---|
[354] | 584 | |
---|
[68] | 585 | """Gather all data on current jobs in Torque""" |
---|
[26] | 586 | |
---|
[354] | 587 | joblist = {} |
---|
[359] | 588 | self.cur_time = 0 |
---|
[349] | 589 | |
---|
[359] | 590 | try: |
---|
| 591 | joblist = self.pq.getjobs() |
---|
| 592 | self.cur_time = time.time() |
---|
[354] | 593 | |
---|
[359] | 594 | except PBSError, detail: |
---|
[354] | 595 | |
---|
[359] | 596 | debug_msg( 10, "Caught PBS unavailable, skipping until next polling interval: " + str( detail ) ) |
---|
| 597 | return None |
---|
[354] | 598 | |
---|
| 599 | jobs_processed = [ ] |
---|
[26] | 600 | |
---|
| 601 | for name, attrs in joblist.items(): |
---|
| 602 | |
---|
[354] | 603 | job_id = name.split( '.' )[0] |
---|
[26] | 604 | |
---|
| 605 | jobs_processed.append( job_id ) |
---|
[61] | 606 | |
---|
[354] | 607 | name = self.getAttr( attrs, 'Job_Name' ) |
---|
| 608 | queue = self.getAttr( attrs, 'queue' ) |
---|
[317] | 609 | |
---|
| 610 | if QUEUE: |
---|
| 611 | |
---|
| 612 | if QUEUE != queue: |
---|
| 613 | |
---|
| 614 | continue |
---|
| 615 | |
---|
[354] | 616 | owner = self.getAttr( attrs, 'Job_Owner' ).split( '@' )[0] |
---|
| 617 | requested_time = self.getAttr( attrs, 'Resource_List.walltime' ) |
---|
| 618 | requested_memory = self.getAttr( attrs, 'Resource_List.mem' ) |
---|
[95] | 619 | |
---|
[354] | 620 | mynoderequest = self.getAttr( attrs, 'Resource_List.nodes' ) |
---|
[95] | 621 | |
---|
[354] | 622 | ppn = '' |
---|
[281] | 623 | |
---|
[26] | 624 | if mynoderequest.find( ':' ) != -1 and mynoderequest.find( 'ppn' ) != -1: |
---|
[95] | 625 | |
---|
[354] | 626 | mynoderequest_fields = mynoderequest.split( ':' ) |
---|
[281] | 627 | |
---|
| 628 | for mynoderequest_field in mynoderequest_fields: |
---|
| 629 | |
---|
| 630 | if mynoderequest_field.find( 'ppn' ) != -1: |
---|
| 631 | |
---|
[354] | 632 | ppn = mynoderequest_field.split( 'ppn=' )[1] |
---|
[281] | 633 | |
---|
[354] | 634 | status = self.getAttr( attrs, 'job_state' ) |
---|
[25] | 635 | |
---|
[354] | 636 | queued_timestamp = self.getAttr( attrs, 'ctime' ) |
---|
[243] | 637 | |
---|
[95] | 638 | if status == 'R': |
---|
[133] | 639 | |
---|
[354] | 640 | start_timestamp = self.getAttr( attrs, 'mtime' ) |
---|
| 641 | nodes = self.getAttr( attrs, 'exec_host' ).split( '+' ) |
---|
[133] | 642 | |
---|
[354] | 643 | nodeslist = [ ] |
---|
| 644 | |
---|
[133] | 645 | for node in nodes: |
---|
| 646 | |
---|
[354] | 647 | host = node.split( '/' )[0] |
---|
| 648 | |
---|
[133] | 649 | if nodeslist.count( host ) == 0: |
---|
[215] | 650 | |
---|
| 651 | for translate_pattern in BATCH_HOST_TRANSLATE: |
---|
| 652 | |
---|
[220] | 653 | if translate_pattern.find( '/' ) != -1: |
---|
[215] | 654 | |
---|
[354] | 655 | translate_orig = translate_pattern.split( '/' )[1] |
---|
| 656 | translate_new = translate_pattern.split( '/' )[2] |
---|
[220] | 657 | |
---|
[354] | 658 | host = re.sub( translate_orig, translate_new, host ) |
---|
[216] | 659 | |
---|
[217] | 660 | if not host in nodeslist: |
---|
[216] | 661 | |
---|
| 662 | nodeslist.append( host ) |
---|
[133] | 663 | |
---|
[185] | 664 | if DETECT_TIME_DIFFS: |
---|
| 665 | |
---|
| 666 | # If a job start if later than our current date, |
---|
| 667 | # that must mean the Torque server's time is later |
---|
| 668 | # than our local time. |
---|
| 669 | |
---|
[354] | 670 | if int( start_timestamp ) > int( int( self.cur_time ) + int( self.timeoffset ) ): |
---|
[185] | 671 | |
---|
[354] | 672 | self.timeoffset = int( int(start_timestamp) - int(self.cur_time) ) |
---|
[185] | 673 | |
---|
[133] | 674 | elif status == 'Q': |
---|
[95] | 675 | |
---|
[354] | 676 | start_timestamp = '' |
---|
| 677 | count_mynodes = 0 |
---|
| 678 | numeric_node = 1 |
---|
| 679 | |
---|
[133] | 680 | for node in mynoderequest.split( '+' ): |
---|
[67] | 681 | |
---|
[354] | 682 | nodepart = node.split( ':' )[0] |
---|
[67] | 683 | |
---|
[133] | 684 | for letter in nodepart: |
---|
[67] | 685 | |
---|
[133] | 686 | if letter not in string.digits: |
---|
| 687 | |
---|
[354] | 688 | numeric_node = 0 |
---|
[133] | 689 | |
---|
| 690 | if not numeric_node: |
---|
[354] | 691 | |
---|
| 692 | count_mynodes = count_mynodes + 1 |
---|
[133] | 693 | else: |
---|
[327] | 694 | try: |
---|
[354] | 695 | count_mynodes = count_mynodes + int( nodepart ) |
---|
| 696 | |
---|
[327] | 697 | except ValueError, detail: |
---|
[354] | 698 | |
---|
[327] | 699 | debug_msg( 10, str( detail ) ) |
---|
| 700 | debug_msg( 10, "Encountered weird node in Resources_List?!" ) |
---|
| 701 | debug_msg( 10, 'nodepart = ' + str( nodepart ) ) |
---|
| 702 | debug_msg( 10, 'job = ' + str( name ) ) |
---|
| 703 | debug_msg( 10, 'attrs = ' + str( attrs ) ) |
---|
[133] | 704 | |
---|
[354] | 705 | nodeslist = str( count_mynodes ) |
---|
[172] | 706 | else: |
---|
[354] | 707 | start_timestamp = '' |
---|
| 708 | nodeslist = '' |
---|
[133] | 709 | |
---|
[354] | 710 | myAttrs = { } |
---|
[26] | 711 | |
---|
[354] | 712 | myAttrs[ 'name' ] = str( name ) |
---|
| 713 | myAttrs[ 'queue' ] = str( queue ) |
---|
| 714 | myAttrs[ 'owner' ] = str( owner ) |
---|
| 715 | myAttrs[ 'requested_time' ] = str( requested_time ) |
---|
| 716 | myAttrs[ 'requested_memory' ] = str( requested_memory ) |
---|
| 717 | myAttrs[ 'ppn' ] = str( ppn ) |
---|
| 718 | myAttrs[ 'status' ] = str( status ) |
---|
| 719 | myAttrs[ 'start_timestamp' ] = str( start_timestamp ) |
---|
| 720 | myAttrs[ 'queued_timestamp' ] = str( queued_timestamp ) |
---|
| 721 | myAttrs[ 'reported' ] = str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) |
---|
| 722 | myAttrs[ 'nodes' ] = nodeslist |
---|
| 723 | myAttrs[ 'domain' ] = string.join( socket.getfqdn().split( '.' )[1:], '.' ) |
---|
| 724 | myAttrs[ 'poll_interval' ] = str( BATCH_POLL_INTERVAL ) |
---|
| 725 | |
---|
[348] | 726 | if self.jobDataChanged( self.jobs, job_id, myAttrs ) and myAttrs['status'] in [ 'R', 'Q' ]: |
---|
[61] | 727 | |
---|
[354] | 728 | self.jobs[ job_id ] = myAttrs |
---|
[26] | 729 | |
---|
[348] | 730 | for id, attrs in self.jobs.items(): |
---|
[76] | 731 | |
---|
| 732 | if id not in jobs_processed: |
---|
| 733 | |
---|
| 734 | # This one isn't there anymore; toedeledoki! |
---|
| 735 | # |
---|
[348] | 736 | del self.jobs[ id ] |
---|
[76] | 737 | |
---|
[348] | 738 | def submitJobData( self ): |
---|
[354] | 739 | |
---|
[65] | 740 | """Submit job info list""" |
---|
| 741 | |
---|
[219] | 742 | self.dp.multicastGmetric( 'MONARCH-HEARTBEAT', str( int( int( self.cur_time ) + int( self.timeoffset ) ) ) ) |
---|
[69] | 743 | |
---|
[61] | 744 | # Now let's spread the knowledge |
---|
| 745 | # |
---|
[348] | 746 | for jobid, jobattrs in self.jobs.items(): |
---|
[61] | 747 | |
---|
[354] | 748 | gmetric_val = self.compileGmetricVal( jobid, jobattrs ) |
---|
| 749 | metric_increment = 0 |
---|
[61] | 750 | |
---|
[354] | 751 | for val in gmetric_val: |
---|
[253] | 752 | |
---|
| 753 | self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), val ) |
---|
[61] | 754 | |
---|
[354] | 755 | metric_increment = metric_increment + 1 |
---|
| 756 | |
---|
[253] | 757 | def compileGmetricVal( self, jobid, jobattrs ): |
---|
[354] | 758 | |
---|
[253] | 759 | """Create a val string for gmetric of jobinfo""" |
---|
[67] | 760 | |
---|
[354] | 761 | gval_lists = [ ] |
---|
| 762 | mystr = None |
---|
| 763 | val_list = { } |
---|
[67] | 764 | |
---|
[253] | 765 | for val_name, val_value in jobattrs.items(): |
---|
[61] | 766 | |
---|
[253] | 767 | val_list_names_len = len( string.join( val_list.keys() ) ) + len(val_list.keys()) |
---|
| 768 | val_list_vals_len = len( string.join( val_list.values() ) ) + len(val_list.values()) |
---|
[95] | 769 | |
---|
[254] | 770 | if val_name == 'nodes' and jobattrs['status'] == 'R': |
---|
[95] | 771 | |
---|
[253] | 772 | node_str = None |
---|
[95] | 773 | |
---|
[253] | 774 | for node in val_value: |
---|
[65] | 775 | |
---|
[253] | 776 | if node_str: |
---|
[354] | 777 | |
---|
[253] | 778 | node_str = node_str + ';' + node |
---|
| 779 | else: |
---|
| 780 | node_str = node |
---|
[65] | 781 | |
---|
[253] | 782 | if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(node_str) ) > METRIC_MAX_VAL_LEN: |
---|
[65] | 783 | |
---|
[354] | 784 | val_list[ val_name ] = node_str |
---|
| 785 | |
---|
[253] | 786 | gval_lists.append( val_list ) |
---|
[65] | 787 | |
---|
[354] | 788 | val_list = { } |
---|
| 789 | node_str = None |
---|
| 790 | |
---|
| 791 | val_list[ val_name ] = node_str |
---|
| 792 | |
---|
[253] | 793 | gval_lists.append( val_list ) |
---|
[65] | 794 | |
---|
[354] | 795 | val_list = { } |
---|
| 796 | |
---|
[254] | 797 | elif val_value != '': |
---|
| 798 | |
---|
| 799 | if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(str(val_value)) ) > METRIC_MAX_VAL_LEN: |
---|
| 800 | |
---|
| 801 | gval_lists.append( val_list ) |
---|
| 802 | |
---|
[354] | 803 | val_list = { } |
---|
[254] | 804 | |
---|
[354] | 805 | val_list[ val_name ] = val_value |
---|
| 806 | |
---|
| 807 | if len( val_list ) > 0: |
---|
| 808 | |
---|
[254] | 809 | gval_lists.append( val_list ) |
---|
| 810 | |
---|
[354] | 811 | str_list = [ ] |
---|
[65] | 812 | |
---|
[253] | 813 | for val_list in gval_lists: |
---|
[65] | 814 | |
---|
[354] | 815 | my_val_str = None |
---|
[65] | 816 | |
---|
[253] | 817 | for val_name, val_value in val_list.items(): |
---|
[65] | 818 | |
---|
[253] | 819 | if my_val_str: |
---|
[65] | 820 | |
---|
[253] | 821 | my_val_str = my_val_str + ' ' + val_name + '=' + val_value |
---|
| 822 | else: |
---|
| 823 | my_val_str = val_name + '=' + val_value |
---|
| 824 | |
---|
| 825 | str_list.append( my_val_str ) |
---|
| 826 | |
---|
| 827 | return str_list |
---|
| 828 | |
---|
[362] | 829 | GMETRIC_DEFAULT_TYPE = 'string' |
---|
| 830 | GMETRIC_DEFAULT_HOST = '127.0.0.1' |
---|
| 831 | GMETRIC_DEFAULT_PORT = '8649' |
---|
| 832 | |
---|
| 833 | class Gmetric: |
---|
| 834 | |
---|
| 835 | global GMETRIC_DEFAULT_HOST, GMETRIC_DEFAULT_PORT |
---|
| 836 | |
---|
| 837 | slope = { 'zero' : 0, 'positive' : 1, 'negative' : 2, 'both' : 3, 'unspecified' : 4 } |
---|
| 838 | type = ( '', 'string', 'uint16', 'int16', 'uint32', 'int32', 'float', 'double', 'timestamp' ) |
---|
| 839 | protocol = ( 'udp', 'multicast' ) |
---|
| 840 | |
---|
| 841 | def __init__( self, host=GMETRIC_DEFAULT_HOST, port=GMETRIC_DEFAULT_PORT ): |
---|
| 842 | |
---|
| 843 | global GMETRIC_DEFAULT_TYPE |
---|
| 844 | |
---|
| 845 | self.prot = self.checkHostProtocol( host ) |
---|
| 846 | self.msg = xdrlib.Packer() |
---|
| 847 | self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) |
---|
| 848 | |
---|
| 849 | if self.prot not in self.protocol: |
---|
| 850 | |
---|
| 851 | raise ValueError( "Protocol must be one of: " + str( self.protocol ) ) |
---|
| 852 | |
---|
| 853 | if self.prot == 'multicast': |
---|
| 854 | |
---|
| 855 | self.socket.setsockopt( socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20 ) |
---|
| 856 | |
---|
| 857 | self.hostport = ( host, int( port ) ) |
---|
| 858 | self.type = GMETRIC_DEFAULT_TYPE |
---|
| 859 | self.unitstr = '' |
---|
| 860 | self.slopestr = 'both' |
---|
| 861 | self.tmax = 60 |
---|
| 862 | |
---|
| 863 | def checkHostProtocol( self, ip ): |
---|
| 864 | |
---|
| 865 | MULTICAST_ADDRESS_MIN = ( "224", "0", "0", "0" ) |
---|
| 866 | MULTICAST_ADDRESS_MAX = ( "239", "255", "255", "255" ) |
---|
| 867 | |
---|
| 868 | ip_fields = ip.split( '.' ) |
---|
| 869 | |
---|
| 870 | if ip_fields >= MULTICAST_ADDRESS_MIN and ip_fields <= MULTICAST_ADDRESS_MAX: |
---|
| 871 | |
---|
| 872 | return 'multicast' |
---|
| 873 | else: |
---|
| 874 | return 'udp' |
---|
| 875 | |
---|
| 876 | def send( self, name, value, dmax ): |
---|
| 877 | |
---|
| 878 | msg = self.makexdr( name, value, self.type, self.unitstr, self.slopestr, self.tmax, dmax ) |
---|
| 879 | |
---|
| 880 | return self.socket.sendto( msg, self.hostport ) |
---|
| 881 | |
---|
| 882 | def makexdr( self, name, value, typestr, unitstr, slopestr, tmax, dmax ): |
---|
| 883 | |
---|
| 884 | if slopestr not in self.slope: |
---|
| 885 | |
---|
| 886 | raise ValueError( "Slope must be one of: " + str( self.slope.keys() ) ) |
---|
| 887 | |
---|
| 888 | if typestr not in self.type: |
---|
| 889 | |
---|
| 890 | raise ValueError( "Type must be one of: " + str( self.type ) ) |
---|
| 891 | |
---|
| 892 | if len( name ) == 0: |
---|
| 893 | |
---|
| 894 | raise ValueError( "Name must be non-empty" ) |
---|
| 895 | |
---|
| 896 | self.msg.reset() |
---|
| 897 | self.msg.pack_int( 0 ) |
---|
| 898 | self.msg.pack_string( typestr ) |
---|
| 899 | self.msg.pack_string( name ) |
---|
| 900 | self.msg.pack_string( str( value ) ) |
---|
| 901 | self.msg.pack_string( unitstr ) |
---|
| 902 | self.msg.pack_int( self.slope[ slopestr ] ) |
---|
| 903 | self.msg.pack_uint( int( tmax ) ) |
---|
| 904 | self.msg.pack_uint( int( dmax ) ) |
---|
| 905 | |
---|
| 906 | return self.msg.get_buffer() |
---|
| 907 | |
---|
[26] | 908 | def printTime( ): |
---|
[354] | 909 | |
---|
[65] | 910 | """Print current time/date in human readable format for log/debug""" |
---|
[26] | 911 | |
---|
| 912 | return time.strftime("%a, %d %b %Y %H:%M:%S") |
---|
| 913 | |
---|
| 914 | def debug_msg( level, msg ): |
---|
[354] | 915 | |
---|
[65] | 916 | """Print msg if at or above current debug level""" |
---|
[26] | 917 | |
---|
| 918 | if (DEBUG_LEVEL >= level): |
---|
| 919 | sys.stderr.write( msg + '\n' ) |
---|
| 920 | |
---|
[307] | 921 | def write_pidfile(): |
---|
| 922 | |
---|
| 923 | # Write pidfile if PIDFILE exists |
---|
| 924 | if PIDFILE: |
---|
| 925 | |
---|
[354] | 926 | pid = os.getpid() |
---|
| 927 | |
---|
| 928 | pidfile = open(PIDFILE, 'w') |
---|
| 929 | |
---|
| 930 | pidfile.write( str( pid ) ) |
---|
[307] | 931 | pidfile.close() |
---|
| 932 | |
---|
[23] | 933 | def main(): |
---|
[354] | 934 | |
---|
[65] | 935 | """Application start""" |
---|
[23] | 936 | |
---|
[352] | 937 | global PBSQuery, PBSError |
---|
[256] | 938 | |
---|
[212] | 939 | if not processArgs( sys.argv[1:] ): |
---|
[354] | 940 | |
---|
[212] | 941 | sys.exit( 1 ) |
---|
| 942 | |
---|
[256] | 943 | if BATCH_API == 'pbs': |
---|
| 944 | |
---|
| 945 | try: |
---|
[282] | 946 | from PBSQuery import PBSQuery, PBSError |
---|
[256] | 947 | |
---|
| 948 | except ImportError: |
---|
| 949 | |
---|
| 950 | debug_msg( 0, "fatal error: BATCH_API set to 'pbs' but python module 'pbs_python' is not installed" ) |
---|
| 951 | sys.exit( 1 ) |
---|
| 952 | |
---|
| 953 | gather = PbsDataGatherer() |
---|
| 954 | |
---|
| 955 | elif BATCH_API == 'sge': |
---|
| 956 | |
---|
[347] | 957 | gather = SgeDataGatherer() |
---|
[256] | 958 | |
---|
| 959 | else: |
---|
| 960 | debug_msg( 0, "fatal error: unknown BATCH_API '" + BATCH_API + "' is not supported" ) |
---|
[354] | 961 | |
---|
[256] | 962 | sys.exit( 1 ) |
---|
| 963 | |
---|
[26] | 964 | if DAEMONIZE: |
---|
[354] | 965 | |
---|
[26] | 966 | gather.daemon() |
---|
| 967 | else: |
---|
| 968 | gather.run() |
---|
[23] | 969 | |
---|
[256] | 970 | # wh00t? someone started me! :) |
---|
[65] | 971 | # |
---|
[23] | 972 | if __name__ == '__main__': |
---|
| 973 | main() |
---|