Commit d22b8ada by Bach Dániel Committed by Őry Máté

dashboard: fix graphite errors

fixes #58
fixes #56
parent 5a4a29e7
...@@ -66,7 +66,7 @@ urlpatterns = patterns( ...@@ -66,7 +66,7 @@ urlpatterns = patterns(
url(r'^group/list/$', GroupList.as_view(), url(r'^group/list/$', GroupList.as_view(),
name='dashboard.views.group-list'), name='dashboard.views.group-list'),
url((r'^vm/(?P<pk>\d+)/graph/(?P<metric>cpu|memory|network)/' url((r'^vm/(?P<pk>\d+)/graph/(?P<metric>cpu|memory|network)/'
r'(?P<time>[0-9]+[hdwy])$'), r'(?P<time>[0-9]{1,2}[hdwy])$'),
VmGraphView.as_view(), VmGraphView.as_view(),
name='dashboard.views.vm-graph'), name='dashboard.views.vm-graph'),
) )
...@@ -3,6 +3,7 @@ import json ...@@ -3,6 +3,7 @@ import json
import logging import logging
import re import re
from datetime import datetime from datetime import datetime
import requests
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.contrib.messages import warning from django.contrib.messages import warning
...@@ -1345,8 +1346,12 @@ class TransferOwnershipConfirmView(LoginRequiredMixin, View): ...@@ -1345,8 +1346,12 @@ class TransferOwnershipConfirmView(LoginRequiredMixin, View):
class VmGraphView(LoginRequiredMixin, View): class VmGraphView(LoginRequiredMixin, View):
def get(self, request, pk, metric, time, *args, **kwargs): def get(self, request, pk, metric, time, *args, **kwargs):
from urllib import urlencode graphite_host = getenv("GRAPHITE_HOST", None)
import urllib2 graphite_port = getenv("GRAPHITE_PORT", None)
if (graphite_host in ['', None] or graphite_port in ['', None]):
logger.debug('GRAPHITE_HOST is empty.')
raise Http404()
if metric not in ['cpu', 'memory', 'network']: if metric not in ['cpu', 'memory', 'network']:
raise SuspiciousOperation() raise SuspiciousOperation()
...@@ -1371,17 +1376,13 @@ class VmGraphView(LoginRequiredMixin, View): ...@@ -1371,17 +1376,13 @@ class VmGraphView(LoginRequiredMixin, View):
prefix = 'vm.%s' % instance.vm_name prefix = 'vm.%s' % instance.vm_name
target = targets[metric] % prefix target = targets[metric] % prefix
title = '%s (%s) - %s' % (instance.name, instance.vm_name, metric) title = '%s (%s) - %s' % (instance.name, instance.vm_name, metric)
params = {'target': target,
params = urlencode({'target': target,
'from': '-%s' % time, 'from': '-%s' % time,
'title': title.encode('UTF-8')}) 'title': title.encode('UTF-8'),
url = ('http://%s:%s/render/?width=500&height=200&%s' 'width': '500',
% (getenv("GRAPHITE_HOST"), 'height': '200'}
getenv("GRAPHITE_PORT"), url = ('http://%s:%s/render/?%s' % (graphite_host, graphite_port,
params)) params))
print url response = requests.post(url, data=params)
response = urllib2.urlopen(urllib2.Request(url)) return HttpResponse(response.content, mimetype="image/png")
return HttpResponse(response.read(), mimetype="image/png")
print pk, metric
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment