2017-12-08 18 views
0

로그를 splunk에 보내야하는 python 프로그램이 있습니다. 우리의 인 Splunk 관리자는 다음과 같이 로그를 게시하는 서비스 컬렉터 HTTP 엔드 포인트를 만들었습니다파이썬에서 HTTP를 통해 splunk에 로그 항목을 작성하십시오.

  • 인덱스
  • 토큰 호스트 이름
  • URI

우리는 찾을 수 없습니다

  • 곳 스플래시 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

    아, 사용자 정의 유형을 좋아해야합니다 ... about : 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