<?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 we can run splunk python sdk code in splunk as scripted input? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411750#M7151</link>
    <description>&lt;P&gt;@ajitshukla61116 , what is the condition for reading index A and pushing to index B? based on the description seems like this should be a Scheduled Search with to calculate distance between geolocation &lt;CODE&gt;Alert Action&lt;/CODE&gt;, then use &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Collect"&gt;collect&lt;/A&gt; command to insert to a new index. (With option 1 below this all can be done within splunk using SPL, without any need of python coding).&lt;/P&gt;

&lt;P&gt;However, you can try one of the following options. If option 1 does not meet your requirement you can create custom command as per option 2.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 1&lt;/STRONG&gt;: can you refer to the following answer post by @MuS which deals with Distance between two geo-locations: &lt;A href="https://answers.splunk.com/answers/317935/calculating-distances-between-points-with-geoip-us.html"&gt;https://answers.splunk.com/answers/317935/calculating-distances-between-points-with-geoip-us.html&lt;/A&gt;&lt;BR /&gt;
This can be done directly via SPL.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 2&lt;/STRONG&gt;: If this does not help you can read the Custom Command Splunk Dev Example and create your own custom command on similar lines: &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEU2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 3&lt;/STRONG&gt;:  You can use the script command to execute script stored in bin folder of the app. &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 4&lt;/STRONG&gt;: Custom Alert Action to calculate geolocation and collect to index: &lt;A href="https://login.splunk.com/index.php?module=roles&amp;amp;func=showloginform&amp;amp;redirecturl=https://docs.splunk.com/Documentation/Splunk/ltest/AdvancedDev/ModAlertsIntro"&gt;https://login.splunk.com/index.php?module=roles&amp;amp;func=showloginform&amp;amp;redirecturl=https://docs.splunk.com/Documentation/Splunk/ltest/AdvancedDev/ModAlertsIntro&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Apr 2019 03:25:36 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2019-04-22T03:25:36Z</dc:date>
    <item>
      <title>How we can run splunk python sdk code in splunk as scripted input?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411749#M7150</link>
      <description>&lt;P&gt;In this case ,I have Index "A" with Two fields "Latitude" and "Longitude" ,now I want my python script fetch these two fields from Index"A" and calculated distance (using internal logic) and then indexed this distance field into Index "B".&lt;/P&gt;

&lt;P&gt;For this case I have used splunk python sdk to get indexed data ,now I  want this script will run by splunk as scripted input to ingest data in index "B" .&lt;/P&gt;

&lt;P&gt;Please help me out do complete this task. &lt;/P&gt;</description>
      <pubDate>Fri, 19 Apr 2019 10:55:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411749#M7150</guid>
      <dc:creator>ajitshukla61116</dc:creator>
      <dc:date>2019-04-19T10:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: How we can run splunk python sdk code in splunk as scripted input?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411750#M7151</link>
      <description>&lt;P&gt;@ajitshukla61116 , what is the condition for reading index A and pushing to index B? based on the description seems like this should be a Scheduled Search with to calculate distance between geolocation &lt;CODE&gt;Alert Action&lt;/CODE&gt;, then use &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Collect"&gt;collect&lt;/A&gt; command to insert to a new index. (With option 1 below this all can be done within splunk using SPL, without any need of python coding).&lt;/P&gt;

&lt;P&gt;However, you can try one of the following options. If option 1 does not meet your requirement you can create custom command as per option 2.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 1&lt;/STRONG&gt;: can you refer to the following answer post by @MuS which deals with Distance between two geo-locations: &lt;A href="https://answers.splunk.com/answers/317935/calculating-distances-between-points-with-geoip-us.html"&gt;https://answers.splunk.com/answers/317935/calculating-distances-between-points-with-geoip-us.html&lt;/A&gt;&lt;BR /&gt;
This can be done directly via SPL.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 2&lt;/STRONG&gt;: If this does not help you can read the Custom Command Splunk Dev Example and create your own custom command on similar lines: &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEU2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 3&lt;/STRONG&gt;:  You can use the script command to execute script stored in bin folder of the app. &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 4&lt;/STRONG&gt;: Custom Alert Action to calculate geolocation and collect to index: &lt;A href="https://login.splunk.com/index.php?module=roles&amp;amp;func=showloginform&amp;amp;redirecturl=https://docs.splunk.com/Documentation/Splunk/ltest/AdvancedDev/ModAlertsIntro"&gt;https://login.splunk.com/index.php?module=roles&amp;amp;func=showloginform&amp;amp;redirecturl=https://docs.splunk.com/Documentation/Splunk/ltest/AdvancedDev/ModAlertsIntro&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 03:25:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411750#M7151</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-04-22T03:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: How we can run splunk python sdk code in splunk as scripted input?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411751#M7152</link>
      <description>&lt;P&gt;thanks @niketnilay  for these options.&lt;BR /&gt;
Actually I have one index which have multiple fields like latitude,Longitude ,"FieldA","FieldB","FieldC".Using these fields I have to calculate "distance" and again ingest "distance" filed into same index for every events.&lt;BR /&gt;
For distance calculation I have to use python script because distance calculation logic can not be implemented on splunk search.&lt;/P&gt;

&lt;P&gt;Note:in this case I will get already ingested data .I have calculate distance and add distance filed to same index.&lt;/P&gt;

&lt;P&gt;I am trying custom command but I am not able to fetch more than two variable from splunk side .I don't know why I am getting error code 1.&lt;BR /&gt;
please check my code :&lt;BR /&gt;
    import sys&lt;BR /&gt;
    import splunk.Intersplunk &lt;BR /&gt;
    import json&lt;BR /&gt;
    import requests as req&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def calculate_distance(lat,long,fieldA,fieldB,fieldC):

   #internal logic will be here


return distance

def collect_events():
results, dummyresults, settings = splunk.Intersplunk.getOrganizedResults()
for result in results:
lat=result["Latitude"]
long=result["Longitude"]
fieldA=result["FieldA"]
fieldB=result["FieldB"]
fieldC=result["FieldC"]
result[distance]=calculate_distance(lat,long,fieldA,fieldB,fieldC)
splunk.Intersplunk.outputResults(results) 
collect_events()
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Apr 2019 10:41:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-we-can-run-splunk-python-sdk-code-in-splunk-as-scripted/m-p/411751#M7152</guid>
      <dc:creator>ajitshukla61116</dc:creator>
      <dc:date>2019-04-25T10:41:26Z</dc:date>
    </item>
  </channel>
</rss>

