<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How To Run Python On App Install in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-To-Run-Python-On-App-Install/m-p/488665#M8730</link>
    <description>&lt;P&gt;I want to run some authenticated Python code when an app installs, to kick off an action.&lt;/P&gt;
&lt;P&gt;What's the best way to do that?&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jun 2020 21:00:40 GMT</pubDate>
    <dc:creator>David</dc:creator>
    <dc:date>2020-06-17T21:00:40Z</dc:date>
    <item>
      <title>How To Run Python On App Install</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-To-Run-Python-On-App-Install/m-p/488665#M8730</link>
      <description>&lt;P&gt;I want to run some authenticated Python code when an app installs, to kick off an action.&lt;/P&gt;
&lt;P&gt;What's the best way to do that?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 21:00:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-To-Run-Python-On-App-Install/m-p/488665#M8730</guid>
      <dc:creator>David</dc:creator>
      <dc:date>2020-06-17T21:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: How To Run Python On App Install</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-To-Run-Python-On-App-Install/m-p/488666#M8731</link>
      <description>&lt;P&gt;There are generally two ways of doing this: with a scripted input that runs on Splunk start (will also run on app install) and with triggers.&lt;/P&gt;

&lt;P&gt;Scripted inputs are covered elsewhere, but functionally it's an inputs.conf stanza that has an interval set to -1 (run once on Splunk start / app install).&lt;/P&gt;

&lt;P&gt;Triggers are defined in app.conf and are less well documented.&lt;BR /&gt;
Docs: &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/Admin/appconf#.5Btriggers.5D"&gt;https://docs.splunk.com/Documentation/Splunk/latest/Admin/appconf#.5Btriggers.5D&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;In order to trigger a custom rest endpoint, you need to define a conf file that matches the configuration.&lt;/P&gt;

&lt;P&gt;Here is a working example, tested on Splunk 7.2 (so before Python3 changes -- script might / will likely fail when Splunk switches to python3):&lt;/P&gt;

&lt;P&gt;In app.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[triggers]
reload.ssenav = http_get /SSEResetLocalNav
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;ssenav.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[general]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In web.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[expose:SSEResetLocalNav]
methods = GET
pattern = SSEResetLocalNav
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In restmap.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[script:SSEResetLocalNAV]
match                 = /SSEResetLocalNav
script                = resetLocalNAV.py
scripttype            = persist
handler               = resetLocalNAV.ResetLocalNav
requireAuthentication = true
output_modes          = json
passPayload           = true
passHttpHeaders       = true
passHttpCookies       = true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;resetLocalNAV.py (this script will overwrite a local default.xml navigation with the default default.xml):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;from __future__ import absolute_import

import os
import sys
import time
import csv

import splunk.rest as rest
from splunk.appserver.mrsparkle.lib.util import make_splunkhome_path

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 ResetLocalNav(PersistentServerConnectionApplication):
    def __init__(self, command_line, command_arg):
        PersistentServerConnectionApplication.__init__(self)

    def handle(self, in_string):
        default_nav = ""
        f = open("/tmp/dvtest.log", "wb")
        from time import gmtime, strftime
        f.write("STARTING - " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n")
        try: 
            input = json.loads(in_string)
            sessionKey = input['session']['authtoken']
        except Exception as e:
            f.write("Error Early")
            return {'payload': {"status": "error early", "message": str(e)},  
                    'status': 200          # HTTP status code
            }

        try:
            localfilepath = make_splunkhome_path(['etc', 'apps', 'Splunk_Security_Essentials', 'local', 'data', 'ui', 'nav', 'default.xml'])
            if not os.path.exists(localfilepath):
                f.write("No Update Needed\n")
                return {'payload': {"status": "no update needed"},  
                        'status': 200          # HTTP status code
                }
        except Exception as e:
            f.write("Error 1 - " + str(e) + "\n")
            return {'payload': {"status": "error", "message": str(e)},  
                    'status': 200          # HTTP status code
            }

        try:
            filepath = make_splunkhome_path(['etc', 'apps', 'Splunk_Security_Essentials', 'default', 'data', 'ui', 'nav', 'default.xml'])
            with open(filepath, 'rU') as fh:
                default_nav = fh.read()
            url = "/servicesNS/nobody/Splunk_Security_Essentials/data/ui/nav/default"
            postargs = {
                "eai:data": default_nav
            }

            rest.simpleRequest(url, postargs=postargs, sessionKey=sessionKey, raiseAllErrors=True)
            f.write("Update Successful\n")
            return {'payload': {"status": "update successful", "more": default_nav},  
                    'status': 200          # HTTP status code
            }
        except Exception as e:
            f.write("error 2 - " + str(e) + "\n")
            return {'payload': {"status": "error", "message": str(e)},  
                    'status': 200          # HTTP status code
            }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above was courtesy of @sideview &lt;/P&gt;

&lt;P&gt;Below courtesy of @hazekamp :&lt;BR /&gt;
You can also ship a default.meta entry&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[ssenav]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you don't want to ship a dummy conf&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 23:03:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-To-Run-Python-On-App-Install/m-p/488666#M8731</guid>
      <dc:creator>David</dc:creator>
      <dc:date>2019-09-20T23:03:30Z</dc:date>
    </item>
  </channel>
</rss>

