Skip to content

Commit

Permalink
Added possibility to switch between HTTP and HTTPS (#26)
Browse files Browse the repository at this point in the history
* Update __init__.py

Added possibility to switch between http and https connection

* Proxies Configuration 

Now is possible to use proxies configurations.
The proxies conf have to be defined in standard urllib3 way, e.g.
proxyes:{'http':'http://USER:PSWD@proxy.url:PORT', 'https':'https://USER:PSWD@proxy.url:PORT'}

* Formatting in json the log

In case use send a python dictionary to a _json type collector you can enable this feature to translate to a valid json for splunk

* fix whitespaces
  • Loading branch information
dabrign authored and zach-taylor committed Oct 30, 2018
1 parent e3531d9 commit c40483d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions splunk_handler/__init__.py
Expand Up @@ -48,7 +48,8 @@ def __init__(self, host, port, token, index,
hostname=None, source=None, sourcetype='text',
verify=True, timeout=60, flush_interval=15.0,
queue_size=5000, debug=False, retry_count=5,
retry_backoff=2.0):
retry_backoff=2.0, protocol='https', proxies=None,
record_format=False):

global instances
instances.append(self)
Expand All @@ -73,6 +74,9 @@ def __init__(self, host, port, token, index,
self.session = requests.Session()
self.retry_count = retry_count
self.retry_backoff = retry_backoff
self.protocol = protocol
self.proxies = proxies
self.record_format = record_format

self.write_debug_log("Starting debug mode")

Expand All @@ -93,14 +97,20 @@ def __init__(self, host, port, token, index,
# disable all warnings from urllib3 package
if not self.verify:
requests.packages.urllib3.disable_warnings()


if self.verify and self.protocol == 'http':
print("[SplunkHandler DEBUG] " + 'cannot use SSL Verify and unsecure connection')

if self.proxies is not None:
self.session.proxies = self.proxies

# Set up automatic retry with back-off
self.write_debug_log("Preparing to create a Requests session")
retry = Retry(total=self.retry_count,
backoff_factor=self.retry_backoff,
method_whitelist=False, # Retry for any HTTP verb
status_forcelist=[500, 502, 503, 504])
self.session.mount('https://', HTTPAdapter(max_retries=retry))
self.session.mount(self.protocol+'://', HTTPAdapter(max_retries=retry))

self.start_worker_thread()

Expand Down Expand Up @@ -162,6 +172,12 @@ def format_record(self, record):
if self.testing:
current_time = None

if self.record_format:
try:
record = json.dumps(record)
except:
pass

params = {
'time': current_time,
'host': self.hostname,
Expand Down Expand Up @@ -193,7 +209,7 @@ def _splunk_worker(self, payload=None):

if payload:
self.write_debug_log("Payload available for sending")
url = 'https://%s:%s/services/collector' % (self.host, self.port)
url = '%s://%s:%s/services/collector' % (self.protocol, self.host, self.port)
self.write_debug_log("Destination URL is " + url)

try:
Expand All @@ -203,7 +219,7 @@ def _splunk_worker(self, payload=None):
data=payload,
headers={'Authorization': "Splunk %s" % self.token},
verify=self.verify,
timeout=self.timeout,
timeout=self.timeout
)
r.raise_for_status() # Throws exception for 4xx/5xx status
self.write_debug_log("Payload sent successfully")
Expand Down

0 comments on commit c40483d

Please sign in to comment.