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