splunksplunk-sdk

Creating a REST Handler for any of Splunk's REST endpoints


How to create a Persistent(or any for that matter) REST HANDLER for any given(inbuilt) SPLUNK REST API Endpoint? How to use PersistentServerConnectionApplication class ?

I have gone through https://gist.github.com/LukeMurphey/238004c8976804a8e79570d22721fd99 but cant figure out where to start and how to make one.


Solution

  • There was a great .conf presentation about REST Handlers by James Ervin from a few years ago, https://conf.splunk.com/files/2016/slides/extending-splunks-rest-api-for-fun-and-profit.pdf

    Sample code is available from https://github.com/jrervin/splunk-rest-examples

    James' echo example is quite straight forward. Make sure you also pay attention to the additions that are necessary in web.conf and restmap.conf.

    import os
    import sys
    
    if sys.platform == "win32":
        import msvcrt
        # Binary mode is required for persistent mode on Windows.
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
    
    from splunk.persistconn.application import PersistentServerConnectionApplication
    
    
    class EchoHandler(PersistentServerConnectionApplication):
        def __init__(self, command_line, command_arg):
            PersistentServerConnectionApplication.__init__(self)
    
        def handle(self, in_string):
            return {'payload': in_string,  # Payload of the request.
                    'status': 200          # HTTP status code
            }
    

    Suggest you just get a copy of his app and deploy it, confirm it all works, then modify if for your particular use-case.