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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
import re
import os
import errno
from os.path import getmtime
from six.moves.urllib.parse import urlencode
from six.moves.configparser import ConfigParser
from django.shortcuts import render
from django.http import QueryDict
from django.conf import settings
from django.contrib.auth import login, authenticate, logout
from django.contrib.staticfiles import finders
from django.utils.safestring import mark_safe
from graphite.compat import HttpResponse
from graphite.dashboard.models import Dashboard, Template
from graphite.dashboard.send_graph import send_graph_email
from graphite.render.views import renderView
from graphite.util import json
from graphite.user_util import isAuthenticated
from graphite.errors import handleInputParameterError, str_param
fieldRegex = re.compile(r'<([^>]+)>')
defaultScheme = {
'name' : 'Everything',
'pattern' : '<category>',
'fields' : [ dict(name='category', label='Category') ],
}
defaultUIConfig = {
'default_graph_width' : 400,
'default_graph_height' : 250,
'refresh_interval' : 60,
'autocomplete_delay' : 375,
'merge_hover_delay' : 700,
'theme' : 'default',
}
defaultKeyboardShortcuts = {
'toggle_toolbar' : 'ctrl-z',
'toggle_metrics_panel' : 'ctrl-space',
'erase_all_graphs' : 'alt-x',
'save_dashboard' : 'alt-s',
'completer_add_metrics' : 'alt-enter',
'completer_del_metrics' : 'alt-backspace',
'give_completer_focus' : 'shift-space',
}
ALL_PERMISSIONS = ['change', 'delete']
class DashboardConfig:
def __init__(self):
self.last_read = 0
self.schemes = [defaultScheme]
self.ui_config = defaultUIConfig.copy()
def check(self):
if getmtime(settings.DASHBOARD_CONF) > self.last_read:
self.load()
def load(self):
schemes = [defaultScheme]
parser = ConfigParser()
parser.read(settings.DASHBOARD_CONF)
for option, default_value in defaultUIConfig.items():
if parser.has_option('ui', option):
try:
self.ui_config[option] = parser.getint('ui', option)
except ValueError:
self.ui_config[option] = parser.get('ui', option)
else:
self.ui_config[option] = default_value
if parser.has_option('ui', 'automatic_variants'):
self.ui_config['automatic_variants'] = parser.getboolean('ui', 'automatic_variants')
else:
self.ui_config['automatic_variants'] = True
self.ui_config['keyboard_shortcuts'] = defaultKeyboardShortcuts.copy()
if parser.has_section('keyboard-shortcuts'):
self.ui_config['keyboard_shortcuts'].update( parser.items('keyboard-shortcuts') )
for section in parser.sections():
if section in ('ui', 'keyboard-shortcuts'):
continue
scheme = parser.get(section, 'scheme')
fields = []
for match in fieldRegex.finditer(scheme):
field = match.group(1)
if parser.has_option(section, '%s.label' % field):
label = parser.get(section, '%s.label' % field)
else:
label = field
fields.append({
'name' : field,
'label' : label
})
schemes.append({
'name' : section,
'pattern' : scheme,
'fields' : fields,
})
self.schemes = schemes
config = DashboardConfig()
@handleInputParameterError
def dashboard(request, name=None):
name = str_param('name', name)
dashboard_conf_missing = False
try:
config.check()
except OSError as e:
if e.errno == errno.ENOENT:
dashboard_conf_missing = True
else:
raise
initialError = None
debug = request.GET.get('debug', False)
theme = request.GET.get('theme', config.ui_config['theme'])
css_file = finders.find('css/dashboard-%s.css' % theme)
if css_file is None:
# Also search in STATIC_ROOT for dist packages
css_file = os.path.isfile(os.path.join(settings.STATIC_ROOT,
'css/dashboard-%s.css' % theme))
if css_file is None:
initialError = "Invalid theme '%s'" % theme
theme = config.ui_config['theme']
context = {
'schemes_json': mark_safe(json.dumps(config.schemes)),
'ui_config_json': mark_safe(json.dumps(config.ui_config)),
'jsdebug': debug or settings.JAVASCRIPT_DEBUG,
'debug': debug,
'theme': theme,
'initialError': initialError,
'querystring': mark_safe(json.dumps(dict(request.GET.items()))),
'dashboard_conf_missing': dashboard_conf_missing,
'userName': '',
'permissions': mark_safe(json.dumps(getPermissions(request.user))),
'permissionsUnauthenticated': mark_safe(json.dumps(getPermissions(None)))
}
user = request.user
if user:
context['userName'] = user.username
if name is not None:
try:
dashboard = Dashboard.objects.get(name=name)
except Dashboard.DoesNotExist:
context['initialError'] = "Dashboard '%s' does not exist." % name
else:
context['initialState'] = dashboard.state
return render(request, "dashboard.html", context)
@handleInputParameterError
def template(request, name, val):
name = str_param('name', name)
template_conf_missing = False
try:
config.check()
except OSError as e:
if e.errno == errno.ENOENT:
template_conf_missing = True
else:
raise
initialError = None
debug = request.GET.get('debug', False)
theme = request.GET.get('theme', config.ui_config['theme'])
css_file = finders.find('css/dashboard-%s.css' % theme)
if css_file is None:
initialError = "Invalid theme '%s'" % theme
theme = config.ui_config['theme']
context = {
'schemes_json' : json.dumps(config.schemes),
'ui_config_json' : json.dumps(config.ui_config),
'jsdebug' : debug or settings.JAVASCRIPT_DEBUG,
'debug' : debug,
'theme' : theme,
'initialError' : initialError,
'querystring' : json.dumps( dict( request.GET.items() ) ),
'template_conf_missing' : template_conf_missing,
'userName': '',
'permissions': json.dumps(getPermissions(request.user)),
'permissionsUnauthenticated': json.dumps(getPermissions(None))
}
user = request.user
if user:
context['userName'] = user.username
try:
template = Template.objects.get(name=name)
except Template.DoesNotExist:
context['initialError'] = "Template '%s' does not exist." % name
else:
state = json.loads(template.loadState(val))
state['name'] = '%s/%s' % (name, val)
context['initialState'] = json.dumps(state)
return render(request, "dashboard.html", context)
def getPermissions(user):
"""Return [change, delete] based on authorisation model and user privileges/groups"""
if user and not isAuthenticated(user):
user = None
if not settings.DASHBOARD_REQUIRE_AUTHENTICATION:
return ALL_PERMISSIONS # don't require login
if not user:
return []
# from here on, we have a user
permissions = ALL_PERMISSIONS
if settings.DASHBOARD_REQUIRE_PERMISSIONS:
permissions = [permission for permission in ALL_PERMISSIONS if user.has_perm('dashboard.%s_dashboard' % permission)]
editGroup = settings.DASHBOARD_REQUIRE_EDIT_GROUP
if editGroup and len(user.groups.filter(name = editGroup)) == 0:
permissions = []
return permissions
@handleInputParameterError
def save(request, name):
name = str_param('name', name)
if 'change' not in getPermissions(request.user):
return json_response( dict(error="Must be logged in with appropriate permissions to save") )
# Deserialize and reserialize as a validation step
state = str( json.dumps( json.loads( request.POST['state'] ) ) )
try:
dashboard = Dashboard.objects.get(name=name)
except Dashboard.DoesNotExist:
dashboard = Dashboard.objects.create(name=name, state=state)
else:
dashboard.state = state
dashboard.save()
return json_response( dict(success=True) )
@handleInputParameterError
def save_template(request, name, key):
name = str_param('name', name)
key = str_param('key', key)
if 'change' not in getPermissions(request.user):
return json_response( dict(error="Must be logged in with appropriate permissions to save the template") )
# Deserialize and reserialize as a validation step
state = str( json.dumps( json.loads( request.POST['state'] ) ) )
try:
template = Template.objects.get(name=name)
except Template.DoesNotExist:
template = Template.objects.create(name=name)
template.setState(state, key)
template.save()
else:
template.setState(state, key)
template.save()
return json_response( dict(success=True) )
@handleInputParameterError
def load(request, name):
name = str_param('name', name)
try:
dashboard = Dashboard.objects.get(name=name)
except Dashboard.DoesNotExist:
return json_response( dict(error="Dashboard '%s' does not exist. " % name) )
return json_response( dict(state=json.loads(dashboard.state)) )
@handleInputParameterError
def load_template(request, name, val):
name = str_param('name', name)
try:
template = Template.objects.get(name=name)
except Template.DoesNotExist:
return json_response( dict(error="Template '%s' does not exist. " % name) )
state = json.loads(template.loadState(val))
state['name'] = '%s/%s' % (name, val)
return json_response( dict(state=state) )
@handleInputParameterError
def delete(request, name):
name = str_param('name', name)
if 'delete' not in getPermissions(request.user):
return json_response( dict(error="Must be logged in with appropriate permissions to delete") )
try:
dashboard = Dashboard.objects.get(name=name)
except Dashboard.DoesNotExist:
return json_response( dict(error="Dashboard '%s' does not exist. " % name) )
else:
dashboard.delete()
return json_response( dict(success=True) )
@handleInputParameterError
def delete_template(request, name):
name = str_param('name', name)
if 'delete' not in getPermissions(request.user):
return json_response( dict(error="Must be logged in with appropriate permissions to delete the template") )
try:
template = Template.objects.get(name=name)
except Template.DoesNotExist:
return json_response( dict(error="Template '%s' does not exist. " % name) )
else:
template.delete()
return json_response( dict(success=True) )
def find(request):
queryParams = request.GET.copy()
queryParams.update(request.POST)
query = queryParams.get('query', False)
query_terms = set( query.lower().split() )
results = []
# Find all dashboard names that contain each of our query terms as a substring
for dashboard_name in Dashboard.objects.order_by('name').values_list('name', flat=True):
name = dashboard_name.lower()
if name.startswith('temporary-'):
continue
found = True # blank queries return everything
for term in query_terms:
if term in name:
found = True
else:
found = False
break
if found:
results.append( dict(name=dashboard_name) )
return json_response( dict(dashboards=results) )
def find_template(request):
queryParams = request.GET.copy()
queryParams.update(request.POST)
query = queryParams.get('query', False)
query_terms = set( query.lower().split() )
results = []
# Find all dashboard names that contain each of our query terms as a substring
for template in Template.objects.all():
name = template.name.lower()
found = True # blank queries return everything
for term in query_terms:
if term in name:
found = True
else:
found = False
break
if found:
results.append( dict(name=template.name) )
return json_response( dict(templates=results) )
def help(request):
context = {}
return render(request, "dashboardHelp.html", context)
def email(request):
sender = request.POST['sender']
recipients = request.POST['recipients'].split()
subject = request.POST['subject']
message = request.POST['message']
# these need to be passed to the render function in an HTTP request.
graph_params = json.loads(request.POST['graph_params'], parse_int=str)
target = QueryDict(urlencode({'target': graph_params.pop('target')}))
graph_params = QueryDict(urlencode(graph_params))
new_post = request.POST.copy()
new_post.update(graph_params)
new_post.update(target)
request.POST = new_post
resp = renderView(request)
img = resp.content
if img:
attachments = [('graph.png', img, 'image/png')]
send_graph_email(subject, sender, recipients, attachments, message)
return json_response(dict(success=True))
def create_temporary(request):
state = str( json.dumps( json.loads( request.POST['state'] ) ) )
i = 0
while True:
name = "temporary-%d" % i
try:
Dashboard.objects.get(name=name)
except Dashboard.DoesNotExist:
dashboard = Dashboard.objects.create(name=name, state=state)
break
else:
i += 1
return json_response( dict(name=dashboard.name) )
def json_response(obj):
return HttpResponse(content_type='application/json', content=json.dumps(obj))
def user_login(request):
response = dict(errors={}, text={}, success=False, permissions=[])
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
response['success'] = True
response['permissions'].extend(getPermissions(user))
else:
response['errors']['reason'] = 'Account disabled.'
else:
response['errors']['reason'] = 'Username and/or password invalid.'
return json_response(response)
def user_logout(request):
response = dict(errors={}, text={}, success=True)
logout(request)
return json_response(response)
|