在python中通过HTTP将日志条目写入splunk

Lig*_*ard 2 python splunk splunk-sdk

我们有一个python程序需要将日志发送到splunk。我们的 splunk 管理员创建了一个服务收集器 HTTP 端点,用于将日志发布到以下位置:

  • 指数
  • 代币
  • 主机名
  • 统一资源标识符

我们找不到在 splunk python SDK客户端中输入 URI 的位置。例如:

import splunklib.client as client
import splunklib.results as results_util

HOST="splunkcollector.hostname.com"
URI="services/collector/raw"
TOKEN="ABCDEFG-8A55-4ABB-HIJK-1A7E6637LMNO"
PORT=443

# Create a Service instance and log in
service = client.connect(
    host=HOST,
    port=PORT,
    token=TOKEN)

# Retrieve the index for the data
myindex = service.indexes["cloud_custodian"]

# Submit an event over HTTP
myindex.submit("Dummy test python client log")
Run Code Online (Sandbox Code Playgroud)

如您所见,我从不使用 URI 变量。上述代码的结果是:

Traceback (most recent call last):
  File "splunk_log.py", line 15, in <module>
    myindex = service.indexes["cloud_custodian"]
  File "/usr/local/lib/python2.7/site-packages/splunklib/client.py", line 1230, in __getitem__
    raise KeyError(key)
KeyError: UrlEncoded('cloud_custodian')
Run Code Online (Sandbox Code Playgroud)

Lig*_*ard 5

最终执行了带有requests 的库存 POST 。我不确定 splunk 客户端是否打算支持 HTTP 事件收集器。

import requests

url='https://splunkcollector.hostname.com:443/services/collector/event'
authHeader = {'Authorization': 'Splunk {}'.format('ABCDEFG-8A55-4ABB-HIJK-1A7E6637LMNO')}
jsonDict = {"index":"cloud_custodian", "event": { 'message' : "Dummy test python client log" } }

r = requests.post(url, headers=authHeader, json=jsonDict, verify=False)
print r.text
Run Code Online (Sandbox Code Playgroud)