Splunk Dev

How to convert Intersplunk's readResults() to dataframe?

agnesramos
Engager

Hi,

I am trying to add a Python script as a Splunk custom command and I'm having trouble reading the data from Intersplunk and formatting it as a pandas dataframe. I have:

results = splunk.Intersplunk.readResults()
df = pandas.DataFrame(results)
ip_list = df['ip'].tolist()

So I'm converting the list of dictionaries returned by readResults() to a pandas DF and then extracting what would be the csv "ip" column as a list. But I am getting an error on that last code line.

I have also tried df = pandas.DataFrame.from_records(results) and ip_list = df['ip'].values.tolist(), but it's not working.

I'd appreciate any help.

selcukozer
New Member

You better to use service jobs as following:

Function to Perform a Splunk search

def execute_query(searchquery_normal,
kwargs_normalsearch={"exec_mode": "normal"},
kwargs_options={"output_mode": "csv", "count": 1000000}):
# Execute Search
job = service.jobs.create(searchquery_normal, **kwargs_normalsearch)

# A normal search returns the job's SID right away, so we need to poll for completion
while True:
    while not job.is_ready():
        pass
    stats = {"isDone": job["isDone"], "doneProgress": float(job["doneProgress"])*100, 
             "scanCount": int(job["scanCount"]), "eventCount": int(job["eventCount"]), 
             "resultCount": int(job["resultCount"])}
    status = ("\r%(doneProgress)03.1f%%   %(scanCount)d scanned   " 
              "%(eventCount)d matched   %(resultCount)d results") % stats

    sys.stdout.write(status + '\n')
    sys.stdout.flush()
    if stats["isDone"] == "1":
        sys.stdout.write("\nDone!")
        break
    time.sleep(0.5)

# Get the results and display them
csv_results = job.results(**kwargs_options).read()
job.cancel()

for row in csv_results:
            if row[0] not in (None, ""):
                df = pd.read_csv(StringIO.StringIO(csv_results), encoding='utf8', sep=',', low_memory=False)
                df.to_csv(filename_new, sep=',', encoding='utf-8')
                break
            break

you can find whole project from following:
https://github.com/selcukozer/splunk_python

0 Karma

danbar6
Explorer

Some more info will help - What error are you getting? What does your 'ip' field look like?

umm and also, why are you converting it to a DF just to go back to a list right away?

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...