-
Notifications
You must be signed in to change notification settings - Fork 0
/
splunkforwarder.py
55 lines (42 loc) · 1.87 KB
/
splunkforwarder.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
50
51
52
53
54
55
import json
import requests
class SplunkForwarder:
def __init__(self, authorization_token, splunk_ingester_domain, connection_port='443'):
assert 'http' in splunk_ingester_domain
assert authorization_token
self.token = authorization_token
self.ingester_url = "{}:{}{}".format(splunk_ingester_domain, connection_port, "/services/collector/event")
self.port = connection_port
def build_metadata(self, index_name=None, **other_metadata):
metadata = dict()
if index_name:
metadata["index"] = index_name
if other_metadata:
metadata.update(other_metadata)
def send(self):
headers = dict()
headers['Authorization'] = 'Splunk {}'.format(self.token)
if hasattr(self, "payload") and self.payload:
response = requests.post(self.ingester_url, data=self.payload, headers=headers)
if response.status_code != 200:
print("Issues in sending to splunk - URL -> {}".format(self.ingester_url))
def build_payload(self, events, metadata):
if isinstance(events, list):
concatenated_payload = ""
for event in events:
payload = dict()
payload["host"] = self.ingester_url
payload["event"] = event
if metadata:
payload.update(metadata)
concatenated_payload += json.dumps(payload)
if concatenated_payload:
setattr(self, "payload", concatenated_payload)
# r = requests.post(self.ingester_url?, data=concatenated_payload, headers=headers)
else:
payload = dict()
payload["host"] = self.ingester_url
payload["event"] = events
if metadata:
payload.update(metadata)
setattr(self, "payload", json.dumps(payload))