[3] | 1 | #!/usr/bin/env python |
---|
[225] | 2 | # |
---|
| 3 | # This file is part of Jobmonarch |
---|
| 4 | # |
---|
[771] | 5 | # Copyright (C) 2006-2013 Ramon Bastiaans |
---|
[225] | 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 | # |
---|
[230] | 21 | # SVN $Id: jobarchived.py 771 2013-03-28 15:43:38Z ramonb $ |
---|
| 22 | # |
---|
[3] | 23 | |
---|
[466] | 24 | import getopt, syslog, ConfigParser, sys |
---|
[3] | 25 | |
---|
[770] | 26 | VERSION='0.4+SVN' |
---|
[284] | 27 | |
---|
[466] | 28 | def usage( ver ): |
---|
[284] | 29 | |
---|
[770] | 30 | print 'jobarchived %s' %VERSION |
---|
[284] | 31 | |
---|
[770] | 32 | if ver: |
---|
| 33 | return 0 |
---|
[284] | 34 | |
---|
[770] | 35 | print |
---|
| 36 | print 'Purpose:' |
---|
| 37 | print ' The Job Archive Daemon (jobarchived) stores batch job information in a SQL database' |
---|
| 38 | print ' and node statistics in a RRD archive' |
---|
| 39 | print |
---|
| 40 | print 'Usage: jobarchived [OPTIONS]' |
---|
| 41 | print |
---|
| 42 | print ' -c, --config=FILE The configuration file to use (default: /etc/jobarchived.conf)' |
---|
| 43 | print ' -p, --pidfile=FILE Use pid file to store the process id' |
---|
| 44 | print ' -h, --help Print help and exit' |
---|
| 45 | print ' -v, --version Print version and exit' |
---|
| 46 | print |
---|
[435] | 47 | |
---|
[214] | 48 | def processArgs( args ): |
---|
[6] | 49 | |
---|
[770] | 50 | SHORT_L = 'p:hvc:' |
---|
| 51 | LONG_L = [ 'help', 'config=', 'pidfile=', 'version' ] |
---|
[169] | 52 | |
---|
[466] | 53 | config_filename = '/etc/jobarchived.conf' |
---|
[169] | 54 | |
---|
[770] | 55 | global PIDFILE |
---|
[435] | 56 | |
---|
[770] | 57 | PIDFILE = None |
---|
[435] | 58 | |
---|
[214] | 59 | try: |
---|
[169] | 60 | |
---|
[214] | 61 | opts, args = getopt.getopt( args, SHORT_L, LONG_L ) |
---|
[9] | 62 | |
---|
[214] | 63 | except getopt.error, detail: |
---|
[60] | 64 | |
---|
[214] | 65 | print detail |
---|
| 66 | sys.exit(1) |
---|
[9] | 67 | |
---|
[214] | 68 | for opt, value in opts: |
---|
[60] | 69 | |
---|
[214] | 70 | if opt in [ '--config', '-c' ]: |
---|
[13] | 71 | |
---|
[214] | 72 | config_filename = value |
---|
[198] | 73 | |
---|
[770] | 74 | if opt in [ '--pidfile', '-p' ]: |
---|
[435] | 75 | |
---|
[770] | 76 | PIDFILE = value |
---|
[435] | 77 | |
---|
[770] | 78 | if opt in [ '--help', '-h' ]: |
---|
[435] | 79 | |
---|
[770] | 80 | usage( False ) |
---|
| 81 | sys.exit( 0 ) |
---|
[435] | 82 | |
---|
[770] | 83 | if opt in [ '--version', '-v' ]: |
---|
[60] | 84 | |
---|
[770] | 85 | usage( True ) |
---|
| 86 | sys.exit( 0 ) |
---|
[22] | 87 | |
---|
[770] | 88 | try: |
---|
| 89 | return loadConfig( config_filename ) |
---|
[13] | 90 | |
---|
[770] | 91 | except ConfigParser.NoOptionError, detail: |
---|
[214] | 92 | |
---|
[770] | 93 | print detail |
---|
| 94 | sys.exit( 1 ) |
---|
[214] | 95 | |
---|
| 96 | def loadConfig( filename ): |
---|
| 97 | |
---|
[770] | 98 | def getlist( cfg_string ): |
---|
[214] | 99 | |
---|
[770] | 100 | my_list = [ ] |
---|
[214] | 101 | |
---|
[770] | 102 | for item_txt in cfg_string.split( ',' ): |
---|
[214] | 103 | |
---|
[770] | 104 | sep_char = None |
---|
[214] | 105 | |
---|
[770] | 106 | item_txt = item_txt.strip() |
---|
[214] | 107 | |
---|
[770] | 108 | for s_char in [ "'", '"' ]: |
---|
[214] | 109 | |
---|
[770] | 110 | if item_txt.find( s_char ) != -1: |
---|
[214] | 111 | |
---|
[770] | 112 | if item_txt.count( s_char ) != 2: |
---|
[214] | 113 | |
---|
[770] | 114 | print 'Missing quote: %s' %item_txt |
---|
| 115 | sys.exit( 1 ) |
---|
[214] | 116 | |
---|
[770] | 117 | else: |
---|
[214] | 118 | |
---|
[770] | 119 | sep_char = s_char |
---|
| 120 | break |
---|
[214] | 121 | |
---|
[770] | 122 | if sep_char: |
---|
[214] | 123 | |
---|
[770] | 124 | item_txt = item_txt.split( sep_char )[1] |
---|
[214] | 125 | |
---|
[770] | 126 | my_list.append( item_txt ) |
---|
[214] | 127 | |
---|
[770] | 128 | return my_list |
---|
[214] | 129 | |
---|
[770] | 130 | cfg = ConfigParser.ConfigParser() |
---|
[214] | 131 | |
---|
[770] | 132 | cfg.read( filename ) |
---|
[214] | 133 | |
---|
[770] | 134 | global DEBUG_LEVEL, USE_SYSLOG, SYSLOG_LEVEL, SYSLOG_FACILITY, GMETAD_CONF, ARCHIVE_XMLSOURCE |
---|
| 135 | global ARCHIVE_DATASOURCES, ARCHIVE_PATH, ARCHIVE_HOURS_PER_RRD, ARCHIVE_EXCLUDE_METRICS |
---|
| 136 | global JOB_SQL_DBASE, DAEMONIZE, RRDTOOL, JOB_TIMEOUT, MODRRDTOOL |
---|
[214] | 137 | |
---|
[770] | 138 | ARCHIVE_PATH = cfg.get( 'DEFAULT', 'ARCHIVE_PATH' ) |
---|
[214] | 139 | |
---|
[770] | 140 | ARCHIVE_HOURS_PER_RRD = cfg.getint( 'DEFAULT', 'ARCHIVE_HOURS_PER_RRD' ) |
---|
[214] | 141 | |
---|
[770] | 142 | DEBUG_LEVEL = cfg.getint( 'DEFAULT', 'DEBUG_LEVEL' ) |
---|
[214] | 143 | |
---|
[770] | 144 | USE_SYSLOG = cfg.getboolean( 'DEFAULT', 'USE_SYSLOG' ) |
---|
[214] | 145 | |
---|
[770] | 146 | SYSLOG_LEVEL = cfg.getint( 'DEFAULT', 'SYSLOG_LEVEL' ) |
---|
[214] | 147 | |
---|
[770] | 148 | MODRRDTOOL = False |
---|
[375] | 149 | |
---|
[770] | 150 | try: |
---|
| 151 | global rrdtool |
---|
| 152 | import rrdtool |
---|
[214] | 153 | |
---|
[770] | 154 | MODRRDTOOL = True |
---|
[375] | 155 | |
---|
[770] | 156 | except ImportError: |
---|
[375] | 157 | |
---|
[770] | 158 | MODRRDTOOL = False |
---|
[375] | 159 | |
---|
[770] | 160 | print "ERROR: py-rrdtool import FAILED: failing back to DEPRECATED use of rrdtool binary. This will slow down jobarchived significantly!" |
---|
[375] | 161 | |
---|
[770] | 162 | RRDTOOL = cfg.get( 'DEFAULT', 'RRDTOOL' ) |
---|
[455] | 163 | |
---|
[770] | 164 | try: |
---|
[375] | 165 | |
---|
[770] | 166 | SYSLOG_FACILITY = eval( 'syslog.LOG_' + cfg.get( 'DEFAULT', 'SYSLOG_FACILITY' ) ) |
---|
[214] | 167 | |
---|
[770] | 168 | except AttributeError, detail: |
---|
[214] | 169 | |
---|
[770] | 170 | print 'Unknown syslog facility' |
---|
| 171 | sys.exit( 1 ) |
---|
[214] | 172 | |
---|
[770] | 173 | GMETAD_CONF = cfg.get( 'DEFAULT', 'GMETAD_CONF' ) |
---|
[214] | 174 | |
---|
[770] | 175 | ARCHIVE_XMLSOURCE = cfg.get( 'DEFAULT', 'ARCHIVE_XMLSOURCE' ) |
---|
[214] | 176 | |
---|
[770] | 177 | ARCHIVE_DATASOURCES = getlist( cfg.get( 'DEFAULT', 'ARCHIVE_DATASOURCES' ) ) |
---|
[214] | 178 | |
---|
[770] | 179 | ARCHIVE_EXCLUDE_METRICS = getlist( cfg.get( 'DEFAULT', 'ARCHIVE_EXCLUDE_METRICS' ) ) |
---|
[214] | 180 | |
---|
[770] | 181 | JOB_SQL_DBASE = cfg.get( 'DEFAULT', 'JOB_SQL_DBASE' ) |
---|
[214] | 182 | |
---|
[770] | 183 | JOB_TIMEOUT = cfg.getint( 'DEFAULT', 'JOB_TIMEOUT' ) |
---|
[295] | 184 | |
---|
[770] | 185 | DAEMONIZE = cfg.getboolean( 'DEFAULT', 'DAEMONIZE' ) |
---|
[214] | 186 | |
---|
[224] | 187 | |
---|
[770] | 188 | return True |
---|
[214] | 189 | |
---|
[17] | 190 | # What XML data types not to store |
---|
[13] | 191 | # |
---|
[17] | 192 | UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ] |
---|
[9] | 193 | |
---|
[47] | 194 | # Maximum time (in seconds) a parsethread may run |
---|
| 195 | # |
---|
| 196 | PARSE_TIMEOUT = 60 |
---|
| 197 | |
---|
| 198 | # Maximum time (in seconds) a storethread may run |
---|
| 199 | # |
---|
| 200 | STORE_TIMEOUT = 360 |
---|
| 201 | |
---|
[8] | 202 | """ |
---|
[224] | 203 | The Job Archiving Daemon |
---|
[8] | 204 | """ |
---|
| 205 | |
---|
[214] | 206 | from types import * |
---|
| 207 | |
---|
| 208 | import xml.sax, xml.sax.handler, socket, string, os, os.path, time, thread, threading, random, re |
---|
| 209 | |
---|
[486] | 210 | try: |
---|
[770] | 211 | from pyPgSQL import PgSQL |
---|
[486] | 212 | |
---|
| 213 | except ImportError, details: |
---|
| 214 | |
---|
[770] | 215 | print "FATAL ERROR: pyPgSQL python module not found" |
---|
| 216 | sys.exit( 1 ) |
---|
[486] | 217 | |
---|
[379] | 218 | # Orginal from Andre van der Vlies <andre@vandervlies.xs4all.nl> for MySQL. Changed |
---|
| 219 | # and added some more functions for postgres. |
---|
| 220 | # |
---|
| 221 | # |
---|
| 222 | # Changed by: Bas van der Vlies <basv@sara.nl> |
---|
| 223 | # |
---|
| 224 | # SARA API for Postgres Database |
---|
| 225 | # |
---|
| 226 | # Changed by: Ramon Bastiaans for Job Monarch |
---|
| 227 | # |
---|
| 228 | |
---|
| 229 | class InitVars: |
---|
| 230 | Vars = {} |
---|
| 231 | |
---|
| 232 | def __init__(self, **key_arg): |
---|
| 233 | for (key, value) in key_arg.items(): |
---|
| 234 | if value: |
---|
| 235 | self.Vars[key] = value |
---|
| 236 | else: |
---|
| 237 | self.Vars[key] = None |
---|
| 238 | |
---|
| 239 | def __call__(self, *key): |
---|
| 240 | key = "%s" % key |
---|
| 241 | return self.Vars[key] |
---|
| 242 | |
---|
| 243 | def __getitem__(self, key): |
---|
| 244 | return self.Vars[key] |
---|
| 245 | |
---|
| 246 | def __repr__(self): |
---|
| 247 | return repr(self.Vars) |
---|
| 248 | |
---|
| 249 | def keys(self): |
---|
| 250 | barf = map(None, self.Vars.keys()) |
---|
| 251 | return barf |
---|
| 252 | |
---|
| 253 | def values(self): |
---|
| 254 | barf = map(None, self.Vars.values()) |
---|
| 255 | return barf |
---|
| 256 | |
---|
| 257 | def has_key(self, key): |
---|
| 258 | if self.Vars.has_key(key): |
---|
| 259 | return 1 |
---|
| 260 | else: |
---|
| 261 | return 0 |
---|
| 262 | |
---|
| 263 | class DBError(Exception): |
---|
| 264 | def __init__(self, msg=''): |
---|
| 265 | self.msg = msg |
---|
| 266 | Exception.__init__(self, msg) |
---|
| 267 | def __repr__(self): |
---|
| 268 | return self.msg |
---|
| 269 | __str__ = __repr__ |
---|
| 270 | |
---|
| 271 | # |
---|
| 272 | # Class to connect to a database |
---|
| 273 | # and return the queury in a list or dictionairy. |
---|
| 274 | # |
---|
| 275 | class DB: |
---|
| 276 | def __init__(self, db_vars): |
---|
| 277 | |
---|
| 278 | self.dict = db_vars |
---|
| 279 | |
---|
| 280 | if self.dict.has_key('User'): |
---|
| 281 | self.user = self.dict['User'] |
---|
| 282 | else: |
---|
| 283 | self.user = 'postgres' |
---|
| 284 | |
---|
| 285 | if self.dict.has_key('Host'): |
---|
| 286 | self.host = self.dict['Host'] |
---|
| 287 | else: |
---|
| 288 | self.host = 'localhost' |
---|
| 289 | |
---|
| 290 | if self.dict.has_key('Password'): |
---|
| 291 | self.passwd = self.dict['Password'] |
---|
| 292 | else: |
---|
| 293 | self.passwd = '' |
---|
| 294 | |
---|
| 295 | if self.dict.has_key('DataBaseName'): |
---|
| 296 | self.db = self.dict['DataBaseName'] |
---|
| 297 | else: |
---|
| 298 | self.db = 'uva_cluster_db' |
---|
| 299 | |
---|
| 300 | # connect_string = 'host:port:database:user:password: |
---|
| 301 | dsn = "%s::%s:%s:%s" %(self.host, self.db, self.user, self.passwd) |
---|
| 302 | |
---|
| 303 | try: |
---|
| 304 | self.SQL = PgSQL.connect(dsn) |
---|
| 305 | except PgSQL.Error, details: |
---|
| 306 | str = "%s" %details |
---|
| 307 | raise DBError(str) |
---|
| 308 | |
---|
| 309 | def __repr__(self): |
---|
| 310 | return repr(self.result) |
---|
| 311 | |
---|
| 312 | def __nonzero__(self): |
---|
| 313 | return not(self.result == None) |
---|
| 314 | |
---|
| 315 | def __len__(self): |
---|
| 316 | return len(self.result) |
---|
| 317 | |
---|
| 318 | def __getitem__(self,i): |
---|
| 319 | return self.result[i] |
---|
| 320 | |
---|
| 321 | def __getslice__(self,i,j): |
---|
| 322 | return self.result[i:j] |
---|
| 323 | |
---|
| 324 | def Get(self, q_str): |
---|
| 325 | c = self.SQL.cursor() |
---|
| 326 | try: |
---|
| 327 | c.execute(q_str) |
---|
| 328 | result = c.fetchall() |
---|
| 329 | except PgSQL.Error, details: |
---|
| 330 | c.close() |
---|
| 331 | str = "%s" %details |
---|
| 332 | raise DBError(str) |
---|
| 333 | |
---|
| 334 | c.close() |
---|
| 335 | return result |
---|
| 336 | |
---|
| 337 | def Set(self, q_str): |
---|
| 338 | c = self.SQL.cursor() |
---|
| 339 | try: |
---|
| 340 | c.execute(q_str) |
---|
| 341 | result = c.oidValue |
---|
| 342 | |
---|
| 343 | except PgSQL.Error, details: |
---|
| 344 | c.close() |
---|
| 345 | str = "%s" %details |
---|
| 346 | raise DBError(str) |
---|
| 347 | |
---|
| 348 | c.close() |
---|
| 349 | return result |
---|
| 350 | |
---|
| 351 | def Commit(self): |
---|
| 352 | self.SQL.commit() |
---|
| 353 | |
---|
[84] | 354 | class DataSQLStore: |
---|
| 355 | |
---|
[770] | 356 | db_vars = None |
---|
| 357 | dbc = None |
---|
[84] | 358 | |
---|
[770] | 359 | def __init__( self, hostname, database ): |
---|
[84] | 360 | |
---|
[770] | 361 | self.db_vars = InitVars(DataBaseName=database, |
---|
| 362 | User='root', |
---|
| 363 | Host=hostname, |
---|
| 364 | Password='', |
---|
| 365 | Dictionary='true') |
---|
[84] | 366 | |
---|
[770] | 367 | try: |
---|
| 368 | self.dbc = DB(self.db_vars) |
---|
| 369 | except DBError, details: |
---|
| 370 | debug_msg( 0, 'FATAL ERROR: Unable to connect to database!: ' +str(details) ) |
---|
| 371 | sys.exit(1) |
---|
[84] | 372 | |
---|
[770] | 373 | def setDatabase(self, statement): |
---|
| 374 | ret = self.doDatabase('set', statement) |
---|
| 375 | return ret |
---|
| 376 | |
---|
| 377 | def getDatabase(self, statement): |
---|
| 378 | ret = self.doDatabase('get', statement) |
---|
| 379 | return ret |
---|
[84] | 380 | |
---|
[770] | 381 | def doDatabase(self, type, statement): |
---|
[84] | 382 | |
---|
[770] | 383 | debug_msg( 10, 'doDatabase(): %s: %s' %(type, statement) ) |
---|
| 384 | try: |
---|
| 385 | if type == 'set': |
---|
| 386 | result = self.dbc.Set( statement ) |
---|
| 387 | self.dbc.Commit() |
---|
| 388 | elif type == 'get': |
---|
| 389 | result = self.dbc.Get( statement ) |
---|
| 390 | |
---|
| 391 | except DBError, detail: |
---|
| 392 | operation = statement.split(' ')[0] |
---|
| 393 | debug_msg( 0, 'FATAL ERROR: ' +operation+ ' on database failed while doing ['+statement+'] full msg: '+str(detail) ) |
---|
| 394 | sys.exit(1) |
---|
[84] | 395 | |
---|
[770] | 396 | debug_msg( 10, 'doDatabase(): result: %s' %(result) ) |
---|
| 397 | return result |
---|
[84] | 398 | |
---|
[770] | 399 | def getJobNodeId( self, job_id, node_id ): |
---|
[191] | 400 | |
---|
[770] | 401 | id = self.getDatabase( "SELECT job_id,node_id FROM job_nodes WHERE job_id = '%s' AND node_id = '%s'" %(job_id, node_id) ) |
---|
| 402 | if len( id ) > 0: |
---|
[191] | 403 | |
---|
[770] | 404 | if len( id[0] ) > 0 and id[0] != '': |
---|
| 405 | |
---|
| 406 | return 1 |
---|
[191] | 407 | |
---|
[770] | 408 | return 0 |
---|
[191] | 409 | |
---|
[770] | 410 | def getNodeId( self, hostname ): |
---|
[84] | 411 | |
---|
[770] | 412 | id = self.getDatabase( "SELECT node_id FROM nodes WHERE node_hostname = '%s'" %hostname ) |
---|
[84] | 413 | |
---|
[770] | 414 | if len( id ) > 0: |
---|
[84] | 415 | |
---|
[770] | 416 | id = id[0][0] |
---|
[89] | 417 | |
---|
[770] | 418 | return id |
---|
| 419 | else: |
---|
| 420 | return None |
---|
[84] | 421 | |
---|
[770] | 422 | def getNodeIds( self, hostnames ): |
---|
[84] | 423 | |
---|
[770] | 424 | ids = [ ] |
---|
[84] | 425 | |
---|
[770] | 426 | for node in hostnames: |
---|
[84] | 427 | |
---|
[770] | 428 | id = self.getNodeId( node ) |
---|
[84] | 429 | |
---|
[770] | 430 | if id: |
---|
| 431 | ids.append( id ) |
---|
[84] | 432 | |
---|
[770] | 433 | return ids |
---|
[84] | 434 | |
---|
[770] | 435 | def getJobId( self, jobid ): |
---|
[84] | 436 | |
---|
[770] | 437 | id = self.getDatabase( "SELECT job_id FROM jobs WHERE job_id = '%s'" %jobid ) |
---|
[84] | 438 | |
---|
[770] | 439 | if id: |
---|
| 440 | id = id[0][0] |
---|
[84] | 441 | |
---|
[770] | 442 | return id |
---|
| 443 | else: |
---|
| 444 | return None |
---|
[84] | 445 | |
---|
[770] | 446 | def addJob( self, job_id, jobattrs ): |
---|
[84] | 447 | |
---|
[770] | 448 | if not self.getJobId( job_id ): |
---|
[84] | 449 | |
---|
[770] | 450 | self.mutateJob( 'insert', job_id, jobattrs ) |
---|
| 451 | else: |
---|
| 452 | self.mutateJob( 'update', job_id, jobattrs ) |
---|
[84] | 453 | |
---|
[770] | 454 | def mutateJob( self, action, job_id, jobattrs ): |
---|
[84] | 455 | |
---|
[770] | 456 | job_values = [ 'name', 'queue', 'owner', 'requested_time', 'requested_memory', 'ppn', 'status', 'start_timestamp', 'stop_timestamp' ] |
---|
[84] | 457 | |
---|
[770] | 458 | insert_col_str = 'job_id' |
---|
| 459 | insert_val_str = "'%s'" %job_id |
---|
| 460 | update_str = None |
---|
[84] | 461 | |
---|
[770] | 462 | debug_msg( 10, 'mutateJob(): %s %s' %(action,job_id)) |
---|
[84] | 463 | |
---|
[770] | 464 | ids = [ ] |
---|
[99] | 465 | |
---|
[770] | 466 | for valname, value in jobattrs.items(): |
---|
[84] | 467 | |
---|
[770] | 468 | if valname in job_values and value != '': |
---|
[84] | 469 | |
---|
[770] | 470 | column_name = 'job_' + valname |
---|
[84] | 471 | |
---|
[770] | 472 | if action == 'insert': |
---|
[84] | 473 | |
---|
[770] | 474 | if not insert_col_str: |
---|
| 475 | insert_col_str = column_name |
---|
| 476 | else: |
---|
| 477 | insert_col_str = insert_col_str + ',' + column_name |
---|
[84] | 478 | |
---|
[770] | 479 | if not insert_val_str: |
---|
| 480 | insert_val_str = value |
---|
| 481 | else: |
---|
| 482 | insert_val_str = insert_val_str + ",'%s'" %value |
---|
[84] | 483 | |
---|
[770] | 484 | elif action == 'update': |
---|
| 485 | |
---|
| 486 | if not update_str: |
---|
| 487 | update_str = "%s='%s'" %(column_name, value) |
---|
| 488 | else: |
---|
| 489 | update_str = update_str + ",%s='%s'" %(column_name, value) |
---|
[84] | 490 | |
---|
[770] | 491 | elif valname == 'nodes' and value: |
---|
[84] | 492 | |
---|
[770] | 493 | node_valid = 1 |
---|
[190] | 494 | |
---|
[770] | 495 | if len(value) == 1: |
---|
| 496 | |
---|
| 497 | if jobattrs['status'] == 'Q': |
---|
[190] | 498 | |
---|
[770] | 499 | node_valid = 0 |
---|
[190] | 500 | |
---|
[770] | 501 | else: |
---|
[190] | 502 | |
---|
[770] | 503 | node_valid = 0 |
---|
[190] | 504 | |
---|
[770] | 505 | for node_char in str(value[0]): |
---|
[190] | 506 | |
---|
[770] | 507 | if string.find( string.digits, node_char ) != -1 and not node_valid: |
---|
[190] | 508 | |
---|
[770] | 509 | node_valid = 1 |
---|
[84] | 510 | |
---|
[770] | 511 | if node_valid: |
---|
[191] | 512 | |
---|
[770] | 513 | ids = self.addNodes( value, jobattrs['domain'] ) |
---|
[191] | 514 | |
---|
[770] | 515 | if action == 'insert': |
---|
[84] | 516 | |
---|
[770] | 517 | self.setDatabase( "INSERT INTO jobs ( %s ) VALUES ( %s )" %( insert_col_str, insert_val_str ) ) |
---|
[86] | 518 | |
---|
[770] | 519 | elif action == 'update': |
---|
[84] | 520 | |
---|
[770] | 521 | self.setDatabase( "UPDATE jobs SET %s WHERE job_id=%s" %(update_str, job_id) ) |
---|
[84] | 522 | |
---|
[770] | 523 | if len( ids ) > 0: |
---|
| 524 | self.addJobNodes( job_id, ids ) |
---|
[190] | 525 | |
---|
[770] | 526 | def addNodes( self, hostnames, domain ): |
---|
[84] | 527 | |
---|
[770] | 528 | ids = [ ] |
---|
[98] | 529 | |
---|
[770] | 530 | for node in hostnames: |
---|
[84] | 531 | |
---|
[770] | 532 | node = '%s.%s' %( node, domain ) |
---|
| 533 | id = self.getNodeId( node ) |
---|
| 534 | |
---|
| 535 | if not id: |
---|
| 536 | self.setDatabase( "INSERT INTO nodes ( node_hostname ) VALUES ( '%s' )" %node ) |
---|
| 537 | id = self.getNodeId( node ) |
---|
[84] | 538 | |
---|
[770] | 539 | ids.append( id ) |
---|
[98] | 540 | |
---|
[770] | 541 | return ids |
---|
[98] | 542 | |
---|
[770] | 543 | def addJobNodes( self, jobid, nodes ): |
---|
[86] | 544 | |
---|
[770] | 545 | for node in nodes: |
---|
[86] | 546 | |
---|
[770] | 547 | if not self.getJobNodeId( jobid, node ): |
---|
[191] | 548 | |
---|
[770] | 549 | self.addJobNode( jobid, node ) |
---|
[191] | 550 | |
---|
[770] | 551 | def addJobNode( self, jobid, nodeid ): |
---|
[84] | 552 | |
---|
[770] | 553 | self.setDatabase( "INSERT INTO job_nodes (job_id,node_id) VALUES ( %s,%s )" %(jobid, nodeid) ) |
---|
[84] | 554 | |
---|
[770] | 555 | def storeJobInfo( self, jobid, jobattrs ): |
---|
[84] | 556 | |
---|
[770] | 557 | self.addJob( jobid, jobattrs ) |
---|
[84] | 558 | |
---|
[770] | 559 | def checkStaleJobs( self ): |
---|
[295] | 560 | |
---|
[770] | 561 | # Locate all jobs in the database that are not set to finished |
---|
| 562 | # |
---|
| 563 | q = "SELECT * from jobs WHERE job_status != 'F'" |
---|
[295] | 564 | |
---|
[770] | 565 | r = self.getDatabase( q ) |
---|
[295] | 566 | |
---|
[770] | 567 | if len( r ) == 0: |
---|
[295] | 568 | |
---|
[770] | 569 | return None |
---|
[295] | 570 | |
---|
[770] | 571 | cleanjobs = [ ] |
---|
| 572 | timeoutjobs = [ ] |
---|
[295] | 573 | |
---|
[770] | 574 | jobtimeout_sec = JOB_TIMEOUT * (60 * 60) |
---|
| 575 | cur_time = time.time() |
---|
[295] | 576 | |
---|
[770] | 577 | for row in r: |
---|
[295] | 578 | |
---|
[770] | 579 | job_id = row[0] |
---|
| 580 | job_requested_time = row[4] |
---|
| 581 | job_status = row[7] |
---|
| 582 | job_start_timestamp = row[8] |
---|
[295] | 583 | |
---|
[770] | 584 | # If it was set to queued and we didn't see it started |
---|
| 585 | # there's not point in keeping it around |
---|
| 586 | # |
---|
| 587 | if job_status == 'Q' or not job_start_timestamp: |
---|
[295] | 588 | |
---|
[770] | 589 | cleanjobs.append( job_id ) |
---|
[295] | 590 | |
---|
[770] | 591 | else: |
---|
[295] | 592 | |
---|
[770] | 593 | start_timestamp = int( job_start_timestamp ) |
---|
[295] | 594 | |
---|
[770] | 595 | # If it was set to running longer than JOB_TIMEOUT |
---|
| 596 | # close the job: it probably finished while we were not running |
---|
| 597 | # |
---|
| 598 | if ( cur_time - start_timestamp ) > jobtimeout_sec: |
---|
[295] | 599 | |
---|
[770] | 600 | if job_requested_time: |
---|
[295] | 601 | |
---|
[770] | 602 | rtime_epoch = reqtime2epoch( job_requested_time ) |
---|
| 603 | else: |
---|
| 604 | rtime_epoch = None |
---|
| 605 | |
---|
| 606 | timeoutjobs.append( (job_id, job_start_timestamp, rtime_epoch) ) |
---|
[295] | 607 | |
---|
[770] | 608 | debug_msg( 1, 'Found ' + str( len( cleanjobs ) ) + ' stale jobs in database: deleting entries' ) |
---|
[295] | 609 | |
---|
[770] | 610 | # Purge these from database |
---|
| 611 | # |
---|
| 612 | for j in cleanjobs: |
---|
[295] | 613 | |
---|
[770] | 614 | q = "DELETE FROM jobs WHERE job_id = '" + str( j ) + "'" |
---|
| 615 | self.setDatabase( q ) |
---|
[295] | 616 | |
---|
[770] | 617 | debug_msg( 1, 'Found ' + str( len( timeoutjobs ) ) + ' timed out jobs in database: closing entries' ) |
---|
[295] | 618 | |
---|
[770] | 619 | # Close these jobs in the database |
---|
| 620 | # update the stop_timestamp to: start_timestamp + requested wallclock |
---|
| 621 | # and set state: finished |
---|
| 622 | # |
---|
| 623 | for j in timeoutjobs: |
---|
[295] | 624 | |
---|
[770] | 625 | ( i, s, r ) = j |
---|
[295] | 626 | |
---|
[770] | 627 | if r: |
---|
| 628 | new_end_timestamp = int( s ) + r |
---|
[295] | 629 | |
---|
[770] | 630 | q = "UPDATE jobs SET job_status='F',job_stop_timestamp = '" + str( new_end_timestamp ) + "' WHERE job_id = '" + str(i) + "'" |
---|
| 631 | self.setDatabase( q ) |
---|
[295] | 632 | |
---|
[37] | 633 | class RRDMutator: |
---|
[770] | 634 | """A class for performing RRD mutations""" |
---|
[37] | 635 | |
---|
[770] | 636 | binary = None |
---|
[37] | 637 | |
---|
[770] | 638 | def __init__( self, binary=None ): |
---|
| 639 | """Set alternate binary if supplied""" |
---|
[37] | 640 | |
---|
[770] | 641 | if binary: |
---|
| 642 | self.binary = binary |
---|
[37] | 643 | |
---|
[770] | 644 | def create( self, filename, args ): |
---|
| 645 | """Create a new rrd with args""" |
---|
[63] | 646 | |
---|
[770] | 647 | global MODRRDTOOL |
---|
[37] | 648 | |
---|
[770] | 649 | if MODRRDTOOL: |
---|
| 650 | return self.perform( 'create', filename, args ) |
---|
| 651 | else: |
---|
| 652 | return self.perform( 'create', '"' + filename + '"', args ) |
---|
[375] | 653 | |
---|
[770] | 654 | def update( self, filename, args ): |
---|
| 655 | """Update a rrd with args""" |
---|
[63] | 656 | |
---|
[770] | 657 | global MODRRDTOOL |
---|
[37] | 658 | |
---|
[770] | 659 | if MODRRDTOOL: |
---|
| 660 | return self.perform( 'update', filename, args ) |
---|
| 661 | else: |
---|
| 662 | return self.perform( 'update', '"' + filename + '"', args ) |
---|
[375] | 663 | |
---|
[770] | 664 | def grabLastUpdate( self, filename ): |
---|
| 665 | """Determine the last update time of filename rrd""" |
---|
[42] | 666 | |
---|
[770] | 667 | global MODRRDTOOL |
---|
[375] | 668 | |
---|
[770] | 669 | last_update = 0 |
---|
[42] | 670 | |
---|
[770] | 671 | # Use the py-rrdtool module if it's available on this system |
---|
| 672 | # |
---|
| 673 | if MODRRDTOOL: |
---|
[53] | 674 | |
---|
[770] | 675 | debug_msg( 8, 'rrdtool.info( ' + filename + ' )' ) |
---|
[42] | 676 | |
---|
[770] | 677 | rrd_header = { } |
---|
[292] | 678 | |
---|
[770] | 679 | try: |
---|
| 680 | rrd_header = rrdtool.info( filename ) |
---|
| 681 | except rrdtool.error, msg: |
---|
| 682 | debug_msg( 8, str( msg ) ) |
---|
[375] | 683 | |
---|
[770] | 684 | if rrd_header.has_key( 'last_update' ): |
---|
| 685 | return last_update |
---|
| 686 | else: |
---|
| 687 | return 0 |
---|
[375] | 688 | |
---|
[770] | 689 | # For backwards compatiblity: use the rrdtool binary if py-rrdtool is unavailable |
---|
| 690 | # DEPRECATED (slow!) |
---|
| 691 | # |
---|
| 692 | else: |
---|
| 693 | debug_msg( 8, self.binary + ' info ' + filename ) |
---|
[42] | 694 | |
---|
[770] | 695 | my_pipe = os.popen( self.binary + ' info "' + filename + '"' ) |
---|
[375] | 696 | |
---|
[770] | 697 | for line in my_pipe.readlines(): |
---|
[375] | 698 | |
---|
[770] | 699 | if line.find( 'last_update') != -1: |
---|
[375] | 700 | |
---|
[770] | 701 | last_update = line.split( ' = ' )[1] |
---|
[375] | 702 | |
---|
[770] | 703 | if my_pipe: |
---|
[375] | 704 | |
---|
[770] | 705 | my_pipe.close() |
---|
[375] | 706 | |
---|
[770] | 707 | if last_update: |
---|
| 708 | return last_update |
---|
| 709 | else: |
---|
| 710 | return 0 |
---|
[375] | 711 | |
---|
| 712 | |
---|
[770] | 713 | def perform( self, action, filename, args ): |
---|
| 714 | """Perform action on rrd filename with args""" |
---|
[37] | 715 | |
---|
[770] | 716 | global MODRRDTOOL |
---|
[375] | 717 | |
---|
[770] | 718 | arg_string = None |
---|
[37] | 719 | |
---|
[770] | 720 | if type( args ) is not ListType: |
---|
| 721 | debug_msg( 8, 'Arguments needs to be of type List' ) |
---|
| 722 | return 1 |
---|
[40] | 723 | |
---|
[770] | 724 | for arg in args: |
---|
[37] | 725 | |
---|
[770] | 726 | if not arg_string: |
---|
[37] | 727 | |
---|
[770] | 728 | arg_string = arg |
---|
| 729 | else: |
---|
| 730 | arg_string = arg_string + ' ' + arg |
---|
[37] | 731 | |
---|
[770] | 732 | if MODRRDTOOL: |
---|
[37] | 733 | |
---|
[770] | 734 | debug_msg( 8, 'rrdtool.' + action + "( " + filename + ' ' + arg_string + ")" ) |
---|
[292] | 735 | |
---|
[770] | 736 | try: |
---|
| 737 | debug_msg( 8, "filename '" + str(filename) + "' type "+ str(type(filename)) + " args " + str( args ) ) |
---|
[37] | 738 | |
---|
[770] | 739 | if action == 'create': |
---|
[146] | 740 | |
---|
[770] | 741 | rrdtool.create( str( filename ), *args ) |
---|
[37] | 742 | |
---|
[770] | 743 | elif action == 'update': |
---|
[37] | 744 | |
---|
[770] | 745 | rrdtool.update( str( filename ), *args ) |
---|
[365] | 746 | |
---|
[770] | 747 | except rrdtool.error, msg: |
---|
[365] | 748 | |
---|
[770] | 749 | error_msg = str( msg ) |
---|
| 750 | debug_msg( 8, error_msg ) |
---|
| 751 | return 1 |
---|
[375] | 752 | |
---|
[770] | 753 | else: |
---|
[375] | 754 | |
---|
[770] | 755 | debug_msg( 8, self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ) |
---|
[375] | 756 | |
---|
[770] | 757 | cmd = os.popen( self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ) |
---|
| 758 | lines = cmd.readlines() |
---|
[375] | 759 | |
---|
[770] | 760 | cmd.close() |
---|
[375] | 761 | |
---|
[770] | 762 | for line in lines: |
---|
[375] | 763 | |
---|
[770] | 764 | if line.find( 'ERROR' ) != -1: |
---|
[375] | 765 | |
---|
[770] | 766 | error_msg = string.join( line.split( ' ' )[1:] ) |
---|
| 767 | debug_msg( 8, error_msg ) |
---|
| 768 | return 1 |
---|
[375] | 769 | |
---|
[770] | 770 | return 0 |
---|
[37] | 771 | |
---|
[78] | 772 | class XMLProcessor: |
---|
[770] | 773 | """Skeleton class for XML processor's""" |
---|
[78] | 774 | |
---|
[770] | 775 | def run( self ): |
---|
| 776 | """Do main processing of XML here""" |
---|
[78] | 777 | |
---|
[770] | 778 | pass |
---|
[78] | 779 | |
---|
| 780 | class TorqueXMLProcessor( XMLProcessor ): |
---|
[770] | 781 | """Main class for processing XML and acting with it""" |
---|
[78] | 782 | |
---|
[770] | 783 | def __init__( self, XMLSource, DataStore ): |
---|
| 784 | """Setup initial XML connection and handlers""" |
---|
[78] | 785 | |
---|
[770] | 786 | self.myXMLSource = XMLSource |
---|
| 787 | self.myXMLHandler = TorqueXMLHandler( DataStore ) |
---|
| 788 | self.myXMLError = XMLErrorHandler() |
---|
[78] | 789 | |
---|
[770] | 790 | self.config = GangliaConfigParser( GMETAD_CONF ) |
---|
[287] | 791 | |
---|
[770] | 792 | def run( self ): |
---|
| 793 | """Main XML processing""" |
---|
[78] | 794 | |
---|
[770] | 795 | debug_msg( 1, 'torque_xml_thread(): started.' ) |
---|
[87] | 796 | |
---|
[770] | 797 | while( 1 ): |
---|
[78] | 798 | |
---|
[770] | 799 | #self.myXMLSource = self.mXMLGatherer.getFileObject() |
---|
| 800 | debug_msg( 1, 'torque_xml_thread(): Retrieving XML data..' ) |
---|
[176] | 801 | |
---|
[770] | 802 | my_data = self.myXMLSource.getData() |
---|
[287] | 803 | |
---|
[770] | 804 | debug_msg( 1, 'torque_xml_thread(): Done retrieving.' ) |
---|
[469] | 805 | |
---|
[770] | 806 | if my_data: |
---|
| 807 | debug_msg( 1, 'ganglia_parse_thread(): Parsing XML..' ) |
---|
[469] | 808 | |
---|
[770] | 809 | xml.sax.parseString( my_data, self.myXMLHandler, self.myXMLError ) |
---|
[469] | 810 | |
---|
[770] | 811 | debug_msg( 1, 'ganglia_parse_thread(): Done parsing.' ) |
---|
| 812 | |
---|
| 813 | debug_msg( 1, 'torque_xml_thread(): Sleeping.. (%ss)' %(str( self.config.getLowestInterval() ) ) ) |
---|
| 814 | time.sleep( self.config.getLowestInterval() ) |
---|
[78] | 815 | |
---|
[71] | 816 | class TorqueXMLHandler( xml.sax.handler.ContentHandler ): |
---|
[770] | 817 | """Parse Torque's jobinfo XML from our plugin""" |
---|
[63] | 818 | |
---|
[770] | 819 | jobAttrs = { } |
---|
[72] | 820 | |
---|
[770] | 821 | def __init__( self, datastore ): |
---|
[84] | 822 | |
---|
[770] | 823 | #self.ds = DataSQLStore( JOB_SQL_DBASE.split( '/' )[0], JOB_SQL_DBASE.split( '/' )[1] ) |
---|
| 824 | self.ds = datastore |
---|
| 825 | self.jobs_processed = [ ] |
---|
| 826 | self.jobs_to_store = [ ] |
---|
[84] | 827 | |
---|
[770] | 828 | def startDocument( self ): |
---|
[183] | 829 | |
---|
[770] | 830 | self.heartbeat = 0 |
---|
| 831 | self.elementct = 0 |
---|
[183] | 832 | |
---|
[770] | 833 | def startElement( self, name, attrs ): |
---|
| 834 | """ |
---|
| 835 | This XML will be all gmetric XML |
---|
| 836 | so there will be no specific start/end element |
---|
| 837 | just one XML statement with all info |
---|
| 838 | """ |
---|
| 839 | |
---|
| 840 | jobinfo = { } |
---|
[63] | 841 | |
---|
[770] | 842 | self.elementct += 1 |
---|
[365] | 843 | |
---|
[770] | 844 | if name == 'CLUSTER': |
---|
[63] | 845 | |
---|
[770] | 846 | self.clustername = str( attrs.get( 'NAME', "" ) ) |
---|
[199] | 847 | |
---|
[770] | 848 | elif name == 'METRIC' and self.clustername in ARCHIVE_DATASOURCES: |
---|
[199] | 849 | |
---|
[770] | 850 | metricname = str( attrs.get( 'NAME', "" ) ) |
---|
[63] | 851 | |
---|
[770] | 852 | if metricname == 'MONARCH-HEARTBEAT': |
---|
| 853 | self.heartbeat = str( attrs.get( 'VAL', "" ) ) |
---|
[63] | 854 | |
---|
[770] | 855 | elif metricname.find( 'MONARCH-JOB' ) != -1: |
---|
[63] | 856 | |
---|
[770] | 857 | job_id = metricname.split( 'MONARCH-JOB-' )[1].split( '-' )[0] |
---|
| 858 | val = str( attrs.get( 'VAL', "" ) ) |
---|
[63] | 859 | |
---|
[770] | 860 | if not job_id in self.jobs_processed: |
---|
[292] | 861 | |
---|
[770] | 862 | self.jobs_processed.append( job_id ) |
---|
[96] | 863 | |
---|
[770] | 864 | check_change = 0 |
---|
[73] | 865 | |
---|
[770] | 866 | if self.jobAttrs.has_key( job_id ): |
---|
[292] | 867 | |
---|
[770] | 868 | check_change = 1 |
---|
[73] | 869 | |
---|
[770] | 870 | valinfo = val.split( ' ' ) |
---|
[63] | 871 | |
---|
[770] | 872 | for myval in valinfo: |
---|
[63] | 873 | |
---|
[770] | 874 | if len( myval.split( '=' ) ) > 1: |
---|
[63] | 875 | |
---|
[770] | 876 | valname = myval.split( '=' )[0] |
---|
| 877 | value = myval.split( '=' )[1] |
---|
[70] | 878 | |
---|
[770] | 879 | if valname == 'nodes': |
---|
| 880 | value = value.split( ';' ) |
---|
[72] | 881 | |
---|
[770] | 882 | jobinfo[ valname ] = value |
---|
[84] | 883 | |
---|
[770] | 884 | if check_change: |
---|
| 885 | if self.jobinfoChanged( self.jobAttrs, job_id, jobinfo ) and self.jobAttrs[ job_id ]['status'] in [ 'R', 'Q' ]: |
---|
| 886 | self.jobAttrs[ job_id ]['stop_timestamp'] = '' |
---|
| 887 | self.jobAttrs[ job_id ] = self.setJobAttrs( self.jobAttrs[ job_id ], jobinfo ) |
---|
| 888 | if not job_id in self.jobs_to_store: |
---|
| 889 | self.jobs_to_store.append( job_id ) |
---|
[84] | 890 | |
---|
[770] | 891 | debug_msg( 10, 'jobinfo for job %s has changed' %job_id ) |
---|
| 892 | else: |
---|
| 893 | self.jobAttrs[ job_id ] = jobinfo |
---|
[84] | 894 | |
---|
[770] | 895 | if not job_id in self.jobs_to_store: |
---|
| 896 | self.jobs_to_store.append( job_id ) |
---|
[84] | 897 | |
---|
[770] | 898 | debug_msg( 10, 'jobinfo for job %s has changed' %job_id ) |
---|
| 899 | |
---|
| 900 | def endDocument( self ): |
---|
| 901 | """When all metrics have gone, check if any jobs have finished""" |
---|
[72] | 902 | |
---|
[770] | 903 | debug_msg( 1, "XML: Processed "+str(self.elementct)+ " elements - found "+str(len(self.jobs_to_store))+" (updated) jobs" ) |
---|
[365] | 904 | |
---|
[770] | 905 | if self.heartbeat: |
---|
| 906 | for jobid, jobinfo in self.jobAttrs.items(): |
---|
[74] | 907 | |
---|
[770] | 908 | # This is an old job, not in current jobinfo list anymore |
---|
| 909 | # it must have finished, since we _did_ get a new heartbeat |
---|
| 910 | # |
---|
| 911 | mytime = int( jobinfo['reported'] ) + int( jobinfo['poll_interval'] ) |
---|
[102] | 912 | |
---|
[770] | 913 | if (mytime < self.heartbeat) and (jobid not in self.jobs_processed) and (jobinfo['status'] == 'R'): |
---|
[74] | 914 | |
---|
[770] | 915 | if not jobid in self.jobs_processed: |
---|
| 916 | self.jobs_processed.append( jobid ) |
---|
[96] | 917 | |
---|
[770] | 918 | self.jobAttrs[ jobid ]['status'] = 'F' |
---|
| 919 | self.jobAttrs[ jobid ]['stop_timestamp'] = str( self.heartbeat ) |
---|
[96] | 920 | |
---|
[770] | 921 | if not jobid in self.jobs_to_store: |
---|
| 922 | self.jobs_to_store.append( jobid ) |
---|
[74] | 923 | |
---|
[770] | 924 | debug_msg( 1, 'torque_xml_thread(): Storing..' ) |
---|
[87] | 925 | |
---|
[770] | 926 | for jobid in self.jobs_to_store: |
---|
| 927 | if self.jobAttrs[ jobid ]['status'] in [ 'R', 'F' ]: |
---|
[84] | 928 | |
---|
[770] | 929 | self.ds.storeJobInfo( jobid, self.jobAttrs[ jobid ] ) |
---|
[184] | 930 | |
---|
[770] | 931 | if self.jobAttrs[ jobid ]['status'] == 'F': |
---|
| 932 | del self.jobAttrs[ jobid ] |
---|
[184] | 933 | |
---|
[770] | 934 | debug_msg( 1, 'torque_xml_thread(): Done storing.' ) |
---|
[87] | 935 | |
---|
[770] | 936 | self.jobs_processed = [ ] |
---|
| 937 | self.jobs_to_store = [ ] |
---|
[84] | 938 | |
---|
[770] | 939 | def setJobAttrs( self, old, new ): |
---|
| 940 | """ |
---|
| 941 | Set new job attributes in old, but not lose existing fields |
---|
| 942 | if old attributes doesn't have those |
---|
| 943 | """ |
---|
[82] | 944 | |
---|
[770] | 945 | for valname, value in new.items(): |
---|
| 946 | old[ valname ] = value |
---|
[82] | 947 | |
---|
[770] | 948 | return old |
---|
| 949 | |
---|
[82] | 950 | |
---|
[770] | 951 | def jobinfoChanged( self, jobattrs, jobid, jobinfo ): |
---|
| 952 | """ |
---|
| 953 | Check if jobinfo has changed from jobattrs[jobid] |
---|
| 954 | if it's report time is bigger than previous one |
---|
| 955 | and it is report time is recent (equal to heartbeat) |
---|
| 956 | """ |
---|
[72] | 957 | |
---|
[770] | 958 | ignore_changes = [ 'reported' ] |
---|
[87] | 959 | |
---|
[770] | 960 | if jobattrs.has_key( jobid ): |
---|
[73] | 961 | |
---|
[770] | 962 | for valname, value in jobinfo.items(): |
---|
[73] | 963 | |
---|
[770] | 964 | if valname not in ignore_changes: |
---|
[73] | 965 | |
---|
[770] | 966 | if jobattrs[ jobid ].has_key( valname ): |
---|
[73] | 967 | |
---|
[770] | 968 | if value != jobattrs[ jobid ][ valname ]: |
---|
[73] | 969 | |
---|
[770] | 970 | if jobinfo['reported'] > jobattrs[ jobid ][ 'reported' ] and jobinfo['reported'] == self.heartbeat: |
---|
| 971 | return True |
---|
[73] | 972 | |
---|
[770] | 973 | else: |
---|
| 974 | return True |
---|
[87] | 975 | |
---|
[770] | 976 | return False |
---|
[73] | 977 | |
---|
[71] | 978 | class GangliaXMLHandler( xml.sax.handler.ContentHandler ): |
---|
[770] | 979 | """Parse Ganglia's XML""" |
---|
[3] | 980 | |
---|
[770] | 981 | def __init__( self, config, datastore ): |
---|
| 982 | """Setup initial variables and gather info on existing rrd archive""" |
---|
[63] | 983 | |
---|
[770] | 984 | self.config = config |
---|
| 985 | self.clusters = { } |
---|
| 986 | self.ds = datastore |
---|
[324] | 987 | |
---|
[770] | 988 | debug_msg( 1, 'Checking database..' ) |
---|
[365] | 989 | |
---|
[770] | 990 | global DEBUG_LEVEL |
---|
[365] | 991 | |
---|
[770] | 992 | if DEBUG_LEVEL <= 2: |
---|
| 993 | self.ds.checkStaleJobs() |
---|
[365] | 994 | |
---|
[770] | 995 | debug_msg( 1, 'Check done.' ) |
---|
| 996 | debug_msg( 1, 'Checking rrd archive..' ) |
---|
| 997 | self.gatherClusters() |
---|
| 998 | debug_msg( 1, 'Check done.' ) |
---|
[33] | 999 | |
---|
[770] | 1000 | def gatherClusters( self ): |
---|
| 1001 | """Find all existing clusters in archive dir""" |
---|
[44] | 1002 | |
---|
[770] | 1003 | archive_dir = check_dir(ARCHIVE_PATH) |
---|
[44] | 1004 | |
---|
[770] | 1005 | hosts = [ ] |
---|
[44] | 1006 | |
---|
[770] | 1007 | if os.path.exists( archive_dir ): |
---|
[44] | 1008 | |
---|
[770] | 1009 | dirlist = os.listdir( archive_dir ) |
---|
[44] | 1010 | |
---|
[770] | 1011 | for cfgcluster in ARCHIVE_DATASOURCES: |
---|
[369] | 1012 | |
---|
[770] | 1013 | if cfgcluster not in dirlist: |
---|
[369] | 1014 | |
---|
[770] | 1015 | # Autocreate a directory for this cluster |
---|
| 1016 | # assume it is new |
---|
| 1017 | # |
---|
| 1018 | cluster_dir = '%s/%s' %( check_dir(ARCHIVE_PATH), cfgcluster ) |
---|
[369] | 1019 | |
---|
[770] | 1020 | os.mkdir( cluster_dir ) |
---|
[369] | 1021 | |
---|
[770] | 1022 | dirlist.append( cfgcluster ) |
---|
[370] | 1023 | |
---|
[770] | 1024 | for item in dirlist: |
---|
[44] | 1025 | |
---|
[770] | 1026 | clustername = item |
---|
[44] | 1027 | |
---|
[770] | 1028 | if not self.clusters.has_key( clustername ) and clustername in ARCHIVE_DATASOURCES: |
---|
[44] | 1029 | |
---|
[770] | 1030 | self.clusters[ clustername ] = RRDHandler( self.config, clustername ) |
---|
[44] | 1031 | |
---|
[770] | 1032 | debug_msg( 9, "Found "+str(len(self.clusters.keys()))+" clusters" ) |
---|
[365] | 1033 | |
---|
[770] | 1034 | def startElement( self, name, attrs ): |
---|
| 1035 | """Memorize appropriate data from xml start tags""" |
---|
[3] | 1036 | |
---|
[770] | 1037 | if name == 'GANGLIA_XML': |
---|
[32] | 1038 | |
---|
[770] | 1039 | self.XMLSource = str( attrs.get( 'SOURCE', "" ) ) |
---|
| 1040 | self.gangliaVersion = str( attrs.get( 'VERSION', "" ) ) |
---|
[32] | 1041 | |
---|
[770] | 1042 | debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) ) |
---|
[6] | 1043 | |
---|
[770] | 1044 | elif name == 'GRID': |
---|
[32] | 1045 | |
---|
[770] | 1046 | self.gridName = str( attrs.get( 'NAME', "" ) ) |
---|
| 1047 | self.time = str( attrs.get( 'LOCALTIME', "" ) ) |
---|
[32] | 1048 | |
---|
[770] | 1049 | debug_msg( 10, '`-Grid found: %s' %( self.gridName ) ) |
---|
[6] | 1050 | |
---|
[770] | 1051 | elif name == 'CLUSTER': |
---|
[32] | 1052 | |
---|
[770] | 1053 | self.clusterName = str( attrs.get( 'NAME', "" ) ) |
---|
| 1054 | self.time = str( attrs.get( 'LOCALTIME', "" ) ) |
---|
[32] | 1055 | |
---|
[770] | 1056 | if not self.clusters.has_key( self.clusterName ) and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[32] | 1057 | |
---|
[770] | 1058 | self.clusters[ self.clusterName ] = RRDHandler( self.config, self.clusterName ) |
---|
[33] | 1059 | |
---|
[770] | 1060 | debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) ) |
---|
[6] | 1061 | |
---|
[770] | 1062 | elif name == 'HOST' and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[32] | 1063 | |
---|
[770] | 1064 | self.hostName = str( attrs.get( 'NAME', "" ) ) |
---|
| 1065 | self.hostIp = str( attrs.get( 'IP', "" ) ) |
---|
| 1066 | self.hostReported = str( attrs.get( 'REPORTED', "" ) ) |
---|
[32] | 1067 | |
---|
[770] | 1068 | debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) ) |
---|
[6] | 1069 | |
---|
[770] | 1070 | elif name == 'METRIC' and self.clusterName in ARCHIVE_DATASOURCES: |
---|
[6] | 1071 | |
---|
[770] | 1072 | type = str( attrs.get( 'TYPE', "" ) ) |
---|
| 1073 | |
---|
| 1074 | exclude_metric = False |
---|
| 1075 | |
---|
| 1076 | for ex_metricstr in ARCHIVE_EXCLUDE_METRICS: |
---|
[6] | 1077 | |
---|
[770] | 1078 | orig_name = str( attrs.get( 'NAME', "" ) ) |
---|
[3] | 1079 | |
---|
[770] | 1080 | if string.lower( orig_name ) == string.lower( ex_metricstr ): |
---|
| 1081 | |
---|
| 1082 | exclude_metric = True |
---|
[198] | 1083 | |
---|
[770] | 1084 | elif re.match( ex_metricstr, orig_name ): |
---|
[198] | 1085 | |
---|
[770] | 1086 | exclude_metric = True |
---|
[198] | 1087 | |
---|
[770] | 1088 | if type not in UNSUPPORTED_ARCHIVE_TYPES and not exclude_metric: |
---|
[198] | 1089 | |
---|
[770] | 1090 | myMetric = { } |
---|
| 1091 | myMetric['name'] = str( attrs.get( 'NAME', "" ) ) |
---|
| 1092 | myMetric['val'] = str( attrs.get( 'VAL', "" ) ) |
---|
| 1093 | myMetric['time'] = self.hostReported |
---|
[3] | 1094 | |
---|
[770] | 1095 | self.clusters[ self.clusterName ].memMetric( self.hostName, myMetric ) |
---|
[3] | 1096 | |
---|
[770] | 1097 | debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) ) |
---|
[6] | 1098 | |
---|
[770] | 1099 | def storeMetrics( self ): |
---|
| 1100 | """Store metrics of each cluster rrd handler""" |
---|
[9] | 1101 | |
---|
[770] | 1102 | for clustername, rrdh in self.clusters.items(): |
---|
[16] | 1103 | |
---|
[770] | 1104 | ret = rrdh.storeMetrics() |
---|
[9] | 1105 | |
---|
[770] | 1106 | if ret: |
---|
| 1107 | debug_msg( 9, 'An error occured while storing metrics for cluster %s' %clustername ) |
---|
| 1108 | return 1 |
---|
[38] | 1109 | |
---|
[770] | 1110 | return 0 |
---|
[38] | 1111 | |
---|
[71] | 1112 | class XMLErrorHandler( xml.sax.handler.ErrorHandler ): |
---|
| 1113 | |
---|
[770] | 1114 | def error( self, exception ): |
---|
| 1115 | """Recoverable error""" |
---|
[71] | 1116 | |
---|
[770] | 1117 | debug_msg( 0, 'Recoverable XML error ' + str( exception ) + ' ignored.' ) |
---|
[71] | 1118 | |
---|
[770] | 1119 | def fatalError( self, exception ): |
---|
| 1120 | """Non-recoverable error""" |
---|
[71] | 1121 | |
---|
[770] | 1122 | exception_str = str( exception ) |
---|
[71] | 1123 | |
---|
[770] | 1124 | # Ignore 'no element found' errors |
---|
| 1125 | if exception_str.find( 'no element found' ) != -1: |
---|
| 1126 | debug_msg( 0, 'No XML data found: Socket not (re)connected or datasource not available.' ) |
---|
| 1127 | return 0 |
---|
[71] | 1128 | |
---|
[770] | 1129 | debug_msg( 0, 'FATAL ERROR: Non-recoverable XML error ' + str( exception ) ) |
---|
| 1130 | sys.exit( 1 ) |
---|
[71] | 1131 | |
---|
[770] | 1132 | def warning( self, exception ): |
---|
| 1133 | """Warning""" |
---|
[71] | 1134 | |
---|
[770] | 1135 | debug_msg( 0, 'Warning ' + str( exception ) ) |
---|
[71] | 1136 | |
---|
[78] | 1137 | class XMLGatherer: |
---|
[770] | 1138 | """Setup a connection and file object to Ganglia's XML""" |
---|
[3] | 1139 | |
---|
[770] | 1140 | s = None |
---|
| 1141 | fd = None |
---|
| 1142 | data = None |
---|
| 1143 | slot = None |
---|
[8] | 1144 | |
---|
[770] | 1145 | # Time since the last update |
---|
| 1146 | # |
---|
| 1147 | LAST_UPDATE = 0 |
---|
[287] | 1148 | |
---|
[770] | 1149 | # Minimum interval between updates |
---|
| 1150 | # |
---|
| 1151 | MIN_UPDATE_INT = 10 |
---|
[287] | 1152 | |
---|
[770] | 1153 | # Is a update occuring now |
---|
| 1154 | # |
---|
| 1155 | update_now = False |
---|
[287] | 1156 | |
---|
[770] | 1157 | def __init__( self, host, port ): |
---|
| 1158 | """Store host and port for connection""" |
---|
[8] | 1159 | |
---|
[770] | 1160 | self.host = host |
---|
| 1161 | self.port = port |
---|
| 1162 | self.slot = threading.Lock() |
---|
[3] | 1163 | |
---|
[770] | 1164 | self.retrieveData() |
---|
[287] | 1165 | |
---|
[770] | 1166 | def retrieveData( self ): |
---|
| 1167 | """Setup connection to XML source""" |
---|
[8] | 1168 | |
---|
[770] | 1169 | self.update_now = True |
---|
[287] | 1170 | |
---|
[770] | 1171 | self.slot.acquire() |
---|
[293] | 1172 | |
---|
[770] | 1173 | self.data = None |
---|
[469] | 1174 | |
---|
[770] | 1175 | for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): |
---|
[32] | 1176 | |
---|
[770] | 1177 | af, socktype, proto, canonname, sa = res |
---|
[32] | 1178 | |
---|
[770] | 1179 | try: |
---|
[32] | 1180 | |
---|
[770] | 1181 | self.s = socket.socket( af, socktype, proto ) |
---|
[32] | 1182 | |
---|
[770] | 1183 | except ( socket.error, socket.gaierror, socket.herror, socket.timeout ), msg: |
---|
[32] | 1184 | |
---|
[770] | 1185 | self.s = None |
---|
| 1186 | continue |
---|
[32] | 1187 | |
---|
[770] | 1188 | try: |
---|
[32] | 1189 | |
---|
[770] | 1190 | self.s.connect( sa ) |
---|
[32] | 1191 | |
---|
[770] | 1192 | except ( socket.error, socket.gaierror, socket.herror, socket.timeout ), msg: |
---|
[32] | 1193 | |
---|
[770] | 1194 | self.disconnect() |
---|
| 1195 | continue |
---|
[32] | 1196 | |
---|
[770] | 1197 | break |
---|
[3] | 1198 | |
---|
[770] | 1199 | if self.s is None: |
---|
[32] | 1200 | |
---|
[770] | 1201 | debug_msg( 0, 'FATAL ERROR: Could not open socket or unable to connect to datasource!' ) |
---|
| 1202 | self.update_now = False |
---|
| 1203 | #sys.exit( 1 ) |
---|
[5] | 1204 | |
---|
[770] | 1205 | else: |
---|
| 1206 | #self.s.send( '\n' ) |
---|
[287] | 1207 | |
---|
[770] | 1208 | my_fp = self.s.makefile( 'r' ) |
---|
| 1209 | my_data = my_fp.readlines() |
---|
| 1210 | my_data = string.join( my_data, '' ) |
---|
[287] | 1211 | |
---|
[770] | 1212 | self.data = my_data |
---|
[287] | 1213 | |
---|
[770] | 1214 | self.LAST_UPDATE = time.time() |
---|
[287] | 1215 | |
---|
[770] | 1216 | self.slot.release() |
---|
[293] | 1217 | |
---|
[770] | 1218 | self.update_now = False |
---|
[287] | 1219 | |
---|
[770] | 1220 | def disconnect( self ): |
---|
| 1221 | """Close socket""" |
---|
[33] | 1222 | |
---|
[770] | 1223 | if self.s: |
---|
| 1224 | #self.s.shutdown( 2 ) |
---|
| 1225 | self.s.close() |
---|
| 1226 | self.s = None |
---|
[33] | 1227 | |
---|
[770] | 1228 | def __del__( self ): |
---|
| 1229 | """Kill the socket before we leave""" |
---|
[33] | 1230 | |
---|
[770] | 1231 | self.disconnect() |
---|
[33] | 1232 | |
---|
[770] | 1233 | def reGetData( self ): |
---|
| 1234 | """Reconnect""" |
---|
[33] | 1235 | |
---|
[770] | 1236 | while self.update_now: |
---|
[287] | 1237 | |
---|
[770] | 1238 | # Must be another update in progress: |
---|
| 1239 | # Wait until the update is complete |
---|
| 1240 | # |
---|
| 1241 | time.sleep( 1 ) |
---|
[287] | 1242 | |
---|
[770] | 1243 | if self.s: |
---|
| 1244 | self.disconnect() |
---|
[33] | 1245 | |
---|
[770] | 1246 | self.retrieveData() |
---|
[5] | 1247 | |
---|
[770] | 1248 | def getData( self ): |
---|
[287] | 1249 | |
---|
[770] | 1250 | """Return the XML data""" |
---|
[287] | 1251 | |
---|
[770] | 1252 | # If more than MIN_UPDATE_INT seconds passed since last data update |
---|
| 1253 | # update the XML first before returning it |
---|
| 1254 | # |
---|
[287] | 1255 | |
---|
[770] | 1256 | cur_time = time.time() |
---|
[287] | 1257 | |
---|
[770] | 1258 | if ( cur_time - self.LAST_UPDATE ) > self.MIN_UPDATE_INT: |
---|
[287] | 1259 | |
---|
[770] | 1260 | self.reGetData() |
---|
[287] | 1261 | |
---|
[770] | 1262 | while self.update_now: |
---|
[287] | 1263 | |
---|
[770] | 1264 | # Must be another update in progress: |
---|
| 1265 | # Wait until the update is complete |
---|
| 1266 | # |
---|
| 1267 | time.sleep( 1 ) |
---|
| 1268 | |
---|
| 1269 | return self.data |
---|
[287] | 1270 | |
---|
[770] | 1271 | def makeFileDescriptor( self ): |
---|
| 1272 | """Make file descriptor that points to our socket connection""" |
---|
[70] | 1273 | |
---|
[770] | 1274 | self.reconnect() |
---|
[70] | 1275 | |
---|
[770] | 1276 | if self.s: |
---|
| 1277 | self.fd = self.s.makefile( 'r' ) |
---|
[70] | 1278 | |
---|
[770] | 1279 | def getFileObject( self ): |
---|
| 1280 | """Connect, and return a file object""" |
---|
[70] | 1281 | |
---|
[770] | 1282 | self.makeFileDescriptor() |
---|
[78] | 1283 | |
---|
[770] | 1284 | if self.fd: |
---|
| 1285 | return self.fd |
---|
[70] | 1286 | |
---|
[78] | 1287 | class GangliaXMLProcessor( XMLProcessor ): |
---|
[770] | 1288 | """Main class for processing XML and acting with it""" |
---|
[5] | 1289 | |
---|
[770] | 1290 | def __init__( self, XMLSource, DataStore ): |
---|
| 1291 | """Setup initial XML connection and handlers""" |
---|
[33] | 1292 | |
---|
[770] | 1293 | self.config = GangliaConfigParser( GMETAD_CONF ) |
---|
[33] | 1294 | |
---|
[770] | 1295 | #self.myXMLGatherer = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) |
---|
| 1296 | #self.myXMLSource = self.myXMLGatherer.getFileObject() |
---|
| 1297 | self.myXMLSource = XMLSource |
---|
| 1298 | self.ds = DataStore |
---|
| 1299 | self.myXMLHandler = GangliaXMLHandler( self.config, self.ds ) |
---|
| 1300 | self.myXMLError = XMLErrorHandler() |
---|
[73] | 1301 | |
---|
[770] | 1302 | def run( self ): |
---|
| 1303 | """Main XML processing; start a xml and storethread""" |
---|
[8] | 1304 | |
---|
[770] | 1305 | xml_thread = threading.Thread( None, self.processXML, 'xmlthread' ) |
---|
| 1306 | store_thread = threading.Thread( None, self.storeMetrics, 'storethread' ) |
---|
[22] | 1307 | |
---|
[770] | 1308 | while( 1 ): |
---|
[36] | 1309 | |
---|
[770] | 1310 | if not xml_thread.isAlive(): |
---|
| 1311 | # Gather XML at the same interval as gmetad |
---|
[36] | 1312 | |
---|
[770] | 1313 | # threaded call to: self.processXML() |
---|
| 1314 | # |
---|
| 1315 | try: |
---|
| 1316 | xml_thread = threading.Thread( None, self.processXML, 'xml_thread' ) |
---|
| 1317 | xml_thread.start() |
---|
| 1318 | except thread.error, msg: |
---|
| 1319 | debug_msg( 0, 'ERROR: Unable to start xml_thread!: '+str(msg)) |
---|
| 1320 | #return 1 |
---|
[36] | 1321 | |
---|
[770] | 1322 | if not store_thread.isAlive(): |
---|
| 1323 | # Store metrics every .. sec |
---|
[36] | 1324 | |
---|
[770] | 1325 | # threaded call to: self.storeMetrics() |
---|
| 1326 | # |
---|
| 1327 | try: |
---|
| 1328 | store_thread = threading.Thread( None, self.storeMetrics, 'store_thread' ) |
---|
| 1329 | store_thread.start() |
---|
| 1330 | except thread.error, msg: |
---|
| 1331 | debug_msg( 0, 'ERROR: Unable to start store_thread!: '+str(msg)) |
---|
| 1332 | #return 1 |
---|
| 1333 | |
---|
| 1334 | # Just sleep a sec here, to prevent daemon from going mad. We're all threads here anyway |
---|
| 1335 | time.sleep( 1 ) |
---|
[36] | 1336 | |
---|
[770] | 1337 | def storeMetrics( self ): |
---|
| 1338 | """Store metrics retained in memory to disk""" |
---|
[22] | 1339 | |
---|
[770] | 1340 | global DEBUG_LEVEL |
---|
[365] | 1341 | |
---|
[770] | 1342 | # Store metrics somewhere between every 360 and 640 seconds |
---|
| 1343 | # |
---|
| 1344 | if DEBUG_LEVEL > 2: |
---|
| 1345 | #STORE_INTERVAL = 60 |
---|
| 1346 | STORE_INTERVAL = random.randint( 360, 640 ) |
---|
| 1347 | else: |
---|
| 1348 | STORE_INTERVAL = random.randint( 360, 640 ) |
---|
[22] | 1349 | |
---|
[770] | 1350 | try: |
---|
| 1351 | store_metric_thread = threading.Thread( None, self.storeThread, 'store_metric_thread' ) |
---|
| 1352 | store_metric_thread.start() |
---|
| 1353 | except thread.error, msg: |
---|
| 1354 | debug_msg( 0, 'ERROR: Unable to start ganglia_store_thread()!: '+str(msg) ) |
---|
| 1355 | return 1 |
---|
[36] | 1356 | |
---|
[770] | 1357 | debug_msg( 1, 'ganglia_store_thread(): started.' ) |
---|
[169] | 1358 | |
---|
[770] | 1359 | debug_msg( 1, 'ganglia_store_thread(): Sleeping.. (%ss)' %STORE_INTERVAL ) |
---|
| 1360 | time.sleep( STORE_INTERVAL ) |
---|
| 1361 | debug_msg( 1, 'ganglia_store_thread(): Done sleeping.' ) |
---|
[36] | 1362 | |
---|
[770] | 1363 | if store_metric_thread.isAlive(): |
---|
[36] | 1364 | |
---|
[770] | 1365 | debug_msg( 1, 'ganglia_store_thread(): storemetricthread() still running, waiting to finish..' ) |
---|
| 1366 | store_metric_thread.join( STORE_TIMEOUT ) # Maximum time is for storing thread to finish |
---|
| 1367 | debug_msg( 1, 'ganglia_store_thread(): Done waiting.' ) |
---|
[36] | 1368 | |
---|
[770] | 1369 | debug_msg( 1, 'ganglia_store_thread(): finished.' ) |
---|
[36] | 1370 | |
---|
[770] | 1371 | return 0 |
---|
[36] | 1372 | |
---|
[770] | 1373 | def storeThread( self ): |
---|
| 1374 | """Actual metric storing thread""" |
---|
[39] | 1375 | |
---|
[770] | 1376 | debug_msg( 1, 'ganglia_store_metric_thread(): started.' ) |
---|
| 1377 | debug_msg( 1, 'ganglia_store_metric_thread(): Storing data..' ) |
---|
| 1378 | ret = self.myXMLHandler.storeMetrics() |
---|
| 1379 | if ret > 0: |
---|
| 1380 | debug_msg( 0, 'ganglia_store_metric_thread(): UNKNOWN ERROR %s while storing Metrics!' %str(ret) ) |
---|
| 1381 | debug_msg( 1, 'ganglia_store_metric_thread(): Done storing.' ) |
---|
| 1382 | debug_msg( 1, 'ganglia_store_metric_thread(): finished.' ) |
---|
| 1383 | |
---|
| 1384 | return 0 |
---|
[39] | 1385 | |
---|
[770] | 1386 | def processXML( self ): |
---|
| 1387 | """Process XML""" |
---|
[8] | 1388 | |
---|
[770] | 1389 | try: |
---|
| 1390 | parsethread = threading.Thread( None, self.parseThread, 'parsethread' ) |
---|
| 1391 | parsethread.start() |
---|
| 1392 | except thread.error, msg: |
---|
| 1393 | debug_msg( 0, 'ERROR: Unable to start ganglia_xml_thread()!: ' + str(msg) ) |
---|
| 1394 | return 1 |
---|
[8] | 1395 | |
---|
[770] | 1396 | debug_msg( 1, 'ganglia_xml_thread(): started.' ) |
---|
[36] | 1397 | |
---|
[770] | 1398 | debug_msg( 1, 'ganglia_xml_thread(): Sleeping.. (%ss)' %self.config.getLowestInterval() ) |
---|
| 1399 | time.sleep( float( self.config.getLowestInterval() ) ) |
---|
| 1400 | debug_msg( 1, 'ganglia_xml_thread(): Done sleeping.' ) |
---|
[36] | 1401 | |
---|
[770] | 1402 | if parsethread.isAlive(): |
---|
[36] | 1403 | |
---|
[770] | 1404 | debug_msg( 1, 'ganglia_xml_thread(): parsethread() still running, waiting (%ss) to finish..' %PARSE_TIMEOUT ) |
---|
| 1405 | parsethread.join( PARSE_TIMEOUT ) # Maximum time for XML thread to finish |
---|
| 1406 | debug_msg( 1, 'ganglia_xml_thread(): Done waiting.' ) |
---|
[36] | 1407 | |
---|
[770] | 1408 | debug_msg( 1, 'ganglia_xml_thread(): finished.' ) |
---|
[36] | 1409 | |
---|
[770] | 1410 | return 0 |
---|
[36] | 1411 | |
---|
[770] | 1412 | def parseThread( self ): |
---|
| 1413 | """Actual parsing thread""" |
---|
[39] | 1414 | |
---|
[770] | 1415 | debug_msg( 1, 'ganglia_parse_thread(): started.' ) |
---|
| 1416 | debug_msg( 1, 'ganglia_parse_thread(): Retrieving XML data..' ) |
---|
| 1417 | |
---|
| 1418 | my_data = self.myXMLSource.getData() |
---|
[176] | 1419 | |
---|
[770] | 1420 | debug_msg( 1, 'ganglia_parse_thread(): Done retrieving.' ) |
---|
[293] | 1421 | |
---|
[770] | 1422 | if my_data: |
---|
| 1423 | debug_msg( 1, 'ganglia_parse_thread(): Parsing XML..' ) |
---|
| 1424 | xml.sax.parseString( my_data, self.myXMLHandler, self.myXMLError ) |
---|
| 1425 | debug_msg( 1, 'ganglia_parse_thread(): Done parsing.' ) |
---|
[176] | 1426 | |
---|
[770] | 1427 | debug_msg( 1, 'ganglia_parse_thread(): finished.' ) |
---|
[39] | 1428 | |
---|
[770] | 1429 | return 0 |
---|
[39] | 1430 | |
---|
[9] | 1431 | class GangliaConfigParser: |
---|
| 1432 | |
---|
[770] | 1433 | sources = [ ] |
---|
[9] | 1434 | |
---|
[770] | 1435 | def __init__( self, config ): |
---|
| 1436 | """Parse some stuff from our gmetad's config, such as polling interval""" |
---|
[32] | 1437 | |
---|
[770] | 1438 | self.config = config |
---|
| 1439 | self.parseValues() |
---|
[9] | 1440 | |
---|
[770] | 1441 | def parseValues( self ): |
---|
| 1442 | """Parse certain values from gmetad.conf""" |
---|
[9] | 1443 | |
---|
[770] | 1444 | readcfg = open( self.config, 'r' ) |
---|
[9] | 1445 | |
---|
[770] | 1446 | for line in readcfg.readlines(): |
---|
[9] | 1447 | |
---|
[770] | 1448 | if line.count( '"' ) > 1: |
---|
[9] | 1449 | |
---|
[770] | 1450 | if line.find( 'data_source' ) != -1 and line[0] != '#': |
---|
[9] | 1451 | |
---|
[770] | 1452 | source = { } |
---|
| 1453 | source['name'] = line.split( '"' )[1] |
---|
| 1454 | source_words = line.split( '"' )[2].split( ' ' ) |
---|
[9] | 1455 | |
---|
[770] | 1456 | for word in source_words: |
---|
[9] | 1457 | |
---|
[770] | 1458 | valid_interval = 1 |
---|
[9] | 1459 | |
---|
[770] | 1460 | for letter in word: |
---|
[32] | 1461 | |
---|
[770] | 1462 | if letter not in string.digits: |
---|
[32] | 1463 | |
---|
[770] | 1464 | valid_interval = 0 |
---|
[9] | 1465 | |
---|
[770] | 1466 | if valid_interval and len(word) > 0: |
---|
[32] | 1467 | |
---|
[770] | 1468 | source['interval'] = word |
---|
| 1469 | debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) ) |
---|
| 1470 | |
---|
| 1471 | # No interval found, use Ganglia's default |
---|
| 1472 | if not source.has_key( 'interval' ): |
---|
| 1473 | source['interval'] = 15 |
---|
| 1474 | debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) ) |
---|
[32] | 1475 | |
---|
[770] | 1476 | self.sources.append( source ) |
---|
[9] | 1477 | |
---|
[770] | 1478 | def getInterval( self, source_name ): |
---|
| 1479 | """Return interval for source_name""" |
---|
[32] | 1480 | |
---|
[770] | 1481 | for source in self.sources: |
---|
[32] | 1482 | |
---|
[770] | 1483 | if source['name'] == source_name: |
---|
[32] | 1484 | |
---|
[770] | 1485 | return source['interval'] |
---|
[32] | 1486 | |
---|
[770] | 1487 | return None |
---|
[9] | 1488 | |
---|
[770] | 1489 | def getLowestInterval( self ): |
---|
| 1490 | """Return the lowest interval of all clusters""" |
---|
[34] | 1491 | |
---|
[770] | 1492 | lowest_interval = 0 |
---|
[34] | 1493 | |
---|
[770] | 1494 | for source in self.sources: |
---|
[34] | 1495 | |
---|
[770] | 1496 | if not lowest_interval or source['interval'] <= lowest_interval: |
---|
[34] | 1497 | |
---|
[770] | 1498 | lowest_interval = source['interval'] |
---|
[34] | 1499 | |
---|
[770] | 1500 | # Return 15 when nothing is found, so that the daemon won't go insane with 0 sec delays |
---|
| 1501 | if lowest_interval: |
---|
| 1502 | return lowest_interval |
---|
| 1503 | else: |
---|
| 1504 | return 15 |
---|
[34] | 1505 | |
---|
[9] | 1506 | class RRDHandler: |
---|
[770] | 1507 | """Class for handling RRD activity""" |
---|
[9] | 1508 | |
---|
[770] | 1509 | myMetrics = { } |
---|
| 1510 | lastStored = { } |
---|
| 1511 | timeserials = { } |
---|
| 1512 | slot = None |
---|
[32] | 1513 | |
---|
[770] | 1514 | def __init__( self, config, cluster ): |
---|
| 1515 | """Setup initial variables""" |
---|
[78] | 1516 | |
---|
[770] | 1517 | global MODRRDTOOL |
---|
[455] | 1518 | |
---|
[770] | 1519 | self.block = 0 |
---|
| 1520 | self.cluster = cluster |
---|
| 1521 | self.config = config |
---|
| 1522 | self.slot = threading.Lock() |
---|
[292] | 1523 | |
---|
[770] | 1524 | if MODRRDTOOL: |
---|
[455] | 1525 | |
---|
[770] | 1526 | self.rrdm = RRDMutator() |
---|
| 1527 | else: |
---|
| 1528 | self.rrdm = RRDMutator( RRDTOOL ) |
---|
[455] | 1529 | |
---|
[770] | 1530 | global DEBUG_LEVEL |
---|
[9] | 1531 | |
---|
[770] | 1532 | if DEBUG_LEVEL <= 2: |
---|
| 1533 | self.gatherLastUpdates() |
---|
[365] | 1534 | |
---|
[770] | 1535 | def gatherLastUpdates( self ): |
---|
| 1536 | """Populate the lastStored list, containing timestamps of all last updates""" |
---|
[42] | 1537 | |
---|
[770] | 1538 | cluster_dir = '%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster ) |
---|
[42] | 1539 | |
---|
[770] | 1540 | hosts = [ ] |
---|
[42] | 1541 | |
---|
[770] | 1542 | if os.path.exists( cluster_dir ): |
---|
[42] | 1543 | |
---|
[770] | 1544 | dirlist = os.listdir( cluster_dir ) |
---|
[42] | 1545 | |
---|
[770] | 1546 | for dir in dirlist: |
---|
[42] | 1547 | |
---|
[770] | 1548 | hosts.append( dir ) |
---|
[42] | 1549 | |
---|
[770] | 1550 | for host in hosts: |
---|
[42] | 1551 | |
---|
[770] | 1552 | host_dir = cluster_dir + '/' + host |
---|
| 1553 | dirlist = os.listdir( host_dir ) |
---|
[47] | 1554 | |
---|
[770] | 1555 | for dir in dirlist: |
---|
[47] | 1556 | |
---|
[770] | 1557 | if not self.timeserials.has_key( host ): |
---|
[47] | 1558 | |
---|
[770] | 1559 | self.timeserials[ host ] = [ ] |
---|
[47] | 1560 | |
---|
[770] | 1561 | self.timeserials[ host ].append( dir ) |
---|
[47] | 1562 | |
---|
[770] | 1563 | last_serial = self.getLastRrdTimeSerial( host ) |
---|
[292] | 1564 | |
---|
[770] | 1565 | if last_serial: |
---|
[42] | 1566 | |
---|
[770] | 1567 | metric_dir = cluster_dir + '/' + host + '/' + last_serial |
---|
[292] | 1568 | |
---|
[770] | 1569 | if os.path.exists( metric_dir ): |
---|
[42] | 1570 | |
---|
[770] | 1571 | dirlist = os.listdir( metric_dir ) |
---|
[42] | 1572 | |
---|
[770] | 1573 | for file in dirlist: |
---|
[42] | 1574 | |
---|
[770] | 1575 | metricname = file.split( '.rrd' )[0] |
---|
[42] | 1576 | |
---|
[770] | 1577 | if not self.lastStored.has_key( host ): |
---|
[42] | 1578 | |
---|
[770] | 1579 | self.lastStored[ host ] = { } |
---|
[42] | 1580 | |
---|
[770] | 1581 | self.lastStored[ host ][ metricname ] = self.rrdm.grabLastUpdate( metric_dir + '/' + file ) |
---|
[42] | 1582 | |
---|
[770] | 1583 | def getClusterName( self ): |
---|
| 1584 | """Return clustername""" |
---|
[63] | 1585 | |
---|
[770] | 1586 | return self.cluster |
---|
[32] | 1587 | |
---|
[770] | 1588 | def memMetric( self, host, metric ): |
---|
| 1589 | """Store metric from host in memory""" |
---|
[32] | 1590 | |
---|
[770] | 1591 | # <ATOMIC> |
---|
| 1592 | # |
---|
| 1593 | self.slot.acquire() |
---|
| 1594 | |
---|
| 1595 | if self.myMetrics.has_key( host ): |
---|
[32] | 1596 | |
---|
[770] | 1597 | if self.myMetrics[ host ].has_key( metric['name'] ): |
---|
[32] | 1598 | |
---|
[770] | 1599 | for mymetric in self.myMetrics[ host ][ metric['name'] ]: |
---|
[32] | 1600 | |
---|
[770] | 1601 | if mymetric['time'] == metric['time']: |
---|
[32] | 1602 | |
---|
[770] | 1603 | # Allready have this metric, abort |
---|
| 1604 | self.slot.release() |
---|
| 1605 | return 1 |
---|
| 1606 | else: |
---|
| 1607 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
| 1608 | else: |
---|
| 1609 | self.myMetrics[ host ] = { } |
---|
| 1610 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
[32] | 1611 | |
---|
[770] | 1612 | # Push new metric onto stack |
---|
| 1613 | # atomic code; only 1 thread at a time may access the stack |
---|
[63] | 1614 | |
---|
[770] | 1615 | self.myMetrics[ host ][ metric['name'] ].append( metric ) |
---|
[32] | 1616 | |
---|
[770] | 1617 | self.slot.release() |
---|
| 1618 | # |
---|
| 1619 | # </ATOMIC> |
---|
[40] | 1620 | |
---|
[770] | 1621 | def makeUpdateList( self, host, metriclist ): |
---|
| 1622 | """ |
---|
| 1623 | Make a list of update values for rrdupdate |
---|
| 1624 | but only those that we didn't store before |
---|
| 1625 | """ |
---|
[37] | 1626 | |
---|
[770] | 1627 | update_list = [ ] |
---|
| 1628 | metric = None |
---|
[37] | 1629 | |
---|
[770] | 1630 | while len( metriclist ) > 0: |
---|
[37] | 1631 | |
---|
[770] | 1632 | metric = metriclist.pop( 0 ) |
---|
[37] | 1633 | |
---|
[770] | 1634 | if self.checkStoreMetric( host, metric ): |
---|
[292] | 1635 | |
---|
[770] | 1636 | u_val = str( metric['time'] ) + ':' + str( metric['val'] ) |
---|
| 1637 | #update_list.append( str('%s:%s') %( metric['time'], metric['val'] ) ) |
---|
| 1638 | update_list.append( u_val ) |
---|
[40] | 1639 | |
---|
[770] | 1640 | return update_list |
---|
[37] | 1641 | |
---|
[770] | 1642 | def checkStoreMetric( self, host, metric ): |
---|
| 1643 | """Check if supplied metric if newer than last one stored""" |
---|
[40] | 1644 | |
---|
[770] | 1645 | if self.lastStored.has_key( host ): |
---|
[40] | 1646 | |
---|
[770] | 1647 | if self.lastStored[ host ].has_key( metric['name'] ): |
---|
[40] | 1648 | |
---|
[770] | 1649 | if metric['time'] <= self.lastStored[ host ][ metric['name'] ]: |
---|
[40] | 1650 | |
---|
[770] | 1651 | # This is old |
---|
| 1652 | return 0 |
---|
[40] | 1653 | |
---|
[770] | 1654 | return 1 |
---|
[50] | 1655 | |
---|
[770] | 1656 | def memLastUpdate( self, host, metricname, metriclist ): |
---|
| 1657 | """ |
---|
| 1658 | Memorize the time of the latest metric from metriclist |
---|
| 1659 | but only if it wasn't allready memorized |
---|
| 1660 | """ |
---|
[50] | 1661 | |
---|
[770] | 1662 | if not self.lastStored.has_key( host ): |
---|
| 1663 | self.lastStored[ host ] = { } |
---|
[54] | 1664 | |
---|
[770] | 1665 | last_update_time = 0 |
---|
[50] | 1666 | |
---|
[770] | 1667 | for metric in metriclist: |
---|
[50] | 1668 | |
---|
[770] | 1669 | if metric['name'] == metricname: |
---|
[50] | 1670 | |
---|
[770] | 1671 | if metric['time'] > last_update_time: |
---|
[50] | 1672 | |
---|
[770] | 1673 | last_update_time = metric['time'] |
---|
[40] | 1674 | |
---|
[770] | 1675 | if self.lastStored[ host ].has_key( metricname ): |
---|
| 1676 | |
---|
| 1677 | if last_update_time <= self.lastStored[ host ][ metricname ]: |
---|
| 1678 | return 1 |
---|
[40] | 1679 | |
---|
[770] | 1680 | self.lastStored[ host ][ metricname ] = last_update_time |
---|
[52] | 1681 | |
---|
[770] | 1682 | def storeMetrics( self ): |
---|
| 1683 | """ |
---|
| 1684 | Store all metrics from memory to disk |
---|
| 1685 | and do it to the RRD's in appropriate timeperiod directory |
---|
| 1686 | """ |
---|
[33] | 1687 | |
---|
[770] | 1688 | debug_msg( 5, "Entering storeMetrics()") |
---|
[365] | 1689 | |
---|
[770] | 1690 | count_values = 0 |
---|
| 1691 | count_metrics = 0 |
---|
| 1692 | count_bits = 0 |
---|
[365] | 1693 | |
---|
[770] | 1694 | for hostname, mymetrics in self.myMetrics.items(): |
---|
[33] | 1695 | |
---|
[770] | 1696 | for metricname, mymetric in mymetrics.items(): |
---|
[33] | 1697 | |
---|
[770] | 1698 | count_metrics += 1 |
---|
[365] | 1699 | |
---|
[770] | 1700 | for dmetric in mymetric: |
---|
[365] | 1701 | |
---|
[770] | 1702 | count_values += 1 |
---|
[365] | 1703 | |
---|
[770] | 1704 | count_bits += len( dmetric['time'] ) |
---|
| 1705 | count_bits += len( dmetric['val'] ) |
---|
[365] | 1706 | |
---|
[770] | 1707 | count_bytes = count_bits / 8 |
---|
[365] | 1708 | |
---|
[770] | 1709 | debug_msg( 5, "size of cluster '" + self.cluster + "': " + |
---|
| 1710 | str( len( self.myMetrics.keys() ) ) + " hosts " + |
---|
| 1711 | str( count_metrics ) + " metrics " + str( count_values ) + " values " + |
---|
| 1712 | str( count_bits ) + " bits " + str( count_bytes ) + " bytes " ) |
---|
[365] | 1713 | |
---|
[770] | 1714 | for hostname, mymetrics in self.myMetrics.items(): |
---|
[365] | 1715 | |
---|
[770] | 1716 | for metricname, mymetric in mymetrics.items(): |
---|
[365] | 1717 | |
---|
[770] | 1718 | metrics_to_store = [ ] |
---|
[53] | 1719 | |
---|
[770] | 1720 | # Pop metrics from stack for storing until none is left |
---|
| 1721 | # atomic code: only 1 thread at a time may access myMetrics |
---|
[63] | 1722 | |
---|
[770] | 1723 | # <ATOMIC> |
---|
| 1724 | # |
---|
| 1725 | self.slot.acquire() |
---|
[33] | 1726 | |
---|
[770] | 1727 | while len( self.myMetrics[ hostname ][ metricname ] ) > 0: |
---|
[53] | 1728 | |
---|
[770] | 1729 | if len( self.myMetrics[ hostname ][ metricname ] ) > 0: |
---|
[53] | 1730 | |
---|
[770] | 1731 | try: |
---|
| 1732 | metrics_to_store.append( self.myMetrics[ hostname ][ metricname ].pop( 0 ) ) |
---|
| 1733 | except IndexError, msg: |
---|
[176] | 1734 | |
---|
[770] | 1735 | # Somehow sometimes myMetrics[ hostname ][ metricname ] |
---|
| 1736 | # is still len 0 when the statement is executed. |
---|
| 1737 | # Just ignore indexerror's.. |
---|
| 1738 | pass |
---|
[176] | 1739 | |
---|
[770] | 1740 | self.slot.release() |
---|
| 1741 | # |
---|
| 1742 | # </ATOMIC> |
---|
[53] | 1743 | |
---|
[770] | 1744 | # Create a mapping table, each metric to the period where it should be stored |
---|
| 1745 | # |
---|
| 1746 | metric_serial_table = self.determineSerials( hostname, metricname, metrics_to_store ) |
---|
[33] | 1747 | |
---|
[770] | 1748 | update_rets = [ ] |
---|
[50] | 1749 | |
---|
[770] | 1750 | for period, pmetric in metric_serial_table.items(): |
---|
[47] | 1751 | |
---|
[770] | 1752 | create_ret = self.createCheck( hostname, metricname, period ) |
---|
[47] | 1753 | |
---|
[770] | 1754 | update_ret = self.update( hostname, metricname, period, pmetric ) |
---|
[47] | 1755 | |
---|
[770] | 1756 | if update_ret == 0: |
---|
[47] | 1757 | |
---|
[770] | 1758 | debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) ) |
---|
| 1759 | else: |
---|
| 1760 | debug_msg( 9, 'metric update failed' ) |
---|
[47] | 1761 | |
---|
[770] | 1762 | update_rets.append( create_ret ) |
---|
| 1763 | update_rets.append( update_ret ) |
---|
[47] | 1764 | |
---|
[770] | 1765 | # Lets ignore errors here for now, we need to make sure last update time |
---|
| 1766 | # is correct! |
---|
| 1767 | # |
---|
| 1768 | #if not (1) in update_rets: |
---|
[50] | 1769 | |
---|
[770] | 1770 | self.memLastUpdate( hostname, metricname, metrics_to_store ) |
---|
[50] | 1771 | |
---|
[770] | 1772 | debug_msg( 5, "Leaving storeMetrics()") |
---|
[365] | 1773 | |
---|
[770] | 1774 | def makeTimeSerial( self ): |
---|
| 1775 | """Generate a time serial. Seconds since epoch""" |
---|
[17] | 1776 | |
---|
[770] | 1777 | # Seconds since epoch |
---|
| 1778 | mytime = int( time.time() ) |
---|
[17] | 1779 | |
---|
[770] | 1780 | return mytime |
---|
[17] | 1781 | |
---|
[770] | 1782 | def makeRrdPath( self, host, metricname, timeserial ): |
---|
| 1783 | """Make a RRD location/path and filename""" |
---|
[17] | 1784 | |
---|
[770] | 1785 | rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial ) |
---|
| 1786 | rrd_file = '%s/%s.rrd' %( rrd_dir, metricname ) |
---|
[17] | 1787 | |
---|
[770] | 1788 | return rrd_dir, rrd_file |
---|
[17] | 1789 | |
---|
[770] | 1790 | def getLastRrdTimeSerial( self, host ): |
---|
| 1791 | """Find the last timeserial (directory) for this host""" |
---|
[17] | 1792 | |
---|
[770] | 1793 | newest_timeserial = 0 |
---|
[19] | 1794 | |
---|
[770] | 1795 | for dir in self.timeserials[ host ]: |
---|
[32] | 1796 | |
---|
[770] | 1797 | valid_dir = 1 |
---|
[17] | 1798 | |
---|
[770] | 1799 | for letter in dir: |
---|
| 1800 | if letter not in string.digits: |
---|
| 1801 | valid_dir = 0 |
---|
[17] | 1802 | |
---|
[770] | 1803 | if valid_dir: |
---|
| 1804 | timeserial = dir |
---|
| 1805 | if timeserial > newest_timeserial: |
---|
| 1806 | newest_timeserial = timeserial |
---|
[17] | 1807 | |
---|
[770] | 1808 | if newest_timeserial: |
---|
| 1809 | return newest_timeserial |
---|
| 1810 | else: |
---|
| 1811 | return 0 |
---|
[17] | 1812 | |
---|
[770] | 1813 | def determinePeriod( self, host, check_serial ): |
---|
| 1814 | """Determine to which period (directory) this time(serial) belongs""" |
---|
[47] | 1815 | |
---|
[770] | 1816 | period_serial = 0 |
---|
[47] | 1817 | |
---|
[770] | 1818 | if self.timeserials.has_key( host ): |
---|
[47] | 1819 | |
---|
[770] | 1820 | for serial in self.timeserials[ host ]: |
---|
[47] | 1821 | |
---|
[770] | 1822 | if check_serial >= serial and period_serial < serial: |
---|
[47] | 1823 | |
---|
[770] | 1824 | period_serial = serial |
---|
[56] | 1825 | |
---|
[770] | 1826 | return period_serial |
---|
[47] | 1827 | |
---|
[770] | 1828 | def determineSerials( self, host, metricname, metriclist ): |
---|
| 1829 | """ |
---|
| 1830 | Determine the correct serial and corresponding rrd to store |
---|
| 1831 | for a list of metrics |
---|
| 1832 | """ |
---|
[47] | 1833 | |
---|
[770] | 1834 | metric_serial_table = { } |
---|
[47] | 1835 | |
---|
[770] | 1836 | for metric in metriclist: |
---|
[47] | 1837 | |
---|
[770] | 1838 | if metric['name'] == metricname: |
---|
[47] | 1839 | |
---|
[770] | 1840 | period = self.determinePeriod( host, metric['time'] ) |
---|
[47] | 1841 | |
---|
[770] | 1842 | archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60) |
---|
[47] | 1843 | |
---|
[770] | 1844 | if (int( metric['time'] ) - int( period ) ) > archive_secs: |
---|
[47] | 1845 | |
---|
[770] | 1846 | # This one should get it's own new period |
---|
| 1847 | period = metric['time'] |
---|
[57] | 1848 | |
---|
[770] | 1849 | if not self.timeserials.has_key( host ): |
---|
| 1850 | self.timeserials[ host ] = [ ] |
---|
[57] | 1851 | |
---|
[770] | 1852 | self.timeserials[ host ].append( period ) |
---|
[47] | 1853 | |
---|
[770] | 1854 | if not metric_serial_table.has_key( period ): |
---|
[47] | 1855 | |
---|
[770] | 1856 | metric_serial_table[ period ] = [ ] |
---|
[47] | 1857 | |
---|
[770] | 1858 | metric_serial_table[ period ].append( metric ) |
---|
[47] | 1859 | |
---|
[770] | 1860 | return metric_serial_table |
---|
[47] | 1861 | |
---|
[770] | 1862 | def createCheck( self, host, metricname, timeserial ): |
---|
| 1863 | """Check if an rrd allready exists for this metric, create if not""" |
---|
[9] | 1864 | |
---|
[770] | 1865 | debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
| 1866 | |
---|
| 1867 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[17] | 1868 | |
---|
[770] | 1869 | if not os.path.exists( rrd_dir ): |
---|
[58] | 1870 | |
---|
[770] | 1871 | try: |
---|
| 1872 | os.makedirs( rrd_dir ) |
---|
[58] | 1873 | |
---|
[770] | 1874 | except os.OSError, msg: |
---|
[58] | 1875 | |
---|
[770] | 1876 | if msg.find( 'File exists' ) != -1: |
---|
[58] | 1877 | |
---|
[770] | 1878 | # Ignore exists errors |
---|
| 1879 | pass |
---|
[58] | 1880 | |
---|
[770] | 1881 | else: |
---|
[58] | 1882 | |
---|
[770] | 1883 | print msg |
---|
| 1884 | return |
---|
[58] | 1885 | |
---|
[770] | 1886 | debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) ) |
---|
[9] | 1887 | |
---|
[770] | 1888 | if not os.path.exists( rrd_file ): |
---|
[9] | 1889 | |
---|
[770] | 1890 | interval = self.config.getInterval( self.cluster ) |
---|
| 1891 | heartbeat = 8 * int( interval ) |
---|
[9] | 1892 | |
---|
[770] | 1893 | params = [ ] |
---|
[12] | 1894 | |
---|
[770] | 1895 | params.append( '--step' ) |
---|
| 1896 | params.append( str( interval ) ) |
---|
[12] | 1897 | |
---|
[770] | 1898 | params.append( '--start' ) |
---|
| 1899 | params.append( str( int( timeserial ) - 1 ) ) |
---|
[12] | 1900 | |
---|
[770] | 1901 | params.append( 'DS:sum:GAUGE:%d:U:U' %heartbeat ) |
---|
| 1902 | params.append( 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) ) |
---|
[13] | 1903 | |
---|
[770] | 1904 | self.rrdm.create( str(rrd_file), params ) |
---|
[37] | 1905 | |
---|
[770] | 1906 | debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) ) |
---|
[14] | 1907 | |
---|
[770] | 1908 | def update( self, host, metricname, timeserial, metriclist ): |
---|
| 1909 | """ |
---|
| 1910 | Update rrd file for host with metricname |
---|
| 1911 | in directory timeserial with metriclist |
---|
| 1912 | """ |
---|
[9] | 1913 | |
---|
[770] | 1914 | debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
[9] | 1915 | |
---|
[770] | 1916 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[18] | 1917 | |
---|
[770] | 1918 | update_list = self.makeUpdateList( host, metriclist ) |
---|
[15] | 1919 | |
---|
[770] | 1920 | if len( update_list ) > 0: |
---|
| 1921 | ret = self.rrdm.update( str(rrd_file), update_list ) |
---|
[32] | 1922 | |
---|
[770] | 1923 | if ret: |
---|
| 1924 | return 1 |
---|
| 1925 | |
---|
| 1926 | debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), string.join( update_list ) ) ) |
---|
[15] | 1927 | |
---|
[770] | 1928 | return 0 |
---|
[36] | 1929 | |
---|
[169] | 1930 | def daemon(): |
---|
[770] | 1931 | """daemonized threading""" |
---|
[8] | 1932 | |
---|
[770] | 1933 | # Fork the first child |
---|
| 1934 | # |
---|
| 1935 | pid = os.fork() |
---|
[169] | 1936 | |
---|
[770] | 1937 | if pid > 0: |
---|
[169] | 1938 | |
---|
[770] | 1939 | sys.exit(0) # end parent |
---|
[169] | 1940 | |
---|
[770] | 1941 | # creates a session and sets the process group ID |
---|
| 1942 | # |
---|
| 1943 | os.setsid() |
---|
[169] | 1944 | |
---|
[770] | 1945 | # Fork the second child |
---|
| 1946 | # |
---|
| 1947 | pid = os.fork() |
---|
[169] | 1948 | |
---|
[770] | 1949 | if pid > 0: |
---|
[169] | 1950 | |
---|
[770] | 1951 | sys.exit(0) # end parent |
---|
[169] | 1952 | |
---|
[770] | 1953 | write_pidfile() |
---|
[435] | 1954 | |
---|
[770] | 1955 | # Go to the root directory and set the umask |
---|
| 1956 | # |
---|
| 1957 | os.chdir('/') |
---|
| 1958 | os.umask(0) |
---|
[169] | 1959 | |
---|
[770] | 1960 | sys.stdin.close() |
---|
| 1961 | sys.stdout.close() |
---|
| 1962 | sys.stderr.close() |
---|
[169] | 1963 | |
---|
[770] | 1964 | os.open('/dev/null', os.O_RDWR) |
---|
| 1965 | os.dup2(0, 1) |
---|
| 1966 | os.dup2(0, 2) |
---|
[169] | 1967 | |
---|
[770] | 1968 | run() |
---|
[169] | 1969 | |
---|
| 1970 | def run(): |
---|
[770] | 1971 | """Threading start""" |
---|
[169] | 1972 | |
---|
[770] | 1973 | config = GangliaConfigParser( GMETAD_CONF ) |
---|
| 1974 | s_timeout = int( config.getLowestInterval() - 1 ) |
---|
[469] | 1975 | |
---|
[770] | 1976 | socket.setdefaulttimeout( s_timeout ) |
---|
[469] | 1977 | |
---|
[770] | 1978 | myXMLSource = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) |
---|
| 1979 | myDataStore = DataSQLStore( JOB_SQL_DBASE.split( '/' )[0], JOB_SQL_DBASE.split( '/' )[1] ) |
---|
[8] | 1980 | |
---|
[770] | 1981 | myTorqueProcessor = TorqueXMLProcessor( myXMLSource, myDataStore ) |
---|
| 1982 | myGangliaProcessor = GangliaXMLProcessor( myXMLSource, myDataStore ) |
---|
[287] | 1983 | |
---|
[770] | 1984 | try: |
---|
| 1985 | torque_xml_thread = threading.Thread( None, myTorqueProcessor.run, 'torque_proc_thread' ) |
---|
| 1986 | ganglia_xml_thread = threading.Thread( None, myGangliaProcessor.run, 'ganglia_proc_thread' ) |
---|
[22] | 1987 | |
---|
[770] | 1988 | torque_xml_thread.start() |
---|
| 1989 | ganglia_xml_thread.start() |
---|
| 1990 | |
---|
| 1991 | except thread.error, msg: |
---|
| 1992 | debug_msg( 0, 'FATAL ERROR: Unable to start main threads!: '+ str(msg) ) |
---|
| 1993 | syslog.closelog() |
---|
| 1994 | sys.exit(1) |
---|
| 1995 | |
---|
| 1996 | debug_msg( 0, 'main threading started.' ) |
---|
[78] | 1997 | |
---|
[169] | 1998 | def main(): |
---|
[770] | 1999 | """Program startup""" |
---|
[169] | 2000 | |
---|
[770] | 2001 | global DAEMONIZE, USE_SYSLOG |
---|
[375] | 2002 | |
---|
[770] | 2003 | if not processArgs( sys.argv[1:] ): |
---|
| 2004 | sys.exit( 1 ) |
---|
[214] | 2005 | |
---|
[770] | 2006 | if( DAEMONIZE and USE_SYSLOG ): |
---|
| 2007 | syslog.openlog( 'jobarchived', syslog.LOG_NOWAIT, SYSLOG_FACILITY ) |
---|
[169] | 2008 | |
---|
[770] | 2009 | if DAEMONIZE: |
---|
| 2010 | daemon() |
---|
| 2011 | else: |
---|
| 2012 | run() |
---|
[169] | 2013 | |
---|
| 2014 | # |
---|
[81] | 2015 | # Global functions |
---|
[169] | 2016 | # |
---|
[81] | 2017 | |
---|
[9] | 2018 | def check_dir( directory ): |
---|
[770] | 2019 | """Check if directory is a proper directory. I.e.: Does _not_ end with a '/'""" |
---|
[9] | 2020 | |
---|
[770] | 2021 | if directory[-1] == '/': |
---|
| 2022 | directory = directory[:-1] |
---|
[9] | 2023 | |
---|
[770] | 2024 | return directory |
---|
[9] | 2025 | |
---|
[295] | 2026 | def reqtime2epoch( rtime ): |
---|
| 2027 | |
---|
[770] | 2028 | (hours, minutes, seconds ) = rtime.split( ':' ) |
---|
[295] | 2029 | |
---|
[770] | 2030 | etime = int(seconds) |
---|
| 2031 | etime = etime + ( int(minutes) * 60 ) |
---|
| 2032 | etime = etime + ( int(hours) * 60 * 60 ) |
---|
[295] | 2033 | |
---|
[770] | 2034 | return etime |
---|
[295] | 2035 | |
---|
[12] | 2036 | def debug_msg( level, msg ): |
---|
[770] | 2037 | """Only print msg if correct levels""" |
---|
[12] | 2038 | |
---|
[770] | 2039 | if (not DAEMONIZE and DEBUG_LEVEL >= level): |
---|
| 2040 | sys.stderr.write( printTime() + ' - ' + msg + '\n' ) |
---|
| 2041 | |
---|
| 2042 | if (DAEMONIZE and USE_SYSLOG and SYSLOG_LEVEL >= level): |
---|
| 2043 | syslog.syslog( msg ) |
---|
[12] | 2044 | |
---|
[46] | 2045 | def printTime( ): |
---|
[770] | 2046 | """Print current time in human readable format""" |
---|
[46] | 2047 | |
---|
[770] | 2048 | return time.strftime("%a %d %b %Y %H:%M:%S") |
---|
[46] | 2049 | |
---|
[435] | 2050 | def write_pidfile(): |
---|
| 2051 | |
---|
[770] | 2052 | # Write pidfile if PIDFILE exists |
---|
| 2053 | if PIDFILE: |
---|
[435] | 2054 | |
---|
[770] | 2055 | pid = os.getpid() |
---|
[435] | 2056 | |
---|
[770] | 2057 | pidfile = open(PIDFILE, 'w') |
---|
[435] | 2058 | |
---|
[770] | 2059 | pidfile.write( str( pid ) ) |
---|
| 2060 | pidfile.close() |
---|
[435] | 2061 | |
---|
[63] | 2062 | # Ooohh, someone started me! Let's go.. |
---|
[469] | 2063 | # |
---|
[9] | 2064 | if __name__ == '__main__': |
---|
[770] | 2065 | main() |
---|