Skip to content

Instantly share code, notes, and snippets.

@n-bar
Forked from jgoodall/README.md
Last active February 16, 2017 13:46
Show Gist options
  • Save n-bar/b0c5fb869ec0d18ca145b87b75f9222a to your computer and use it in GitHub Desktop.
Save n-bar/b0c5fb869ec0d18ca145b87b75f9222a to your computer and use it in GitHub Desktop.
This is a sample of how to send some information to logstash via the TCP input from python.

This is a sample of how to send some information to logstash via the TCP input in python. It assumes the logstash host is on "localhost" and the TCP listening input is 5959.

The logstash.conf should look something like the sample file.

The log message should be a stringified JSON object with the log message in the @message field.

To use, run the python script python sendMessageToLogstash.js

input {
tcp {
codec => json
port => 5959
}
}
output {
stdout {
code => rubydebug
}
}
import socket
import json
import sys
HOST = socket.gethostbyname("localhost")
PORT = 5959
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
sock.connect((HOST, PORT))
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
msg = {'@message': 'python test message', '@tags': ['python', 'test']}
sock.send(json.dumps(msg) + '\n')
sock.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment