splunk / splunk-sdk-python

Splunk Software Development Kit for Python
http://dev.splunk.com
Apache License 2.0
698 stars 370 forks source link

Bug: Unable to find the file #512

Closed tanmoyio closed 1 year ago

tanmoyio commented 1 year ago

Describe the bug index.upload() couldn't find the file (tried both relative and absolute path)

To Reproduce

import os
import splunklib.client as client

def splunk_connector(username, password, host):
    try:
        service = client.connect(host=host, username=username, password=password)
        if service:
            print("Splunk connection established")
        return service

    except Exception as e:
        print(e)

def splunk_write(service, idx):
    indexes = service.indexes
    all_indexes_name = [index.name for index in indexes]

    if idx not in all_indexes_name:
        service_index = service.indexes.create(idx)
    else:
        service_index = service.indexes[idx]

    service_index.upload('/home/misha/out.txt')

service = splunk_connector(
        username=os.environ["USERNAME"],
        password=os.environ["PASSWORD"],
        host=os.environ["HOST"]
)

splunk_write(service, idx)

Expected behavior It should grab the file content and upload it to selected index

Logs or Screenshots

Splunk connection established
Traceback (most recent call last):
  File "splunk_upload.py", line 33, in <module>
    splunk_write(service, 'auth1m_out')
  File "splunk_upload.py", line 24, in splunk_write
    service_index.upload('/home/misha/out.txt')
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/client.py", line 2219, in upload
    self.service.post(path, name=filename, **kwargs)
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/binding.py", line 291, in wrapper
    return request_fun(self, *args, **kwargs)
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/binding.py", line 72, in new_f
    val = f(*args, **kwargs)
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/binding.py", line 781, in post
    response = self.http.post(path, all_headers, **query)
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/binding.py", line 1273, in post
    return self.request(url, message)
  File "/home/misha/miniconda3/envs/morpheus-22.11/lib/python3.8/site-packages/splunklib/binding.py", line 1302, in request
    raise HTTPError(response)
splunklib.binding.HTTPError: HTTP 400 Bad Request -- unable to open file: path='/home/misha/out.txt' error='No such file or directory'

Splunk (please complete the following information):

SDK (please complete the following information):

Additional context Add any other context about the problem here.

akaila-splunk commented 1 year ago

Hi @tanmoyio , To upload file it must be locally accessible from the server (ref), hence we would request you to upload the file to the Splunk instance and pass the absolute path(i.e opt/splunk/) as argument, let us know if you still face the issue.

akaila-splunk commented 1 year ago

Closing the Issue as we haven't received any response. @tanmoyio please reopen the Issue if you are still facing the issue.