Hello Splunk Community:
I'm trying to convert several stand alone Python scripts into splunk External Lookups and running into problems.
Any thoughts?
I've looked at the external_lookup.py example that ships with Splunk and created a simple example that should just output the first field and create content for the second field.
It's working in the Splunk CLI
sh-3.2# cat csv_test.csv
field1,field2
mydataexample,
sh-3.2# cat csv_test.csv | /Applications/Splunk/bin/splunk cmd python test_output.py field1 field2
field1,field2
mydataexample,NoField2_Data
But not from the Splunk UI
index="ex_firewall" accept or allowed
| stats count by dst_ip
| lookup test_output.py dst_ip as field1
Throwing error
Error in 'lookup' command: Could not construct lookup 'test_output, dst_ip, as, field1'. See search.log for more details.
I've also placed the script in the the proper location
/Applications/Splunk/etc/apps/splunk/etc/system/bin/test_output.py
Added it to the transforms.conf
sh-3.2# cat /Applications/Splunk/etc/apps/splunk/etc/system/local/transforms.conf
# Example external lookup
#[dnslookup]
#external_cmd = external_lookup.py clienthost clientip
#fields_list = clienthost,clientip
# Test output external lookup
[test_output.py]
external_cmd = test_output.py field1 field2
fields_list = field1, field2
external_type = python
This is the script itself
#!/usr/bin/env python
###
#
# Testing Stub - Splunk lookup external/scripted
#
###
import csv
import sys
import socket
def main():
#Check input
if len(sys.argv) != 3:
print "Usage: python thisfile.py [field1] [field2]"
sys.exit(1)
#Input
field1 = sys.argv[1]
field2 = sys.argv[2]
infile = sys.stdin
outfile = sys.stdout
r = csv.DictReader(infile)
header = r.fieldnames
w = csv.DictWriter(outfile, fieldnames=r.fieldnames)
w.writeheader()
# Do something with the fields
for result in r:
# if both fields are there write out
if result[field1] and result[field2]:
w.writerow(result)
elif result[field1]:
result[field2] = "NoField2_Data"
w.writerow(result)
elif result[field2]:
result[field1] = "NoField1_Data"
w.writerow(result)
main()
... View more