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