LookUp Script NOT To Read from CSV File
Yes i am looking for script which reads from the context of Splunk Searching instead of taking the input from CSV file from stdin
Here is the script which works from standalone but not from context of splunk search
# File namelookup.py
# ------------------------------
import os,csv
import sys
import logging
import logging.config
import pyodbc
FIELDS = [ "memberId", "memberName" ]
# Given a id, find its name
def lookupName(idf, cur):
try:
selString = "SELECT first_name FROM emp where Member_ID="
cur.execute (selString + idf )
row = cur.fetchone()
return row[0]
except:
return []
def main():
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]
# THIS IS NOT DESIRED
#r = csv.reader(open('C:\\Splunk\\etc\\system\\bin\\memberInput.csv'))
#r = csv.reader(sys.stdin)
result = {}
w = csv.DictWriter(sys.stdout, FIELDS)
header = []
first = True
conn = pyodbc.connect('DSN=IBM_DB')
cursor = conn.cursor()
if len( idf):
result[namef] = lookupName(idf, cursor)
print("Result of"+ namef)
print(result[namef])
if len(result[namef]):
w.writerow(result)
cursor.close()
conn.close()
main()
Pl let me know how to rewrite the script to read/write from the context of splun search
... View more