Skip to content

Instantly share code, notes, and snippets.

@ncracker
Created October 16, 2019 00:34
Show Gist options
  • Save ncracker/7624c3a2052337fe43eff6c421f5dafc to your computer and use it in GitHub Desktop.
Save ncracker/7624c3a2052337fe43eff6c421f5dafc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import socket
import json
import time
import ssl
host = "intake.logs.datadoghq.com"
ssl_port = 10516
ddApiKey = "API_KEY"
dd_source = "MY_SOURCE"
dd_app_name = "MY_APP"
dd_hostname = "IFnoHOSTNAMEinLOGS"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s)
s.connect((host, ssl_port))
metadata = { "ddsource": dd_source,
"syslog.appname": dd_app_name,
"syslog.hostname": dd_hostname,
}
def send_entry(s, log_entry):
# The log_entry can only be a string or a dict
log_entry = {"message": log_entry}
# Merge with metadata
log_entry = merge_dicts(log_entry, metadata)
# Send to Datadog
str_entry = json.dumps(log_entry)
prefix = "%s " % ddApiKey
return s.send((prefix + str_entry + "\n").encode("UTF-8"))
def merge_dicts(a, b, path=None):
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge_dicts(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
raise Exception(
'Conflict while merging metadatas and the log entry at %s' % '.'.join(path + [str(key)])
)
else:
a[key] = b[key]
return a
with open('logs-to-replay.log', 'r') as logfile:
for line in logfile:
timestamp = time.strftime("%d/%b/%Y:%H:%M:%S")
log = "%s" % line
#delay between each submission
time.sleep(30)
#print(log) - if you want to send to stdout
send_entry(s, log)
@ncracker
Copy link
Author

This is Python 2.7 btw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment