<?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 Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard? in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708977#M57997</link>
    <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/275198"&gt;@rohithvr19&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The python file should be in bin folder of your app.&lt;/P&gt;&lt;P&gt;Can you please confirm whether the individual script is working fine?&lt;/P&gt;&lt;P&gt;Have you tried my shared app on your local machine? If my app is working fine then try to add your Python code to this app.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;KV&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2025 12:07:49 GMT</pubDate>
    <dc:creator>kamlesh_vaghela</dc:creator>
    <dc:date>2025-01-16T12:07:49Z</dc:date>
    <item>
      <title>How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708691#M57978</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;I’ve been receiving a lot of helpful responses regarding this topic, and I truly appreciate the support. However, I’m currently stuck on how to execute a Python script via a button in Splunk and display the results on a dashboard.&lt;/P&gt;
&lt;P&gt;Here’s the Python script I’m using:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;import json
import requests
import logging

class ZabbixHandler:
def __init__(self):
self.logger = logging.getLogger('zabbix_handler')
self.ZABBIX_API_URL = "http://localhost/zabbix/api_jsonrpc.php" # Replace with your Zabbix API URL
self.ZABBIX_USERNAME = "user" # Replace with your Zabbix username
self.ZABBIX_PASSWORD = "password" # Replace with your Zabbix password
self.SPLUNK_HEC_URL = "http://localhost:8088/services/collector" # Replace with your Splunk HEC URL
self.SPLUNK_HEC_TOKEN = "myhectoken" # Replace with your Splunk HEC token
self.HEC_INDEX = "summary" # Splunk index for the logs
self.HEC_SOURCETYPE = "zabbix:audit:logs" # Splunk sourcetype

def authenticate_with_zabbix(self):
payload = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"username": self.ZABBIX_USERNAME,
"password": self.ZABBIX_PASSWORD,
},
"id": 1,
}
response = requests.post(self.ZABBIX_API_URL, json=payload, verify=False)
response_data = response.json()
if "result" in response_data:
return response_data["result"]
else:
raise Exception(f"Zabbix authentication failed: {response_data}")

def fetch_audit_logs(self, auth_token):
payload = {
"jsonrpc": "2.0",
"method": "auditlog.get",
"params": {
"output": "extend",
"filter": {
"action": 0 # Fetch specific actions if needed
}
},
"auth": auth_token,
"id": 2,
}
response = requests.post(self.ZABBIX_API_URL, json=payload, verify=False)
response_data = response.json()
if "result" in response_data:
return response_data["result"]
else:
raise Exception(f"Failed to fetch audit logs: {response_data}")

def send_logs_to_splunk(self, logs):
headers = {
"Authorization": f"Splunk {self.SPLUNK_HEC_TOKEN}",
"Content-Type": "application/json",
}
for log in logs:
payload = {
"index": self.HEC_INDEX,
"sourcetype": self.HEC_SOURCETYPE,
"event": log,
}
response = requests.post(self.SPLUNK_HEC_URL, headers=headers, json=payload, verify=False)
if response.status_code != 200:
self.logger.error(f"Failed to send log to Splunk: {response.status_code} - {response.text}")

def handle_request(self):
try:
auth_token = self.authenticate_with_zabbix()
logs = self.fetch_audit_logs(auth_token)
self.send_logs_to_splunk(logs)
return {"status": "success", "message": "Logs fetched and sent to Splunk successfully."}
except Exception as e:
self.logger.error(f"Error during operation: {str(e)}")
return {"status": "error", "message": str(e)}

if __name__ == "__main__":
handler = ZabbixHandler()
response = handler.handle_request()
print(json.dumps(response))
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;My restmap.conf&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;[script:zabbix_handler]&lt;BR /&gt;match = /zabbix_handler&lt;BR /&gt;script = zabbix_handler.py&lt;BR /&gt;handler = python&lt;BR /&gt;output_modes = json&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Current Status:&lt;/STRONG&gt;&lt;/H3&gt;
&lt;P&gt;The script is working correctly, and I am successfully retrieving data from Zabbix and sending it to Splunk. The logs are being indexed in Splunk’s summary index, and I can verify this via manual execution of the script.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Requirements:&lt;/STRONG&gt;&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;I want to create a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;button&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;in a Splunk dashboard that, when clicked, executes the above Python script.&lt;/LI&gt;
&lt;LI&gt;The script (zabbix_handler.py) is located in the /opt/splunk/bin/ directory.&lt;/LI&gt;
&lt;LI&gt;The script extracts logs from Zabbix, sends them to Splunk’s HEC endpoint, and stores them in the summary index.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;After the button is clicked and the script is executed, I would like to display the query results from index="summary" on the same dashboard.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Questions:&lt;/STRONG&gt;&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;JavaScript for the Button&lt;/STRONG&gt;: How should I write the JavaScript code for the button to execute this script and display the results?&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Placement of JavaScript Code&lt;/STRONG&gt;: Where exactly in the Splunk app directory should I place the JavaScript code?&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Triggering the Script&lt;/STRONG&gt;: How can I integrate this setup with Splunk’s framework to ensure the Python script is executed and results are shown in the dashboard?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/127939"&gt;@kamlesh_vaghela&lt;/a&gt;&amp;nbsp; can u help me on this task.?im kind of stuck on this and ur videos helped me a lot ..!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 19:58:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708691#M57978</guid>
      <dc:creator>rohithvr19</dc:creator>
      <dc:date>2025-01-14T19:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708867#M57988</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/275198"&gt;@rohithvr19&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your script won't work on my machine so I have created a sample script which returns simple "Hello world" text on click of dashboard button. You just create a similar configuration and Python file as per your requirement.&amp;nbsp; Below is the code and file/folder structure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-01-15 at 5.24.42 PM.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/34141i78AF58100FCB9D7B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2025-01-15 at 5.24.42 PM.png" alt="Screenshot 2025-01-15 at 5.24.42 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;hello_world.py&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import splunk, sys
from json import dumps

class HelloWorld(splunk.rest.BaseRestHandler):
    '''
    Class for service custom endpoint.
    '''

    def handle_POST(self):
        '''
        This endpoint handler
        :return: None
        '''
        payload = {
                "text": "Hello world!"
        }
        response = dumps({"data": payload, "status": "OK", "error": "None"})
        self.response.setHeader('content-type', 'application/json')
        self.response.write(response)

    #handle verbs, otherwise Splunk will throw an error
    handle_GET = handle_POST
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;restmap.conf&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;[script:my_custom_endpoint]
match = /my_custom_endpoint
handler = hello_world.HelloWorld&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;web.conf&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;[expose:my_custom_endpoint]
pattern = my_custom_endpoint
methods = GET, POST&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;XML&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;dashboard script="fetch_data.js" version="1.1"&amp;gt;
    &amp;lt;label&amp;gt;My Dashboard&amp;lt;/label&amp;gt;
    &amp;lt;description&amp;gt;Dynamic Result Example&amp;lt;/description&amp;gt;
    &amp;lt;row&amp;gt;
        &amp;lt;panel&amp;gt;
            &amp;lt;html&amp;gt;
                &amp;lt;div&amp;gt;
                    &amp;lt;button id="fetch-data-button"&amp;gt;Fetch Data&amp;lt;/button&amp;gt;
                    &amp;lt;div id="div_result" style="margin-top: 10px; border: 1px solid #ccc; padding: 10px;"&amp;gt;Result will be displayed here.&amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/html&amp;gt;
        &amp;lt;/panel&amp;gt;
    &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;fetch_data.js&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function($, mvc) {
    $('#fetch-data-button').on('click', function() {
        var service = mvc.createService();
        service.post('/services/my_custom_endpoint', {}, function (err, response) {
            console.log(response);
            console.log(response.data);
            console.log(response.data.data);
            console.log(response.data.data.text);
            $('#div_result').html(response.data.data.text);
        });
        return false;
    });
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Screenshot&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-01-15 at 5.30.12 PM.png" style="width: 999px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/34142iD13772870494D573/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-01-15 at 5.30.12 PM.png" alt="Screenshot 2025-01-15 at 5.30.12 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code to learn and understand the custom endpoint and develop a new endpoint as per your needs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this will help you.&lt;/P&gt;&lt;P&gt;Thanks&lt;BR /&gt;KV&lt;BR /&gt;An upvote would be appreciated if any of my replies help you solve the problem or gain knowledge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 12:00:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708867#M57988</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2025-01-15T12:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708907#M57990</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/127939"&gt;@kamlesh_vaghela&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"I followed your previous instructions but encountered an error in my console, which is consistent with the issue in my primary use case. I suspect the problem lies in the placement of my JavaScript file.&lt;/P&gt;&lt;P&gt;Currently, the directory structure is as follows:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Python script: /opt/splunk/etc/apps/search/bin&lt;/LI&gt;&lt;LI&gt;JavaScript file: /opt/splunk/etc/apps/search/appserver/static&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Could you please help me identify if this directory setup might be causing the issue?"&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 17:55:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708907#M57990</guid>
      <dc:creator>rohithvr19</dc:creator>
      <dc:date>2025-01-15T17:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708937#M57992</link>
      <description>&lt;P class="lia-indent-padding-left-30px"&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/275198"&gt;@rohithvr19&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;It looks like there is some error in the endpoint.&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;Can you please check logs in "splunk/var/log/splunk/python.log"?&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;Sharing my sample &lt;A href="https://drive.google.com/file/d/1dHiJtkLhb8mHzBSUJ5BrCyDtigP4LkP4/view?usp=drive_link" target="_blank" rel="noopener"&gt;code&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;KV&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 03:42:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708937#M57992</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2025-01-16T03:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708943#M57993</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/127939"&gt;@kamlesh_vaghela&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did not observe any errors in the python.log file, but I noticed errors in the splunkd.log file.&lt;/P&gt;&lt;P&gt;Here is the relevant log entry:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;01-16-2025 12:01:24.958 +0530 ERROR ScriptRunner [40857 TcpChannelThread] - stderr from '/opt/splunk/bin/python3.9 /opt/splunk/bin/runScript.py zabbix_handler.Zabbix_handler': Traceback (most recent call last):&lt;BR /&gt;01-16-2025 12:01:24.958 +0530 ERROR ScriptRunner [40857 TcpChannelThread] - stderr from '/opt/splunk/bin/python3.9 /opt/splunk/bin/runScript.py zabbix_handler.Zabbix_handler': File "/opt/splunk/bin/runScript.py", line 72, in &amp;lt;module&amp;gt;&lt;BR /&gt;01-16-2025 12:01:24.958 +0530 ERROR ScriptRunner [40857 TcpChannelThread] - stderr from '/opt/splunk/bin/python3.9 /opt/splunk/bin/runScript.py zabbix_handler.Zabbix_handler': os.chdir(scriptDir)&lt;BR /&gt;01-16-2025 12:01:24.958 +0530 ERROR ScriptRunner [40857 TcpChannelThread] - stderr from '/opt/splunk/bin/python3.9 /opt/splunk/bin/runScript.py zabbix_handler.Zabbix_handler': FileNotFoundError: [Errno 2] No such file or directory: ''&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;This error occurs because the scriptDir variable is empty or invalid, which leads to the os.chdir(scriptDir) function attempting to change to a directory that does not exist.&lt;/P&gt;&lt;P&gt;Could you assist in identifying why the scriptDir value might be undefined or improperly set in this context?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 06:35:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708943#M57993</guid>
      <dc:creator>rohithvr19</dc:creator>
      <dc:date>2025-01-16T06:35:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708977#M57997</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/275198"&gt;@rohithvr19&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The python file should be in bin folder of your app.&lt;/P&gt;&lt;P&gt;Can you please confirm whether the individual script is working fine?&lt;/P&gt;&lt;P&gt;Have you tried my shared app on your local machine? If my app is working fine then try to add your Python code to this app.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;KV&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 12:07:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/708977#M57997</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2025-01-16T12:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to Execute a Python Script via a Button and Display the Results in a Splunk Dashboard?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/757726#M59372</link>
      <description>&lt;P&gt;Hi, I tried your solution and it works — in the dashboard I get "Hello world!" Now I wanted to know if it's possible to pass a JSON from JavaScript to Python. Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jan 2026 16:59:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-Execute-a-Python-Script-via-a-Button-and-Display-the/m-p/757726#M59372</guid>
      <dc:creator>simo</dc:creator>
      <dc:date>2026-01-28T16:59:10Z</dc:date>
    </item>
  </channel>
</rss>

