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 | # >10 = metric XML |
---|
17 | # >9 = host,cluster,grid,ganglia XML |
---|
18 | # >8 = RRD activity,gmetad config parsing |
---|
19 | # |
---|
20 | DEBUG_LEVEL = 7 |
---|
21 | |
---|
22 | # Where is the gmetad.conf located |
---|
23 | # |
---|
24 | GMETAD_CONF = '/etc/gmetad.conf' |
---|
25 | |
---|
26 | # Where to store the archived rrd's |
---|
27 | # |
---|
28 | ARCHIVE_PATH = '/data/toga/rrds' |
---|
29 | |
---|
30 | # List of data_source names to archive for |
---|
31 | # |
---|
32 | ARCHIVE_SOURCES = [ "LISA Cluster" ] |
---|
33 | |
---|
34 | # Amount of hours to store in one single archived .rrd |
---|
35 | # |
---|
36 | ARCHIVE_HOURS_PER_RRD = 24 |
---|
37 | |
---|
38 | ###################### |
---|
39 | # # |
---|
40 | # Configuration ends # |
---|
41 | # # |
---|
42 | ###################### |
---|
43 | |
---|
44 | # What XML data types not to store |
---|
45 | # |
---|
46 | UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ] |
---|
47 | |
---|
48 | """ |
---|
49 | This is TOrque-GAnglia's data Daemon |
---|
50 | """ |
---|
51 | |
---|
52 | class GangliaXMLHandler( ContentHandler ): |
---|
53 | "Parse Ganglia's XML" |
---|
54 | |
---|
55 | metrics = [ ] |
---|
56 | |
---|
57 | def startElement( self, name, attrs ): |
---|
58 | "Store appropriate data from xml start tags" |
---|
59 | |
---|
60 | if name == 'GANGLIA_XML': |
---|
61 | self.XMLSource = attrs.get('SOURCE',"") |
---|
62 | self.gangliaVersion = attrs.get('VERSION',"") |
---|
63 | debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) ) |
---|
64 | |
---|
65 | elif name == 'GRID': |
---|
66 | self.gridName = attrs.get('NAME',"") |
---|
67 | self.time = attrs.get('LOCALTIME',"") |
---|
68 | debug_msg( 10, '`-Grid found: %s' %( self.gridName ) ) |
---|
69 | |
---|
70 | elif name == 'CLUSTER': |
---|
71 | self.clusterName = attrs.get('NAME',"") |
---|
72 | self.time = attrs.get('LOCALTIME',"") |
---|
73 | self.rrd = RRDHandler( self.clusterName ) |
---|
74 | debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) ) |
---|
75 | |
---|
76 | elif name == 'HOST' and self.clusterName in ARCHIVE_SOURCES: |
---|
77 | self.hostName = attrs.get('NAME',"") |
---|
78 | self.hostIp = attrs.get('IP',"") |
---|
79 | self.hostReported = attrs.get('REPORTED',"") |
---|
80 | # Reset the metrics list for each host |
---|
81 | self.metrics = [ ] |
---|
82 | debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) ) |
---|
83 | |
---|
84 | elif name == 'METRIC' and self.clusterName in ARCHIVE_SOURCES: |
---|
85 | myMetric = { } |
---|
86 | myMetric['name'] = attrs.get('NAME',"") |
---|
87 | myMetric['val'] = attrs.get('VAL',"") |
---|
88 | myMetric['time'] = self.time |
---|
89 | myMetric['type'] = attrs.get('TYPE',"") |
---|
90 | |
---|
91 | self.metrics.append( myMetric ) |
---|
92 | debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) ) |
---|
93 | |
---|
94 | return |
---|
95 | |
---|
96 | def endElement( self, name ): |
---|
97 | #if name == 'GANGLIA_XML': |
---|
98 | |
---|
99 | #if name == 'GRID': |
---|
100 | |
---|
101 | #if name == 'CLUSTER': |
---|
102 | |
---|
103 | if name == 'HOST' and self.clusterName in ARCHIVE_SOURCES: |
---|
104 | |
---|
105 | # Determine time here, so all use same time in this run |
---|
106 | mytime = self.rrd.makeTimeSerial() |
---|
107 | correct_serial = self.rrd.checkNewRrdPeriod( self.hostName, mytime ) |
---|
108 | |
---|
109 | self.storeMetrics( self.hostName, correct_serial ) |
---|
110 | |
---|
111 | #if name == 'METRIC': |
---|
112 | |
---|
113 | def storeMetrics( self, hostname, timeserial ): |
---|
114 | |
---|
115 | for metric in self.metrics: |
---|
116 | if metric['type'] not in UNSUPPORTED_ARCHIVE_TYPES: |
---|
117 | |
---|
118 | self.rrd.createCheck( hostname, metric, timeserial ) |
---|
119 | self.rrd.update( hostname, metric, timeserial ) |
---|
120 | debug_msg( 9, 'stored metric %s for %s: %s' %( hostname, metric['name'], metric['val'] ) ) |
---|
121 | #sys.exit(1) |
---|
122 | |
---|
123 | |
---|
124 | class GangliaXMLGatherer: |
---|
125 | "Setup a connection and file object to Ganglia's XML" |
---|
126 | |
---|
127 | s = None |
---|
128 | |
---|
129 | def __init__( self, host, port ): |
---|
130 | "Store host and port for connection" |
---|
131 | |
---|
132 | self.host = host |
---|
133 | self.port = port |
---|
134 | |
---|
135 | def __del__( self ): |
---|
136 | "Kill the socket before we leave" |
---|
137 | |
---|
138 | self.s.close() |
---|
139 | |
---|
140 | def getFileObject( self ): |
---|
141 | "Connect, and return a file object" |
---|
142 | |
---|
143 | for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): |
---|
144 | af, socktype, proto, canonname, sa = res |
---|
145 | try: |
---|
146 | self.s = socket.socket( af, socktype, proto ) |
---|
147 | except socket.error, msg: |
---|
148 | self.s = None |
---|
149 | continue |
---|
150 | try: |
---|
151 | self.s.connect( sa ) |
---|
152 | except socket.error, msg: |
---|
153 | self.s.close() |
---|
154 | self.s = None |
---|
155 | continue |
---|
156 | break |
---|
157 | |
---|
158 | if self.s is None: |
---|
159 | print 'Could not open socket' |
---|
160 | sys.exit(1) |
---|
161 | |
---|
162 | return self.s.makefile( 'r' ) |
---|
163 | |
---|
164 | class GangliaXMLProcessor: |
---|
165 | |
---|
166 | def daemon( self ): |
---|
167 | "Run as daemon forever" |
---|
168 | |
---|
169 | self.DAEMON = 1 |
---|
170 | |
---|
171 | # Fork the first child |
---|
172 | # |
---|
173 | pid = os.fork() |
---|
174 | if pid > 0: |
---|
175 | sys.exit(0) # end parrent |
---|
176 | |
---|
177 | # creates a session and sets the process group ID |
---|
178 | # |
---|
179 | os.setsid() |
---|
180 | |
---|
181 | # Fork the second child |
---|
182 | # |
---|
183 | pid = os.fork() |
---|
184 | if pid > 0: |
---|
185 | sys.exit(0) # end parrent |
---|
186 | |
---|
187 | # Go to the root directory and set the umask |
---|
188 | # |
---|
189 | os.chdir('/') |
---|
190 | os.umask(0) |
---|
191 | |
---|
192 | sys.stdin.close() |
---|
193 | sys.stdout.close() |
---|
194 | if (DEBUGLEVEL == 0): |
---|
195 | sys.stderr.close() |
---|
196 | |
---|
197 | os.open('/dev/null', 0) |
---|
198 | os.dup(0) |
---|
199 | os.dup(0) |
---|
200 | |
---|
201 | self.run() |
---|
202 | |
---|
203 | def run( self ): |
---|
204 | "Main thread" |
---|
205 | |
---|
206 | while ( 1 ): |
---|
207 | self.processXML() |
---|
208 | time.sleep( 5 ) |
---|
209 | |
---|
210 | def processXML( self ): |
---|
211 | "Process XML" |
---|
212 | |
---|
213 | myXMLGatherer = GangliaXMLGatherer( 'localhost', 8651 ) |
---|
214 | |
---|
215 | myParser = make_parser() |
---|
216 | myHandler = GangliaXMLHandler() |
---|
217 | myParser.setContentHandler( myHandler ) |
---|
218 | |
---|
219 | myParser.parse( myXMLGatherer.getFileObject() ) |
---|
220 | |
---|
221 | class GangliaConfigParser: |
---|
222 | |
---|
223 | sources = [ ] |
---|
224 | |
---|
225 | def __init__( self, config ): |
---|
226 | self.config = config |
---|
227 | self.parseValues() |
---|
228 | |
---|
229 | def parseValues(self): |
---|
230 | "Parse certain values from gmetad.conf" |
---|
231 | |
---|
232 | readcfg = open( self.config, 'r' ) |
---|
233 | |
---|
234 | for line in readcfg.readlines(): |
---|
235 | |
---|
236 | if line.count( '"' ) > 1: |
---|
237 | |
---|
238 | if line.find( 'data_source' ) != -1 and line[0] != '#': |
---|
239 | |
---|
240 | source = { } |
---|
241 | source['name'] = line.split( '"' )[1] |
---|
242 | source_words = line.split( '"' )[2].split( ' ' ) |
---|
243 | |
---|
244 | for word in source_words: |
---|
245 | |
---|
246 | valid_interval = 1 |
---|
247 | |
---|
248 | for letter in word: |
---|
249 | if letter not in string.digits: |
---|
250 | valid_interval = 0 |
---|
251 | |
---|
252 | if valid_interval and len(word) > 0: |
---|
253 | source['interval'] = word |
---|
254 | debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) ) |
---|
255 | |
---|
256 | # No interval found, use Ganglia's default |
---|
257 | if not source.has_key( 'interval' ): |
---|
258 | source['interval'] = 15 |
---|
259 | debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) ) |
---|
260 | |
---|
261 | self.sources.append( source ) |
---|
262 | |
---|
263 | def getInterval( self, source_name ): |
---|
264 | for source in self.sources: |
---|
265 | if source['name'] == source_name: |
---|
266 | return source['interval'] |
---|
267 | return None |
---|
268 | |
---|
269 | class RRDHandler: |
---|
270 | |
---|
271 | def __init__( self, cluster ): |
---|
272 | self.cluster = cluster |
---|
273 | self.gmetad_conf = GangliaConfigParser( GMETAD_CONF ) |
---|
274 | |
---|
275 | def makeTimeSerial( self ): |
---|
276 | |
---|
277 | # YYYYMMDDhhmmss: 20050321143411 |
---|
278 | #mytime = time.strftime( "%Y%m%d%H%M%S" ) |
---|
279 | |
---|
280 | # Seconds since epoch |
---|
281 | mytime = int( time.time() ) |
---|
282 | |
---|
283 | return mytime |
---|
284 | |
---|
285 | def makeRrdPath( self, host, metric=None, timeserial=None ): |
---|
286 | |
---|
287 | if not timeserial: |
---|
288 | rrd_dir = '%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host ) |
---|
289 | else: |
---|
290 | rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial ) |
---|
291 | if metric: |
---|
292 | rrd_file = '%s/%s.rrd' %( rrd_dir, metric['name'] ) |
---|
293 | else: |
---|
294 | rrd_file = None |
---|
295 | |
---|
296 | return rrd_dir, rrd_file |
---|
297 | |
---|
298 | def getLastRrdTimeSerial( self, host ): |
---|
299 | |
---|
300 | rrd_dir, rrd_file = self.makeRrdPath( host ) |
---|
301 | |
---|
302 | newest_timeserial = 0 |
---|
303 | |
---|
304 | if os.path.exists( rrd_dir ): |
---|
305 | for root, dirs, files in os.walk( rrd_dir ): |
---|
306 | |
---|
307 | for dir in dirs: |
---|
308 | |
---|
309 | valid_dir = 1 |
---|
310 | |
---|
311 | for letter in dir: |
---|
312 | if letter not in string.digits: |
---|
313 | valid_dir = 0 |
---|
314 | |
---|
315 | if valid_dir: |
---|
316 | timeserial = dir |
---|
317 | if timeserial > newest_timeserial: |
---|
318 | newest_timeserial = timeserial |
---|
319 | |
---|
320 | if newest_timeserial: |
---|
321 | return newest_timeserial |
---|
322 | else: |
---|
323 | return 0 |
---|
324 | |
---|
325 | def checkNewRrdPeriod( self, host, current_timeserial ): |
---|
326 | |
---|
327 | last_timeserial = int( self.getLastRrdTimeSerial( host ) ) |
---|
328 | debug_msg( 8, 'last timeserial of %s is %s' %( host, last_timeserial ) ) |
---|
329 | |
---|
330 | if not last_timeserial: |
---|
331 | serial = current_timeserial |
---|
332 | else: |
---|
333 | |
---|
334 | archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60) |
---|
335 | |
---|
336 | if (current_timeserial - last_timeserial) >= archive_secs: |
---|
337 | serial = current_timeserial |
---|
338 | else: |
---|
339 | serial = last_timeserial |
---|
340 | |
---|
341 | return serial |
---|
342 | |
---|
343 | def createCheck( self, host, metric, timeserial ): |
---|
344 | "Check if an .rrd allready exists for this metric, create if not" |
---|
345 | |
---|
346 | debug_msg( 8, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metric['name'] ) ) |
---|
347 | |
---|
348 | rrd_dir, rrd_file = self.makeRrdPath( host, metric, timeserial ) |
---|
349 | |
---|
350 | if not os.path.exists( rrd_dir ): |
---|
351 | os.makedirs( rrd_dir ) |
---|
352 | debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) ) |
---|
353 | |
---|
354 | if not os.path.exists( rrd_file ): |
---|
355 | |
---|
356 | interval = self.gmetad_conf.getInterval( self.cluster ) |
---|
357 | heartbeat = 8 * int(interval) |
---|
358 | |
---|
359 | param_step1 = '--step' |
---|
360 | param_step2 = str( interval ) |
---|
361 | |
---|
362 | param_start1 = '--start' |
---|
363 | param_start2 = str( int( metric['time'] ) - 1 ) |
---|
364 | |
---|
365 | param_ds = 'DS:sum:GAUGE:%d:U:U' %heartbeat |
---|
366 | param_rra = 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) |
---|
367 | |
---|
368 | rrdtool.create( str(rrd_file), param_step1, param_step2, param_start1, param_start2, param_ds, param_rra ) |
---|
369 | |
---|
370 | debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) ) |
---|
371 | |
---|
372 | def update( self, host, metric, timeserial ): |
---|
373 | |
---|
374 | debug_msg( 8, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metric['name'] ) ) |
---|
375 | |
---|
376 | rrd_dir, rrd_file = self.makeRrdPath( host, metric, timeserial ) |
---|
377 | |
---|
378 | timestamp = metric['time'] |
---|
379 | val = metric['val'] |
---|
380 | |
---|
381 | update_string = '%s:%s' %(timestamp, val) |
---|
382 | |
---|
383 | rrdtool.update( str(rrd_file), str(update_string) ) |
---|
384 | debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), update_string ) ) |
---|
385 | |
---|
386 | def main(): |
---|
387 | "Program startup" |
---|
388 | |
---|
389 | myProcessor = GangliaXMLProcessor() |
---|
390 | myProcessor.processXML() |
---|
391 | |
---|
392 | def check_dir( directory ): |
---|
393 | "Check if directory is a proper directory. I.e.: Does _not_ end with a '/'" |
---|
394 | |
---|
395 | if directory[-1] == '/': |
---|
396 | directory = directory[:-1] |
---|
397 | |
---|
398 | return directory |
---|
399 | |
---|
400 | def debug_msg( level, msg ): |
---|
401 | |
---|
402 | if (DEBUG_LEVEL >= level): |
---|
403 | sys.stderr.write( msg + '\n' ) |
---|
404 | |
---|
405 | # Let's go |
---|
406 | if __name__ == '__main__': |
---|
407 | main() |
---|