forked from mockee/graphite-observer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (59 loc) · 2.43 KB
/
Copy pathapp.py
File metadata and controls
76 lines (59 loc) · 2.43 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
#!/usr/bin/env python
import os, logging, json, urllib2
from collections import defaultdict
import plugins
import config
from bottle import route, run, template, static_file, default_app
logging.basicConfig(format = '%(asctime)-15s %(message)s', level = logging.DEBUG)
targets_all = {}
categories = defaultdict(list)
def load_plugins():
global targets_all
plugins_dir = os.path.dirname(plugins.__file__)
for f in os.listdir(plugins_dir):
if f == '__init__.py' or not f.endswith('.py'):
continue
module = f[:-3]
try:
plugin = __import__('plugins.' + module, globals(), locals(), ['*'])
targets_all[module] = plugin.targets[:40]
if hasattr(plugin, 'category') and not callable(getattr(plugin, 'category')):
category = plugin.category.lower()
else:
category = 'default'
categories[category].append(module)
logging.info('Loading plugin - %s', module)
except:
logging.info('Error when loading plugin - %s', module)
load_plugins()
def render_page(body, page='index', **kwargs):
return str(template('templates/base', body = body, page = page, **kwargs))
@route('/', method = 'GET')
@route('/index', method = 'GET')
@route('/dashboard', method = 'GET')
def index():
body = template('templates/body.index', targets_all = targets_all, categories = categories)
return render_page(body)
@route('/dashboard/:plugin', method = 'GET')
def dashboard(plugin = ''):
body = template('templates/body.dashboard', targets = targets_all[plugin])
return render_page(body, page = 'dashboard', plugin = plugin)
@route('/metric_value/:metric_name', method = 'GET')
def metric_value(metric_name = ''):
try:
url = config.graphite_url.rstrip('/') + '/render/?from=-1min&format=json&target='
datapoints = json.loads(urllib2.urlopen(url + metric_name).read())[0]['datapoints']
# average value in last 1 min
value = [p[0] for p in datapoints if p[0] is not None][-1]
value = float('%.2f' % value)
except:
value = None
return json.dumps(value)
@route('/debug', method = 'GET')
def debug():
body = template('templates/body.debug', targets_all = targets_all)
return render_page(body, page = 'debug')
@route('<path:re:/static/css/.*css>')
@route('<path:re:/static/js/.*js>')
def static(path, method = 'GET'):
return static_file(path, root = '.')