Running Python script without printing results

Hi Community!

I hope that you're doing great! I've been involved in a project, and we have to process data with a python script and then insert it with Logstash into Elasticsearch. Having that said, I use the exec plugin to execute the script, and then, I print the result in the script, such as: print(response), so Logstash can detect and insert the data into Elasticsearch.

The problem is that this script is being logged in a monitor, and the output is sensitive data that shouldn't log.

Is there any way to pass the value through Python to Logstash output without printing in console, such as executing the script and just using return response?

An example of my Logstash pipeline would be the following:

input {
	exec {
		command => "/usr/bin/python3 /usr/share/logstash/pipeline/example.py"
		interval => 60
		codec => "json"
  	}
}
output {
	elasticsearch {
		index => "example"
		hosts => [ "https://elasticsearch:9200" ]
        user => "${ELASTIC_USERNAME}"
        password => "${ELASTIC_PASSWORD}"
        ssl => true
        ssl_certificate_verification => true
        cacert => '/certs/ca.crt'
        codec => json_lines
   }
   stdout {}
}

and a small example of a python script would be like:

def hello_world():
     hello_world = { "first_message": "hello world!" }
     
     print(hello_world)

if "__main__" == __name__:
     hello_world()

The ideal way to do this would be to use return hello_world or something else to don't log the message in the console.

Hi,

You have the possibility to implement the TCP protocol in the python script and configuring the logstash input ro get data from TCP protocol.
For more informations see the documentations.

Cad.

What is being logged? I would say that the cause is that you have a stdout {} output in the output block, this makes logstash prints out every message it process to the stdout with could be the terminal if you are running via command line, or a log file when running as a service.

Remove the stdout {} output and see if it solves your problem.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.