forked from modehq/python_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_exporter.py
More file actions
executable file
·358 lines (235 loc) · 8.76 KB
/
python_exporter.py
File metadata and controls
executable file
·358 lines (235 loc) · 8.76 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
# Copyright (c) 2018-2020 The Mode Group
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Export shell and Python functions into Prometheus.
"""
import sys
import os
import yaml
import imp
import subprocess
import operator
import signal
import getopt
from multiprocessing import Process
from flask import *
# Stop logging all the request messages
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# Globals
CONFIG_DATA = None
# Cache for importing Python modules. So we dont always re-import them
MODULE_CACHE = {}
# Listening for Flask
LISTEN_PORT = None
LISTEN_ON = None
# When we are quitting, this is true
CONTROL_QUITTING = False
def GetQuitting():
"""Are we quitting? Loaded modules can check."""
global CONTROL_QUITTING
return CONTROL_QUITTING
def ProcessYamls(config_data):
"""Process all the YAML data, starting Exporters as needed."""
exporter_port = {}
for path, data in config_data.items():
# print 'ProcessYamls: %s (%s)' % (path, data['port'])
export_list = []
for command_data in data['commands']:
export_data = ExecuteCommand(command_data)
# Append to list in place
export_list += export_data
exporter_port[data['port']] = export_list
output = ''
for port, export_port_data in exporter_port.items():
# print 'Port: %s' % port
output += ExporterFormat(export_port_data)
# print output
return output
def ExporterFormat(export_port_data):
output = ''
# Sort the items by metric name, so we group them nicely
export_port_data.sort(key=operator.itemgetter('metric'))
# Print out the HELP/TYPE only once per metric
help_cache = {}
for item in export_port_data:
if item['metric'] not in help_cache:
help_cache[item['metric']] = True
if 'help' in item:
output += '# HELP %s\n' % item['help']
if 'type' in item:
output += '# TYPE %s\n' % item['type']
# Metric - Labelset - Value
output += '%s%s %s\n' % (item['metric'], FormatLabelset(item['labelset']), item['value'])
return output
def FormatLabelset(labelset):
output_list = []
keys = labelset.keys()
keys.sort()
for key in keys:
key_pair = '%s="%s"' % (key, labelset[key])
output_list.append(key_pair)
output = '{%s}' % ','.join(output_list)
return output
def ExecuteCommand(command_data):
"""Execute one of our YAML commands, and parse it for exporter data"""
# If we are executing a Shell Command, handle that
if 'shell' in command_data:
if 'shell_vars' in command_data and len(command_data['shell_vars']) > 0:
shell = command_data['shell'] % command_data['shell_vars']
else:
shell = command_data['shell']
# print 'Execute: %s' % shell
output = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE).stdout.read()
# Else, we are not executing a Shell Command, so output=None
else:
output = None
# print 'Output: %s' % output
command_module = LoadModule(command_data['module'])
export_data = command_module.Parse(output, command_data)
# print 'Export Data: %s' % export_data
return export_data
def LoadModule(path):
"""Load the pluggable module.
TODO(geoff): Cache these so we dont have to do it every request.
"""
global MODULE_CACHE
if path in MODULE_CACHE:
return MODULE_CACHE[path]
print 'Load Module: %s' % path
module = imp.load_source(path, path)
module.GetQuitting = GetQuitting
print 'Load Module: Result: %s' % module
MODULE_CACHE[path] = module
return module
def Usage(error=None):
if error:
status = 1
print '\nERROR: %s' % error
else:
status = 0
print '\nusage: %s <yaml> <optional yaml>...' % os.path.basename(sys.argv[0])
sys.exit(status)
def RunFlask(port, listen_on='0.0.0.0'):
"""Run the Flask server. Implement all functions as sub-functions so we can set the port dynamically"""
if listen_on == None:
listen_on = '0.0.0.0'
print 'Starting Flask: %s (%s)' % (port, listen_on)
app = Flask(__name__)
# app.add_url_rule('/', endpoint='index', view_func=RedirectToMetric)
# app.add_url_rule('/metric', endpoint='metric', view_func=Metric)
app.add_url_rule('/', endpoint='RedirectToMetric', view_func=RedirectToMetric)
app.add_url_rule('/metrics', endpoint='Metric', view_func=Metric)
app.run(host=listen_on, port=port, threaded=True)
def RedirectToMetric():
return redirect('/metrics')
def Metric():
try:
output = ProcessYamls(CONFIG_DATA)
response = Response(output, mimetype='text/plain')
except Exception, e:
print 'Metric Exception: %s' % e
return response
def QuittingHandler(signum, frame):
global CONTROL_QUITTING
print 'Quit Signal: %s' % signum
CONTROL_QUITTING = True
# Sleep a little, and then exit
# time.sleep(0) # No need to delay, leaving so this can be used as a template/pattern
raise RuntimeError('Quitting')
def IgnoreHandler(signum, frame):
print 'Ignore Signal: %s' % signum
def Go():
"""Run in our own process, launching off of Main()"""
global LISTEN_PORT, LISTEN_ON
# Run the Flask server
RunFlask(LISTEN_PORT, LISTEN_ON)
def Usage(error=None):
if error:
exit_code = 1
print 'Error: %s\n' % error
else:
exit_code = 0
print 'Usage: %s <options>' % os.path.basename(sys.argv[0])
print ''
print ' -h --help Help'
print ' --daemon Use python multiprocessing to daemonize'
print
sys.exit(exit_code)
def Main(args=None):
global CONFIG_DATA
global LISTEN_PORT, LISTEN_ON
port = None
if not args:
args = []
(options, args) = getopt.getopt(args, 'h', ['help', 'daemon'])
if len(args) < 1:
Usage("Need at least 1 argument. 0 given.")
# Run as a daemon with Python Multiprocessing. Not useful under systemd, we lose the logs
run_as_daemon = False
for option, value in options:
if option in ('-h', '--help'):
Usage()
elif option in ('--daemon'):
run_as_daemon = True
config_data = {}
# Change the working directory based on the executable, so we can use relative paths for modules
cwd = os.path.dirname(sys.argv[0])
os.chdir(cwd)
print 'Working Directory: %s' % cwd
# print 'Data Directory: %s' % os.path.abspath(os.path.dirname(sys.argv[0]))
# print 'User Directory: %s' % os.path.abspath(os.path.expanduser("~"))
# Listen on IP
listen_on = None
for arg in args:
if not os.path.isfile(arg):
Usage('%s is not a valid file')
with open(arg, "r") as stream:
try:
content = yaml.load(stream)
except Exception, e:
Usage("Failed to load YAML file: %s :: %s" % (arg, e))
if port == None:
port = int(content['port'])
config_data[arg] = content
# If we specify a listening port, take the first one
if 'listen_on' in content and listen_on == None:
listen_on = content['listen_on']
if len(args) > 1:
print 'All exporter results will return on the same port. Multi-port not-yet implemented.'
# Send this to our global, because of the Flask disconnect...
CONFIG_DATA = config_data
# Set Flask info
LISTEN_ON = listen_on
LISTEN_PORT = port
# Handle the program quitting, so we can gracefully close our threads
signal.signal(signal.SIGTERM, QuittingHandler) # Kill -15
signal.signal(signal.SIGINT, QuittingHandler) # Keyboard Interrupt
signal.signal(signal.SIGHUP, IgnoreHandler) # Terminal Hangup, keep on going
# If we arent running as a daemon, just run. This is better under systemd
if not run_as_daemon:
Go()
# Else, run as a daeomn with Python Multiprocessing
else:
# Initialize everything
p = Process(target=Go)
p.start()
# When the above function completes, we will terminate it
p.terminate()
# Exit without error
sys.exit(0)
if __name__ == '__main__':
Main(sys.argv[1:])