<?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: I want sample code to connect to Oracle database, and lookup a table at search time in All Apps and Add-ons</title>
    <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13605#M83</link>
    <description>&lt;P&gt;You can query oracle database directly from python using &lt;A href="http://cx-oracle.sourceforge.net/" rel="nofollow"&gt;cx_Oracle&lt;/A&gt;.  (You will want the 5.0.3 or higher since you are using an 11g database).  This does require that you have a working oracle client environment setup, or you can use the &lt;A href="http://www.oracle.com/technology/tech/oci/instantclient/index.html" rel="nofollow"&gt;Oracle Database Instant Client&lt;/A&gt; if you don't already have a client installed.&lt;/P&gt;

&lt;P&gt;It's not too hard to get this setup and working but there are some potential gotchas in the process as well.  For example, using &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; (or any other python database module) requires that you somehow install that module in a way that is accessible from splunk's python environment.  And you also have to make sure that the splunk environment can access your oracle libraries, which is normally handled via environmental variables.  For those familiar with python, there are a number of ways to accomplish this, but so far I don't know splunk preferred method, and so there could be issues with some install approaches that could work, but may end up breaking during a splunk upgrade.  (If anyone has some recommendation on this, please point us in the right direction on this.)&lt;/P&gt;

&lt;P&gt;I had to build the &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; library myself on my Ubuntu 8.04 server, which wasn't too bad.  The one gotcha I hit was that Splunk's embedded python install was lacking the full &lt;CODE&gt;distutils&lt;/CODE&gt;, which are needed before you can build python C modules.  So I had to manually add in some missing modules components before I could get the &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; module to build with splunk's python.  (And I couldn't build it with a separate Python 2.6 install because of a compile-time Unicode difference I encountered.  Ugh!  If you can use a pre-compiled version, you shouldn't hit this issue.)&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Also, before you jump into writing an external lookup script (not that's it all that difficult), but there may be a more simplistic approach to consider.  You should consider how often your lookup data will change.  For example, we have a simple script that pulls lookup values from a database and drops them in a CSV file.  Then we have splunk configured to use the CSV file as the actual lookup table instead of launching a database query for every lookup.  Since this particular lookup info changes very infrequently, this removes the search-time dependency on the database and a local csv file lookup is much faster than a database query.  (Our dataset is also only a few hundred records, so the CSV file is pretty small too.)  We have this lookup-generating script scheduled to run periodically.  It only refreshes the CSV file if something in the lookup table changes, which also lets us create an date-effectivity within our lookup file).  Again, this only works for pretty-static lookup info which may or may not be applicable in your scenario.  One advantage to this approach is that you can use &lt;EM&gt;any&lt;/EM&gt; tool you want, all it needs to do is create a simple CSV file, so you can use whatever is most ideal for your scenario.&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Hopefully you'll find some of this helpful.&lt;/P&gt;

&lt;P&gt;Additional resources:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.oracle.com/technology/pub/articles/devlin-python-oracle.html" rel="nofollow"&gt;Wrapping Your Brain Around Oracle + Python&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Mon, 17 May 2010 22:42:25 GMT</pubDate>
    <dc:creator>Lowell</dc:creator>
    <dc:date>2010-05-17T22:42:25Z</dc:date>
    <item>
      <title>I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13603#M81</link>
      <description>&lt;P&gt;I have seen documentation on lookups in general. I am most interested in dynamic lookups. Specifically I have the following scenario:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Splunk 4.0.10&lt;/LI&gt;
&lt;LI&gt;Oracle 11&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;how do I setup a JDBC connection to Oracle in the Splunk config files?&lt;/P&gt;

&lt;P&gt;How can I use the Oracle connection to lookup a table in Splunk at search time?&lt;/P&gt;

&lt;P&gt;Please provide some examples. Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2010 07:55:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13603#M81</guid>
      <dc:creator>nbharadwaj</dc:creator>
      <dc:date>2010-05-16T07:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13604#M82</link>
      <description>&lt;P&gt;You don't set up a JDBC connection directly in Splunk. You need to build a lookup script yourself to perform whatever lookups you want to do. There is an example lookup script (in Python) shipped with Splunk in &lt;CODE&gt;$SPLUNK_HOME/etc/system/bin/external_lookup.py&lt;/CODE&gt;, as well as documentation on it here: &lt;A href="http://www.splunk.com/base/Documentation/latest/Knowledge/Addfieldsfromexternaldatasources#Set_up_a_fields_lookup_based_on_an_external_command" rel="nofollow"&gt;http://www.splunk.com/base/Documentation/latest/Knowledge/Addfieldsfromexternaldatasources#Set_up_a_fields_lookup_based_on_an_external_command&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Lookup scripts must be written in Python, but they can simply be wrappers around a Python &lt;CODE&gt;subprocess.Popen()&lt;/CODE&gt; call to another program.&lt;/P&gt;

&lt;P&gt;Of course the Python example script is different from Java, but the basic logic is the same, and not very complex.&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2010 14:11:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13604#M82</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-05-16T14:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13605#M83</link>
      <description>&lt;P&gt;You can query oracle database directly from python using &lt;A href="http://cx-oracle.sourceforge.net/" rel="nofollow"&gt;cx_Oracle&lt;/A&gt;.  (You will want the 5.0.3 or higher since you are using an 11g database).  This does require that you have a working oracle client environment setup, or you can use the &lt;A href="http://www.oracle.com/technology/tech/oci/instantclient/index.html" rel="nofollow"&gt;Oracle Database Instant Client&lt;/A&gt; if you don't already have a client installed.&lt;/P&gt;

&lt;P&gt;It's not too hard to get this setup and working but there are some potential gotchas in the process as well.  For example, using &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; (or any other python database module) requires that you somehow install that module in a way that is accessible from splunk's python environment.  And you also have to make sure that the splunk environment can access your oracle libraries, which is normally handled via environmental variables.  For those familiar with python, there are a number of ways to accomplish this, but so far I don't know splunk preferred method, and so there could be issues with some install approaches that could work, but may end up breaking during a splunk upgrade.  (If anyone has some recommendation on this, please point us in the right direction on this.)&lt;/P&gt;

&lt;P&gt;I had to build the &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; library myself on my Ubuntu 8.04 server, which wasn't too bad.  The one gotcha I hit was that Splunk's embedded python install was lacking the full &lt;CODE&gt;distutils&lt;/CODE&gt;, which are needed before you can build python C modules.  So I had to manually add in some missing modules components before I could get the &lt;CODE&gt;cx_Oracle&lt;/CODE&gt; module to build with splunk's python.  (And I couldn't build it with a separate Python 2.6 install because of a compile-time Unicode difference I encountered.  Ugh!  If you can use a pre-compiled version, you shouldn't hit this issue.)&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Also, before you jump into writing an external lookup script (not that's it all that difficult), but there may be a more simplistic approach to consider.  You should consider how often your lookup data will change.  For example, we have a simple script that pulls lookup values from a database and drops them in a CSV file.  Then we have splunk configured to use the CSV file as the actual lookup table instead of launching a database query for every lookup.  Since this particular lookup info changes very infrequently, this removes the search-time dependency on the database and a local csv file lookup is much faster than a database query.  (Our dataset is also only a few hundred records, so the CSV file is pretty small too.)  We have this lookup-generating script scheduled to run periodically.  It only refreshes the CSV file if something in the lookup table changes, which also lets us create an date-effectivity within our lookup file).  Again, this only works for pretty-static lookup info which may or may not be applicable in your scenario.  One advantage to this approach is that you can use &lt;EM&gt;any&lt;/EM&gt; tool you want, all it needs to do is create a simple CSV file, so you can use whatever is most ideal for your scenario.&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Hopefully you'll find some of this helpful.&lt;/P&gt;

&lt;P&gt;Additional resources:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.oracle.com/technology/pub/articles/devlin-python-oracle.html" rel="nofollow"&gt;Wrapping Your Brain Around Oracle + Python&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 17 May 2010 22:42:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13605#M83</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-05-17T22:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13606#M84</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; A this point (2017), this answer should only be used as a python external (scripted) lookup example.   If you need a DB lookup, please use DB connect instead.  Maintaining python db libraries/modules within the python environment that ships with Splunk is a serious pain that should be avoided whenever possible.  Nevertheless, you may find some of the following useful.&lt;/P&gt;

&lt;P&gt;Here is a sample oracle external lookup script.  Obviously you'll have to heavily modify this script to suite your needs.  This includes all login information, the SQL query, column and field name mappings, etc.&lt;/P&gt;

&lt;P&gt;This script does a lookup on a single primary key and returns adds two new fields.  You could make the script more intelligent so that you could do reverse lookups (or lookups on any other field) but that would just make thinks more complicated and it didn't seem suitable for an example.&lt;/P&gt;

&lt;P&gt;Additional error handling would be recommended.  Right now, the only "exception" handled gracefully is when no row is found (the lookup fails because no value exist in the table.)&lt;/P&gt;

&lt;P&gt;Here is the contents of my &lt;CODE&gt;$SPLUNK_HOME/etc/apps/YourApp/bin/ora_lookup_example.py&lt;/CODE&gt; script:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import csv
import cx_Oracle

#-------------------------------------------------------------------------------
# This is for debugging the script. You should disable this once everything is
# working. Output is written to $SPLUNK_HOME/var/log/splunk/python.log, which is
# searchable in splunk with:   index=_internal source=*python.log ORALOOKUP:
# Be sure to also remove/disable 'logger.info(...)' lines when you disable this.
import logging
logger = logging.getLogger("ora_lookup_example")
#-------------------------------------------------------------------------------

FIELDS = [ "PartnerId", "CorporationName", "OrgUnitName" ]

def db_connect():
    # Build a DSN (can be substituted for a TNS name)
    dsn = cx_Oracle.makedsn("host.example.net", 1521, "SID")
    db = cx_Oracle.connect("username", "password", dsn)
    cursor = db.cursor()
    return cursor

def db_lookup(cursor, key):
    cursor.execute("""
        SELECT CORPORATIONNAME, ORGUNITNAME
        FROM PARTNER
        WHERE PARTNERID = :id
          AND STATUS = 'Active'""", dict(id=key))
    row = cursor.fetchone()
    return row

def main(input, output, cursor):
    csv_in = csv.DictReader(input)
    csv_out = csv.DictWriter(output, FIELDS)

    # Write header row
    csv_out.writerow(dict(zip(FIELDS,FIELDS)))

    for row in csv_in:
        # Get the input file we are looking up
        key = row["PartnerId"]

        # Fetch two fields we are looking up in the table
        db_row = db_lookup(cursor, key)
        if db_row:
            (corp, org) = db_row
            row["CorporationName"] = corp
            row["OrgUnitName"] = "" or org  # Use empty string if org is NULL
            logger.info("ORALOOKUP:  PartnerId=%r:   corp=%r, org=%r", key, corp, org)
        else:
            logger.info("ORALOOKUP:  PartnerId=%r:  No matching entry in table.", key)
        csv_out.writerow(row)

if __name__ == '__main__':
    import sys
    cursor = db_connect()
    main(sys.stdin, sys.stdout, cursor)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You also need to setup the external lookup script in &lt;CODE&gt;transforms.conf&lt;/CODE&gt;, like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[ora_lookup_example]
external_cmd = ora_lookup_example.py
fields_list = PartnerId,CorporationName,OrgUnitName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is optional, but if you want automatic lookups enabled for a sourcetype, you can add the following to your &lt;CODE&gt;props.conf&lt;/CODE&gt; file:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[my_source_type]
LOOKUP-oralookup = ora_lookup_example PartnerId AS RelatedPartnerId OUTPUT CorporationName OrgUnitName 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you don't setup the automatic lookups, then you can do this on a search-by-search basis (which you may find to be much better for overall performance), in which case you would do something like this:  (the syntax may look familiar)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=my_source_type ... | lookup ora_lookup_example PartnerId AS RelatedPartnerId OUTPUT CorporationName OrgUnitName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;EM&gt;Note:  In my sourcetype the &lt;CODE&gt;PartnerId&lt;/CODE&gt; value is under the field named &lt;CODE&gt;RelatedPartnerId&lt;/CODE&gt;, which is why the &lt;CODE&gt;AS&lt;/CODE&gt; is necessary for my setup&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;I tried to add helpful comments in the script.  Please make note of them.  Hope this helps you get started.&lt;/P&gt;

&lt;P&gt;BTW.  I recommend that you get started by getting your cx_Oracle install working with splunk's python install first.  You can use the code in db_connect() and db_lookup() to do some database connectivity testing before you try to get the whole thing working.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 08:55:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13606#M84</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2020-09-28T08:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13607#M85</link>
      <description>&lt;P&gt;I think JDBC connection is irrespective of Splunk. Even if you write a lookup script you need to make a connection to database and load specific driver thats where JDBC/ODBC connection comes into play. &lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2010 04:07:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13607#M85</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2010-12-15T04:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: I want sample code to connect to Oracle database, and lookup a table at search time</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13608#M86</link>
      <description>&lt;P&gt;(2+ years later) there is now the Splunk DB Connect App:&lt;BR /&gt;
&lt;A href="http://splunk-base.splunk.com/apps/50803/splunk-db-connect"&gt;http://splunk-base.splunk.com/apps/50803/splunk-db-connect&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2013 02:58:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/I-want-sample-code-to-connect-to-Oracle-database-and-lookup-a/m-p/13608#M86</guid>
      <dc:creator>piebob</dc:creator>
      <dc:date>2013-03-06T02:58:09Z</dc:date>
    </item>
  </channel>
</rss>

