Skip to content

Instantly share code, notes, and snippets.

@SiroDiaz
Forked from jgoodall/README.md
Created December 13, 2018 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiroDiaz/2bcfcfb70ea349e2f191d29c4570f76d to your computer and use it in GitHub Desktop.
Save SiroDiaz/2bcfcfb70ea349e2f191d29c4570f76d 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 node.js or python.

This is a sample of how to send some information to logstash via the TCP input in nodejs or python. It assumes the logstash host is on 10.10.10.100 and the TCP listening input is 9563.

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 node script node sendMessageToLogstash.js, or the python script python sendMessageToLogstash.js

input {
tcp {
'charset' => 'UTF-8'
'format' => 'json_event'
'port' => '9563'
'type' => 'stucco-tcp'
}
}
output {
elasticsearch_http {
'host' => 'localhost'
'port' => '9200'
}
file {
'path' => '/usr/local/logstash/server/log/output.log'
}
}
/* globals require:true, console:true, process:true */
// This script will send a message to a [logstash](http://logstash.net/)
// server using the [TCP input](http://logstash.net/docs/1.1.13/inputs/tcp)
// and then quit. If there is no listener, it will just quit.
'use strict';
var net = require('net');
var logHost = '10.10.10.100'
, logPort = 9563
, sender = require('os').hostname();
var conn = net.createConnection({host: logHost, port: logPort}, function() {
var message = {
'@tags': ['nodejs', 'test']
, '@message': 'tcp test ' + Math.floor(Math.random() * 10000)
, '@fields': {'sender': sender}
}
conn.write(JSON.stringify(message));
process.exit(0);
})
.on('error', function(err) {
console.error(err);
process.exit(1);
});
import socket
import json
import sys
HOST = '10.10.10.100'
PORT = 9563
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))
sock.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment