[3] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[70] | 3 | import xml.sax |
---|
[71] | 4 | import xml.sax.handler |
---|
[5] | 5 | import socket |
---|
| 6 | import sys |
---|
[169] | 7 | import syslog |
---|
[9] | 8 | import string |
---|
[12] | 9 | import os |
---|
| 10 | import os.path |
---|
[17] | 11 | import time |
---|
[36] | 12 | import threading |
---|
[38] | 13 | import random |
---|
[40] | 14 | from types import * |
---|
[60] | 15 | import DBClass |
---|
[3] | 16 | |
---|
[169] | 17 | # Specify debugging level here (only when _not_ DAEMONIZE) |
---|
[8] | 18 | # |
---|
[38] | 19 | # 11 = XML: metrics |
---|
| 20 | # 10 = XML: host, cluster, grid, ganglia |
---|
| 21 | # 9 = RRD activity, gmetad config parsing |
---|
[40] | 22 | # 8 = RRD file activity |
---|
[84] | 23 | # 6 = SQL |
---|
[87] | 24 | # 1 = daemon threading |
---|
[169] | 25 | # 0 = errors |
---|
[8] | 26 | # |
---|
[169] | 27 | # default: 0 |
---|
[102] | 28 | DEBUG_LEVEL = 1 |
---|
[6] | 29 | |
---|
[169] | 30 | # Enable logging to syslog? |
---|
| 31 | # |
---|
| 32 | USE_SYSLOG = 1 |
---|
| 33 | |
---|
| 34 | # What level msg'es should be logged to syslog? |
---|
| 35 | # |
---|
| 36 | # default: lvl 0 (errors) |
---|
| 37 | # |
---|
| 38 | SYSLOG_LEVEL = 0 |
---|
| 39 | |
---|
| 40 | # Which facility to use in syslog |
---|
| 41 | # |
---|
| 42 | # Syntax I.e.: |
---|
| 43 | # LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_LPR, |
---|
| 44 | # LOG_NEWS, LOG_UUCP, LOG_CRON and LOG_LOCAL0 to LOG_LOCAL7 |
---|
| 45 | # |
---|
| 46 | SYSLOG_FACILITY = syslog.LOG_DAEMON |
---|
| 47 | |
---|
[9] | 48 | # Where is the gmetad.conf located |
---|
| 49 | # |
---|
| 50 | GMETAD_CONF = '/etc/gmetad.conf' |
---|
| 51 | |
---|
[60] | 52 | # Where to grab XML data from |
---|
| 53 | # Normally: local gmetad (port 8651) |
---|
| 54 | # |
---|
[63] | 55 | # Syntax: <hostname>:<port> |
---|
| 56 | # |
---|
| 57 | ARCHIVE_XMLSOURCE = "localhost:8651" |
---|
[60] | 58 | |
---|
[9] | 59 | # List of data_source names to archive for |
---|
| 60 | # |
---|
[63] | 61 | # Syntax: [ "<clustername>", "<clustername>" ] |
---|
| 62 | # |
---|
[60] | 63 | ARCHIVE_DATASOURCES = [ "LISA Cluster" ] |
---|
[9] | 64 | |
---|
[60] | 65 | # Where to store the archived rrd's |
---|
| 66 | # |
---|
| 67 | ARCHIVE_PATH = '/data/toga/rrds' |
---|
| 68 | |
---|
[13] | 69 | # Amount of hours to store in one single archived .rrd |
---|
[9] | 70 | # |
---|
[43] | 71 | ARCHIVE_HOURS_PER_RRD = 12 |
---|
[13] | 72 | |
---|
[63] | 73 | # Toga's SQL dbase to use |
---|
[60] | 74 | # |
---|
[63] | 75 | # Syntax: <hostname>/<database> |
---|
[60] | 76 | # |
---|
[63] | 77 | TOGA_SQL_DBASE = "localhost/toga" |
---|
[60] | 78 | |
---|
[22] | 79 | # Wether or not to run as a daemon in background |
---|
| 80 | # |
---|
[169] | 81 | DAEMONIZE = 1 |
---|
[22] | 82 | |
---|
[17] | 83 | ###################### |
---|
| 84 | # # |
---|
| 85 | # Configuration ends # |
---|
| 86 | # # |
---|
| 87 | ###################### |
---|
[13] | 88 | |
---|
[60] | 89 | ### |
---|
| 90 | # You'll only want to change anything below here unless you |
---|
[63] | 91 | # know what you are doing (i.e. your name is Ramon Bastiaans :D ) |
---|
[60] | 92 | ### |
---|
| 93 | |
---|
[17] | 94 | # What XML data types not to store |
---|
[13] | 95 | # |
---|
[17] | 96 | UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ] |
---|
[9] | 97 | |
---|
[47] | 98 | # Maximum time (in seconds) a parsethread may run |
---|
| 99 | # |
---|
| 100 | PARSE_TIMEOUT = 60 |
---|
| 101 | |
---|
| 102 | # Maximum time (in seconds) a storethread may run |
---|
| 103 | # |
---|
| 104 | STORE_TIMEOUT = 360 |
---|
| 105 | |
---|
[8] | 106 | """ |
---|
| 107 | This is TOrque-GAnglia's data Daemon |
---|
| 108 | """ |
---|
| 109 | |
---|
[84] | 110 | class DataSQLStore: |
---|
| 111 | |
---|
| 112 | db_vars = None |
---|
| 113 | dbc = None |
---|
| 114 | |
---|
| 115 | def __init__( self, hostname, database ): |
---|
| 116 | |
---|
| 117 | self.db_vars = DBClass.InitVars(DataBaseName=database, |
---|
| 118 | User='root', |
---|
| 119 | Host=hostname, |
---|
| 120 | Password='', |
---|
| 121 | Dictionary='true') |
---|
| 122 | |
---|
| 123 | try: |
---|
| 124 | self.dbc = DBClass.DB(self.db_vars) |
---|
| 125 | except DBClass.DBError, details: |
---|
[169] | 126 | debug_msg( 0, 'FATAL ERROR: Unable to connect to database!: ' +str(details) ) |
---|
[84] | 127 | sys.exit(1) |
---|
| 128 | |
---|
| 129 | def setDatabase(self, statement): |
---|
| 130 | ret = self.doDatabase('set', statement) |
---|
| 131 | return ret |
---|
| 132 | |
---|
| 133 | def getDatabase(self, statement): |
---|
| 134 | ret = self.doDatabase('get', statement) |
---|
| 135 | return ret |
---|
| 136 | |
---|
| 137 | def doDatabase(self, type, statement): |
---|
| 138 | |
---|
| 139 | debug_msg( 6, 'doDatabase(): %s: %s' %(type, statement) ) |
---|
| 140 | try: |
---|
| 141 | if type == 'set': |
---|
| 142 | result = self.dbc.Set( statement ) |
---|
| 143 | self.dbc.Commit() |
---|
| 144 | elif type == 'get': |
---|
| 145 | result = self.dbc.Get( statement ) |
---|
| 146 | |
---|
| 147 | except DBClass.DBError, detail: |
---|
| 148 | operation = statement.split(' ')[0] |
---|
[169] | 149 | debug_msg( 0, 'FATAL ERROR: ' +operation+ ' on database failed while doing ['+statement+'] full msg: '+str(detail) ) |
---|
[84] | 150 | sys.exit(1) |
---|
| 151 | |
---|
| 152 | debug_msg( 6, 'doDatabase(): result: %s' %(result) ) |
---|
| 153 | return result |
---|
| 154 | |
---|
| 155 | def getNodeId( self, hostname ): |
---|
| 156 | |
---|
[89] | 157 | id = self.getDatabase( "SELECT node_id FROM nodes WHERE node_hostname = '%s'" %hostname ) |
---|
[84] | 158 | |
---|
[89] | 159 | if len( id ) > 0: |
---|
[84] | 160 | |
---|
[89] | 161 | id = id[0][0] |
---|
| 162 | |
---|
[84] | 163 | return id |
---|
| 164 | else: |
---|
| 165 | return None |
---|
| 166 | |
---|
| 167 | def getNodeIds( self, hostnames ): |
---|
| 168 | |
---|
| 169 | ids = [ ] |
---|
| 170 | |
---|
| 171 | for node in hostnames: |
---|
| 172 | |
---|
| 173 | id = self.getNodeId( node ) |
---|
| 174 | |
---|
| 175 | if id: |
---|
| 176 | ids.append( id ) |
---|
| 177 | |
---|
| 178 | return ids |
---|
| 179 | |
---|
| 180 | def getJobId( self, jobid ): |
---|
| 181 | |
---|
| 182 | id = self.getDatabase( "SELECT job_id FROM jobs WHERE job_id = '%s'" %jobid ) |
---|
| 183 | |
---|
| 184 | if id: |
---|
[89] | 185 | id = id[0][0] |
---|
[84] | 186 | |
---|
| 187 | return id |
---|
| 188 | else: |
---|
| 189 | return None |
---|
| 190 | |
---|
| 191 | def addJob( self, job_id, jobattrs ): |
---|
| 192 | |
---|
| 193 | if not self.getJobId( job_id ): |
---|
| 194 | |
---|
| 195 | self.mutateJob( 'insert', job_id, jobattrs ) |
---|
| 196 | else: |
---|
| 197 | self.mutateJob( 'update', job_id, jobattrs ) |
---|
| 198 | |
---|
| 199 | def mutateJob( self, action, job_id, jobattrs ): |
---|
| 200 | |
---|
| 201 | job_values = [ 'name', 'queue', 'owner', 'requested_time', 'requested_memory', 'ppn', 'status', 'start_timestamp', 'stop_timestamp' ] |
---|
| 202 | |
---|
| 203 | insert_col_str = 'job_id' |
---|
| 204 | insert_val_str = "'%s'" %job_id |
---|
| 205 | update_str = None |
---|
| 206 | |
---|
| 207 | debug_msg( 6, 'mutateJob(): %s %s' %(action,job_id)) |
---|
| 208 | |
---|
[99] | 209 | ids = [ ] |
---|
| 210 | |
---|
[84] | 211 | for valname, value in jobattrs.items(): |
---|
| 212 | |
---|
[96] | 213 | if valname in job_values and value != '': |
---|
[84] | 214 | |
---|
| 215 | column_name = 'job_' + valname |
---|
| 216 | |
---|
| 217 | if action == 'insert': |
---|
| 218 | |
---|
| 219 | if not insert_col_str: |
---|
| 220 | insert_col_str = column_name |
---|
| 221 | else: |
---|
| 222 | insert_col_str = insert_col_str + ',' + column_name |
---|
| 223 | |
---|
| 224 | if not insert_val_str: |
---|
| 225 | insert_val_str = value |
---|
| 226 | else: |
---|
| 227 | insert_val_str = insert_val_str + ",'%s'" %value |
---|
| 228 | |
---|
| 229 | elif action == 'update': |
---|
| 230 | |
---|
| 231 | if not update_str: |
---|
| 232 | update_str = "%s='%s'" %(column_name, value) |
---|
| 233 | else: |
---|
| 234 | update_str = update_str + ",%s='%s'" %(column_name, value) |
---|
| 235 | |
---|
[90] | 236 | elif valname == 'nodes' and value: |
---|
[84] | 237 | |
---|
[154] | 238 | ids = self.addNodes( value, jobattrs['domain'] ) |
---|
[86] | 239 | node_list = value |
---|
[84] | 240 | |
---|
| 241 | if action == 'insert': |
---|
| 242 | |
---|
| 243 | self.setDatabase( "INSERT INTO jobs ( %s ) VALUES ( %s )" %( insert_col_str, insert_val_str ) ) |
---|
[86] | 244 | |
---|
[99] | 245 | if len( ids ) > 0: |
---|
| 246 | self.addJobNodes( job_id, ids ) |
---|
| 247 | |
---|
[84] | 248 | elif action == 'update': |
---|
| 249 | |
---|
[89] | 250 | self.setDatabase( "UPDATE jobs SET %s WHERE job_id=%s" %(update_str, job_id) ) |
---|
[84] | 251 | |
---|
[154] | 252 | def addNodes( self, hostnames, domain ): |
---|
[84] | 253 | |
---|
[98] | 254 | ids = [ ] |
---|
| 255 | |
---|
[84] | 256 | for node in hostnames: |
---|
| 257 | |
---|
[154] | 258 | node = '%s.%s' %( node, domain ) |
---|
[84] | 259 | id = self.getNodeId( node ) |
---|
| 260 | |
---|
| 261 | if not id: |
---|
| 262 | self.setDatabase( "INSERT INTO nodes ( node_hostname ) VALUES ( '%s' )" %node ) |
---|
[98] | 263 | id = self.getNodeId( node ) |
---|
[84] | 264 | |
---|
[98] | 265 | ids.append( id ) |
---|
| 266 | |
---|
| 267 | return ids |
---|
| 268 | |
---|
[86] | 269 | def addJobNodes( self, jobid, nodes ): |
---|
| 270 | |
---|
| 271 | for node in nodes: |
---|
| 272 | self.addJobNode( jobid, node ) |
---|
| 273 | |
---|
[84] | 274 | def addJobNode( self, jobid, nodeid ): |
---|
| 275 | |
---|
| 276 | self.setDatabase( "INSERT INTO job_nodes (job_id,node_id) VALUES ( %s,%s )" %(jobid, nodeid) ) |
---|
| 277 | |
---|
| 278 | def storeJobInfo( self, jobid, jobattrs ): |
---|
| 279 | |
---|
| 280 | self.addJob( jobid, jobattrs ) |
---|
| 281 | |
---|
[37] | 282 | class RRDMutator: |
---|
[63] | 283 | """A class for performing RRD mutations""" |
---|
[37] | 284 | |
---|
| 285 | binary = '/usr/bin/rrdtool' |
---|
| 286 | |
---|
| 287 | def __init__( self, binary=None ): |
---|
[63] | 288 | """Set alternate binary if supplied""" |
---|
[37] | 289 | |
---|
| 290 | if binary: |
---|
| 291 | self.binary = binary |
---|
| 292 | |
---|
| 293 | def create( self, filename, args ): |
---|
[63] | 294 | """Create a new rrd with args""" |
---|
| 295 | |
---|
[40] | 296 | return self.perform( 'create', '"' + filename + '"', args ) |
---|
[37] | 297 | |
---|
| 298 | def update( self, filename, args ): |
---|
[63] | 299 | """Update a rrd with args""" |
---|
| 300 | |
---|
[40] | 301 | return self.perform( 'update', '"' + filename + '"', args ) |
---|
[37] | 302 | |
---|
[42] | 303 | def grabLastUpdate( self, filename ): |
---|
[63] | 304 | """Determine the last update time of filename rrd""" |
---|
[42] | 305 | |
---|
| 306 | last_update = 0 |
---|
| 307 | |
---|
[53] | 308 | debug_msg( 8, self.binary + ' info "' + filename + '"' ) |
---|
| 309 | |
---|
[42] | 310 | for line in os.popen( self.binary + ' info "' + filename + '"' ).readlines(): |
---|
| 311 | |
---|
| 312 | if line.find( 'last_update') != -1: |
---|
| 313 | |
---|
| 314 | last_update = line.split( ' = ' )[1] |
---|
| 315 | |
---|
| 316 | if last_update: |
---|
| 317 | return last_update |
---|
| 318 | else: |
---|
| 319 | return 0 |
---|
| 320 | |
---|
[40] | 321 | def perform( self, action, filename, args ): |
---|
[63] | 322 | """Perform action on rrd filename with args""" |
---|
[37] | 323 | |
---|
| 324 | arg_string = None |
---|
| 325 | |
---|
[40] | 326 | if type( args ) is not ListType: |
---|
| 327 | debug_msg( 8, 'Arguments needs to be of type List' ) |
---|
| 328 | return 1 |
---|
| 329 | |
---|
[37] | 330 | for arg in args: |
---|
| 331 | |
---|
| 332 | if not arg_string: |
---|
| 333 | |
---|
| 334 | arg_string = arg |
---|
| 335 | else: |
---|
| 336 | arg_string = arg_string + ' ' + arg |
---|
| 337 | |
---|
[54] | 338 | debug_msg( 8, self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ) |
---|
[37] | 339 | |
---|
[146] | 340 | cmd = os.popen( self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ) |
---|
| 341 | lines = cmd.readlines() |
---|
| 342 | cmd.close() |
---|
[37] | 343 | |
---|
[146] | 344 | for line in lines: |
---|
| 345 | |
---|
[37] | 346 | if line.find( 'ERROR' ) != -1: |
---|
| 347 | |
---|
| 348 | error_msg = string.join( line.split( ' ' )[1:] ) |
---|
[40] | 349 | debug_msg( 8, error_msg ) |
---|
[37] | 350 | return 1 |
---|
| 351 | |
---|
| 352 | return 0 |
---|
| 353 | |
---|
[78] | 354 | class XMLProcessor: |
---|
| 355 | """Skeleton class for XML processor's""" |
---|
| 356 | |
---|
| 357 | def run( self ): |
---|
| 358 | """Do main processing of XML here""" |
---|
| 359 | |
---|
| 360 | pass |
---|
| 361 | |
---|
| 362 | class TorqueXMLProcessor( XMLProcessor ): |
---|
| 363 | """Main class for processing XML and acting with it""" |
---|
| 364 | |
---|
| 365 | def __init__( self ): |
---|
| 366 | """Setup initial XML connection and handlers""" |
---|
| 367 | |
---|
| 368 | self.myXMLGatherer = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) |
---|
| 369 | self.myXMLSource = self.myXMLGatherer.getFileObject() |
---|
| 370 | self.myXMLHandler = TorqueXMLHandler() |
---|
| 371 | self.myXMLError = XMLErrorHandler() |
---|
[87] | 372 | self.config = GangliaConfigParser( GMETAD_CONF ) |
---|
[78] | 373 | |
---|
| 374 | def run( self ): |
---|
| 375 | """Main XML processing""" |
---|
| 376 | |
---|
[169] | 377 | debug_msg( 1, 'torque_xml_thread(): started.' ) |
---|
[87] | 378 | |
---|
[78] | 379 | while( 1 ): |
---|
| 380 | |
---|
| 381 | self.myXMLSource = self.myXMLGatherer.getFileObject() |
---|
[169] | 382 | debug_msg( 1, 'torque_xml_thread(): Parsing..' ) |
---|
[78] | 383 | xml.sax.parse( self.myXMLSource, self.myXMLHandler, self.myXMLError ) |
---|
[169] | 384 | debug_msg( 1, 'torque_xml_thread(): Done parsing.' ) |
---|
| 385 | debug_msg( 1, 'torque_xml_thread(): Sleeping.. (%ss)' %(str( self.config.getLowestInterval() ) ) ) |
---|
[87] | 386 | time.sleep( self.config.getLowestInterval() ) |
---|
[78] | 387 | |
---|
[71] | 388 | class TorqueXMLHandler( xml.sax.handler.ContentHandler ): |
---|
[63] | 389 | """Parse Torque's jobinfo XML from our plugin""" |
---|
| 390 | |
---|
[72] | 391 | jobAttrs = { } |
---|
| 392 | |
---|
[84] | 393 | def __init__( self ): |
---|
| 394 | |
---|
| 395 | self.ds = DataSQLStore( TOGA_SQL_DBASE.split( '/' )[0], TOGA_SQL_DBASE.split( '/' )[1] ) |
---|
[98] | 396 | self.jobs_processed = [ ] |
---|
| 397 | self.jobs_to_store = [ ] |
---|
[84] | 398 | |
---|
[63] | 399 | def startElement( self, name, attrs ): |
---|
| 400 | """ |
---|
| 401 | This XML will be all gmetric XML |
---|
| 402 | so there will be no specific start/end element |
---|
| 403 | just one XML statement with all info |
---|
| 404 | """ |
---|
| 405 | |
---|
| 406 | heartbeat = 0 |
---|
[73] | 407 | |
---|
| 408 | jobinfo = { } |
---|
[63] | 409 | |
---|
| 410 | if name == 'METRIC': |
---|
| 411 | |
---|
[70] | 412 | metricname = attrs.get( 'NAME', "" ) |
---|
[63] | 413 | |
---|
| 414 | if metricname == 'TOGA-HEARTBEAT': |
---|
[70] | 415 | self.heartbeat = attrs.get( 'VAL', "" ) |
---|
[63] | 416 | |
---|
[70] | 417 | elif metricname.find( 'TOGA-JOB' ) != -1: |
---|
[63] | 418 | |
---|
[72] | 419 | job_id = metricname.split( 'TOGA-JOB-' )[1] |
---|
[63] | 420 | val = attrs.get( 'VAL', "" ) |
---|
| 421 | |
---|
[96] | 422 | if not job_id in self.jobs_processed: |
---|
| 423 | self.jobs_processed.append( job_id ) |
---|
| 424 | |
---|
[73] | 425 | check_change = 0 |
---|
| 426 | |
---|
| 427 | if self.jobAttrs.has_key( job_id ): |
---|
| 428 | check_change = 1 |
---|
| 429 | |
---|
[63] | 430 | valinfo = val.split( ' ' ) |
---|
| 431 | |
---|
| 432 | for myval in valinfo: |
---|
| 433 | |
---|
[84] | 434 | if len( myval.split( '=' ) ) > 1: |
---|
[63] | 435 | |
---|
[84] | 436 | valname = myval.split( '=' )[0] |
---|
| 437 | value = myval.split( '=' )[1] |
---|
[70] | 438 | |
---|
[84] | 439 | if valname == 'nodes': |
---|
| 440 | value = value.split( ';' ) |
---|
[72] | 441 | |
---|
[84] | 442 | jobinfo[ valname ] = value |
---|
| 443 | |
---|
[73] | 444 | if check_change: |
---|
[100] | 445 | if self.jobinfoChanged( self.jobAttrs, job_id, jobinfo ) and self.jobAttrs[ job_id ]['status'] != 'E': |
---|
| 446 | self.jobAttrs[ job_id ]['stop_timestamp'] = '' |
---|
[82] | 447 | self.jobAttrs[ job_id ] = self.setJobAttrs( self.jobAttrs[ job_id ], jobinfo ) |
---|
[84] | 448 | if not job_id in self.jobs_to_store: |
---|
| 449 | self.jobs_to_store.append( job_id ) |
---|
| 450 | |
---|
[102] | 451 | debug_msg( 6, 'jobinfo for job %s has changed' %job_id ) |
---|
[73] | 452 | else: |
---|
| 453 | self.jobAttrs[ job_id ] = jobinfo |
---|
[84] | 454 | |
---|
| 455 | if not job_id in self.jobs_to_store: |
---|
| 456 | self.jobs_to_store.append( job_id ) |
---|
| 457 | |
---|
[102] | 458 | debug_msg( 6, 'jobinfo for job %s has changed' %job_id ) |
---|
[73] | 459 | |
---|
[77] | 460 | def endDocument( self ): |
---|
[74] | 461 | """When all metrics have gone, check if any jobs have finished""" |
---|
[72] | 462 | |
---|
[74] | 463 | for jobid, jobinfo in self.jobAttrs.items(): |
---|
| 464 | |
---|
| 465 | # This is an old job, not in current jobinfo list anymore |
---|
| 466 | # it must have finished, since we _did_ get a new heartbeat |
---|
| 467 | # |
---|
[96] | 468 | mytime = int( jobinfo['reported'] ) + int( jobinfo['poll_interval'] ) |
---|
[102] | 469 | |
---|
[96] | 470 | if mytime < self.heartbeat and jobid not in self.jobs_processed and jobinfo['status'] == 'R': |
---|
[74] | 471 | |
---|
[97] | 472 | if not jobid in self.jobs_processed: |
---|
[96] | 473 | self.jobs_processed.append( jobid ) |
---|
| 474 | |
---|
[74] | 475 | self.jobAttrs[ jobid ]['status'] = 'F' |
---|
[151] | 476 | self.jobAttrs[ jobid ]['stop_timestamp'] = str( mytime ) |
---|
[96] | 477 | |
---|
[84] | 478 | if not jobid in self.jobs_to_store: |
---|
| 479 | self.jobs_to_store.append( jobid ) |
---|
[74] | 480 | |
---|
[169] | 481 | debug_msg( 1, 'torque_xml_thread(): Storing..' ) |
---|
[87] | 482 | |
---|
[84] | 483 | for jobid in self.jobs_to_store: |
---|
[155] | 484 | if self.jobAttrs[ jobid ]['status'] in [ 'R', 'Q', 'F' ]: |
---|
[151] | 485 | self.ds.storeJobInfo( jobid, self.jobAttrs[ jobid ] ) |
---|
[84] | 486 | |
---|
[169] | 487 | debug_msg( 1, 'torque_xml_thread(): Done storing.' ) |
---|
[87] | 488 | |
---|
[96] | 489 | self.jobs_processed = [ ] |
---|
[84] | 490 | self.jobs_to_store = [ ] |
---|
| 491 | |
---|
[82] | 492 | def setJobAttrs( self, old, new ): |
---|
| 493 | """ |
---|
| 494 | Set new job attributes in old, but not lose existing fields |
---|
| 495 | if old attributes doesn't have those |
---|
| 496 | """ |
---|
| 497 | |
---|
| 498 | for valname, value in new.items(): |
---|
| 499 | old[ valname ] = value |
---|
| 500 | |
---|
| 501 | return old |
---|
| 502 | |
---|
| 503 | |
---|
[73] | 504 | def jobinfoChanged( self, jobattrs, jobid, jobinfo ): |
---|
[74] | 505 | """ |
---|
| 506 | Check if jobinfo has changed from jobattrs[jobid] |
---|
| 507 | if it's report time is bigger than previous one |
---|
| 508 | and it is report time is recent (equal to heartbeat) |
---|
| 509 | """ |
---|
[72] | 510 | |
---|
[87] | 511 | ignore_changes = [ 'reported' ] |
---|
| 512 | |
---|
[73] | 513 | if jobattrs.has_key( jobid ): |
---|
| 514 | |
---|
| 515 | for valname, value in jobinfo.items(): |
---|
| 516 | |
---|
[87] | 517 | if valname not in ignore_changes: |
---|
[73] | 518 | |
---|
[87] | 519 | if jobattrs[ jobid ].has_key( valname ): |
---|
[73] | 520 | |
---|
[87] | 521 | if value != jobattrs[ jobid ][ valname ]: |
---|
[73] | 522 | |
---|
[87] | 523 | if jobinfo['reported'] > jobattrs[ jobid ][ 'reported' ] and jobinfo['reported'] == self.heartbeat: |
---|
| 524 | return 1 |
---|
[73] | 525 | |
---|
[87] | 526 | else: |
---|
| 527 | return 1 |
---|
| 528 | |
---|
[73] | 529 | return 0 |
---|
| 530 | |
---|
[71] | 531 | class GangliaXMLHandler( xml.sax.handler.ContentHandler ): |
---|
[63] | 532 | """Parse Ganglia's XML""" |
---|
[3] | 533 | |
---|
[33] | 534 | def __init__( self, config ): |
---|
[63] | 535 | """Setup initial variables and gather info on existing rrd archive""" |
---|
| 536 | |
---|
[33] | 537 | self.config = config |
---|
[35] | 538 | self.clusters = { } |
---|
[169] | 539 | debug_msg( 1, 'Checking existing toga rrd archive..' ) |
---|
[44] | 540 | self.gatherClusters() |
---|
[169] | 541 | debug_msg( 1, 'Check done.' ) |
---|
[33] | 542 | |
---|
[44] | 543 | def gatherClusters( self ): |
---|
[63] | 544 | """Find all existing clusters in archive dir""" |
---|
[44] | 545 | |
---|
[45] | 546 | archive_dir = check_dir(ARCHIVE_PATH) |
---|
[44] | 547 | |
---|
| 548 | hosts = [ ] |
---|
| 549 | |
---|
| 550 | if os.path.exists( archive_dir ): |
---|
| 551 | |
---|
| 552 | dirlist = os.listdir( archive_dir ) |
---|
| 553 | |
---|
| 554 | for item in dirlist: |
---|
| 555 | |
---|
| 556 | clustername = item |
---|
| 557 | |
---|
[60] | 558 | if not self.clusters.has_key( clustername ) and clustername in ARCHIVE_DATASOURCES: |
---|
[44] | 559 | |
---|
| 560 | self.clusters[ clustername ] = RRDHandler( self.config, clustername ) |
---|
| 561 | |
---|
[6] | 562 | def startElement( self, name, attrs ): |
---|
[63] | 563 | """Memorize appropriate data from xml start tags""" |
---|
[3] | 564 | |
---|
[7] | 565 | if name == 'GANGLIA_XML': |
---|
[32] | 566 | |
---|
| 567 | self.XMLSource = attrs.get( 'SOURCE', "" ) |
---|
| 568 | self.gangliaVersion = attrs.get( 'VERSION', "" ) |
---|
| 569 | |
---|
[12] | 570 | debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) ) |
---|
[6] | 571 | |
---|
[7] | 572 | elif name == 'GRID': |
---|
[32] | 573 | |
---|
| 574 | self.gridName = attrs.get( 'NAME', "" ) |
---|
| 575 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
| 576 | |
---|
[12] | 577 | debug_msg( 10, '`-Grid found: %s' %( self.gridName ) ) |
---|
[6] | 578 | |
---|
[7] | 579 | elif name == 'CLUSTER': |
---|
[32] | 580 | |
---|
| 581 | self.clusterName = attrs.get( 'NAME', "" ) |
---|
| 582 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
| 583 | |
---|
[60] | 584 | if not self.clusters.has_key( self.clusterName ) and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[32] | 585 | |
---|
[34] | 586 | self.clusters[ self.clusterName ] = RRDHandler( self.config, self.clusterName ) |
---|
[33] | 587 | |
---|
[35] | 588 | debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) ) |
---|
[6] | 589 | |
---|
[60] | 590 | elif name == 'HOST' and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[32] | 591 | |
---|
| 592 | self.hostName = attrs.get( 'NAME', "" ) |
---|
| 593 | self.hostIp = attrs.get( 'IP', "" ) |
---|
| 594 | self.hostReported = attrs.get( 'REPORTED', "" ) |
---|
| 595 | |
---|
[12] | 596 | debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) ) |
---|
[6] | 597 | |
---|
[60] | 598 | elif name == 'METRIC' and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[6] | 599 | |
---|
[32] | 600 | type = attrs.get( 'TYPE', "" ) |
---|
[6] | 601 | |
---|
[32] | 602 | if type not in UNSUPPORTED_ARCHIVE_TYPES: |
---|
[3] | 603 | |
---|
[32] | 604 | myMetric = { } |
---|
| 605 | myMetric['name'] = attrs.get( 'NAME', "" ) |
---|
| 606 | myMetric['val'] = attrs.get( 'VAL', "" ) |
---|
| 607 | myMetric['time'] = self.hostReported |
---|
[3] | 608 | |
---|
[34] | 609 | self.clusters[ self.clusterName ].memMetric( self.hostName, myMetric ) |
---|
[3] | 610 | |
---|
[34] | 611 | debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) ) |
---|
[6] | 612 | |
---|
[34] | 613 | def storeMetrics( self ): |
---|
[63] | 614 | """Store metrics of each cluster rrd handler""" |
---|
[9] | 615 | |
---|
[34] | 616 | for clustername, rrdh in self.clusters.items(): |
---|
[16] | 617 | |
---|
[38] | 618 | ret = rrdh.storeMetrics() |
---|
[9] | 619 | |
---|
[38] | 620 | if ret: |
---|
| 621 | debug_msg( 9, 'An error occured while storing metrics for cluster %s' %clustername ) |
---|
| 622 | return 1 |
---|
| 623 | |
---|
| 624 | return 0 |
---|
| 625 | |
---|
[71] | 626 | class XMLErrorHandler( xml.sax.handler.ErrorHandler ): |
---|
| 627 | |
---|
| 628 | def error( self, exception ): |
---|
| 629 | """Recoverable error""" |
---|
| 630 | |
---|
[169] | 631 | debug_msg( 0, 'Recoverable XML error ' + str( exception ) + ' ignored.' ) |
---|
[71] | 632 | |
---|
| 633 | def fatalError( self, exception ): |
---|
| 634 | """Non-recoverable error""" |
---|
| 635 | |
---|
| 636 | exception_str = str( exception ) |
---|
| 637 | |
---|
| 638 | # Ignore 'no element found' errors |
---|
| 639 | if exception_str.find( 'no element found' ) != -1: |
---|
[169] | 640 | debug_msg( 0, 'No XML data found: Socket not (re)connected or datasource not available.' ) |
---|
[71] | 641 | return 0 |
---|
| 642 | |
---|
[169] | 643 | debug_msg( 0, 'Non-recoverable XML error ' + str( exception ) ) |
---|
[71] | 644 | sys.exit( 1 ) |
---|
| 645 | |
---|
| 646 | def warning( self, exception ): |
---|
| 647 | """Warning""" |
---|
| 648 | |
---|
| 649 | debug_msg( 0, 'Warning ' + str( exception ) ) |
---|
| 650 | |
---|
[78] | 651 | class XMLGatherer: |
---|
[63] | 652 | """Setup a connection and file object to Ganglia's XML""" |
---|
[3] | 653 | |
---|
[8] | 654 | s = None |
---|
[70] | 655 | fd = None |
---|
[8] | 656 | |
---|
| 657 | def __init__( self, host, port ): |
---|
[63] | 658 | """Store host and port for connection""" |
---|
[8] | 659 | |
---|
[5] | 660 | self.host = host |
---|
| 661 | self.port = port |
---|
[33] | 662 | self.connect() |
---|
[70] | 663 | self.makeFileDescriptor() |
---|
[3] | 664 | |
---|
[33] | 665 | def connect( self ): |
---|
[63] | 666 | """Setup connection to XML source""" |
---|
[8] | 667 | |
---|
| 668 | for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): |
---|
[32] | 669 | |
---|
[5] | 670 | af, socktype, proto, canonname, sa = res |
---|
[32] | 671 | |
---|
[5] | 672 | try: |
---|
[32] | 673 | |
---|
[8] | 674 | self.s = socket.socket( af, socktype, proto ) |
---|
[32] | 675 | |
---|
[5] | 676 | except socket.error, msg: |
---|
[32] | 677 | |
---|
[8] | 678 | self.s = None |
---|
[5] | 679 | continue |
---|
[32] | 680 | |
---|
[5] | 681 | try: |
---|
[32] | 682 | |
---|
[8] | 683 | self.s.connect( sa ) |
---|
[32] | 684 | |
---|
[5] | 685 | except socket.error, msg: |
---|
[32] | 686 | |
---|
[70] | 687 | self.disconnect() |
---|
[5] | 688 | continue |
---|
[32] | 689 | |
---|
[5] | 690 | break |
---|
[3] | 691 | |
---|
[8] | 692 | if self.s is None: |
---|
[32] | 693 | |
---|
[169] | 694 | debug_msg( 0, 'ERROR: Could not open socket or unable to connect to datasource!' ) |
---|
[33] | 695 | sys.exit( 1 ) |
---|
[5] | 696 | |
---|
[33] | 697 | def disconnect( self ): |
---|
[63] | 698 | """Close socket""" |
---|
[33] | 699 | |
---|
| 700 | if self.s: |
---|
[71] | 701 | self.s.shutdown( 2 ) |
---|
[33] | 702 | self.s.close() |
---|
| 703 | self.s = None |
---|
| 704 | |
---|
| 705 | def __del__( self ): |
---|
[63] | 706 | """Kill the socket before we leave""" |
---|
[33] | 707 | |
---|
| 708 | self.disconnect() |
---|
| 709 | |
---|
[70] | 710 | def reconnect( self ): |
---|
| 711 | """Reconnect""" |
---|
[33] | 712 | |
---|
[38] | 713 | if self.s: |
---|
| 714 | self.disconnect() |
---|
[33] | 715 | |
---|
[70] | 716 | self.connect() |
---|
[5] | 717 | |
---|
[70] | 718 | def makeFileDescriptor( self ): |
---|
| 719 | """Make file descriptor that points to our socket connection""" |
---|
| 720 | |
---|
| 721 | self.reconnect() |
---|
| 722 | |
---|
| 723 | if self.s: |
---|
| 724 | self.fd = self.s.makefile( 'r' ) |
---|
| 725 | |
---|
| 726 | def getFileObject( self ): |
---|
| 727 | """Connect, and return a file object""" |
---|
| 728 | |
---|
[78] | 729 | self.makeFileDescriptor() |
---|
| 730 | |
---|
[70] | 731 | if self.fd: |
---|
| 732 | return self.fd |
---|
| 733 | |
---|
[78] | 734 | class GangliaXMLProcessor( XMLProcessor ): |
---|
[63] | 735 | """Main class for processing XML and acting with it""" |
---|
[5] | 736 | |
---|
[33] | 737 | def __init__( self ): |
---|
[63] | 738 | """Setup initial XML connection and handlers""" |
---|
[33] | 739 | |
---|
| 740 | self.config = GangliaConfigParser( GMETAD_CONF ) |
---|
| 741 | |
---|
[78] | 742 | self.myXMLGatherer = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) |
---|
[70] | 743 | self.myXMLSource = self.myXMLGatherer.getFileObject() |
---|
[78] | 744 | self.myXMLHandler = GangliaXMLHandler( self.config ) |
---|
| 745 | self.myXMLError = XMLErrorHandler() |
---|
[73] | 746 | |
---|
[9] | 747 | def run( self ): |
---|
[63] | 748 | """Main XML processing; start a xml and storethread""" |
---|
[8] | 749 | |
---|
[102] | 750 | xml_thread = threading.Thread( None, self.processXML, 'xmlthread' ) |
---|
| 751 | store_thread = threading.Thread( None, self.storeMetrics, 'storethread' ) |
---|
[22] | 752 | |
---|
[36] | 753 | while( 1 ): |
---|
| 754 | |
---|
[102] | 755 | if not xml_thread.isAlive(): |
---|
[36] | 756 | # Gather XML at the same interval as gmetad |
---|
| 757 | |
---|
| 758 | # threaded call to: self.processXML() |
---|
| 759 | # |
---|
[169] | 760 | try: |
---|
| 761 | xml_thread = threading.Thread( None, self.processXML, 'xml_thread' ) |
---|
| 762 | xml_thread.start() |
---|
| 763 | except threading.error, msg: |
---|
| 764 | debug_msg( 0, 'ERROR: Unable to start xml_thread!: '+str(msg)) |
---|
| 765 | #return 1 |
---|
[36] | 766 | |
---|
[102] | 767 | if not store_thread.isAlive(): |
---|
[55] | 768 | # Store metrics every .. sec |
---|
[36] | 769 | |
---|
[55] | 770 | # threaded call to: self.storeMetrics() |
---|
| 771 | # |
---|
[169] | 772 | try: |
---|
| 773 | store_thread = threading.Thread( None, self.storeMetrics, 'store_thread' ) |
---|
| 774 | store_thread.start() |
---|
| 775 | except threading.error, msg: |
---|
| 776 | debug_msg( 0, 'ERROR: Unable to start store_thread!: '+str(msg)) |
---|
| 777 | #return 1 |
---|
[36] | 778 | |
---|
| 779 | # Just sleep a sec here, to prevent daemon from going mad. We're all threads here anyway |
---|
| 780 | time.sleep( 1 ) |
---|
| 781 | |
---|
[33] | 782 | def storeMetrics( self ): |
---|
[63] | 783 | """Store metrics retained in memory to disk""" |
---|
[22] | 784 | |
---|
[63] | 785 | # Store metrics somewhere between every 360 and 640 seconds |
---|
[38] | 786 | # |
---|
[55] | 787 | STORE_INTERVAL = random.randint( 360, 640 ) |
---|
[22] | 788 | |
---|
[169] | 789 | try: |
---|
| 790 | store_metric_thread = threading.Thread( None, self.storeThread, 'store_metric_thread' ) |
---|
| 791 | store_metric_thread.start() |
---|
| 792 | except threading.error, msg: |
---|
| 793 | debug_msg( 0, 'ERROR: Unable to start ganglia_store_thread()!: '+str(msg) ) |
---|
| 794 | return 1 |
---|
[36] | 795 | |
---|
[169] | 796 | debug_msg( 1, 'ganglia_store_thread(): started.' ) |
---|
| 797 | |
---|
| 798 | debug_msg( 1, 'ganglia_store_thread(): Sleeping.. (%ss)' %STORE_INTERVAL ) |
---|
[36] | 799 | time.sleep( STORE_INTERVAL ) |
---|
[169] | 800 | debug_msg( 1, 'ganglia_store_thread(): Done sleeping.' ) |
---|
[36] | 801 | |
---|
[102] | 802 | if store_metric_thread.isAlive(): |
---|
[36] | 803 | |
---|
[169] | 804 | debug_msg( 1, 'ganglia_store_thread(): storemetricthread() still running, waiting to finish..' ) |
---|
[136] | 805 | store_metric_thread.join( STORE_TIMEOUT ) # Maximum time is for storing thread to finish |
---|
[169] | 806 | debug_msg( 1, 'ganglia_store_thread(): Done waiting.' ) |
---|
[36] | 807 | |
---|
[169] | 808 | debug_msg( 1, 'ganglia_store_thread(): finished.' ) |
---|
[36] | 809 | |
---|
| 810 | return 0 |
---|
| 811 | |
---|
[39] | 812 | def storeThread( self ): |
---|
[63] | 813 | """Actual metric storing thread""" |
---|
[39] | 814 | |
---|
[169] | 815 | debug_msg( 1, 'ganglia_store_metric_thread(): started.' ) |
---|
| 816 | debug_msg( 1, 'ganglia_store_metric_thread(): Storing data..' ) |
---|
[78] | 817 | ret = self.myXMLHandler.storeMetrics() |
---|
[169] | 818 | debug_msg( 1, 'ganglia_store_metric_thread(): Done storing.' ) |
---|
| 819 | debug_msg( 1, 'ganglia_store_metric_thread(): finished.' ) |
---|
[39] | 820 | |
---|
| 821 | return ret |
---|
| 822 | |
---|
[8] | 823 | def processXML( self ): |
---|
[63] | 824 | """Process XML""" |
---|
[8] | 825 | |
---|
[169] | 826 | try: |
---|
| 827 | parsethread = threading.Thread( None, self.parseThread, 'parsethread' ) |
---|
| 828 | parsethread.start() |
---|
| 829 | except threading.error, msg: |
---|
| 830 | debug_msg( 0, 'ERROR: Unable to start ganglia_xml_thread()!: ' + str(msg) ) |
---|
| 831 | return 1 |
---|
[8] | 832 | |
---|
[169] | 833 | debug_msg( 1, 'ganglia_xml_thread(): started.' ) |
---|
[36] | 834 | |
---|
[169] | 835 | debug_msg( 1, 'ganglia_xml_thread(): Sleeping.. (%ss)' %self.config.getLowestInterval() ) |
---|
[36] | 836 | time.sleep( float( self.config.getLowestInterval() ) ) |
---|
[169] | 837 | debug_msg( 1, 'ganglia_xml_thread(): Done sleeping.' ) |
---|
[36] | 838 | |
---|
| 839 | if parsethread.isAlive(): |
---|
| 840 | |
---|
[169] | 841 | debug_msg( 1, 'ganglia_xml_thread(): parsethread() still running, waiting (%ss) to finish..' %PARSE_TIMEOUT ) |
---|
[47] | 842 | parsethread.join( PARSE_TIMEOUT ) # Maximum time for XML thread to finish |
---|
[169] | 843 | debug_msg( 1, 'ganglia_xml_thread(): Done waiting.' ) |
---|
[36] | 844 | |
---|
[169] | 845 | debug_msg( 1, 'ganglia_xml_thread(): finished.' ) |
---|
[36] | 846 | |
---|
| 847 | return 0 |
---|
| 848 | |
---|
[39] | 849 | def parseThread( self ): |
---|
[63] | 850 | """Actual parsing thread""" |
---|
[39] | 851 | |
---|
[169] | 852 | debug_msg( 1, 'ganglia_parse_thread(): started.' ) |
---|
| 853 | debug_msg( 1, 'ganglia_parse_thread(): Parsing XML..' ) |
---|
[78] | 854 | self.myXMLSource = self.myXMLGatherer.getFileObject() |
---|
| 855 | ret = xml.sax.parse( self.myXMLSource, self.myXMLHandler, self.myXMLError ) |
---|
[169] | 856 | debug_msg( 1, 'ganglia_parse_thread(): Done parsing.' ) |
---|
| 857 | debug_msg( 1, 'ganglia_parse_thread(): finished.' ) |
---|
[39] | 858 | |
---|
| 859 | return ret |
---|
| 860 | |
---|
[9] | 861 | class GangliaConfigParser: |
---|
| 862 | |
---|
[34] | 863 | sources = [ ] |
---|
[9] | 864 | |
---|
| 865 | def __init__( self, config ): |
---|
[63] | 866 | """Parse some stuff from our gmetad's config, such as polling interval""" |
---|
[32] | 867 | |
---|
[9] | 868 | self.config = config |
---|
| 869 | self.parseValues() |
---|
| 870 | |
---|
[32] | 871 | def parseValues( self ): |
---|
[63] | 872 | """Parse certain values from gmetad.conf""" |
---|
[9] | 873 | |
---|
| 874 | readcfg = open( self.config, 'r' ) |
---|
| 875 | |
---|
| 876 | for line in readcfg.readlines(): |
---|
| 877 | |
---|
| 878 | if line.count( '"' ) > 1: |
---|
| 879 | |
---|
[10] | 880 | if line.find( 'data_source' ) != -1 and line[0] != '#': |
---|
[9] | 881 | |
---|
[11] | 882 | source = { } |
---|
| 883 | source['name'] = line.split( '"' )[1] |
---|
[9] | 884 | source_words = line.split( '"' )[2].split( ' ' ) |
---|
| 885 | |
---|
| 886 | for word in source_words: |
---|
| 887 | |
---|
| 888 | valid_interval = 1 |
---|
| 889 | |
---|
| 890 | for letter in word: |
---|
[32] | 891 | |
---|
[9] | 892 | if letter not in string.digits: |
---|
[32] | 893 | |
---|
[9] | 894 | valid_interval = 0 |
---|
| 895 | |
---|
[10] | 896 | if valid_interval and len(word) > 0: |
---|
[32] | 897 | |
---|
[9] | 898 | source['interval'] = word |
---|
[12] | 899 | debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) ) |
---|
[33] | 900 | |
---|
| 901 | # No interval found, use Ganglia's default |
---|
| 902 | if not source.has_key( 'interval' ): |
---|
| 903 | source['interval'] = 15 |
---|
| 904 | debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) ) |
---|
[32] | 905 | |
---|
[33] | 906 | self.sources.append( source ) |
---|
[9] | 907 | |
---|
| 908 | def getInterval( self, source_name ): |
---|
[63] | 909 | """Return interval for source_name""" |
---|
[32] | 910 | |
---|
[9] | 911 | for source in self.sources: |
---|
[32] | 912 | |
---|
[12] | 913 | if source['name'] == source_name: |
---|
[32] | 914 | |
---|
[9] | 915 | return source['interval'] |
---|
[32] | 916 | |
---|
[9] | 917 | return None |
---|
| 918 | |
---|
[34] | 919 | def getLowestInterval( self ): |
---|
[63] | 920 | """Return the lowest interval of all clusters""" |
---|
[34] | 921 | |
---|
| 922 | lowest_interval = 0 |
---|
| 923 | |
---|
| 924 | for source in self.sources: |
---|
| 925 | |
---|
| 926 | if not lowest_interval or source['interval'] <= lowest_interval: |
---|
| 927 | |
---|
| 928 | lowest_interval = source['interval'] |
---|
| 929 | |
---|
| 930 | # Return 15 when nothing is found, so that the daemon won't go insane with 0 sec delays |
---|
| 931 | if lowest_interval: |
---|
| 932 | return lowest_interval |
---|
| 933 | else: |
---|
| 934 | return 15 |
---|
| 935 | |
---|
[9] | 936 | class RRDHandler: |
---|
[63] | 937 | """Class for handling RRD activity""" |
---|
[9] | 938 | |
---|
[32] | 939 | myMetrics = { } |
---|
[40] | 940 | lastStored = { } |
---|
[47] | 941 | timeserials = { } |
---|
[36] | 942 | slot = None |
---|
[32] | 943 | |
---|
[33] | 944 | def __init__( self, config, cluster ): |
---|
[63] | 945 | """Setup initial variables""" |
---|
[78] | 946 | |
---|
[44] | 947 | self.block = 0 |
---|
[9] | 948 | self.cluster = cluster |
---|
[33] | 949 | self.config = config |
---|
[40] | 950 | self.slot = threading.Lock() |
---|
[37] | 951 | self.rrdm = RRDMutator() |
---|
[42] | 952 | self.gatherLastUpdates() |
---|
[9] | 953 | |
---|
[42] | 954 | def gatherLastUpdates( self ): |
---|
[63] | 955 | """Populate the lastStored list, containing timestamps of all last updates""" |
---|
[42] | 956 | |
---|
| 957 | cluster_dir = '%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster ) |
---|
| 958 | |
---|
| 959 | hosts = [ ] |
---|
| 960 | |
---|
| 961 | if os.path.exists( cluster_dir ): |
---|
| 962 | |
---|
[44] | 963 | dirlist = os.listdir( cluster_dir ) |
---|
[42] | 964 | |
---|
[44] | 965 | for dir in dirlist: |
---|
[42] | 966 | |
---|
[44] | 967 | hosts.append( dir ) |
---|
[42] | 968 | |
---|
| 969 | for host in hosts: |
---|
| 970 | |
---|
[47] | 971 | host_dir = cluster_dir + '/' + host |
---|
| 972 | dirlist = os.listdir( host_dir ) |
---|
| 973 | |
---|
| 974 | for dir in dirlist: |
---|
| 975 | |
---|
| 976 | if not self.timeserials.has_key( host ): |
---|
| 977 | |
---|
| 978 | self.timeserials[ host ] = [ ] |
---|
| 979 | |
---|
| 980 | self.timeserials[ host ].append( dir ) |
---|
| 981 | |
---|
[42] | 982 | last_serial = self.getLastRrdTimeSerial( host ) |
---|
| 983 | if last_serial: |
---|
| 984 | |
---|
| 985 | metric_dir = cluster_dir + '/' + host + '/' + last_serial |
---|
| 986 | if os.path.exists( metric_dir ): |
---|
| 987 | |
---|
[44] | 988 | dirlist = os.listdir( metric_dir ) |
---|
[42] | 989 | |
---|
[44] | 990 | for file in dirlist: |
---|
[42] | 991 | |
---|
[44] | 992 | metricname = file.split( '.rrd' )[0] |
---|
[42] | 993 | |
---|
[44] | 994 | if not self.lastStored.has_key( host ): |
---|
[42] | 995 | |
---|
[44] | 996 | self.lastStored[ host ] = { } |
---|
[42] | 997 | |
---|
[44] | 998 | self.lastStored[ host ][ metricname ] = self.rrdm.grabLastUpdate( metric_dir + '/' + file ) |
---|
[42] | 999 | |
---|
[32] | 1000 | def getClusterName( self ): |
---|
[63] | 1001 | """Return clustername""" |
---|
| 1002 | |
---|
[32] | 1003 | return self.cluster |
---|
| 1004 | |
---|
| 1005 | def memMetric( self, host, metric ): |
---|
[63] | 1006 | """Store metric from host in memory""" |
---|
[32] | 1007 | |
---|
[34] | 1008 | if self.myMetrics.has_key( host ): |
---|
[32] | 1009 | |
---|
[34] | 1010 | if self.myMetrics[ host ].has_key( metric['name'] ): |
---|
[32] | 1011 | |
---|
[34] | 1012 | for mymetric in self.myMetrics[ host ][ metric['name'] ]: |
---|
[32] | 1013 | |
---|
[34] | 1014 | if mymetric['time'] == metric['time']: |
---|
[32] | 1015 | |
---|
[34] | 1016 | # Allready have this metric, abort |
---|
| 1017 | return 1 |
---|
| 1018 | else: |
---|
| 1019 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
| 1020 | else: |
---|
[32] | 1021 | self.myMetrics[ host ] = { } |
---|
[34] | 1022 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
[32] | 1023 | |
---|
[63] | 1024 | # Push new metric onto stack |
---|
| 1025 | # atomic code; only 1 thread at a time may access the stack |
---|
| 1026 | |
---|
[53] | 1027 | # <ATOMIC> |
---|
[40] | 1028 | # |
---|
| 1029 | self.slot.acquire() |
---|
| 1030 | |
---|
[32] | 1031 | self.myMetrics[ host ][ metric['name'] ].append( metric ) |
---|
| 1032 | |
---|
[40] | 1033 | self.slot.release() |
---|
[53] | 1034 | # |
---|
| 1035 | # </ATOMIC> |
---|
[40] | 1036 | |
---|
[47] | 1037 | def makeUpdateList( self, host, metriclist ): |
---|
[63] | 1038 | """ |
---|
| 1039 | Make a list of update values for rrdupdate |
---|
| 1040 | but only those that we didn't store before |
---|
| 1041 | """ |
---|
[37] | 1042 | |
---|
| 1043 | update_list = [ ] |
---|
[41] | 1044 | metric = None |
---|
[37] | 1045 | |
---|
[47] | 1046 | while len( metriclist ) > 0: |
---|
[37] | 1047 | |
---|
[53] | 1048 | metric = metriclist.pop( 0 ) |
---|
[37] | 1049 | |
---|
[53] | 1050 | if self.checkStoreMetric( host, metric ): |
---|
| 1051 | update_list.append( '%s:%s' %( metric['time'], metric['val'] ) ) |
---|
[40] | 1052 | |
---|
[37] | 1053 | return update_list |
---|
| 1054 | |
---|
[49] | 1055 | def checkStoreMetric( self, host, metric ): |
---|
[63] | 1056 | """Check if supplied metric if newer than last one stored""" |
---|
[40] | 1057 | |
---|
| 1058 | if self.lastStored.has_key( host ): |
---|
| 1059 | |
---|
[47] | 1060 | if self.lastStored[ host ].has_key( metric['name'] ): |
---|
[40] | 1061 | |
---|
[47] | 1062 | if metric['time'] <= self.lastStored[ host ][ metric['name'] ]: |
---|
[40] | 1063 | |
---|
[50] | 1064 | # This is old |
---|
[40] | 1065 | return 0 |
---|
| 1066 | |
---|
[50] | 1067 | return 1 |
---|
| 1068 | |
---|
[54] | 1069 | def memLastUpdate( self, host, metricname, metriclist ): |
---|
[63] | 1070 | """ |
---|
| 1071 | Memorize the time of the latest metric from metriclist |
---|
| 1072 | but only if it wasn't allready memorized |
---|
| 1073 | """ |
---|
[50] | 1074 | |
---|
[54] | 1075 | if not self.lastStored.has_key( host ): |
---|
| 1076 | self.lastStored[ host ] = { } |
---|
| 1077 | |
---|
[50] | 1078 | last_update_time = 0 |
---|
| 1079 | |
---|
| 1080 | for metric in metriclist: |
---|
| 1081 | |
---|
[54] | 1082 | if metric['name'] == metricname: |
---|
[50] | 1083 | |
---|
[54] | 1084 | if metric['time'] > last_update_time: |
---|
[50] | 1085 | |
---|
[54] | 1086 | last_update_time = metric['time'] |
---|
[40] | 1087 | |
---|
[54] | 1088 | if self.lastStored[ host ].has_key( metricname ): |
---|
[52] | 1089 | |
---|
[54] | 1090 | if last_update_time <= self.lastStored[ host ][ metricname ]: |
---|
[52] | 1091 | return 1 |
---|
[40] | 1092 | |
---|
[54] | 1093 | self.lastStored[ host ][ metricname ] = last_update_time |
---|
[52] | 1094 | |
---|
[33] | 1095 | def storeMetrics( self ): |
---|
[63] | 1096 | """ |
---|
| 1097 | Store all metrics from memory to disk |
---|
| 1098 | and do it to the RRD's in appropriate timeperiod directory |
---|
| 1099 | """ |
---|
[33] | 1100 | |
---|
| 1101 | for hostname, mymetrics in self.myMetrics.items(): |
---|
| 1102 | |
---|
| 1103 | for metricname, mymetric in mymetrics.items(): |
---|
| 1104 | |
---|
[53] | 1105 | metrics_to_store = [ ] |
---|
| 1106 | |
---|
[63] | 1107 | # Pop metrics from stack for storing until none is left |
---|
| 1108 | # atomic code: only 1 thread at a time may access myMetrics |
---|
| 1109 | |
---|
[53] | 1110 | # <ATOMIC> |
---|
[50] | 1111 | # |
---|
[47] | 1112 | self.slot.acquire() |
---|
[33] | 1113 | |
---|
[54] | 1114 | while len( self.myMetrics[ hostname ][ metricname ] ) > 0: |
---|
[53] | 1115 | |
---|
[54] | 1116 | if len( self.myMetrics[ hostname ][ metricname ] ) > 0: |
---|
| 1117 | metrics_to_store.append( self.myMetrics[ hostname ][ metricname ].pop( 0 ) ) |
---|
[53] | 1118 | |
---|
| 1119 | self.slot.release() |
---|
| 1120 | # |
---|
| 1121 | # </ATOMIC> |
---|
| 1122 | |
---|
[47] | 1123 | # Create a mapping table, each metric to the period where it should be stored |
---|
| 1124 | # |
---|
[53] | 1125 | metric_serial_table = self.determineSerials( hostname, metricname, metrics_to_store ) |
---|
[33] | 1126 | |
---|
[50] | 1127 | update_rets = [ ] |
---|
| 1128 | |
---|
[47] | 1129 | for period, pmetric in metric_serial_table.items(): |
---|
| 1130 | |
---|
[146] | 1131 | create_ret = self.createCheck( hostname, metricname, period ) |
---|
[47] | 1132 | |
---|
| 1133 | update_ret = self.update( hostname, metricname, period, pmetric ) |
---|
| 1134 | |
---|
| 1135 | if update_ret == 0: |
---|
| 1136 | |
---|
| 1137 | debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) ) |
---|
| 1138 | else: |
---|
| 1139 | debug_msg( 9, 'metric update failed' ) |
---|
| 1140 | |
---|
[146] | 1141 | update_rets.append( create_ret ) |
---|
[50] | 1142 | update_rets.append( update_ret ) |
---|
[47] | 1143 | |
---|
[50] | 1144 | if not (1) in update_rets: |
---|
| 1145 | |
---|
[54] | 1146 | self.memLastUpdate( hostname, metricname, metrics_to_store ) |
---|
[50] | 1147 | |
---|
[17] | 1148 | def makeTimeSerial( self ): |
---|
[63] | 1149 | """Generate a time serial. Seconds since epoch""" |
---|
[17] | 1150 | |
---|
| 1151 | # Seconds since epoch |
---|
| 1152 | mytime = int( time.time() ) |
---|
| 1153 | |
---|
| 1154 | return mytime |
---|
| 1155 | |
---|
[50] | 1156 | def makeRrdPath( self, host, metricname, timeserial ): |
---|
[63] | 1157 | """Make a RRD location/path and filename""" |
---|
[17] | 1158 | |
---|
[50] | 1159 | rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial ) |
---|
| 1160 | rrd_file = '%s/%s.rrd' %( rrd_dir, metricname ) |
---|
[17] | 1161 | |
---|
| 1162 | return rrd_dir, rrd_file |
---|
| 1163 | |
---|
[20] | 1164 | def getLastRrdTimeSerial( self, host ): |
---|
[63] | 1165 | """Find the last timeserial (directory) for this host""" |
---|
[17] | 1166 | |
---|
[19] | 1167 | newest_timeserial = 0 |
---|
| 1168 | |
---|
[47] | 1169 | for dir in self.timeserials[ host ]: |
---|
[32] | 1170 | |
---|
[47] | 1171 | valid_dir = 1 |
---|
[17] | 1172 | |
---|
[47] | 1173 | for letter in dir: |
---|
| 1174 | if letter not in string.digits: |
---|
| 1175 | valid_dir = 0 |
---|
[17] | 1176 | |
---|
[47] | 1177 | if valid_dir: |
---|
| 1178 | timeserial = dir |
---|
| 1179 | if timeserial > newest_timeserial: |
---|
| 1180 | newest_timeserial = timeserial |
---|
[17] | 1181 | |
---|
| 1182 | if newest_timeserial: |
---|
[18] | 1183 | return newest_timeserial |
---|
[17] | 1184 | else: |
---|
| 1185 | return 0 |
---|
| 1186 | |
---|
[47] | 1187 | def determinePeriod( self, host, check_serial ): |
---|
[63] | 1188 | """Determine to which period (directory) this time(serial) belongs""" |
---|
[47] | 1189 | |
---|
| 1190 | period_serial = 0 |
---|
| 1191 | |
---|
[56] | 1192 | if self.timeserials.has_key( host ): |
---|
[47] | 1193 | |
---|
[56] | 1194 | for serial in self.timeserials[ host ]: |
---|
[47] | 1195 | |
---|
[56] | 1196 | if check_serial >= serial and period_serial < serial: |
---|
[47] | 1197 | |
---|
[56] | 1198 | period_serial = serial |
---|
| 1199 | |
---|
[47] | 1200 | return period_serial |
---|
| 1201 | |
---|
| 1202 | def determineSerials( self, host, metricname, metriclist ): |
---|
| 1203 | """ |
---|
| 1204 | Determine the correct serial and corresponding rrd to store |
---|
| 1205 | for a list of metrics |
---|
| 1206 | """ |
---|
| 1207 | |
---|
| 1208 | metric_serial_table = { } |
---|
| 1209 | |
---|
| 1210 | for metric in metriclist: |
---|
| 1211 | |
---|
| 1212 | if metric['name'] == metricname: |
---|
| 1213 | |
---|
| 1214 | period = self.determinePeriod( host, metric['time'] ) |
---|
| 1215 | |
---|
| 1216 | archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60) |
---|
| 1217 | |
---|
[49] | 1218 | if (int( metric['time'] ) - int( period ) ) > archive_secs: |
---|
[47] | 1219 | |
---|
| 1220 | # This one should get it's own new period |
---|
| 1221 | period = metric['time'] |
---|
[57] | 1222 | |
---|
| 1223 | if not self.timeserials.has_key( host ): |
---|
| 1224 | self.timeserials[ host ] = [ ] |
---|
| 1225 | |
---|
[50] | 1226 | self.timeserials[ host ].append( period ) |
---|
[47] | 1227 | |
---|
| 1228 | if not metric_serial_table.has_key( period ): |
---|
| 1229 | |
---|
[49] | 1230 | metric_serial_table[ period ] = [ ] |
---|
[47] | 1231 | |
---|
| 1232 | metric_serial_table[ period ].append( metric ) |
---|
| 1233 | |
---|
| 1234 | return metric_serial_table |
---|
| 1235 | |
---|
[33] | 1236 | def createCheck( self, host, metricname, timeserial ): |
---|
[63] | 1237 | """Check if an rrd allready exists for this metric, create if not""" |
---|
[9] | 1238 | |
---|
[35] | 1239 | debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
[47] | 1240 | |
---|
[33] | 1241 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[17] | 1242 | |
---|
[9] | 1243 | if not os.path.exists( rrd_dir ): |
---|
[58] | 1244 | |
---|
| 1245 | try: |
---|
| 1246 | os.makedirs( rrd_dir ) |
---|
| 1247 | |
---|
[169] | 1248 | except os.OSError, msg: |
---|
[58] | 1249 | |
---|
| 1250 | if msg.find( 'File exists' ) != -1: |
---|
| 1251 | |
---|
| 1252 | # Ignore exists errors |
---|
| 1253 | pass |
---|
| 1254 | |
---|
| 1255 | else: |
---|
| 1256 | |
---|
| 1257 | print msg |
---|
| 1258 | return |
---|
| 1259 | |
---|
[14] | 1260 | debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) ) |
---|
[9] | 1261 | |
---|
[14] | 1262 | if not os.path.exists( rrd_file ): |
---|
[9] | 1263 | |
---|
[33] | 1264 | interval = self.config.getInterval( self.cluster ) |
---|
[47] | 1265 | heartbeat = 8 * int( interval ) |
---|
[9] | 1266 | |
---|
[37] | 1267 | params = [ ] |
---|
[12] | 1268 | |
---|
[37] | 1269 | params.append( '--step' ) |
---|
| 1270 | params.append( str( interval ) ) |
---|
[12] | 1271 | |
---|
[37] | 1272 | params.append( '--start' ) |
---|
[47] | 1273 | params.append( str( int( timeserial ) - 1 ) ) |
---|
[12] | 1274 | |
---|
[37] | 1275 | params.append( 'DS:sum:GAUGE:%d:U:U' %heartbeat ) |
---|
| 1276 | params.append( 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) ) |
---|
[13] | 1277 | |
---|
[37] | 1278 | self.rrdm.create( str(rrd_file), params ) |
---|
| 1279 | |
---|
[14] | 1280 | debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) ) |
---|
| 1281 | |
---|
[47] | 1282 | def update( self, host, metricname, timeserial, metriclist ): |
---|
[63] | 1283 | """ |
---|
| 1284 | Update rrd file for host with metricname |
---|
| 1285 | in directory timeserial with metriclist |
---|
| 1286 | """ |
---|
[9] | 1287 | |
---|
[35] | 1288 | debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
[9] | 1289 | |
---|
[33] | 1290 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[18] | 1291 | |
---|
[47] | 1292 | update_list = self.makeUpdateList( host, metriclist ) |
---|
[15] | 1293 | |
---|
[41] | 1294 | if len( update_list ) > 0: |
---|
| 1295 | ret = self.rrdm.update( str(rrd_file), update_list ) |
---|
[32] | 1296 | |
---|
[41] | 1297 | if ret: |
---|
| 1298 | return 1 |
---|
[27] | 1299 | |
---|
[41] | 1300 | debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), string.join( update_list ) ) ) |
---|
[15] | 1301 | |
---|
[36] | 1302 | return 0 |
---|
| 1303 | |
---|
[169] | 1304 | def daemon(): |
---|
| 1305 | """daemonized threading""" |
---|
[8] | 1306 | |
---|
[169] | 1307 | print 'daemon 1' |
---|
| 1308 | # Fork the first child |
---|
| 1309 | # |
---|
| 1310 | pid = os.fork() |
---|
| 1311 | |
---|
| 1312 | print 'daemon 2' |
---|
| 1313 | if pid > 0: |
---|
| 1314 | |
---|
| 1315 | sys.exit(0) # end parent |
---|
| 1316 | |
---|
| 1317 | print 'daemon 3' |
---|
| 1318 | # creates a session and sets the process group ID |
---|
| 1319 | # |
---|
| 1320 | os.setsid() |
---|
| 1321 | |
---|
| 1322 | # Fork the second child |
---|
| 1323 | # |
---|
| 1324 | pid = os.fork() |
---|
| 1325 | |
---|
| 1326 | print 'daemon 4' |
---|
| 1327 | if pid > 0: |
---|
| 1328 | |
---|
| 1329 | sys.exit(0) # end parent |
---|
| 1330 | |
---|
| 1331 | print 'daemon 5' |
---|
| 1332 | # Go to the root directory and set the umask |
---|
| 1333 | # |
---|
| 1334 | os.chdir('/') |
---|
| 1335 | os.umask(0) |
---|
| 1336 | |
---|
| 1337 | print 'daemon 6' |
---|
| 1338 | sys.stdin.close() |
---|
| 1339 | sys.stdout.close() |
---|
| 1340 | sys.stderr.close() |
---|
| 1341 | |
---|
| 1342 | os.open('/dev/null', 0) |
---|
| 1343 | os.dup(0) |
---|
| 1344 | os.dup(0) |
---|
| 1345 | |
---|
| 1346 | debug_msg( 0, 'daemon started.' ) |
---|
| 1347 | run() |
---|
| 1348 | |
---|
| 1349 | def run(): |
---|
| 1350 | """Threading start""" |
---|
| 1351 | |
---|
[102] | 1352 | myTorqueProcessor = TorqueXMLProcessor() |
---|
| 1353 | myGangliaProcessor = GangliaXMLProcessor() |
---|
[8] | 1354 | |
---|
[169] | 1355 | try: |
---|
[102] | 1356 | torque_xml_thread = threading.Thread( None, myTorqueProcessor.run, 'torque_proc_thread' ) |
---|
| 1357 | ganglia_xml_thread = threading.Thread( None, myGangliaProcessor.run, 'ganglia_proc_thread' ) |
---|
[22] | 1358 | |
---|
[169] | 1359 | torque_xml_thread.start() |
---|
| 1360 | ganglia_xml_thread.start() |
---|
| 1361 | |
---|
| 1362 | except threading.error, msg: |
---|
| 1363 | debug_msg( 0, 'FATAL ERROR: Unable to start main threads!: '+ str(msg) ) |
---|
| 1364 | syslog.closelog() |
---|
| 1365 | sys.exit(1) |
---|
| 1366 | |
---|
| 1367 | debug_msg( 0, 'main threading started.' ) |
---|
[78] | 1368 | |
---|
[169] | 1369 | def main(): |
---|
| 1370 | """Program startup""" |
---|
| 1371 | |
---|
| 1372 | if( DAEMONIZE and USE_SYSLOG ): |
---|
| 1373 | syslog.openlog( 'jobarchived', syslog.LOG_NOWAIT, SYSLOG_FACILITY ) |
---|
| 1374 | |
---|
| 1375 | if DAEMONIZE: |
---|
| 1376 | daemon() |
---|
| 1377 | else: |
---|
| 1378 | run() |
---|
| 1379 | |
---|
| 1380 | # |
---|
[81] | 1381 | # Global functions |
---|
[169] | 1382 | # |
---|
[81] | 1383 | |
---|
[9] | 1384 | def check_dir( directory ): |
---|
[63] | 1385 | """Check if directory is a proper directory. I.e.: Does _not_ end with a '/'""" |
---|
[9] | 1386 | |
---|
| 1387 | if directory[-1] == '/': |
---|
| 1388 | directory = directory[:-1] |
---|
| 1389 | |
---|
| 1390 | return directory |
---|
| 1391 | |
---|
[12] | 1392 | def debug_msg( level, msg ): |
---|
[169] | 1393 | """Only print msg if correct levels""" |
---|
[12] | 1394 | |
---|
[169] | 1395 | if (not DAEMONIZE and DEBUG_LEVEL >= level): |
---|
| 1396 | sys.stderr.write( printTime() + ' - ' + msg + '\n' ) |
---|
| 1397 | |
---|
| 1398 | if (DAEMONIZE and USE_SYSLOG and SYSLOG_LEVEL >= level): |
---|
| 1399 | syslog.syslog( msg ) |
---|
[12] | 1400 | |
---|
[46] | 1401 | def printTime( ): |
---|
[63] | 1402 | """Print current time in human readable format""" |
---|
[46] | 1403 | |
---|
| 1404 | return time.strftime("%a %d %b %Y %H:%M:%S") |
---|
| 1405 | |
---|
[63] | 1406 | # Ooohh, someone started me! Let's go.. |
---|
[9] | 1407 | if __name__ == '__main__': |
---|
| 1408 | main() |
---|