<?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: Lookup Script doesn't return search results in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107934#M28086</link>
    <description>&lt;P&gt;I'm only read the script diagonally, but it looks like it maps ID to Name, but not Name to ID.  Splunk maps in reverse in order to build an efficient search, and then maps forward in order to decorate / enrich the events.&lt;/P&gt;

&lt;P&gt;Therefore the script should be able to handle the case where it receives names and emit completed table entries with both the ID and the name. If this is not possible, you may get what you want by emitting an asterisk as the ID.&lt;/P&gt;

&lt;P&gt;As for your test method, I would recommend &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Open a command prompt&lt;/LI&gt;
&lt;LI&gt;From the command prompt, run &lt;CODE&gt;splunk cmd python namelookup.py 123 &lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;You could also try this from any python, such as one downloaded yourself from the internets.&lt;/P&gt;

&lt;P&gt;As a side note, you may want to configure the csv reader to handle large data sizes if you could imagine that you might ever receive malformed data.&lt;/P&gt;</description>
    <pubDate>Wed, 22 Dec 2010 10:00:08 GMT</pubDate>
    <dc:creator>jrodman</dc:creator>
    <dc:date>2010-12-22T10:00:08Z</dc:date>
    <item>
      <title>Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107933#M28085</link>
      <description>&lt;P&gt;Below is the props.conf at $SPLUNK_HOME/etc/system/local:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    [SPLUNK_SERVICE_Log]
lookup_table = namelookup Id OUTPUT Name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Below is the transforms.conf at $SPLUNK_HOME/etc/system/local:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[namelookup]
external_cmd = namelookup.py Id Name
external_type = python
fields_list = Id, Name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Script location : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$SPLUNK_HOME/etc/system/bin/namelookup.py


# File namelookup.py
# ------------------------------
import os,csv
#import pyodbc
import sys
import logging
import logging.config
def main():
      #if len(sys.argv) != 3:
        #print "Usage: python name_lookup.py [id field] [name field]"
        #sys.exit(0)
       logging.config.fileConfig("logging.conf")
       # create logger
       logger = logging.getLogger("namelookup")
         # "application" code
       logger.debug("====Inside Main=====")
       idf = sys.argv[1]
       namef = sys.argv[2]
       r = csv.reader(sys.stdin)
       w = None
       header = []
       first = True
       d1 = {}
      # Add items
       d1["006981166"] = "John"
       d1["007094117"] = "Mike"
       d1["007094118"] = "Scott"
       for line in r:
        if first:
            header = line
            print "Header:", header
            if idf not in header or namef not in header:
                print "Id and Name fields must exist in CSV data"
                sys.exit(0)
            csv.writer(sys.stdout).writerow(header)
            w = csv.DictWriter(sys.stdout, header)
            first = False
            continue

        # Read the result
        result = {}
        i = 0
        while i &amp;lt; len(header):
            if i &amp;lt; len(line):
                result[header[i]] = line[i]
            else:
                result[header[i]] = ''
            i += 1

        # Perform the lookup 
        if len(result[idf]) and len(result[namef]) :
            w.writerow(result)

        elif len(result[idf]):
        result[namef] = lookup(result[idf], d1)
            if len(result[namef]):
                w.writerow(result)




# Given a Id, find its Name
def lookup(id, d1):
     try:        
         for key in d1.keys():
        if key == id:
            #print "Value=", d1[key]
            return d1[key]       
     except:
        return []

main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, when I run the below search, It doesn't return any search results under name&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="Test_Log.txt" | xmlkv entry | lookup namelookup  Id OUTPUT Name | table Id, name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please let me know where i am going wrong in the script or where exactly is the script failing. Is their a way to debug the script using Komodo Edit IDE . I want debugger to launch the moment you hit enter in the Splunk Web Interface because i am not even sure the script is invoked by Splunk. So i would like to see atleast the first print statement in the script is printed onto console.&lt;/P&gt;

&lt;P&gt;When i tried to run as standlone program using the command&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;splunk cmd namelookup.py 123
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;it opens a command prompt and immediately closes it. So not sure whats going on with this script &lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2010 04:59:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107933#M28085</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2010-12-22T04:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107934#M28086</link>
      <description>&lt;P&gt;I'm only read the script diagonally, but it looks like it maps ID to Name, but not Name to ID.  Splunk maps in reverse in order to build an efficient search, and then maps forward in order to decorate / enrich the events.&lt;/P&gt;

&lt;P&gt;Therefore the script should be able to handle the case where it receives names and emit completed table entries with both the ID and the name. If this is not possible, you may get what you want by emitting an asterisk as the ID.&lt;/P&gt;

&lt;P&gt;As for your test method, I would recommend &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Open a command prompt&lt;/LI&gt;
&lt;LI&gt;From the command prompt, run &lt;CODE&gt;splunk cmd python namelookup.py 123 &lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;You could also try this from any python, such as one downloaded yourself from the internets.&lt;/P&gt;

&lt;P&gt;As a side note, you may want to configure the csv reader to handle large data sizes if you could imagine that you might ever receive malformed data.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2010 10:00:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107934#M28086</guid>
      <dc:creator>jrodman</dc:creator>
      <dc:date>2010-12-22T10:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107935#M28087</link>
      <description>&lt;P&gt;The purpose of lookup script in my case is to given an Id, connect to database and retrieve Name by Id. So i am not sure what you are suggesting. Could you please elaborate. Please note we don't have Name value initially available to us infact we are retriving it from database by passing Id value as an argument in "SELECT" clause. Anyhow that script is far from working so i hard-coded the values in a dictionary with keys as Id and Values as Name as a prrof-of-concept that Splunk will be able to call the lookup script namelookp.py&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2010 02:54:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107935#M28087</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2010-12-23T02:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107936#M28088</link>
      <description>&lt;P&gt;i modified the script to run as standalone program by commenting lines : csv.writer(sys.stdout).writerow(header)&lt;BR /&gt;&lt;BR /&gt;
         w = csv.DictWriter(sys.stdout, header) &lt;BR /&gt;
So the question boils down to how to make it work with Splunk or how to make it write to CSV file&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2010 04:19:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107936#M28088</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2010-12-23T04:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107937#M28089</link>
      <description>&lt;P&gt;Please note the purpose of lookup script in my case is to retrieve Name from database for a given Id in Splunk Search query. i.e.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.....| lookup namelookup  Id OUTPUT Name 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please note i modelled the lookup script based on external_lookup.py shipped with installation or &lt;A href="http://blogs.splunk.com/2009/09/14/enriching-data-with-db-lookups-part-2/" rel="nofollow"&gt;http://blogs.splunk.com/2009/09/14/enriching-data-with-db-lookups-part-2/&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;These scripts take "&lt;CODE&gt;CSV input from Splunk via standard input&lt;/CODE&gt;" 
This doesnt seems to be working in my case. So is their a way to debug the lookup script to make sure CSV input from Splunk is really supplied to lookup script via stdin. &lt;/P&gt;

&lt;P&gt;To prove my point, I modified the lookup script by commenting following lines and It runs perfectly fine as standalone program &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#namef = sys.argv[2]  // This doesnt really make sense.
   #r = csv.reader(sys.stdin)
   #w = None
   #header = []
   #first = True

#csv.writer(sys.stdout).writerow(header) 
#w = csv.DictWriter(sys.stdout, header) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the question boils down to how to make the script &lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;take "&lt;CODE&gt;CSV input from Splunk via standard input&lt;/CODE&gt;" &lt;/LI&gt;
&lt;LI&gt;produce Splunk Search Results with Name value from database. Please note i don't want  CSV standard output.&lt;/LI&gt;
&lt;LI&gt;mostly importantly how to launch debugger &lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Any pointers/suggestions to make the script working will be greatly appreciated&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2010 23:33:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107937#M28089</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2010-12-23T23:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107938#M28090</link>
      <description>&lt;P&gt;Hello Bansi.&lt;/P&gt;

&lt;P&gt;In agreement with jrodman, I believe that at this point it is important to focus on the validation of the external lookup script at a low level, from the Splunk command line.&lt;/P&gt;

&lt;P&gt;To better understand the constraints that an external lookup script must respond to, I would to recommend a careful read of this section of our documentation :&lt;/P&gt;

&lt;P&gt;&lt;A href="http://www.splunk.com/base/Documentation/4.1.6/Knowledge/Addfieldsfromexternaldatasources#More_about_the_external_lookup_script" rel="nofollow"&gt;http://www.splunk.com/base/Documentation/4.1.6/Knowledge/Addfieldsfromexternaldatasources#More_about_the_external_lookup_script&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;As an example, I will demonstrate here how the host/ip external lookup script that ships with Splunk (external_lookup.py) can be validated in that way :&lt;/P&gt;

&lt;H3&gt;1 - Create a partially empty input CSV file that simulates the input that will be passed to the lookup script by splunk-search for resolution :&lt;/H3&gt;

&lt;P&gt;This file should contain a header listing the fields we want to work with. One of these should be the "input" field (in our example, "host") and the other should be the "output" field (in our example, "ip") that are passed on as argument to the script.&lt;/P&gt;

&lt;P&gt;Here's what my input file "input.csv" looks like :&lt;/P&gt;

&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;host,ip
&lt;A href="https://community.splunk.com/www.hardware.fr" target="test_blank"&gt;www.hardware.fr&lt;/A&gt;,
&lt;A href="https://community.splunk.com/www.bash.org" target="test_blank"&gt;www.bash.org&lt;/A&gt;,
&lt;A href="https://community.splunk.com/www.somafm.com" target="test_blank"&gt;www.somafm.com&lt;/A&gt;,
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;Note that only the "host" column is populated here. When I feed this file as input to external_lookup.py while specifying that it should look at the "host" and "ip" fields as they are defined in the CSV header, the script will use external DNS resolution to fill in the blanks.&lt;/P&gt;

&lt;H3&gt;2 - Call the lookup script from the command line, feeding the CSV input file as stdin and specifying the fields to consider within that file as arguments :&lt;/H3&gt;

&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;# $SPLUNK_HOME/bin/splunk cmd python $SPLUNK_HOME/etc/system/bin/external_lookup.py host ip &amp;lt; input.csv
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;H3&gt;3 - Check that the results displayed on stdout are as desired :&lt;/H3&gt;

&lt;P&gt;In our case, we are getting the expected result : The lookup script shows on stdout a now complete CSV file. The "ip" field has been populated for each line by performing DNS resolution on the value of the "host" field for that line :&lt;/P&gt;

&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;host,ip
&lt;A href="https://community.splunk.com/www.hardware.fr,83.243.20.80" target="test_blank"&gt;www.hardware.fr,83.243.20.80&lt;/A&gt;
&lt;A href="https://community.splunk.com/www.bash.org,69.61.106.93" target="test_blank"&gt;www.bash.org,69.61.106.93&lt;/A&gt;
&lt;A href="https://community.splunk.com/www.somafm.com,64.147.167.20" target="test_blank"&gt;www.somafm.com,64.147.167.20&lt;/A&gt;
&lt;/CODE&gt;&lt;/PRE&gt;  &lt;/P&gt;

&lt;P&gt;In the context of search, the splunk-search process would use that CSV output to enrich events by adding an "ip" field and populating it with the values generated.&lt;/P&gt;

&lt;P&gt;My recommendation to you is to make sure that your own scripted lookup behaves in this manner when tested and can operate with the same arguments/inputs. I also think that it would be a good idea to make your lookup bi-directional and able to look up a name given an ID.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Dec 2010 07:24:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107938#M28090</guid>
      <dc:creator>hexx</dc:creator>
      <dc:date>2010-12-24T07:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107939#M28091</link>
      <description>&lt;P&gt;I don't know how to use komodo. A lookup script is typically invoked multiple times and is extremely short lived. If you know a good recipe to attach a debugger to a very short lived process, go for it. Personally I find this approach slow and cumbersome. Open a logfile -- lf = open(logfile, "a", 1 ) -- at the beginning of the script and log variable states and debug lines to it lf.write(), using such things as repr(), str(), the pprint module, and other things produces repeatable rapid results across tests and modifications.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Dec 2010 07:03:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107939#M28091</guid>
      <dc:creator>jrodman</dc:creator>
      <dc:date>2010-12-28T07:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107940#M28092</link>
      <description>&lt;P&gt;Sorry, I assumed this part worked.  It looks like your script is currently configured to produce debug output (print "Header:"...) Did you start with a specific example?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Dec 2010 08:39:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107940#M28092</guid>
      <dc:creator>jrodman</dc:creator>
      <dc:date>2010-12-28T08:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107941#M28093</link>
      <description>&lt;P&gt;More clearly, your print statement is a bug.  Please test your script externally, and validate that the output is csv only.  Once you are certain the script is producing valid csv, if the lookup is still not working you may wish to engage splunk support.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Dec 2010 08:44:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107941#M28093</guid>
      <dc:creator>jrodman</dc:creator>
      <dc:date>2010-12-28T08:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107942#M28094</link>
      <description>&lt;P&gt;Thanks for wonderful explaination. I did exactly as per your recommendation and my scripted lookup behaves in the same manner as yours i.e. works fine&lt;/P&gt;

&lt;P&gt;However, when I run the below search, It doesn't return any search results under name&lt;/P&gt;

&lt;P&gt;source="Test_Log.txt" | xmlkv entry | lookup namelookup  Id OUTPUT Name | table Id, name&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2011 04:43:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107942#M28094</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-20T04:43:26Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107943#M28095</link>
      <description>&lt;P&gt;Thanks for wonderful explaination hexx. &lt;/P&gt;

&lt;P&gt;I did exactly as per your recommendation and my scripted lookup behaves in the same manner as yours i.e. the results displayed on stdout are as desired &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;C:\Splunk\etc\system\bin&amp;gt;db_lookup.py memberId memberName &amp;lt; memberInput.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;produces following output retrieving memberName from database&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;memberId,memberName
006,RANDY
007,LEONY
009,RANDOLPH
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, when I invoke the scripted lookup from splunk search as shown below , It doesn't return any results under memberName column&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="Test_Log.txt" | xmlkv entry | lookup namelookup  memberId OUTPUT memberName | table memberId, memberName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please let me know what am I missing?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2011 04:52:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107943#M28095</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-20T04:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107944#M28096</link>
      <description>&lt;P&gt;jrodman, Thanks for suggestion. Would you mind providing an example code snippet on how to perfor the Logging you suggested i.e. Open a logfile -- lf = open(logfile, "a", 1 ) -- at the beginning of the script and log variable states and debug lines to it lf.write(), using such things as repr(), str(), the pprint module&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2011 02:29:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107944#M28096</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-26T02:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup Script doesn't return search results</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107945#M28097</link>
      <description>&lt;P&gt;any update on this?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2013 16:29:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Lookup-Script-doesn-t-return-search-results/m-p/107945#M28097</guid>
      <dc:creator>stefano_guidoba</dc:creator>
      <dc:date>2013-10-10T16:29:26Z</dc:date>
    </item>
  </channel>
</rss>

