Community:40GUIDevelopment

From Splunk Wiki

Jump to: navigation, search

Developing GUI components in Splunk 4.0?

Let us know what you did and how you did it! Add a topic to this page and explain how you solved your problem with the all-new Splunk 4.0 development interface and architecture!

Packaging custom UI components

This topic shows how to package custom UI components for apps so the custom components can be accessed from Splunk Web.

When configuring custom endpoints for Splunk Web, the custom endpoints behave identically as the standard Splunk Web endpoints:

  • use the route system (Splunk Web uses the route system to map a URL to code)
  • allow authorized and unauthorized access
  • have full access to the request object
  • have full python access, which means full disk access

Configuring custom endpoints for Splunk Web

The mount point for custom endpoints is:

   http://localhost:8000/custom/<AppName>/

This mount point avoids name collision with any standard Splunk endpoint.

Declare a host controller for the custom endpoint in web.conf. To set custom configurations, create or edit web.conf in $SPLUNK_HOME/etc/apps/<APP_NAME>/default/.

Add the following stanza to web.conf:

   [endpoint:<unique identifier>]
   # no other content required

Place the python resources implementing the custom endpoint in the controllers directory, which is at the following location:

   $SPLUNK_HOME/etc/apps/<APP_NAME>/appserver/controllers/

The controller framework imports any valid python module+controller class declared in web.conf.

Note: Changes to web.conf require a full restart; changes to python code require a splunkweb restart.

Example: Sample controller providing network utilities for a network app

This example provides network utilities for a network app, and exposes the following URIs:

  http://localhost:8000/custom/network/netutils/header_echo
  http://localhost:8000/custom/network/netutils/routed?A=B
  http://localhost:8000/custom/network/netutils/mako_template

The endpoint author has full access to the CherryPy, Mako, and Splunk Python modules.

netutils.py

netutils.py provides HTTP utilities for the app. Place the file in the controllers directory:

   $SPLUNK_HOME/etc/apps/network/appserver/controllers/netutils.py
# netutils.py

import cherrypy
import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.decorators import expose_page
from splunk.appserver.mrsparkle.lib.routes import route

class TestController(controllers.BaseController):
    """
    Test instantiation of a BaseController-style class
    """

    @expose_page(must_login=True)
    def header_echo(self, **kwargs):
        cherrypy.response.headers['Content-Type'] = 'text/plain'
        output = []
        for k,v in cherrypy.request.headers.items():
            output.append('%s: %s' % (k, v))
        return '\n'.join(output)


    @route('/:path=routed')
    @expose_page(must_login=True)
    def request_echo(self, **kwargs):
        """
        Example handler that uses the @route() decorator to
        control the URI endpoint mapping
        """
        cherrypy.response.headers['Content-Type'] = 'text/plain'
        output = []
        for k,v in kwargs.items():
            output.append('%s: %s' % (k, v))
        return '\n'.join(output)
    

    @expose_page(must_login=True)
    def mako_template(self, **kwargs):

        # note the path syntax here: in order to reference templates
        # included in an app, use the modified path of the form:
        #     /<YOUR_APP_NAME>:/templates/<YOUR_TEMPLATE_NAME>
        return self.render_template('/network:/templates/controller_test.html', {'qs': kwargs})

web.conf

Place the following single line stanza in web.conf at $SPLUNK_HOME/etc/apps/network/default/web.conf

 [endpoint:netutils]


Example: Executing a Splunk script

This example shows how the network application from the first example executes a script located in $SPLUNK_HOME/bin/scripts.

It exposes a URI for the script, such as:

  http://localhost:8000/custom/network/networkscripts/proxy_raw?script_name=echo.sh&arg=A%20B%20C

networkscripts.py

Place networkscripts.py in the controllers directory:

   $SPLUNK_HOME/etc/apps/network/appserver/controllers/networkscripts.py



# networkscripts.py

import re
import logging
import os
from subprocess import Popen, PIPE

import cherrypy

import splunk.appserver.mrsparkle.controllers as controllers
import splunk.appserver.mrsparkle.lib.util as util
from splunk.appserver.mrsparkle.lib.decorators import expose_page
from splunk.appserver.mrsparkle.lib.routes import route

logger = logging.getLogger('splunk.appserver.custom.testing.shell')

class UserScriptController(controllers.BaseController):
    """ 
    Proof of concept custom shell script proxy runner.
    
    WARNING: DO NOT COPY PASTE THIS INTO PRODUCTION.  IT HAS NOT
             BEEN VETTED FOR SECURITY.

    USAGE:

        http://localhost:8000/custom/testing/shell/proxy_raw?script_name=echo.sh&arg=A%20B%20C
    """

    @expose_page(must_login=True)
    def proxy_raw(self, script_name, arg=, **unused):
    
        # clean the script name
        script_name = re.split(r'[\/\\]+', script_name)[-1]
        script_path = util.make_splunkhome_path(['bin', 'scripts', script_name])

        # verify that script exists
        if not os.path.exists(script_path):
            raise Exception, 'The user script %s was not found' % script_name

        # execute and return script output
        script_line = [script_path, arg]
        logger.info('executing custom script: %s' % script_line)
        return Popen(script_line, stdout=PIPE).communicate()[0]

web.conf

Place the following single line stanza in web.conf at $SPLUNK_HOME/etc/apps/network/default/web.conf

 [endpoint:networkscripts]
Personal tools
Hot Wiki Topics


About Splunk >
  • Search and navigate IT data from applications, servers and network devices in real-time.
  • Download Splunk