2017-12-08 12 views
0

ログをsplunkに送る必要があるpythonプログラムがあります。我々は見つけることができませんpythonでHTTP経由でsplunkにログエントリを書き込む

  • インデックス
  • トークン
  • ホスト名
  • URI

:私たちのSplunk管理者は、次のとにログを公開するサービスコレクタHTTPエンドポイントを作成しましたスプラッシュ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") 

わかりましたとおり、私は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') 
+1

'service.indexes.keys()'でインデックスを表示しようとするとどうなりますか? – zwer

+0

'AttributeError: 'Indexes'オブジェクトに属性 'keys'がありません – Lightbeard

+1

ああ、カスタムタイプが大好きなんだよね...' print( "、"。service.indexesのxのstr(x) ) ' – zwer

答えて

0

ようになります。私はsplunkクライアントがHTTP Event Collectorをサポートしようとしているのかどうかはわかりません。

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 
関連する問題