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 rrdtool |
---|
8 | import string |
---|
9 | import os |
---|
10 | import os.path |
---|
11 | import time |
---|
12 | import re |
---|
13 | |
---|
14 | # Specify debugging level here; |
---|
15 | # |
---|
16 | # <=11 = metric XML |
---|
17 | # <=10 = host,cluster,grid,ganglia XML |
---|
18 | # <=9 = RRD activity,gmetad config parsing |
---|
19 | # <=8 = host processing |
---|
20 | # <=7 = daemon threading - NOTE: Daemon will 'halt on all errors' from this level |
---|
21 | # |
---|
22 | DEBUG_LEVEL = 8 |
---|
23 | |
---|
24 | # Where is the gmetad.conf located |
---|
25 | # |
---|
26 | GMETAD_CONF = '/etc/gmetad.conf' |
---|
27 | |
---|
28 | # Where to store the archived rrd's |
---|
29 | # |
---|
30 | ARCHIVE_PATH = '/data/toga/rrds' |
---|
31 | |
---|
32 | # List of data_source names to archive for |
---|
33 | # |
---|
34 | ARCHIVE_SOURCES = [ "LISA Cluster" ] |
---|
35 | |
---|
36 | # Amount of hours to store in one single archived .rrd |
---|
37 | # |
---|
38 | ARCHIVE_HOURS_PER_RRD = 12 |
---|
39 | |
---|
40 | # Interval at which to grab&store XML |
---|
41 | # |
---|
42 | GRAB_INTERVAL = 15 |
---|
43 | |
---|
44 | # Wether or not to run as a daemon in background |
---|
45 | # |
---|
46 | DAEMONIZE = 0 |
---|
47 | |
---|
48 | ###################### |
---|
49 | # # |
---|
50 | # Configuration ends # |
---|
51 | # # |
---|
52 | ###################### |
---|
53 | |
---|
54 | # What XML data types not to store |
---|
55 | # |
---|
56 | UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ] |
---|
57 | |
---|
58 | """ |
---|
59 | This is TOrque-GAnglia's data Daemon |
---|
60 | """ |
---|
61 | |
---|
62 | class GangliaXMLHandler( ContentHandler ): |
---|
63 | "Parse Ganglia's XML" |
---|
64 | |
---|
65 | def __init__( self ): |
---|
66 | self.metrics = [ ] |
---|
67 | self.clusters = { } |
---|
68 | |
---|
69 | def startElement( self, name, attrs ): |
---|
70 | "Store appropriate data from xml start tags" |
---|
71 | |
---|
72 | if name == 'GANGLIA_XML': |
---|
73 | |
---|
74 | self.XMLSource = attrs.get( 'SOURCE', "" ) |
---|
75 | self.gangliaVersion = attrs.get( 'VERSION', "" ) |
---|
76 | |
---|
77 | debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) ) |
---|
78 | |
---|
79 | elif name == 'GRID': |
---|
80 | |
---|
81 | self.gridName = attrs.get( 'NAME', "" ) |
---|
82 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
83 | |
---|
84 | debug_msg( 10, '`-Grid found: %s' %( self.gridName ) ) |
---|
85 | |
---|
86 | elif name == 'CLUSTER': |
---|
87 | |
---|
88 | self.clusterName = attrs.get( 'NAME', "" ) |
---|
89 | self.time = attrs.get( 'LOCALTIME', "" ) |
---|
90 | |
---|
91 | self.clusters[ self.clusterName ] = RRDHandler( self.clusterName ) |
---|
92 | |
---|
93 | debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) ) |
---|
94 | |
---|
95 | elif name == 'HOST' and self.clusterName in ARCHIVE_SOURCES: |
---|
96 | |
---|
97 | self.hostName = attrs.get( 'NAME', "" ) |
---|
98 | self.hostIp = attrs.get( 'IP', "" ) |
---|
99 | self.hostReported = attrs.get( 'REPORTED', "" ) |
---|
100 | |
---|
101 | debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) ) |
---|
102 | |
---|
103 | elif name == 'METRIC' and self.clusterName in ARCHIVE_SOURCES: |
---|
104 | |
---|
105 | type = attrs.get( 'TYPE', "" ) |
---|
106 | |
---|
107 | if type not in UNSUPPORTED_ARCHIVE_TYPES: |
---|
108 | |
---|
109 | myMetric = { } |
---|
110 | myMetric['name'] = attrs.get( 'NAME', "" ) |
---|
111 | myMetric['val'] = attrs.get( 'VAL', "" ) |
---|
112 | myMetric['time'] = self.hostReported |
---|
113 | |
---|
114 | self.clusters[ self.clusterName ].memMetric( self.hostname, myMetric ) |
---|
115 | |
---|
116 | debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) ) |
---|
117 | |
---|
118 | def storeMetrics( self, hostname, timeserial ): |
---|
119 | |
---|
120 | for metric in self.metrics: |
---|
121 | if metric['type'] not in UNSUPPORTED_ARCHIVE_TYPES: |
---|
122 | |
---|
123 | self.rrd.createCheck( hostname, metric, timeserial ) |
---|
124 | self.rrd.update( hostname, metric, timeserial ) |
---|
125 | debug_msg( 9, 'stored metric %s for %s: %s' %( hostname, metric['name'], metric['val'] ) ) |
---|
126 | #sys.exit(1) |
---|
127 | |
---|
128 | class GangliaXMLGatherer: |
---|
129 | "Setup a connection and file object to Ganglia's XML" |
---|
130 | |
---|
131 | s = None |
---|
132 | |
---|
133 | def __init__( self, host, port ): |
---|
134 | "Store host and port for connection" |
---|
135 | |
---|
136 | self.host = host |
---|
137 | self.port = port |
---|
138 | |
---|
139 | def __del__( self ): |
---|
140 | "Kill the socket before we leave" |
---|
141 | |
---|
142 | self.s.close() |
---|
143 | |
---|
144 | def getFileObject( self ): |
---|
145 | "Connect, and return a file object" |
---|
146 | |
---|
147 | for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): |
---|
148 | |
---|
149 | af, socktype, proto, canonname, sa = res |
---|
150 | |
---|
151 | try: |
---|
152 | |
---|
153 | self.s = socket.socket( af, socktype, proto ) |
---|
154 | |
---|
155 | except socket.error, msg: |
---|
156 | |
---|
157 | self.s = None |
---|
158 | continue |
---|
159 | |
---|
160 | try: |
---|
161 | |
---|
162 | self.s.connect( sa ) |
---|
163 | |
---|
164 | except socket.error, msg: |
---|
165 | |
---|
166 | self.s.close() |
---|
167 | self.s = None |
---|
168 | continue |
---|
169 | |
---|
170 | break |
---|
171 | |
---|
172 | if self.s is None: |
---|
173 | |
---|
174 | print 'Could not open socket' |
---|
175 | sys.exit(1) |
---|
176 | |
---|
177 | return self.s.makefile( 'r' ) |
---|
178 | |
---|
179 | class GangliaXMLProcessor: |
---|
180 | |
---|
181 | def daemon( self ): |
---|
182 | "Run as daemon forever" |
---|
183 | |
---|
184 | self.DAEMON = 1 |
---|
185 | |
---|
186 | # Fork the first child |
---|
187 | # |
---|
188 | pid = os.fork() |
---|
189 | |
---|
190 | if pid > 0: |
---|
191 | |
---|
192 | sys.exit(0) # end parent |
---|
193 | |
---|
194 | # creates a session and sets the process group ID |
---|
195 | # |
---|
196 | os.setsid() |
---|
197 | |
---|
198 | # Fork the second child |
---|
199 | # |
---|
200 | pid = os.fork() |
---|
201 | |
---|
202 | if pid > 0: |
---|
203 | |
---|
204 | sys.exit(0) # end parent |
---|
205 | |
---|
206 | # Go to the root directory and set the umask |
---|
207 | # |
---|
208 | os.chdir('/') |
---|
209 | os.umask(0) |
---|
210 | |
---|
211 | sys.stdin.close() |
---|
212 | sys.stdout.close() |
---|
213 | sys.stderr.close() |
---|
214 | |
---|
215 | os.open('/dev/null', 0) |
---|
216 | os.dup(0) |
---|
217 | os.dup(0) |
---|
218 | |
---|
219 | self.run() |
---|
220 | |
---|
221 | def printTime( self ): |
---|
222 | |
---|
223 | return time.strftime("%a, %d %b %Y %H:%M:%S") |
---|
224 | |
---|
225 | def run( self ): |
---|
226 | "Main thread" |
---|
227 | |
---|
228 | while( 1 ): |
---|
229 | |
---|
230 | debug_msg( 7, self.printTime() + ' - mainthread() - xmlthread() started' ) |
---|
231 | pid = os.fork() |
---|
232 | |
---|
233 | if pid == 0: |
---|
234 | # Child - XML Thread |
---|
235 | # |
---|
236 | # Process XML and exit |
---|
237 | |
---|
238 | debug_msg( 7, self.printTime() + ' - xmlthread() - Start XML processing..' ) |
---|
239 | self.processXML() |
---|
240 | debug_msg( 7, self.printTime() + ' - xmlthread() - Done processing; exiting.' ) |
---|
241 | sys.exit( 0 ) |
---|
242 | |
---|
243 | elif pid > 0: |
---|
244 | # Parent - Daemon Thread |
---|
245 | |
---|
246 | debug_msg( 7, self.printTime() + ' - mainthread() - Sleep '+ str(GRAB_INTERVAL) +'s: zzzzz..' ) |
---|
247 | time.sleep( GRAB_INTERVAL ) |
---|
248 | debug_msg( 7, self.printTime() + ' - mainthread() - Awoken: waiting for XML thread..' ) |
---|
249 | |
---|
250 | r = os.wait() |
---|
251 | ret = r[1] |
---|
252 | if ret != 0: |
---|
253 | debug_msg( 7, self.printTime() + ' - mainthread() - Done waiting: ERROR! xmlthread() exited with status %d' %(ret) ) |
---|
254 | if DEBUG_LEVEL>=7: sys.exit( 1 ) |
---|
255 | else: |
---|
256 | |
---|
257 | debug_msg( 7, self.printTime() + ' - mainthread() - Done waiting: xmlthread() finished succesfully' ) |
---|
258 | |
---|
259 | def processXML( self ): |
---|
260 | "Process XML" |
---|
261 | |
---|
262 | myXMLGatherer = GangliaXMLGatherer( 'localhost', 8651 ) |
---|
263 | |
---|
264 | myParser = make_parser() |
---|
265 | myHandler = GangliaXMLHandler() |
---|
266 | myParser.setContentHandler( myHandler ) |
---|
267 | |
---|
268 | myParser.parse( myXMLGatherer.getFileObject() ) |
---|
269 | |
---|
270 | class GangliaConfigParser: |
---|
271 | |
---|
272 | sources = [ ] |
---|
273 | |
---|
274 | def __init__( self, config ): |
---|
275 | |
---|
276 | self.config = config |
---|
277 | self.parseValues() |
---|
278 | |
---|
279 | def parseValues( self ): |
---|
280 | "Parse certain values from gmetad.conf" |
---|
281 | |
---|
282 | readcfg = open( self.config, 'r' ) |
---|
283 | |
---|
284 | for line in readcfg.readlines(): |
---|
285 | |
---|
286 | if line.count( '"' ) > 1: |
---|
287 | |
---|
288 | if line.find( 'data_source' ) != -1 and line[0] != '#': |
---|
289 | |
---|
290 | source = { } |
---|
291 | source['name'] = line.split( '"' )[1] |
---|
292 | source_words = line.split( '"' )[2].split( ' ' ) |
---|
293 | |
---|
294 | for word in source_words: |
---|
295 | |
---|
296 | valid_interval = 1 |
---|
297 | |
---|
298 | for letter in word: |
---|
299 | |
---|
300 | if letter not in string.digits: |
---|
301 | |
---|
302 | valid_interval = 0 |
---|
303 | |
---|
304 | if valid_interval and len(word) > 0: |
---|
305 | |
---|
306 | source['interval'] = word |
---|
307 | debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) ) |
---|
308 | |
---|
309 | # No interval found, use Ganglia's default |
---|
310 | if not source.has_key( 'interval' ): |
---|
311 | |
---|
312 | source['interval'] = 15 |
---|
313 | debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) ) |
---|
314 | |
---|
315 | self.sources.append( source ) |
---|
316 | |
---|
317 | def getInterval( self, source_name ): |
---|
318 | |
---|
319 | for source in self.sources: |
---|
320 | |
---|
321 | if source['name'] == source_name: |
---|
322 | |
---|
323 | return source['interval'] |
---|
324 | |
---|
325 | return None |
---|
326 | |
---|
327 | class RRDHandler: |
---|
328 | |
---|
329 | myMetrics = { } |
---|
330 | |
---|
331 | def __init__( self, cluster ): |
---|
332 | self.cluster = cluster |
---|
333 | self.gmetad_conf = GangliaConfigParser( GMETAD_CONF ) |
---|
334 | |
---|
335 | def getClusterName( self ): |
---|
336 | return self.cluster |
---|
337 | |
---|
338 | def memMetric( self, host, metric ): |
---|
339 | |
---|
340 | for m in self.myMetrics[ host ]: |
---|
341 | |
---|
342 | if m['time'] == metric['time']: |
---|
343 | |
---|
344 | # Allready have this metric, abort |
---|
345 | return 1 |
---|
346 | |
---|
347 | if not self.myMetrics.has_key( host ): |
---|
348 | |
---|
349 | self.myMetrics[ host ] = { } |
---|
350 | |
---|
351 | if not self.myMetrics[ host ].has_key( metric['name'] ): |
---|
352 | |
---|
353 | self.myMetrics[ host ][ metric['name'] ] = [ ] |
---|
354 | |
---|
355 | self.myMetrics[ host ][ metric['name'] ].append( metric ) |
---|
356 | |
---|
357 | def makeUpdateString( self, host, metric ): |
---|
358 | |
---|
359 | update_string = '' |
---|
360 | |
---|
361 | for m in self.myMetrics[ host ][ metric['name'] ]: |
---|
362 | |
---|
363 | update_string = update_string + ' %s:%s' %( metric['time'], metric['val'] ) |
---|
364 | |
---|
365 | return update_string |
---|
366 | |
---|
367 | def makeTimeSerial( self ): |
---|
368 | "Generate a time serial. Seconds since epoch" |
---|
369 | |
---|
370 | # YYYYMMDDhhmmss: 20050321143411 |
---|
371 | #mytime = time.strftime( "%Y%m%d%H%M%S" ) |
---|
372 | |
---|
373 | # Seconds since epoch |
---|
374 | mytime = int( time.time() ) |
---|
375 | |
---|
376 | return mytime |
---|
377 | |
---|
378 | def makeRrdPath( self, host, metric=None, timeserial=None ): |
---|
379 | """ |
---|
380 | Make a RRD location/path and filename |
---|
381 | If a metric or timeserial are supplied the complete locations |
---|
382 | will be made, else just the host directory |
---|
383 | """ |
---|
384 | |
---|
385 | if not timeserial: |
---|
386 | rrd_dir = '%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host ) |
---|
387 | else: |
---|
388 | rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial ) |
---|
389 | if metric: |
---|
390 | rrd_file = '%s/%s.rrd' %( rrd_dir, metric['name'] ) |
---|
391 | else: |
---|
392 | rrd_file = None |
---|
393 | |
---|
394 | return rrd_dir, rrd_file |
---|
395 | |
---|
396 | def getLastRrdTimeSerial( self, host ): |
---|
397 | """ |
---|
398 | Find the last timeserial (directory) for this host |
---|
399 | This is determined once every host |
---|
400 | """ |
---|
401 | |
---|
402 | rrd_dir, rrd_file = self.makeRrdPath( host ) |
---|
403 | |
---|
404 | newest_timeserial = 0 |
---|
405 | |
---|
406 | if os.path.exists( rrd_dir ): |
---|
407 | |
---|
408 | for root, dirs, files in os.walk( rrd_dir ): |
---|
409 | |
---|
410 | for dir in dirs: |
---|
411 | |
---|
412 | valid_dir = 1 |
---|
413 | |
---|
414 | for letter in dir: |
---|
415 | if letter not in string.digits: |
---|
416 | valid_dir = 0 |
---|
417 | |
---|
418 | if valid_dir: |
---|
419 | timeserial = dir |
---|
420 | if timeserial > newest_timeserial: |
---|
421 | newest_timeserial = timeserial |
---|
422 | |
---|
423 | if newest_timeserial: |
---|
424 | return newest_timeserial |
---|
425 | else: |
---|
426 | return 0 |
---|
427 | |
---|
428 | def checkNewRrdPeriod( self, host, current_timeserial ): |
---|
429 | """ |
---|
430 | Check if current timeserial belongs to recent time period |
---|
431 | or should become a new period (and file). |
---|
432 | |
---|
433 | Returns the serial of the correct time period |
---|
434 | """ |
---|
435 | |
---|
436 | last_timeserial = int( self.getLastRrdTimeSerial( host ) ) |
---|
437 | debug_msg( 9, 'last timeserial of %s is %s' %( host, last_timeserial ) ) |
---|
438 | |
---|
439 | if not last_timeserial: |
---|
440 | serial = current_timeserial |
---|
441 | else: |
---|
442 | |
---|
443 | archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60) |
---|
444 | |
---|
445 | if (current_timeserial - last_timeserial) >= archive_secs: |
---|
446 | serial = current_timeserial |
---|
447 | else: |
---|
448 | serial = last_timeserial |
---|
449 | |
---|
450 | return serial |
---|
451 | |
---|
452 | def createCheck( self, host, metric, timeserial ): |
---|
453 | "Check if an .rrd allready exists for this metric, create if not" |
---|
454 | |
---|
455 | debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metric['name'] ) ) |
---|
456 | |
---|
457 | rrd_dir, rrd_file = self.makeRrdPath( host, metric, timeserial ) |
---|
458 | |
---|
459 | if not os.path.exists( rrd_dir ): |
---|
460 | os.makedirs( rrd_dir ) |
---|
461 | debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) ) |
---|
462 | |
---|
463 | if not os.path.exists( rrd_file ): |
---|
464 | |
---|
465 | interval = self.gmetad_conf.getInterval( self.cluster ) |
---|
466 | heartbeat = 8 * int(interval) |
---|
467 | |
---|
468 | param_step1 = '--step' |
---|
469 | param_step2 = str( interval ) |
---|
470 | |
---|
471 | param_start1 = '--start' |
---|
472 | param_start2 = str( int( metric['time'] ) - 1 ) |
---|
473 | |
---|
474 | param_ds = 'DS:sum:GAUGE:%d:U:U' %heartbeat |
---|
475 | param_rra = 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) |
---|
476 | |
---|
477 | rrdtool.create( str(rrd_file), param_step1, param_step2, param_start1, param_start2, param_ds, param_rra ) |
---|
478 | |
---|
479 | debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) ) |
---|
480 | |
---|
481 | def update( self, host, metric, timeserial ): |
---|
482 | |
---|
483 | debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metric['name'] ) ) |
---|
484 | |
---|
485 | rrd_dir, rrd_file = self.makeRrdPath( host, metric, timeserial ) |
---|
486 | |
---|
487 | timestamp = metric['time'] |
---|
488 | val = metric['val'] |
---|
489 | |
---|
490 | update_string = '%s:%s' %(timestamp, val) |
---|
491 | |
---|
492 | try: |
---|
493 | |
---|
494 | rrdtool.update( str(rrd_file), str(update_string) ) |
---|
495 | |
---|
496 | except rrdtool.error, detail: |
---|
497 | |
---|
498 | debug_msg( 0, 'EXCEPTION! While trying to update rrd:' ) |
---|
499 | debug_msg( 0, '\trrd %s with %s' %( str(rrd_file), update_string ) ) |
---|
500 | debug_msg( 0, str(detail) ) |
---|
501 | |
---|
502 | sys.exit( 1 ) |
---|
503 | |
---|
504 | debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), update_string ) ) |
---|
505 | |
---|
506 | def main(): |
---|
507 | "Program startup" |
---|
508 | |
---|
509 | myProcessor = GangliaXMLProcessor() |
---|
510 | |
---|
511 | if DAEMONIZE: |
---|
512 | myProcessor.daemon() |
---|
513 | else: |
---|
514 | myProcessor.run() |
---|
515 | |
---|
516 | def check_dir( directory ): |
---|
517 | "Check if directory is a proper directory. I.e.: Does _not_ end with a '/'" |
---|
518 | |
---|
519 | if directory[-1] == '/': |
---|
520 | directory = directory[:-1] |
---|
521 | |
---|
522 | return directory |
---|
523 | |
---|
524 | def debug_msg( level, msg ): |
---|
525 | |
---|
526 | if (DEBUG_LEVEL >= level): |
---|
527 | sys.stderr.write( msg + '\n' ) |
---|
528 | |
---|
529 | # Let's go |
---|
530 | if __name__ == '__main__': |
---|
531 | main() |
---|