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