<?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 create a custom generating search command? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638434#M221269</link>
    <description>&lt;P&gt;Check the splunk dev portal. Its has some examples on custom commands.&lt;/P&gt;&lt;P&gt;&lt;A href="https://dev.splunk.com/enterprise/docs/devtools/customsearchcommands/" target="_blank"&gt;https://dev.splunk.com/enterprise/docs/devtools/customsearchcommands/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Also Splunk's git has some other examples for custom commands here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/splunk/splunk-app-examples/blob/master/custom_search_commands/python/generatingsearchcommands_app/bin/generatingcsc.py" target="_blank"&gt;https://github.com/splunk/splunk-app-examples/blob/master/custom_search_commands/python/generatingsearchcommands_app/bin/generatingcsc.py&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In this example you can replace the count with a path to do something along the lines of:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| mycustomcommand path="D:/Temp/temp.txt"&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 03 Apr 2023 18:03:05 GMT</pubDate>
    <dc:creator>diogofgm</dc:creator>
    <dc:date>2023-04-03T18:03:05Z</dc:date>
    <item>
      <title>How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638394#M221254</link>
      <description>&lt;P&gt;So I have a python script called Analysis.py&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;And normally I would run it locally like this Analysis.py &amp;lt;filepath&amp;gt;, &amp;nbsp;so as an example Analysis.py D:/Temp/temp.txt&lt;BR /&gt;&lt;BR /&gt;And what this python script does is it generates a csv file.&lt;BR /&gt;&lt;BR /&gt;What I would like to do is a dashboard in splunk which does visualization on this csv file, eg. like a line chart or some bar graphs. However this python script runs with a &amp;lt;filepath&amp;gt; argument.&lt;BR /&gt;And also, this dashboard would accept a custom input but the user who will input the &amp;lt;filepath&amp;gt; argument and the dashboard will show the results accordingly, visualized in a line chart for example.&lt;BR /&gt;&lt;BR /&gt;How can I write a splunk custom search command such that I can create this dashboard&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 14:25:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638394#M221254</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-04-03T14:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638434#M221269</link>
      <description>&lt;P&gt;Check the splunk dev portal. Its has some examples on custom commands.&lt;/P&gt;&lt;P&gt;&lt;A href="https://dev.splunk.com/enterprise/docs/devtools/customsearchcommands/" target="_blank"&gt;https://dev.splunk.com/enterprise/docs/devtools/customsearchcommands/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Also Splunk's git has some other examples for custom commands here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/splunk/splunk-app-examples/blob/master/custom_search_commands/python/generatingsearchcommands_app/bin/generatingcsc.py" target="_blank"&gt;https://github.com/splunk/splunk-app-examples/blob/master/custom_search_commands/python/generatingsearchcommands_app/bin/generatingcsc.py&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In this example you can replace the count with a path to do something along the lines of:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| mycustomcommand path="D:/Temp/temp.txt"&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 03 Apr 2023 18:03:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638434#M221269</guid>
      <dc:creator>diogofgm</dc:creator>
      <dc:date>2023-04-03T18:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638851#M221357</link>
      <description>&lt;P&gt;Yes it helps but when I have that path=...&lt;BR /&gt;&lt;BR /&gt;I want it to be a user input in the dashboard.&lt;BR /&gt;&lt;BR /&gt;How can I Do so?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 14:44:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638851#M221357</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-04-05T14:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638926#M221393</link>
      <description>&lt;P&gt;Just place a text input in the dashboard and use normal dashboard tokens. Then you can make your search something like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| mycustomcommand path=$path_token$&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 00:37:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638926#M221393</guid>
      <dc:creator>diogofgm</dc:creator>
      <dc:date>2023-04-06T00:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638929#M221395</link>
      <description>&lt;P&gt;There are a couple of easy ways to do this (and the "right" way which others have already posted).&lt;BR /&gt;&lt;BR /&gt;The simplest way is to just write the output file on your search head to "$SPLUNK_HOME/etc/apps/&amp;lt;your app here&amp;gt;/lookups" and the reference it there.&lt;/P&gt;&lt;P&gt;Another more complex way is from my (unaccepted) answer here, quoted verbatim:&lt;BR /&gt;&lt;A href="https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html" rel="nofollow noopener noreferrer" target="_blank"&gt;https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This isn't the "right" way to do this, but it is the most expedient and it allows you to recycle your traditional alert script for use in regular searches, too. This example assumes that you will be passing 2 fields to the scirpt: scr_ip and host; you will need to adjust slightly for the fields that you will be using.&lt;/P&gt;&lt;P&gt;Insert this code to your MyAlertScript.py code right before your existing code that accesses the results.csv.gz file in argv[8]:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;# In order to facilitate Event Workflow Actions using runshellscript,&lt;BR /&gt;# we will hijack the arguments in one special case as follows:&lt;BR /&gt;# 1(sys.argv[1]) = '1'&lt;BR /&gt;# 2(sys.argv[2]) = '"&amp;lt;src_ip&amp;gt;","&amp;lt;host&amp;gt;"'&lt;BR /&gt;# 3(sys.argv[3]) = 'Hack'&lt;BR /&gt;# 4(sys.argv[4]) = 'to'&lt;BR /&gt;# 5(sys.argv[5]) = 'run'&lt;BR /&gt;# 7(sys.argv[6]) = 'from'&lt;BR /&gt;# 6(sys.argv[7]) = 'runshellscript'&lt;BR /&gt;# 8(sys.argv[8]) = * &amp;lt;- DO NOT CHECK because splunk modifieds this on the way in&lt;BR /&gt;# If in this format we will pull the data directly out of the 2nd argument,&lt;BR /&gt;# instead of out of the restults file.&lt;BR /&gt;specialCase = False&lt;BR /&gt;# initialize to FALSE&lt;BR /&gt;if ((sys.argv[1] == '1') and (sys.argv[3] == 'Hack') and (sys.argv[4] == 'to') and (sys.argv[5] == 'run') and (sys.argv[6] == 'from') and (sys.argv[7] == 'runshellscript')): print "SPECIAL CASE!\n"&lt;BR /&gt;# Special case!&lt;BR /&gt;specialCase = True&lt;BR /&gt;# make sure we delete this file at the end!&lt;BR /&gt;fnz = workdir + 'results.csv.gz' with gzip.open(fnz, 'wb') as OFH: OFH.write('src_ip,host\n') OFH.write(sys.argv[2]) OFH.close() sys.argv[8] = fnz #print "SPECIAL CASE: ARGV8=&amp;lt;" + sys.argv[8] + "&amp;gt; fnz=&amp;lt;" + fnz + "&amp;gt;\n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Then at the bottom, add this, too:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if (specialCase): os.remove(sys.argv[8]) # delete fake zip file we made&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Next you need a macro like this (to abstract away the trickery):&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[MyScript] definition = table src_ip host\ | map maxsearches=5000 search="|runshellscript MyAlertScript.py 1 \"\\\"\\\"$src_ip$\\\",\\\"$host$\\\"\\\"\" Hack to run from runshellscript 8" iseval = 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Now, to use it, you just do this:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My Search To Get Events With src_ip And host Here | `MyScript`&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;SPECIAL WARNING! This will not scale nicely if you pass a large number of results to the script because of the map command but it works GREAT for small numbers of events.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 01:01:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638929#M221395</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2023-04-06T01:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638930#M221396</link>
      <description>&lt;P&gt;ahh I see thanks guys,&lt;BR /&gt;&lt;BR /&gt;For my custom generating search command would this work?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration, Option, validators

@Configuration()
class %(command.title())Command(GeneratingCommand):
    filename = Option(require=True)

    def generate(self):
       filename = self.filename
       # Put your event  code here

       # To connect with Splunk, use the instantiated service object which is created using the server-uri and
       # other meta details and can be accessed as shown below
       # Example:-
       #    service = self.service

       pass

dispatch(%(command.title())Command, sys.argv, sys.stdin, sys.stdout, __name__)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;After I have this then I would put the python script which needs the file path argument in commands.conf&lt;BR /&gt;&lt;BR /&gt;And then run the search command using&lt;BR /&gt;| mycustomsearchgeneratingcommand filename="D:\Temp\Temp.txt"&lt;BR /&gt;&lt;BR /&gt;And if that generates the csv file output then I will test it with user input, creating a field in the dashboard&lt;/P&gt;&lt;P&gt;and using as suggested by you guys&lt;BR /&gt;| mycustomsearchgeneratingcommand filename=$field_token$&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 01:22:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/638930#M221396</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-04-06T01:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom generating search command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/641749#M222305</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/1406"&gt;@woodcock&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/90723"&gt;@diogofgm&lt;/a&gt;&amp;nbsp;possible to elab?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 12:08:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-generating-search-command/m-p/641749#M222305</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-04-28T12:08:39Z</dc:date>
    </item>
  </channel>
</rss>

