1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import shlex, sys |
---|
4 | from glob import glob |
---|
5 | |
---|
6 | class GangliaConfigParser: |
---|
7 | |
---|
8 | def __init__( self, filename ): |
---|
9 | |
---|
10 | self.conf_lijst = [ ] |
---|
11 | self.filename = filename |
---|
12 | self.file_pointer = file( filename, 'r' ) |
---|
13 | self.lexx = shlex.shlex( self.file_pointer ) |
---|
14 | self.lexx.whitespace_split = True |
---|
15 | |
---|
16 | self.parse() |
---|
17 | |
---|
18 | def __del__( self ): |
---|
19 | |
---|
20 | self.file_pointer.close() |
---|
21 | del self.lexx |
---|
22 | del self.conf_lijst |
---|
23 | |
---|
24 | def removeQuotes( self, value ): |
---|
25 | |
---|
26 | clean_value = value |
---|
27 | clean_value = clean_value.replace( "'", "" ) |
---|
28 | clean_value = clean_value.replace( '"', '' ) |
---|
29 | clean_value = clean_value.strip() |
---|
30 | |
---|
31 | return clean_value |
---|
32 | |
---|
33 | def removeBraces( self, value ): |
---|
34 | |
---|
35 | clean_value = value |
---|
36 | clean_value = clean_value.replace( "(", "" ) |
---|
37 | clean_value = clean_value.replace( ')', '' ) |
---|
38 | clean_value = clean_value.strip() |
---|
39 | |
---|
40 | return clean_value |
---|
41 | |
---|
42 | def parse( self ): |
---|
43 | |
---|
44 | t = 'bogus' |
---|
45 | c= False |
---|
46 | i= False |
---|
47 | |
---|
48 | while t != self.lexx.eof: |
---|
49 | #print 'get token' |
---|
50 | t = self.lexx.get_token() |
---|
51 | |
---|
52 | if t == '/*': |
---|
53 | c = True |
---|
54 | print 'comment start' |
---|
55 | print 'skipping: %s' %t |
---|
56 | continue |
---|
57 | |
---|
58 | if t == '*/': |
---|
59 | c = False |
---|
60 | print 'skipping: %s' %t |
---|
61 | print 'comment end' |
---|
62 | continue |
---|
63 | |
---|
64 | if c: |
---|
65 | print 'skipping: %s' %t |
---|
66 | continue |
---|
67 | |
---|
68 | if t == 'include': |
---|
69 | i = True |
---|
70 | print 'include start' |
---|
71 | print 'skipping: %s' %t |
---|
72 | continue |
---|
73 | |
---|
74 | if i: |
---|
75 | |
---|
76 | print 'include start: %s' %t |
---|
77 | |
---|
78 | t2 = self.removeQuotes( t ) |
---|
79 | t2 = self.removeBraces( t ) |
---|
80 | |
---|
81 | for in_file in glob( self.removeQuotes(t2) ): |
---|
82 | |
---|
83 | print 'including file: %s' %in_file |
---|
84 | parse_infile = GangliaConfigParser( in_file ) |
---|
85 | |
---|
86 | self.conf_lijst = self.conf_lijst + parse_infile.getConfLijst() |
---|
87 | |
---|
88 | del parse_infile |
---|
89 | |
---|
90 | i = False |
---|
91 | print 'include end' |
---|
92 | print 'skipping: %s' %t |
---|
93 | continue |
---|
94 | |
---|
95 | print 'keep: %s' %t |
---|
96 | self.conf_lijst.append( t ) |
---|
97 | |
---|
98 | def getConfLijst( self ): |
---|
99 | |
---|
100 | return self.conf_lijst |
---|
101 | |
---|
102 | GMOND_LOCATION = '/etc/ganglia/gmond.conf' |
---|
103 | |
---|
104 | g = GangliaConfigParser( GMOND_LOCATION ) |
---|
105 | |
---|
106 | print g.getConfLijst() |
---|
107 | |
---|
108 | print 'exiting..' |
---|
109 | sys.exit(0) |
---|