-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplunk_input.py
49 lines (38 loc) · 1.39 KB
/
splunk_input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
import logging
import requests
import urllib3
# SSL Warning 제거
urllib3.disable_warnings()
with open("./conf/conf.json", "r", encoding="utf-8") as mainconf:
conf = json.load(mainconf)
SPLUNK_SERVER = conf["splunk"]["server"]
SPLUNK_HEC_TOKEN = conf["splunk"]["token"]
SPLUNK_HEADER = {"Authorization": "Splunk " + SPLUNK_HEC_TOKEN}
SPLUNK_HEC_URL = f"http://{SPLUNK_SERVER}:8088/services/collector/raw"
session = requests.Session()
def input_splunk(input_data, filepath=None, num_requests=100):
"""Splunk HEC Input"""
try:
if filepath:
with open(filepath, "r", encoding="utf-8") as input_file:
input_data = json.load(input_file)
for _ in range(num_requests):
response = session.post(
SPLUNK_HEC_URL,
data=json.dumps(input_data),
verify=False,
headers=SPLUNK_HEADER,
timeout=10,
)
if response.status_code == 200:
logging.info("Data sent successfully to Splunk HEC.")
else:
logging.error(f"Error sending data to Splunk HEC: {response.content}")
return True
except Exception as error:
logging.error(error)
return False
if __name__ == "__main__":
test_dict = {"_time": "", "index": "bob12", "msg": "Test 100"}
input_splunk(test_dict, num_requests=100)