<?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: Parameters and variables in dbxlookup DB Connect (v3.1.3) in Monitoring Splunk</title>
    <link>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429171#M6480</link>
    <description>&lt;P&gt;Hi everyone!&lt;/P&gt;

&lt;P&gt;I have workaround solution of my issue. I use external lookup with python script. This comment helped me &lt;A href="https://answers.splunk.com/answers/2580/i-want-sample-code-to-connect-to-oracle-database-and-lookup-a-table-at-search-time.html" target="_blank"&gt;https://answers.splunk.com/answers/2580/i-want-sample-code-to-connect-to-oracle-database-and-lookup-a-table-at-search-time.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Firstly need setup &lt;STRONG&gt;cx_Oracle&lt;/STRONG&gt; for Python. In Linux system Python for Linux and Python for Splunk is different environments. I have installed cx_Oracle for system Python (Linux). Also I installed Oracle Client for Linux (&lt;A href="https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html" target="_blank"&gt;https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html&lt;/A&gt;)&lt;/P&gt;

&lt;P&gt;In my App/bin path need put python script dblookup.py&lt;BR /&gt;
    import csv&lt;BR /&gt;
    import sys&lt;BR /&gt;
    import cx_Oracle&lt;BR /&gt;
    import os&lt;BR /&gt;
    os.environ["NLS_LANG"] = "Russian.AL32UTF8"&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def db_connect():
    dsn = cx_Oracle.makedsn("host", port, service_name="service_name")
    db = cx_Oracle.connect("user", "password", dsn)
    cursor = db.cursor()
    return cursor

def db_lookup(cursor, key):
    cursor.execute("""
    SELECT ID, DATA FROM MyTable WHERE DATA LIKE '%:id%'""", dict(id=key))
    row = cursor.fetchone()
    return row

def main():
    inputfield = sys.argv[1]

    infile = sys.stdin
    outfile = sys.stdout

    r = csv.DictReader(infile)
    header = r.fieldnames

    w = csv.DictWriter(outfile, fieldnames=r.fieldnames)
    w.writeheader()

    cursor = db_connect()

    for row in r:
        key = row["DATA"]
        try:
            db_row = db_lookup(cursor, key)
            if db_row:
                (ID, DATA) = db_row
                row["ID"] = ID
                row["DATA"] = DATA
                w.writerow(row)
        except:
            err=1
    cursor.close()
main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In my case cx_Oracle is not working in Python Splunk therefore I use &lt;STRONG&gt;wrapper.py&lt;/STRONG&gt; script.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os, sys
python_executable = "/usr/bin/python"
os.execv(python_executable, [ python_executable, "/opt/splunk/etc/apps/my_app/bin/dblookup.py" ] + sys.argv[1:])
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can testing python script like this:&lt;BR /&gt;
1. Create CSV file test.csv&lt;BR /&gt;
2. Fill file with data&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ID,DATA
,TEST
,DATA
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;OL&gt;
&lt;LI&gt;Run python script in command line
Run with python Linux [ok]
python dblookup.py DATA &amp;lt; test.csv&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Run with python Splunk [not work - error import cx_Oracle]&lt;BR /&gt;
/opt/splunk/bin/python dblookup.py DATA &amp;lt; test.csv&lt;/P&gt;

&lt;P&gt;Run with python Splunk wrapper.py [ok]&lt;BR /&gt;
/opt/splunk/bin/python wrapper.py DATA &amp;lt; test.csv&lt;/P&gt;

&lt;P&gt;Then I create new External Lookup Defenition in Splunk.&lt;BR /&gt;
Settings » Lookups » Lookup definitions&lt;BR /&gt;
Name: my_db_lookup&lt;BR /&gt;
Type: External&lt;BR /&gt;
Command: wrapper.py DATA&lt;BR /&gt;
Supported fields: ID,DATA&lt;/P&gt;

&lt;P&gt;Finally my search run&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval VAR = "SOMEDATA"
| lookup my_db_lookup DATA as VAR OUTPUT ID, DATA
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Sep 2020 22:32:07 GMT</pubDate>
    <dc:creator>dgelo</dc:creator>
    <dc:date>2020-09-29T22:32:07Z</dc:date>
    <item>
      <title>Parameters and variables in dbxlookup DB Connect (v3.1.3)</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429170#M6479</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a problem how to pass parameters (variables) to dbxlookup (v3.1.3). I need pass variable &lt;STRONG&gt;VAR&lt;/STRONG&gt; from Splunk search to SQL WHERE condition in DB Lookup (dbxlookup).&lt;/P&gt;

&lt;P&gt;For example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval VAR = "SOMEDATA"
| dbxlookup connection="oracle_con" query="SELECT ID, DATA FROM MyTable WHERE DATA LIKE '%$VAR$%'" "VAR" as "VAR" OUTPUT "ID"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In dblookup (v1) same parameters is named as &lt;STRONG&gt;input_fields&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;Can I pass 2 and more parameters? Maybe have workaround using dbxquery command or some other?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 09:33:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429170#M6479</guid>
      <dc:creator>dgelo</dc:creator>
      <dc:date>2018-12-12T09:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Parameters and variables in dbxlookup DB Connect (v3.1.3)</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429171#M6480</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;

&lt;P&gt;I have workaround solution of my issue. I use external lookup with python script. This comment helped me &lt;A href="https://answers.splunk.com/answers/2580/i-want-sample-code-to-connect-to-oracle-database-and-lookup-a-table-at-search-time.html" target="_blank"&gt;https://answers.splunk.com/answers/2580/i-want-sample-code-to-connect-to-oracle-database-and-lookup-a-table-at-search-time.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Firstly need setup &lt;STRONG&gt;cx_Oracle&lt;/STRONG&gt; for Python. In Linux system Python for Linux and Python for Splunk is different environments. I have installed cx_Oracle for system Python (Linux). Also I installed Oracle Client for Linux (&lt;A href="https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html" target="_blank"&gt;https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html&lt;/A&gt;)&lt;/P&gt;

&lt;P&gt;In my App/bin path need put python script dblookup.py&lt;BR /&gt;
    import csv&lt;BR /&gt;
    import sys&lt;BR /&gt;
    import cx_Oracle&lt;BR /&gt;
    import os&lt;BR /&gt;
    os.environ["NLS_LANG"] = "Russian.AL32UTF8"&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def db_connect():
    dsn = cx_Oracle.makedsn("host", port, service_name="service_name")
    db = cx_Oracle.connect("user", "password", dsn)
    cursor = db.cursor()
    return cursor

def db_lookup(cursor, key):
    cursor.execute("""
    SELECT ID, DATA FROM MyTable WHERE DATA LIKE '%:id%'""", dict(id=key))
    row = cursor.fetchone()
    return row

def main():
    inputfield = sys.argv[1]

    infile = sys.stdin
    outfile = sys.stdout

    r = csv.DictReader(infile)
    header = r.fieldnames

    w = csv.DictWriter(outfile, fieldnames=r.fieldnames)
    w.writeheader()

    cursor = db_connect()

    for row in r:
        key = row["DATA"]
        try:
            db_row = db_lookup(cursor, key)
            if db_row:
                (ID, DATA) = db_row
                row["ID"] = ID
                row["DATA"] = DATA
                w.writerow(row)
        except:
            err=1
    cursor.close()
main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In my case cx_Oracle is not working in Python Splunk therefore I use &lt;STRONG&gt;wrapper.py&lt;/STRONG&gt; script.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os, sys
python_executable = "/usr/bin/python"
os.execv(python_executable, [ python_executable, "/opt/splunk/etc/apps/my_app/bin/dblookup.py" ] + sys.argv[1:])
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can testing python script like this:&lt;BR /&gt;
1. Create CSV file test.csv&lt;BR /&gt;
2. Fill file with data&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ID,DATA
,TEST
,DATA
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;OL&gt;
&lt;LI&gt;Run python script in command line
Run with python Linux [ok]
python dblookup.py DATA &amp;lt; test.csv&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Run with python Splunk [not work - error import cx_Oracle]&lt;BR /&gt;
/opt/splunk/bin/python dblookup.py DATA &amp;lt; test.csv&lt;/P&gt;

&lt;P&gt;Run with python Splunk wrapper.py [ok]&lt;BR /&gt;
/opt/splunk/bin/python wrapper.py DATA &amp;lt; test.csv&lt;/P&gt;

&lt;P&gt;Then I create new External Lookup Defenition in Splunk.&lt;BR /&gt;
Settings » Lookups » Lookup definitions&lt;BR /&gt;
Name: my_db_lookup&lt;BR /&gt;
Type: External&lt;BR /&gt;
Command: wrapper.py DATA&lt;BR /&gt;
Supported fields: ID,DATA&lt;/P&gt;

&lt;P&gt;Finally my search run&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval VAR = "SOMEDATA"
| lookup my_db_lookup DATA as VAR OUTPUT ID, DATA
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:32:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429171#M6480</guid>
      <dc:creator>dgelo</dc:creator>
      <dc:date>2020-09-29T22:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Parameters and variables in dbxlookup DB Connect (v3.1.3)</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429172#M6481</link>
      <description>&lt;P&gt;@dgelo, If your problem is resolved, please accept the answer to help future readers.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Dec 2018 16:29:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429172#M6481</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2018-12-25T16:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Parameters and variables in dbxlookup DB Connect (v3.1.3)</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429173#M6482</link>
      <description>&lt;P&gt;Hey guys. Why don't anybody comment on this? Especially from Splunk Inc.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jan 2019 14:13:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/Parameters-and-variables-in-dbxlookup-DB-Connect-v3-1-3/m-p/429173#M6482</guid>
      <dc:creator>highsplunker</dc:creator>
      <dc:date>2019-01-10T14:13:08Z</dc:date>
    </item>
  </channel>
</rss>

