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