[3] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[5] | 3 | from xml.sax import make_parser |
---|
| 4 | from xml.sax.handler import ContentHandler |
---|
| 5 | import socket |
---|
| 6 | import sys |
---|
[9] | 7 | import string |
---|
[12] | 8 | import os |
---|
| 9 | import os.path |
---|
[17] | 10 | import time |
---|
| 11 | import re |
---|
[36] | 12 | import threading |
---|
| 13 | import mutex |
---|
[38] | 14 | import random |
---|
[40] | 15 | from types import * |
---|
[3] | 16 | |
---|
[8] | 17 | # Specify debugging level here; |
---|
| 18 | # |
---|
[38] | 19 | # 11 = XML: metrics |
---|
| 20 | # 10 = XML: host, cluster, grid, ganglia |
---|
| 21 | # 9 = RRD activity, gmetad config parsing |
---|
[40] | 22 | # 8 = RRD file activity |
---|
| 23 | # 7 = daemon threading |
---|
[8] | 24 | # |
---|
[50] | 25 | DEBUG_LEVEL = 7 |
---|
[6] | 26 | |
---|
[9] | 27 | # Where is the gmetad.conf located |
---|
| 28 | # |
---|
| 29 | GMETAD_CONF = '/etc/gmetad.conf' |
---|
| 30 | |
---|
| 31 | # Where to store the archived rrd's |
---|
| 32 | # |
---|
| 33 | ARCHIVE_PATH = '/data/toga/rrds' |
---|
| 34 | |
---|
| 35 | # List of data_source names to archive for |
---|
| 36 | # |
---|
| 37 | ARCHIVE_SOURCES = [ "LISA Cluster" ] |
---|
| 38 | |
---|
[13] | 39 | # Amount of hours to store in one single archived .rrd |
---|
[9] | 40 | # |
---|
[43] | 41 | ARCHIVE_HOURS_PER_RRD = 12 |
---|
[13] | 42 | |
---|
[22] | 43 | # Wether or not to run as a daemon in background |
---|
| 44 | # |
---|
| 45 | DAEMONIZE = 0 |
---|
| 46 | |
---|
[17] | 47 | ###################### |
---|
| 48 | # # |
---|
| 49 | # Configuration ends # |
---|
| 50 | # # |
---|
| 51 | ###################### |
---|
[13] | 52 | |
---|
[17] | 53 | # What XML data types not to store |
---|
[13] | 54 | # |
---|
[17] | 55 | UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ] |
---|
[9] | 56 | |
---|
[47] | 57 | # Maximum time (in seconds) a parsethread may run |
---|
| 58 | # |
---|
| 59 | PARSE_TIMEOUT = 60 |
---|
| 60 | |
---|
| 61 | # Maximum time (in seconds) a storethread may run |
---|
| 62 | # |
---|
| 63 | STORE_TIMEOUT = 360 |
---|
| 64 | |
---|
[8] | 65 | """ |
---|
| 66 | This is TOrque-GAnglia's data Daemon |
---|
| 67 | """ |
---|
| 68 | |
---|
[37] | 69 | class RRDMutator: |
---|
[38] | 70 | "A class for handling .rrd mutations" |
---|
[37] | 71 | |
---|
| 72 | binary = '/usr/bin/rrdtool' |
---|
| 73 | |
---|
| 74 | def __init__( self, binary=None ): |
---|
| 75 | |
---|
| 76 | if binary: |
---|
| 77 | self.binary = binary |
---|
| 78 | |
---|
| 79 | def create( self, filename, args ): |
---|
[40] | 80 | return self.perform( 'create', '"' + filename + '"', args ) |
---|
[37] | 81 | |
---|
| 82 | def update( self, filename, args ): |
---|
[40] | 83 | return self.perform( 'update', '"' + filename + '"', args ) |
---|
[37] | 84 | |
---|
[42] | 85 | def grabLastUpdate( self, filename ): |
---|
| 86 | |
---|
| 87 | last_update = 0 |
---|
| 88 | |
---|
[53] | 89 | debug_msg( 8, self.binary + ' info "' + filename + '"' ) |
---|
| 90 | |
---|
[42] | 91 | for line in os.popen( self.binary + ' info "' + filename + '"' ).readlines(): |
---|
| 92 | |
---|
| 93 | if line.find( 'last_update') != -1: |
---|
| 94 | |
---|
| 95 | last_update = line.split( ' = ' )[1] |
---|
| 96 | |
---|
| 97 | if last_update: |
---|
| 98 | return last_update |
---|
| 99 | else: |
---|
| 100 | return 0 |
---|
| 101 | |
---|
[40] | 102 | def perform( self, action, filename, args ): |
---|
[37] | 103 | |
---|
| 104 | arg_string = None |
---|
| 105 | |
---|
[40] | 106 | if type( args ) is not ListType: |
---|
| 107 | debug_msg( 8, 'Arguments needs to be of type List' ) |
---|
| 108 | return 1 |
---|
| 109 | |
---|
[37] | 110 | for arg in args: |
---|
| 111 | |
---|
| 112 | if not arg_string: |
---|
| 113 | |
---|
| 114 | arg_string = arg |
---|
| 115 | else: |
---|
| 116 | arg_string = arg_string + ' ' + arg |
---|
| 117 | |
---|
[40] | 118 | debug_msg( 8, 'rrdm.perform(): ' + self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ) |
---|
[37] | 119 | |
---|
[40] | 120 | for line in os.popen( self.binary + ' ' + action + ' ' + filename + ' ' + arg_string ).readlines(): |
---|
[37] | 121 | |
---|
| 122 | if line.find( 'ERROR' ) != -1: |
---|
| 123 | |
---|
| 124 | error_msg = string.join( line.split( ' ' )[1:] ) |
---|
[40] | 125 | debug_msg( 8, error_msg ) |
---|
[37] | 126 | return 1 |
---|
| 127 | |
---|
| 128 | return 0 |
---|
| 129 | |
---|
[6] | 130 | class GangliaXMLHandler( ContentHandler ): |
---|
[8] | 131 | "Parse Ganglia's XML" |
---|
[3] | 132 | |
---|
[33] | 133 | def __init__( self, config ): |
---|
| 134 | self.config = config |
---|
[35] | 135 | self.clusters = { } |
---|
[48] | 136 | debug_msg( 0, printTime() + ' Checking existing toga rrd archive..' ) |
---|
[44] | 137 | self.gatherClusters() |
---|
[48] | 138 | debug_msg( 0, printTime() + ' Check done.' ) |
---|
[33] | 139 | |
---|
[44] | 140 | def gatherClusters( self ): |
---|
| 141 | |
---|
[45] | 142 | archive_dir = check_dir(ARCHIVE_PATH) |
---|
[44] | 143 | |
---|
| 144 | hosts = [ ] |
---|
| 145 | |
---|
| 146 | if os.path.exists( archive_dir ): |
---|
| 147 | |
---|
| 148 | dirlist = os.listdir( archive_dir ) |
---|
| 149 | |
---|
| 150 | for item in dirlist: |
---|
| 151 | |
---|
| 152 | clustername = item |
---|
| 153 | |
---|
| 154 | if not self.clusters.has_key( clustername ) and clustername in ARCHIVE_SOURCES: |
---|
| 155 | |
---|
| 156 | self.clusters[ clustername ] = RRDHandler( self.config, clustername ) |
---|
| 157 | |
---|
[6] | 158 | def startElement( self, name, attrs ): |
---|
[8] | 159 | "Store appropriate data from xml start tags" |
---|
[3] | 160 | |
---|
[7] | 161 | if name == 'GANGLIA_XML': |
---|
[32] | 162 | |
---|
| 163 | self.XMLSource = attrs.get( 'SOURCE', "" ) |
---|
| 164 | self.gangliaVersion = attrs.get( 'VERSION', "" ) |
---|
| 165 | |
---|
[12] | 166 | debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) ) |
---|
[6] | 167 | |
---|
[7] | 168 | elif name == 'GRID': |
---|
[32] | 169 | |
---|
| 170 | self.gridName = attrs.get( 'NAME', "" ) |
---|
| 171 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
| 172 | |
---|
[12] | 173 | debug_msg( 10, '`-Grid found: %s' %( self.gridName ) ) |
---|
[6] | 174 | |
---|
[7] | 175 | elif name == 'CLUSTER': |
---|
[32] | 176 | |
---|
| 177 | self.clusterName = attrs.get( 'NAME', "" ) |
---|
| 178 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
| 179 | |
---|
[35] | 180 | if not self.clusters.has_key( self.clusterName ) and self.clusterName in ARCHIVE_SOURCES: |
---|
[32] | 181 | |
---|
[34] | 182 | self.clusters[ self.clusterName ] = RRDHandler( self.config, self.clusterName ) |
---|
[33] | 183 | |
---|
[35] | 184 | debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) ) |
---|
[6] | 185 | |
---|
[14] | 186 | elif name == 'HOST' and self.clusterName in ARCHIVE_SOURCES: |
---|
[32] | 187 | |
---|
| 188 | self.hostName = attrs.get( 'NAME', "" ) |
---|
| 189 | self.hostIp = attrs.get( 'IP', "" ) |
---|
| 190 | self.hostReported = attrs.get( 'REPORTED', "" ) |
---|
| 191 | |
---|
[12] | 192 | debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) ) |
---|
[6] | 193 | |
---|
[14] | 194 | elif name == 'METRIC' and self.clusterName in ARCHIVE_SOURCES: |
---|
[6] | 195 | |
---|
[32] | 196 | type = attrs.get( 'TYPE', "" ) |
---|
[6] | 197 | |
---|
[32] | 198 | if type not in UNSUPPORTED_ARCHIVE_TYPES: |
---|
[3] | 199 | |
---|
[32] | 200 | myMetric = { } |
---|
| 201 | myMetric['name'] = attrs.get( 'NAME', "" ) |
---|
| 202 | myMetric['val'] = attrs.get( 'VAL', "" ) |
---|
| 203 | myMetric['time'] = self.hostReported |
---|
[3] | 204 | |
---|
[34] | 205 | self.clusters[ self.clusterName ].memMetric( self.hostName, myMetric ) |
---|
[3] | 206 | |
---|
[34] | 207 | debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) ) |
---|
[6] | 208 | |
---|
[34] | 209 | def storeMetrics( self ): |
---|
[9] | 210 | |
---|
[34] | 211 | for clustername, rrdh in self.clusters.items(): |
---|
[16] | 212 | |
---|
[38] | 213 | ret = rrdh.storeMetrics() |
---|
[9] | 214 | |
---|
[38] | 215 | if ret: |
---|
| 216 | debug_msg( 9, 'An error occured while storing metrics for cluster %s' %clustername ) |
---|
| 217 | return 1 |
---|
| 218 | |
---|
| 219 | return 0 |
---|
| 220 | |
---|
[5] | 221 | class GangliaXMLGatherer: |
---|
[8] | 222 | "Setup a connection and file object to Ganglia's XML" |
---|
[3] | 223 | |
---|
[8] | 224 | s = None |
---|
| 225 | |
---|
| 226 | def __init__( self, host, port ): |
---|
| 227 | "Store host and port for connection" |
---|
| 228 | |
---|
[5] | 229 | self.host = host |
---|
| 230 | self.port = port |
---|
[33] | 231 | self.connect() |
---|
[3] | 232 | |
---|
[33] | 233 | def connect( self ): |
---|
| 234 | "Setup connection to XML source" |
---|
[8] | 235 | |
---|
| 236 | for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): |
---|
[32] | 237 | |
---|
[5] | 238 | af, socktype, proto, canonname, sa = res |
---|
[32] | 239 | |
---|
[5] | 240 | try: |
---|
[32] | 241 | |
---|
[8] | 242 | self.s = socket.socket( af, socktype, proto ) |
---|
[32] | 243 | |
---|
[5] | 244 | except socket.error, msg: |
---|
[32] | 245 | |
---|
[8] | 246 | self.s = None |
---|
[5] | 247 | continue |
---|
[32] | 248 | |
---|
[5] | 249 | try: |
---|
[32] | 250 | |
---|
[8] | 251 | self.s.connect( sa ) |
---|
[32] | 252 | |
---|
[5] | 253 | except socket.error, msg: |
---|
[32] | 254 | |
---|
[8] | 255 | self.s.close() |
---|
| 256 | self.s = None |
---|
[5] | 257 | continue |
---|
[32] | 258 | |
---|
[5] | 259 | break |
---|
[3] | 260 | |
---|
[8] | 261 | if self.s is None: |
---|
[32] | 262 | |
---|
[33] | 263 | debug_msg( 0, 'Could not open socket' ) |
---|
| 264 | sys.exit( 1 ) |
---|
[5] | 265 | |
---|
[33] | 266 | def disconnect( self ): |
---|
| 267 | "Close socket" |
---|
| 268 | |
---|
| 269 | if self.s: |
---|
| 270 | self.s.close() |
---|
| 271 | self.s = None |
---|
| 272 | |
---|
| 273 | def __del__( self ): |
---|
| 274 | "Kill the socket before we leave" |
---|
| 275 | |
---|
| 276 | self.disconnect() |
---|
| 277 | |
---|
| 278 | def getFileObject( self ): |
---|
| 279 | "Connect, and return a file object" |
---|
| 280 | |
---|
[38] | 281 | if self.s: |
---|
| 282 | # Apearantly, only data is received when a connection is made |
---|
| 283 | # therefor, disconnect and connect |
---|
| 284 | # |
---|
| 285 | self.disconnect() |
---|
| 286 | self.connect() |
---|
[33] | 287 | |
---|
[8] | 288 | return self.s.makefile( 'r' ) |
---|
[5] | 289 | |
---|
[8] | 290 | class GangliaXMLProcessor: |
---|
[5] | 291 | |
---|
[33] | 292 | def __init__( self ): |
---|
| 293 | "Setup initial XML connection and handlers" |
---|
| 294 | |
---|
| 295 | self.config = GangliaConfigParser( GMETAD_CONF ) |
---|
| 296 | |
---|
| 297 | self.myXMLGatherer = GangliaXMLGatherer( 'localhost', 8651 ) |
---|
| 298 | self.myParser = make_parser() |
---|
| 299 | self.myHandler = GangliaXMLHandler( self.config ) |
---|
| 300 | self.myParser.setContentHandler( self.myHandler ) |
---|
| 301 | |
---|
[9] | 302 | def daemon( self ): |
---|
[8] | 303 | "Run as daemon forever" |
---|
[5] | 304 | |
---|
[8] | 305 | self.DAEMON = 1 |
---|
[5] | 306 | |
---|
[8] | 307 | # Fork the first child |
---|
| 308 | # |
---|
| 309 | pid = os.fork() |
---|
[32] | 310 | |
---|
[8] | 311 | if pid > 0: |
---|
[7] | 312 | |
---|
[32] | 313 | sys.exit(0) # end parent |
---|
| 314 | |
---|
[8] | 315 | # creates a session and sets the process group ID |
---|
| 316 | # |
---|
| 317 | os.setsid() |
---|
[7] | 318 | |
---|
[8] | 319 | # Fork the second child |
---|
| 320 | # |
---|
| 321 | pid = os.fork() |
---|
[32] | 322 | |
---|
[8] | 323 | if pid > 0: |
---|
[5] | 324 | |
---|
[32] | 325 | sys.exit(0) # end parent |
---|
| 326 | |
---|
[8] | 327 | # Go to the root directory and set the umask |
---|
| 328 | # |
---|
| 329 | os.chdir('/') |
---|
| 330 | os.umask(0) |
---|
| 331 | |
---|
| 332 | sys.stdin.close() |
---|
| 333 | sys.stdout.close() |
---|
[36] | 334 | #sys.stderr.close() |
---|
[8] | 335 | |
---|
| 336 | os.open('/dev/null', 0) |
---|
| 337 | os.dup(0) |
---|
| 338 | os.dup(0) |
---|
| 339 | |
---|
| 340 | self.run() |
---|
| 341 | |
---|
[22] | 342 | def printTime( self ): |
---|
[33] | 343 | "Print current time in human readable format" |
---|
[22] | 344 | |
---|
[33] | 345 | return time.strftime("%a %d %b %Y %H:%M:%S") |
---|
[22] | 346 | |
---|
[9] | 347 | def run( self ): |
---|
[8] | 348 | "Main thread" |
---|
| 349 | |
---|
[36] | 350 | xmlthread = threading.Thread( None, self.processXML, 'xmlthread' ) |
---|
| 351 | storethread = threading.Thread( None, self.storeMetrics, 'storethread' ) |
---|
[22] | 352 | |
---|
[36] | 353 | while( 1 ): |
---|
| 354 | |
---|
| 355 | if not xmlthread.isAlive(): |
---|
| 356 | # Gather XML at the same interval as gmetad |
---|
| 357 | |
---|
| 358 | # threaded call to: self.processXML() |
---|
| 359 | # |
---|
| 360 | xmlthread = threading.Thread( None, self.processXML, 'xmlthread' ) |
---|
| 361 | xmlthread.start() |
---|
| 362 | |
---|
| 363 | if not storethread.isAlive(): |
---|
| 364 | # Store metrics every .. sec |
---|
| 365 | |
---|
| 366 | # threaded call to: self.storeMetrics() |
---|
| 367 | # |
---|
| 368 | storethread = threading.Thread( None, self.storeMetrics, 'storethread' ) |
---|
| 369 | storethread.start() |
---|
| 370 | |
---|
| 371 | # Just sleep a sec here, to prevent daemon from going mad. We're all threads here anyway |
---|
| 372 | time.sleep( 1 ) |
---|
| 373 | |
---|
[33] | 374 | def storeMetrics( self ): |
---|
| 375 | "Store metrics retained in memory to disk" |
---|
[22] | 376 | |
---|
[40] | 377 | debug_msg( 7, self.printTime() + ' - storethread(): started.' ) |
---|
[39] | 378 | |
---|
[38] | 379 | # Store metrics somewhere between every 60 and 180 seconds |
---|
| 380 | # |
---|
[53] | 381 | STORE_INTERVAL = random.randint( 360, 640 ) |
---|
[22] | 382 | |
---|
[39] | 383 | storethread = threading.Thread( None, self.storeThread, 'storemetricthread' ) |
---|
[36] | 384 | storethread.start() |
---|
| 385 | |
---|
[40] | 386 | debug_msg( 7, self.printTime() + ' - storethread(): Sleeping.. (%ss)' %STORE_INTERVAL ) |
---|
[36] | 387 | time.sleep( STORE_INTERVAL ) |
---|
[40] | 388 | debug_msg( 7, self.printTime() + ' - storethread(): Done sleeping.' ) |
---|
[36] | 389 | |
---|
| 390 | if storethread.isAlive(): |
---|
| 391 | |
---|
[40] | 392 | debug_msg( 7, self.printTime() + ' - storethread(): storemetricthread() still running, waiting to finish..' ) |
---|
[47] | 393 | storethread.join( STORE_TIMEOUT ) # Maximum time is for storing thread to finish |
---|
[40] | 394 | debug_msg( 7, self.printTime() + ' - storethread(): Done waiting.' ) |
---|
[36] | 395 | |
---|
[40] | 396 | debug_msg( 7, self.printTime() + ' - storethread(): finished.' ) |
---|
[36] | 397 | |
---|
| 398 | return 0 |
---|
| 399 | |
---|
[39] | 400 | def storeThread( self ): |
---|
| 401 | |
---|
[40] | 402 | debug_msg( 7, self.printTime() + ' - storemetricthread(): started.' ) |
---|
| 403 | debug_msg( 7, self.printTime() + ' - storemetricthread(): Storing data..' ) |
---|
[39] | 404 | ret = self.myHandler.storeMetrics() |
---|
[40] | 405 | debug_msg( 7, self.printTime() + ' - storemetricthread(): Done storing.' ) |
---|
| 406 | debug_msg( 7, self.printTime() + ' - storemetricthread(): finished.' ) |
---|
[39] | 407 | |
---|
| 408 | return ret |
---|
| 409 | |
---|
[8] | 410 | def processXML( self ): |
---|
| 411 | "Process XML" |
---|
| 412 | |
---|
[40] | 413 | debug_msg( 7, self.printTime() + ' - xmlthread(): started.' ) |
---|
[8] | 414 | |
---|
[39] | 415 | parsethread = threading.Thread( None, self.parseThread, 'parsethread' ) |
---|
[36] | 416 | parsethread.start() |
---|
| 417 | |
---|
[40] | 418 | debug_msg( 7, self.printTime() + ' - xmlthread(): Sleeping.. (%ss)' %self.config.getLowestInterval() ) |
---|
[36] | 419 | time.sleep( float( self.config.getLowestInterval() ) ) |
---|
[40] | 420 | debug_msg( 7, self.printTime() + ' - xmlthread(): Done sleeping.' ) |
---|
[36] | 421 | |
---|
| 422 | if parsethread.isAlive(): |
---|
| 423 | |
---|
[40] | 424 | debug_msg( 7, self.printTime() + ' - xmlthread(): parsethread() still running, waiting to finish..' ) |
---|
[47] | 425 | parsethread.join( PARSE_TIMEOUT ) # Maximum time for XML thread to finish |
---|
[40] | 426 | debug_msg( 7, self.printTime() + ' - xmlthread(): Done waiting.' ) |
---|
[36] | 427 | |
---|
[40] | 428 | debug_msg( 7, self.printTime() + ' - xmlthread(): finished.' ) |
---|
[36] | 429 | |
---|
| 430 | return 0 |
---|
| 431 | |
---|
[39] | 432 | def parseThread( self ): |
---|
| 433 | |
---|
[40] | 434 | debug_msg( 7, self.printTime() + ' - parsethread(): started.' ) |
---|
| 435 | debug_msg( 7, self.printTime() + ' - parsethread(): Parsing XML..' ) |
---|
[39] | 436 | ret = self.myParser.parse( self.myXMLGatherer.getFileObject() ) |
---|
[40] | 437 | debug_msg( 7, self.printTime() + ' - parsethread(): Done parsing.' ) |
---|
| 438 | debug_msg( 7, self.printTime() + ' - parsethread(): finished.' ) |
---|
[39] | 439 | |
---|
| 440 | return ret |
---|
| 441 | |
---|
[9] | 442 | class GangliaConfigParser: |
---|
| 443 | |
---|
[34] | 444 | sources = [ ] |
---|
[9] | 445 | |
---|
| 446 | def __init__( self, config ): |
---|
[32] | 447 | |
---|
[9] | 448 | self.config = config |
---|
| 449 | self.parseValues() |
---|
| 450 | |
---|
[32] | 451 | def parseValues( self ): |
---|
[9] | 452 | "Parse certain values from gmetad.conf" |
---|
| 453 | |
---|
| 454 | readcfg = open( self.config, 'r' ) |
---|
| 455 | |
---|
| 456 | for line in readcfg.readlines(): |
---|
| 457 | |
---|
| 458 | if line.count( '"' ) > 1: |
---|
| 459 | |
---|
[10] | 460 | if line.find( 'data_source' ) != -1 and line[0] != '#': |
---|
[9] | 461 | |
---|
[11] | 462 | source = { } |
---|
| 463 | source['name'] = line.split( '"' )[1] |
---|
[9] | 464 | source_words = line.split( '"' )[2].split( ' ' ) |
---|
| 465 | |
---|
| 466 | for word in source_words: |
---|
| 467 | |
---|
| 468 | valid_interval = 1 |
---|
| 469 | |
---|
| 470 | for letter in word: |
---|
[32] | 471 | |
---|
[9] | 472 | if letter not in string.digits: |
---|
[32] | 473 | |
---|
[9] | 474 | valid_interval = 0 |
---|
| 475 | |
---|
[10] | 476 | if valid_interval and len(word) > 0: |
---|
[32] | 477 | |
---|
[9] | 478 | source['interval'] = word |
---|
[12] | 479 | debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) ) |
---|
[33] | 480 | |
---|
| 481 | # No interval found, use Ganglia's default |
---|
| 482 | if not source.has_key( 'interval' ): |
---|
| 483 | source['interval'] = 15 |
---|
| 484 | debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) ) |
---|
[32] | 485 | |
---|
[33] | 486 | self.sources.append( source ) |
---|
[9] | 487 | |
---|
| 488 | def getInterval( self, source_name ): |
---|
[32] | 489 | |
---|
[9] | 490 | for source in self.sources: |
---|
[32] | 491 | |
---|
[12] | 492 | if source['name'] == source_name: |
---|
[32] | 493 | |
---|
[9] | 494 | return source['interval'] |
---|
[32] | 495 | |
---|
[9] | 496 | return None |
---|
| 497 | |
---|
[34] | 498 | def getLowestInterval( self ): |
---|
| 499 | |
---|
| 500 | lowest_interval = 0 |
---|
| 501 | |
---|
| 502 | for source in self.sources: |
---|
| 503 | |
---|
| 504 | if not lowest_interval or source['interval'] <= lowest_interval: |
---|
| 505 | |
---|
| 506 | lowest_interval = source['interval'] |
---|
| 507 | |
---|
| 508 | # Return 15 when nothing is found, so that the daemon won't go insane with 0 sec delays |
---|
| 509 | if lowest_interval: |
---|
| 510 | return lowest_interval |
---|
| 511 | else: |
---|
| 512 | return 15 |
---|
| 513 | |
---|
[9] | 514 | class RRDHandler: |
---|
| 515 | |
---|
[32] | 516 | myMetrics = { } |
---|
[40] | 517 | lastStored = { } |
---|
[47] | 518 | timeserials = { } |
---|
[36] | 519 | slot = None |
---|
[32] | 520 | |
---|
[33] | 521 | def __init__( self, config, cluster ): |
---|
[44] | 522 | self.block = 0 |
---|
[9] | 523 | self.cluster = cluster |
---|
[33] | 524 | self.config = config |
---|
[40] | 525 | self.slot = threading.Lock() |
---|
[37] | 526 | self.rrdm = RRDMutator() |
---|
[42] | 527 | self.gatherLastUpdates() |
---|
[9] | 528 | |
---|
[42] | 529 | def gatherLastUpdates( self ): |
---|
| 530 | "Populate the lastStored list, containing timestamps of all last updates" |
---|
| 531 | |
---|
| 532 | cluster_dir = '%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster ) |
---|
| 533 | |
---|
| 534 | hosts = [ ] |
---|
| 535 | |
---|
| 536 | if os.path.exists( cluster_dir ): |
---|
| 537 | |
---|
[44] | 538 | dirlist = os.listdir( cluster_dir ) |
---|
[42] | 539 | |
---|
[44] | 540 | for dir in dirlist: |
---|
[42] | 541 | |
---|
[44] | 542 | hosts.append( dir ) |
---|
[42] | 543 | |
---|
| 544 | for host in hosts: |
---|
| 545 | |
---|
[47] | 546 | host_dir = cluster_dir + '/' + host |
---|
| 547 | dirlist = os.listdir( host_dir ) |
---|
| 548 | |
---|
| 549 | for dir in dirlist: |
---|
| 550 | |
---|
| 551 | if not self.timeserials.has_key( host ): |
---|
| 552 | |
---|
| 553 | self.timeserials[ host ] = [ ] |
---|
| 554 | |
---|
| 555 | self.timeserials[ host ].append( dir ) |
---|
| 556 | |
---|
[42] | 557 | last_serial = self.getLastRrdTimeSerial( host ) |
---|
| 558 | if last_serial: |
---|
| 559 | |
---|
| 560 | metric_dir = cluster_dir + '/' + host + '/' + last_serial |
---|
| 561 | if os.path.exists( metric_dir ): |
---|
| 562 | |
---|
[44] | 563 | dirlist = os.listdir( metric_dir ) |
---|
[42] | 564 | |
---|
[44] | 565 | for file in dirlist: |
---|
[42] | 566 | |
---|
[44] | 567 | metricname = file.split( '.rrd' )[0] |
---|
[42] | 568 | |
---|
[44] | 569 | if not self.lastStored.has_key( host ): |
---|
[42] | 570 | |
---|
[44] | 571 | self.lastStored[ host ] = { } |
---|
[42] | 572 | |
---|
[44] | 573 | self.lastStored[ host ][ metricname ] = self.rrdm.grabLastUpdate( metric_dir + '/' + file ) |
---|
[42] | 574 | |
---|
[32] | 575 | def getClusterName( self ): |
---|
| 576 | return self.cluster |
---|
| 577 | |
---|
| 578 | def memMetric( self, host, metric ): |
---|
| 579 | |
---|
[34] | 580 | if self.myMetrics.has_key( host ): |
---|
[32] | 581 | |
---|
[34] | 582 | if self.myMetrics[ host ].has_key( metric['name'] ): |
---|
[32] | 583 | |
---|
[34] | 584 | for mymetric in self.myMetrics[ host ][ metric['name'] ]: |
---|
[32] | 585 | |
---|
[34] | 586 | if mymetric['time'] == metric['time']: |
---|
[32] | 587 | |
---|
[34] | 588 | # Allready have this metric, abort |
---|
| 589 | return 1 |
---|
| 590 | else: |
---|
| 591 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
| 592 | else: |
---|
[32] | 593 | self.myMetrics[ host ] = { } |
---|
[34] | 594 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
[32] | 595 | |
---|
[53] | 596 | # <ATOMIC> |
---|
[40] | 597 | # |
---|
| 598 | self.slot.acquire() |
---|
| 599 | |
---|
[32] | 600 | self.myMetrics[ host ][ metric['name'] ].append( metric ) |
---|
| 601 | |
---|
[40] | 602 | self.slot.release() |
---|
[53] | 603 | # |
---|
| 604 | # </ATOMIC> |
---|
[40] | 605 | |
---|
[47] | 606 | def makeUpdateList( self, host, metriclist ): |
---|
[37] | 607 | |
---|
| 608 | update_list = [ ] |
---|
[41] | 609 | metric = None |
---|
[37] | 610 | |
---|
[47] | 611 | while len( metriclist ) > 0: |
---|
[37] | 612 | |
---|
[53] | 613 | metric = metriclist.pop( 0 ) |
---|
[37] | 614 | |
---|
[53] | 615 | if self.checkStoreMetric( host, metric ): |
---|
| 616 | update_list.append( '%s:%s' %( metric['time'], metric['val'] ) ) |
---|
[40] | 617 | |
---|
[37] | 618 | return update_list |
---|
| 619 | |
---|
[49] | 620 | def checkStoreMetric( self, host, metric ): |
---|
[40] | 621 | |
---|
| 622 | if self.lastStored.has_key( host ): |
---|
| 623 | |
---|
[47] | 624 | if self.lastStored[ host ].has_key( metric['name'] ): |
---|
[40] | 625 | |
---|
[47] | 626 | if metric['time'] <= self.lastStored[ host ][ metric['name'] ]: |
---|
[40] | 627 | |
---|
[50] | 628 | # This is old |
---|
[40] | 629 | return 0 |
---|
| 630 | |
---|
[50] | 631 | return 1 |
---|
| 632 | |
---|
| 633 | def memLastUpdate( self, host, metriclist ): |
---|
| 634 | |
---|
| 635 | last_update_time = 0 |
---|
| 636 | |
---|
| 637 | for metric in metriclist: |
---|
| 638 | |
---|
| 639 | if metric['time'] > last_update_time: |
---|
| 640 | |
---|
| 641 | last_update_time = metric['time'] |
---|
| 642 | |
---|
| 643 | if not self.lastStored.has_key( host ): |
---|
[40] | 644 | self.lastStored[ host ] = { } |
---|
| 645 | |
---|
[52] | 646 | if self.lastStored[ host ].has_key( metric['name'] ): |
---|
| 647 | |
---|
| 648 | if last_update_time <= self.lastStored[ host ][ metric['name'] ]: |
---|
| 649 | return 1 |
---|
[40] | 650 | |
---|
[52] | 651 | self.lastStored[ host ][ metric['name'] ] = last_update_time |
---|
| 652 | |
---|
[33] | 653 | def storeMetrics( self ): |
---|
| 654 | |
---|
| 655 | for hostname, mymetrics in self.myMetrics.items(): |
---|
| 656 | |
---|
| 657 | for metricname, mymetric in mymetrics.items(): |
---|
| 658 | |
---|
[53] | 659 | metrics_to_store = [ ] |
---|
| 660 | |
---|
| 661 | # <ATOMIC> |
---|
[50] | 662 | # |
---|
[47] | 663 | self.slot.acquire() |
---|
[33] | 664 | |
---|
[53] | 665 | while len( mymetric ) > 0: |
---|
| 666 | |
---|
| 667 | metrics_to_store.append( mymetric.pop( 0 ) ) |
---|
| 668 | |
---|
| 669 | self.slot.release() |
---|
| 670 | # |
---|
| 671 | # </ATOMIC> |
---|
| 672 | |
---|
[47] | 673 | # Create a mapping table, each metric to the period where it should be stored |
---|
| 674 | # |
---|
[53] | 675 | metric_serial_table = self.determineSerials( hostname, metricname, metrics_to_store ) |
---|
[47] | 676 | self.myMetrics[ hostname ][ metricname ] = [ ] |
---|
[33] | 677 | |
---|
[50] | 678 | update_rets = [ ] |
---|
| 679 | |
---|
[47] | 680 | for period, pmetric in metric_serial_table.items(): |
---|
| 681 | |
---|
| 682 | self.createCheck( hostname, metricname, period ) |
---|
| 683 | |
---|
| 684 | update_ret = self.update( hostname, metricname, period, pmetric ) |
---|
| 685 | |
---|
| 686 | if update_ret == 0: |
---|
| 687 | |
---|
| 688 | debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) ) |
---|
| 689 | else: |
---|
| 690 | debug_msg( 9, 'metric update failed' ) |
---|
| 691 | |
---|
[50] | 692 | update_rets.append( update_ret ) |
---|
[47] | 693 | |
---|
[50] | 694 | if not (1) in update_rets: |
---|
| 695 | |
---|
| 696 | self.memLastUpdate( hostname, mymetric ) |
---|
| 697 | |
---|
[17] | 698 | def makeTimeSerial( self ): |
---|
[32] | 699 | "Generate a time serial. Seconds since epoch" |
---|
[17] | 700 | |
---|
| 701 | # Seconds since epoch |
---|
| 702 | mytime = int( time.time() ) |
---|
| 703 | |
---|
| 704 | return mytime |
---|
| 705 | |
---|
[50] | 706 | def makeRrdPath( self, host, metricname, timeserial ): |
---|
[32] | 707 | """ |
---|
| 708 | Make a RRD location/path and filename |
---|
| 709 | If a metric or timeserial are supplied the complete locations |
---|
| 710 | will be made, else just the host directory |
---|
| 711 | """ |
---|
[17] | 712 | |
---|
[50] | 713 | rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial ) |
---|
| 714 | rrd_file = '%s/%s.rrd' %( rrd_dir, metricname ) |
---|
[17] | 715 | |
---|
| 716 | return rrd_dir, rrd_file |
---|
| 717 | |
---|
[20] | 718 | def getLastRrdTimeSerial( self, host ): |
---|
[32] | 719 | """ |
---|
| 720 | Find the last timeserial (directory) for this host |
---|
| 721 | This is determined once every host |
---|
| 722 | """ |
---|
[17] | 723 | |
---|
[19] | 724 | newest_timeserial = 0 |
---|
| 725 | |
---|
[47] | 726 | for dir in self.timeserials[ host ]: |
---|
[32] | 727 | |
---|
[47] | 728 | valid_dir = 1 |
---|
[17] | 729 | |
---|
[47] | 730 | for letter in dir: |
---|
| 731 | if letter not in string.digits: |
---|
| 732 | valid_dir = 0 |
---|
[17] | 733 | |
---|
[47] | 734 | if valid_dir: |
---|
| 735 | timeserial = dir |
---|
| 736 | if timeserial > newest_timeserial: |
---|
| 737 | newest_timeserial = timeserial |
---|
[17] | 738 | |
---|
| 739 | if newest_timeserial: |
---|
[18] | 740 | return newest_timeserial |
---|
[17] | 741 | else: |
---|
| 742 | return 0 |
---|
| 743 | |
---|
[47] | 744 | def determinePeriod( self, host, check_serial ): |
---|
| 745 | |
---|
| 746 | period_serial = 0 |
---|
| 747 | |
---|
| 748 | for serial in self.timeserials[ host ]: |
---|
| 749 | |
---|
| 750 | if check_serial >= serial and period_serial < serial: |
---|
| 751 | |
---|
| 752 | period_serial = serial |
---|
| 753 | |
---|
| 754 | return period_serial |
---|
| 755 | |
---|
| 756 | def determineSerials( self, host, metricname, metriclist ): |
---|
| 757 | """ |
---|
| 758 | Determine the correct serial and corresponding rrd to store |
---|
| 759 | for a list of metrics |
---|
| 760 | """ |
---|
| 761 | |
---|
| 762 | metric_serial_table = { } |
---|
| 763 | |
---|
| 764 | for metric in metriclist: |
---|
| 765 | |
---|
| 766 | if metric['name'] == metricname: |
---|
| 767 | |
---|
| 768 | period = self.determinePeriod( host, metric['time'] ) |
---|
| 769 | |
---|
| 770 | archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60) |
---|
| 771 | |
---|
[49] | 772 | if (int( metric['time'] ) - int( period ) ) > archive_secs: |
---|
[47] | 773 | |
---|
| 774 | # This one should get it's own new period |
---|
| 775 | period = metric['time'] |
---|
[50] | 776 | self.timeserials[ host ].append( period ) |
---|
[47] | 777 | |
---|
| 778 | if not metric_serial_table.has_key( period ): |
---|
| 779 | |
---|
[49] | 780 | metric_serial_table[ period ] = [ ] |
---|
[47] | 781 | |
---|
| 782 | metric_serial_table[ period ].append( metric ) |
---|
| 783 | |
---|
| 784 | return metric_serial_table |
---|
| 785 | |
---|
[33] | 786 | def createCheck( self, host, metricname, timeserial ): |
---|
[9] | 787 | "Check if an .rrd allready exists for this metric, create if not" |
---|
| 788 | |
---|
[35] | 789 | debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
[47] | 790 | |
---|
[33] | 791 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[17] | 792 | |
---|
[9] | 793 | if not os.path.exists( rrd_dir ): |
---|
| 794 | os.makedirs( rrd_dir ) |
---|
[14] | 795 | debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) ) |
---|
[9] | 796 | |
---|
[14] | 797 | if not os.path.exists( rrd_file ): |
---|
[9] | 798 | |
---|
[33] | 799 | interval = self.config.getInterval( self.cluster ) |
---|
[47] | 800 | heartbeat = 8 * int( interval ) |
---|
[9] | 801 | |
---|
[37] | 802 | params = [ ] |
---|
[12] | 803 | |
---|
[37] | 804 | params.append( '--step' ) |
---|
| 805 | params.append( str( interval ) ) |
---|
[12] | 806 | |
---|
[37] | 807 | params.append( '--start' ) |
---|
[47] | 808 | params.append( str( int( timeserial ) - 1 ) ) |
---|
[12] | 809 | |
---|
[37] | 810 | params.append( 'DS:sum:GAUGE:%d:U:U' %heartbeat ) |
---|
| 811 | params.append( 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) ) |
---|
[13] | 812 | |
---|
[37] | 813 | self.rrdm.create( str(rrd_file), params ) |
---|
| 814 | |
---|
[14] | 815 | debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) ) |
---|
| 816 | |
---|
[47] | 817 | def update( self, host, metricname, timeserial, metriclist ): |
---|
[9] | 818 | |
---|
[35] | 819 | debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) ) |
---|
[9] | 820 | |
---|
[33] | 821 | rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial ) |
---|
[18] | 822 | |
---|
[47] | 823 | update_list = self.makeUpdateList( host, metriclist ) |
---|
[15] | 824 | |
---|
[41] | 825 | if len( update_list ) > 0: |
---|
| 826 | ret = self.rrdm.update( str(rrd_file), update_list ) |
---|
[32] | 827 | |
---|
[41] | 828 | if ret: |
---|
| 829 | return 1 |
---|
[27] | 830 | |
---|
[41] | 831 | debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), string.join( update_list ) ) ) |
---|
[15] | 832 | |
---|
[36] | 833 | return 0 |
---|
| 834 | |
---|
[8] | 835 | def main(): |
---|
| 836 | "Program startup" |
---|
| 837 | |
---|
| 838 | myProcessor = GangliaXMLProcessor() |
---|
| 839 | |
---|
[22] | 840 | if DAEMONIZE: |
---|
| 841 | myProcessor.daemon() |
---|
| 842 | else: |
---|
| 843 | myProcessor.run() |
---|
| 844 | |
---|
[9] | 845 | def check_dir( directory ): |
---|
| 846 | "Check if directory is a proper directory. I.e.: Does _not_ end with a '/'" |
---|
| 847 | |
---|
| 848 | if directory[-1] == '/': |
---|
| 849 | directory = directory[:-1] |
---|
| 850 | |
---|
| 851 | return directory |
---|
| 852 | |
---|
[12] | 853 | def debug_msg( level, msg ): |
---|
| 854 | |
---|
| 855 | if (DEBUG_LEVEL >= level): |
---|
| 856 | sys.stderr.write( msg + '\n' ) |
---|
| 857 | |
---|
[46] | 858 | def printTime( ): |
---|
| 859 | "Print current time in human readable format" |
---|
| 860 | |
---|
| 861 | return time.strftime("%a %d %b %Y %H:%M:%S") |
---|
| 862 | |
---|
[5] | 863 | # Let's go |
---|
[9] | 864 | if __name__ == '__main__': |
---|
| 865 | main() |
---|