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