Skip to content

Instantly share code, notes, and snippets.

@ank-everstake
Forked from idlethreat/gelf-listener.py
Last active October 28, 2023 09:48
Show Gist options
  • Save ank-everstake/be91ba54a43e8dd68161c60ca9a66af1 to your computer and use it in GitHub Desktop.
Save ank-everstake/be91ba54a43e8dd68161c60ca9a66af1 to your computer and use it in GitHub Desktop.
Gelf Log Listener in Python
#!/usr/bin/env python
############### // gelfListener 0.2 // ###############
#
# Listens on UDP 12201 for Gelf messages
# Extracts the event data and writes the message to disk
# updated to handle both zlib (nxlog) and gzip (graylog server) compressed events
# not perfect, but works okay
#
# Bugs:
#
# decodeGzip() blows up a lot. Take out the try: finally to see all
# the pretty error messages
#
######################################################
import gzip
import zlib
import json
import socket
from io import StringIO
HOST = '127.0.0.1' # Symbolic name meaning all available interfaces
PORT = 12201 # Default port for Gelf UDP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # this creates UDP socket
print("Socket created")
#Bind socket to local host and port
try:
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
#s.setblocking(0)
except socket.error as msg:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
############### // fileWriter // ###############
def fileWriter(myHostName, myMessage):
with open(myHostName, 'a') as fileWriteOperation:
fileWriteOperation.write(myMessage + '\n')
fileWriteOperation.close()
################################################
############### // Zlib // ###############
def decodeZlib(zData):
# decompress
event = zlib.decompress(zData)
parsed_json = json.loads(event)
# assign
#print(parsed_json)
hostname = parsed_json["host"]
#fullMessage = parsed_json["full_message"]
shortMessage = parsed_json["short_message"]
# output
fileWriter(hostname, shortMessage)
print(hostname, shortMessage)
##########################################
############### // Gzip // ###############
def decodeGzip(gData):
try:
# decompress
gzipEvent = StringIO.StringIO(gData)
gzipper = gzip.GzipFile(fileobj=gzipEvent)
extractedData = gzipper.read()
parsed_json = json.loads(extractedData)
# assign
#print(parsed_json)
hostname = str(parsed_json["host"])
#fullMessage = parsed_json["full_message"]
shortMessage = parsed_json["short_message"]
# output
fileWriter(hostname, shortMessage)
print(hostname, shortMessage)
# exception handling
except:
pass
##########################################
############### // Here's the Magic // ###############
print("reading stream now")
while True:
# 8192 is the largest size that a udp packet can handle
data, addr = s.recvfrom(8192) # buffer size is 8192 bytes
try:
decodeZlib(data)
except:
decodeGzip(data)
@xcvbn23
Copy link

xcvbn23 commented Oct 28, 2023

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