forked from python-diamond/Diamond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_doc.py
executable file
·317 lines (238 loc) · 10.2 KB
/
build_doc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# coding=utf-8
################################################################################
import os
import sys
import optparse
import configobj
import traceback
import tempfile
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
def getIncludePaths(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f.endswith('.py'):
sys.path.append(os.path.dirname(cPath))
elif os.path.isdir(cPath):
getIncludePaths(cPath)
collectors = {}
def getCollectors(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f.endswith('.py'):
modname = f[:-3]
if modname.startswith('Test'):
continue
if modname.startswith('test'):
continue
try:
# Import the module
module = __import__(modname, globals(), locals(), ['*'])
# Find the name
for attr in dir(module):
if not attr.endswith('Collector'):
continue
cls = getattr(module, attr)
if cls.__name__ not in collectors:
collectors[cls.__name__] = module
except Exception:
print "Failed to import module: %s. %s" % (
modname, traceback.format_exc())
collectors[modname] = False
elif os.path.isdir(cPath):
getCollectors(cPath)
handlers = {}
def getHandlers(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f.endswith('.py'):
modname = f[:-3]
try:
# Import the module
module = __import__(modname, globals(), locals(), ['*'])
# Find the name
for attr in dir(module):
if (not attr.endswith('Handler')
or attr.startswith('Handler')):
continue
cls = getattr(module, attr)
if cls.__name__ not in handlers:
handlers[cls.__name__] = module
except Exception:
print "Failed to import module: %s. %s" % (
modname, traceback.format_exc())
handlers[modname] = False
elif os.path.isdir(cPath):
getHandlers(cPath)
################################################################################
if __name__ == "__main__":
# Initialize Options
parser = optparse.OptionParser()
parser.add_option("-c", "--configfile",
dest="configfile",
default="/etc/diamond/diamond.conf",
help="Path to the config file")
parser.add_option("-C", "--collector",
dest="collector",
default=None,
help="Configure a single collector")
parser.add_option("-p", "--print",
action="store_true",
dest="dump",
default=False,
help="Just print the defaults")
# Parse Command Line Args
(options, args) = parser.parse_args()
# Initialize Config
if os.path.exists(options.configfile):
config = configobj.ConfigObj(os.path.abspath(options.configfile))
else:
print >> sys.stderr, "ERROR: Config file: %s does not exist." % (
options.configfile)
print >> sys.stderr, ("Please run python config.py -c "
+ "/path/to/diamond.conf")
parser.print_help(sys.stderr)
sys.exit(1)
collector_path = config['server']['collectors_path']
docs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'docs'))
handler_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'src', 'diamond', 'handler'))
getIncludePaths(collector_path)
# Ugly hack for snmp collector overrides
getCollectors(os.path.join(collector_path, 'snmp'))
getCollectors(collector_path)
collectorIndexFile = open(os.path.join(docs_path, "Collectors.md"), 'w')
collectorIndexFile.write("## Collectors\n")
collectorIndexFile.write("\n")
for collector in sorted(collectors.iterkeys()):
# Skip configuring the basic collector object
if collector == "Collector":
continue
if collector.startswith('Test'):
continue
print "Processing %s..." % (collector)
if not hasattr(collectors[collector], collector):
continue
cls = getattr(collectors[collector], collector)
obj = cls(config=config, handlers={})
options = obj.get_default_config_help()
defaultOptions = obj.get_default_config()
docFile = open(os.path.join(docs_path,
"collectors-" + collector + ".md"), 'w')
enabled = ''
collectorIndexFile.write(" - [%s](collectors-%s)%s\n" % (collector,
collector,
enabled))
docFile.write("%s\n" % (collector))
docFile.write("=====\n")
if collectors[collector].__doc__ is None:
print "No __doc__ string!"
docFile.write("%s\n" % (collectors[collector].__doc__))
docFile.write("#### Options - [Generic Options](Configuration)\n")
docFile.write("\n")
docFile.write("<table>")
docFile.write("<tr>")
docFile.write("<th>Setting</th>")
docFile.write("<th>Default</th>")
docFile.write("<th>Description</th>")
docFile.write("<th>Type</th>")
docFile.write("</tr>\n")
for option in sorted(options.keys()):
defaultOption = ''
defaultOptionType = ''
if option in defaultOptions:
defaultOptionType = defaultOptions[option].__class__.__name__
if isinstance(defaultOptions[option], list):
defaultOption = ', '.join(map(str, defaultOptions[option]))
defaultOption += ','
else:
defaultOption = str(defaultOptions[option])
docFile.write("<tr>")
docFile.write("<td>%s</td>" % (option))
docFile.write("<td>%s</td>" % (defaultOption))
docFile.write("<td>%s</td>" % (options[option].replace(
"\n", '<br>\n')))
docFile.write("<td>%s</td>" % (defaultOptionType))
docFile.write("</tr>\n")
docFile.write("</table>\n")
docFile.write("\n")
docFile.write("#### Example Output\n")
docFile.write("\n")
docFile.write("```\n")
docFile.write("__EXAMPLESHERE__\n")
docFile.write("```\n")
docFile.write("\n")
docFile.write("### This file was generated from the python source\n")
docFile.write("### Please edit the source to make changes\n")
docFile.write("\n")
docFile.close()
collectorIndexFile.close()
getIncludePaths(handler_path)
getHandlers(handler_path)
handlerIndexFile = open(os.path.join(docs_path, "Handlers.md"), 'w')
handlerIndexFile.write("## Handlers\n")
handlerIndexFile.write("\n")
for handler in sorted(handlers.iterkeys()):
# Skip configuring the basic handler object
if handler == "Handler":
continue
if handler[0:4] == "Test":
continue
print "Processing %s..." % (handler)
if not hasattr(handlers[handler], handler):
continue
cls = getattr(handlers[handler], handler)
tmpfile = tempfile.mkstemp()
options = None
defaultOptions = None
try:
obj = cls({
'log_file': tmpfile[1],
})
options = obj.get_default_config_help()
defaultOptions = obj.get_default_config()
except Exception, e:
print "Caught Exception %s" % e
os.remove(tmpfile[1])
docFile = open(os.path.join(docs_path,
"handler-" + handler + ".md"), 'w')
handlerIndexFile.write(" - [%s](handler-%s)\n" % (handler, handler))
docFile.write("%s\n" % (handler))
docFile.write("====\n")
docFile.write("%s" % (handlers[handler].__doc__))
docFile.write("#### Options - [Generic Options](Configuration)\n")
docFile.write("\n")
docFile.write("<table>")
docFile.write("<tr>")
docFile.write("<th>Setting</th>")
docFile.write("<th>Default</th>")
docFile.write("<th>Description</th>")
docFile.write("<th>Type</th>")
docFile.write("</tr>\n")
if options:
for option in sorted(options.keys()):
defaultOption = ''
defaultOptionType = ''
if option in defaultOptions:
defaultOptionType = defaultOptions[
option].__class__.__name__
if isinstance(defaultOptions[option], list):
defaultOption = ', '.join(map(str,
defaultOptions[option]))
defaultOption += ','
else:
defaultOption = str(defaultOptions[option])
docFile.write("<tr>")
docFile.write("<td>%s</td>" % (option))
docFile.write("<td>%s</td>" % (defaultOption))
docFile.write("<td>%s</td>" % (options[option].replace(
"\n", '<br>\n')))
docFile.write("<td>%s</td>" % (defaultOptionType))
docFile.write("</tr>\n")
docFile.write("</table>\n")
docFile.write("\n")
docFile.write("### This file was generated from the python source\n")
docFile.write("### Please edit the source to make changes\n")
docFile.write("\n")
docFile.close()
handlerIndexFile.close()