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