Below is the props.conf at $SPLUNK_HOME/etc/system/local:
[Test_Log]
lookup_table = namelookup memberId OUTPUT memberName
Below is the transforms.conf at $SPLUNK_HOME/etc/system/local:
[namelookup]
external_cmd = namelookup.py memberId memberName
external_type = python
fields_list = memberId, memberName
Script location :
$SPLUNK_HOME/etc/system/bin/namelookup.py
# File namelookup.py
# ------------------------------
import os,csv
import sys
import logging
import logging.config
import pyodbc
# Given a id, find its name
def lookupName(idf, cur):
#logger.debug("====Inside Lookup idf====="+idf)
try:
selString = "SELECT first_name FROM emp where Member_ID="
cur.execute (selString + idf )
row = cur.fetchone()
return row[0]
except:
return []
def main():
logging.config.fileConfig("logging.conf")
logger = logging.getLogger("name_lookup")
if len(sys.argv) != 3:
print "Usage: python name_lookup.py [id field] [name field]"
sys.exit(0)
idf = sys.argv[1]
namef = sys.argv[2]
#logger.debug("====idf===="+idf)
#logger.debug("====namef====="+namef)
r = csv.reader(sys.stdin)
w = None
header = []
first = True
conn = pyodbc.connect('DSN=IBM_DB')
cursor = conn.cursor()
for line in r:
if first:
header = line
if idf not in header or namef not in header:
print "Host and IP 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 < len(header):
if i < len(line):
result[header[i]] = line[i]
else:
result[header[i]] = ''
i += 1
# Perform the lookup for city to country if necessary
if len(result[idf]) and len(result[namef]):
w.writerow(result)
elif len(result[idf]):
result[namef] = lookupName(result[idf], cursor)
if len(result[namef]):
w.writerow(result)
cursor.close()
conn.close()
main()
the results displayed on stdout are as desired
C:\Splunk\etc\system\bin>db_lookup.py memberId memberName < memberInput.csv
produces following output retrieving memberName from database
memberId,memberName
006,RANDY
007,LEONY
009,RANDOLPH
However i still have following issues
The script doesn't output similar results in the CSV file. Wondering why it doesn't output in CSV file when the desired results are displayed in STDOUT. So what i am missing here?
The script then outputs from the CSV file and returns it to Splunk, which populates the memberName field in your results
source="Test_Log.txt" | xmlkv entry | lookup namelookup memberId OUTPUT memberName | table memberId, memberName
Please let me know how to populate CSV file with the results
... View more