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