1. Joe Heck
  2. eyes


eyes / eyeswebapp / core / views.py

from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseBadRequest, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext

from django.forms import ModelForm

import os
import time
from core.models import Monitor
from core.models import Host
from core.models import Datastore
from util.monitor import ArgSet
from util.nagios import NagiosPoller

Core Views

* models supporting the core monitoring infrastructure


class HostForm(ModelForm):
    class Meta:
        model = Host

def render_response(req, *args, **kwargs):
    """ a variation on render_to_response that inserts the RequestContext
    into each template context from these views."""
    kwargs['context_instance'] = RequestContext(req)
    return render_to_response(*args, **kwargs)

# Create your views here.
def index(request):
    """ default web view for the 'front page' of the web application. """
    # user = User.get_by_key_name('admin')
    # if not user or user.username != 'admin' or not (user.is_active and
    #         user.is_staff and user.is_superuser and
    #         user.check_password('admin')):
    #     user = User(key_name='admin', username='admin',
    #         email='admin@localhost', first_name='Boss', last_name='Admin',
    #         is_active=True, is_staff=True, is_superuser=True)
    #     user.set_password('admin')
    #     user.put()
    monitor_list = Monitor.objects.all()
    return render_response(request, 'core/index.html', {'monitor_list': monitor_list})

def visual_test(request):
    """ test view """
    context_dict = {}
    context_dict['error'] = "this is an error message"
    context_dict['info'] = "information message"
    message_list = ["message1", "a much longer message 2 is inserted in this location to work the sizing of the format."]
    context_dict['messages'] = message_list
    return render_response(request, 'visualtest.html', context_dict)

def host_detail(request, host_id):
    """ detail view of the individual monitor"""
    host = get_object_or_404(Host, pk=host_id)
    return render_response(request, 'core/host_detail.html', {'host': host})

def monitor_detail(request, monitor_id):
    """ detail view of the individual monitor"""
    mon = get_object_or_404(Monitor, pk=monitor_id)
    return render_response(request, 'core/monitor_detail.html', {'monitor': mon})

def datastore_detail(request, datastore_id):
    """ detail view of the individual monitor"""
    this_ds = get_object_or_404(Datastore, pk=datastore_id)
    return render_response(request, 'core/datastore_detail.html', {'datastore': this_ds})

def datastore_image(request, datastore_id):
    png result of graphing an individual datastore element
    code thoughts on dynamic generation: http://www.djangosnippets.org/snippets/365/
    this_ds = get_object_or_404(Datastore, pk=datastore_id)
    name = "%s.png" % (this_ds.id, )
    png_file = os.path.join(this_ds.png_path, name)
    # check to see if file exists, if not - create:
    if not(os.path.exists(png_file)):
    # check to see if file is older than 5 minutes, if so, regen.
    modified_sec = os.path.getmtime(png_file)
    five_min_ago = time.time() - (60 * 5)
    if modified_sec < five_min_ago:
    from django.core.servers.basehttp import FileWrapper
    wrapper = FileWrapper(file(png_file))
    response = HttpResponse(wrapper, content_type='image/png')
    response['Content-Length'] = os.path.getsize(png_file)
    return response

def host_list(request):
    host_list = Host.objects.all()
    form = HostForm()
    if request.method == 'POST':
        form = HostForm(request.POST)
        if form.is_valid():
    return render_response(request, 'core/host_list.html', {'host_list': host_list, 'form': form})

def monitor_list(request):
    nagios_based_poller = NagiosPoller()
    list_of_plugins = nagios_based_poller.plugin_list()
    return render_response(request, 'core/monitor_list.html', {'plugin_list': list_of_plugins})

def test_monitor(request, plugin_name):
    nagios_based_poller = NagiosPoller()
    help_monitor_result = nagios_based_poller.plugin_help(plugin_name)
    return render_response(request, 'core/test_monitor.html',
                        {'plugin_name': plugin_name,
                         'monitor_result': help_monitor_result})

def ajax_monitor(request, plugin_name):
    nagios_based_poller = NagiosPoller()
    # .plugin_help(plugin_name)
    # .run_plugin(plugin_name, argset)
    if request.is_ajax():
        mimetype = 'application/javascript'
        if request.method == 'GET':
            help_monitor_result = nagios_based_poller.plugin_help(plugin_name)
            json_data = help_monitor_result.json()
        elif request.method == 'POST':
            # expected to get a set of key labeled "argument" - with 1..4 (or higher) elements
            argset = ArgSet()
            for string_arg in request.POST.getlist('argument'):
            mon_result = nagios_based_poller.run_plugin(plugin_name, argset)
            json_data = mon_result.json()
        return HttpResponse(json_data, mimetype)
        return HttpResponseBadRequest