Environment variable in Logstash not being parsed correctly - elasticsearch

I come here after reading How to reference environment variables in logstash configuration file?.
Unfortunately it did not work for me.
I am running:
bin/logstash -f my_filters.conf --debug
And my config file is:
input {
file {
path => "/tmp/${RUN_ID}/*.txt"
start_position => beginning
sincedb_path => "/dev/null"
ignore_older => 0
}
}
output {
elasticsearch {
hosts => [ "localhost:9200" ]
index => "${RUN_ID}"
}
}
And no new index is being created.
This is after setting:
export RUN_ID=500
For example.
If I change my config to have a hard-coded value (e.g. 500), then the index is created without a problem.
I have read the documentation and it mentions exactly what I'm doing right now...
What am I doing wrong, how can I get the environment variable working?

Logstash 2.4 requires a command line argument of --allow-env to do environment substitutions.
Without the flag it doesn't complain (but doesn't work)
bin/logstash -f test.conf
Settings: Default pipeline workers: 8
Pipeline main started
With the flag, it will complain if you don't set it:
bin/logstash --allow-env -f test.conf
fetched an invalid config {:config=>"input {\n file {\n path => \"/tmp/${RUN_ID}/*.txt\"\n start_position => beginning\n sincedb_path => \"/dev/null\"\n ignore_older => 0\n }\n}\n\noutput {\nstdout { codec=>rubydebug}\n elasticsearch {\n hosts => [ \"localhost:9200\" ]\n index => \"${RUN_ID}\"\n }\n}\n\n\n", :reason=>"Cannot evaluate `${RUN_ID}`. Environment variable `RUN_ID` is not set and there is no default value given.", :level=>:error}
And of course with the argument and flag, everything works right:
export RUN_ID=10
bin/logstash --allow-env -f test.conf
Pipeline main started
{
"message" => "asdfasdf",
"#version" => "1",
"#timestamp" => "2016-11-01T21:10:15.964Z",
"path" => "/tmp/10/test.txt",
"host" => "XXXXXXXXX.local"
}

Related

Unable to start Logstash server and throwing error

I want to pass log file as an input to a Logstash input. I have added /bin to the environment variable path so that I can access it from anywhere.
Below is my conf file:
logstash.conf
input{
path => "D:\nest\es-logging-example\log\info\info.log"
start_position => beginning
}
output{
elasticsearch{
hosts => ["localhost:9200"]
index => "indexforlogstash"
}
}
After running this using logstash -f "D:\nest\es-logging-example\logstash.conf" its showing below error in terminal.
`
[2022-03-15T16:14:49,851][ERROR][logstash.agent ] Failed to
execute action
{:action=>LogStash::PipelineAction::Create/pipeline_id:main,
:exception=>"LogStash::ConfigurationError", :message=>"Expected one of [
\\t\\r\\n], \"#\", \"{\" at line 2, column 11 (byte 19) after input{\r\n
path ", :backtrace=>["C:/logstash-8.1.0/logstash-
core/lib/logstash/compiler.rb:32:in `compile_imperative'",
"org/logstash/execution/AbstractPipelineExt.java:189:in `initialize'",
"org/logstash/execution/JavaBasePipelineExt.java:72:in `initialize'",
"C:/logstash-8.1.0/logstash-core/lib/logstash/java_pipeline.rb:47:in
`initialize'", "C:/logstash-8.1.0/logstash-
core/lib/logstash/pipeline_action/create.rb:50:in `execute'",
"C:/logstash-8.1.0/logstash-core/lib/logstash/agent.rb:376:in `block in
converge_state'"]}`
What is this error about?
I think your problem is that the \ is an escape character in the quoted string in your config file.
Can you change
path => "D:\nest\es-logging-example\log\info\info.log"
to
path => "D:\\nest\\es-logging-example\\log\\info\\info.log"
So the \ characters in the path are escaped.
There's no configuration found in C:\logstash-8.1.0\logstash.conf
Specify the absolute path where your logstash.cong file is located instead:
logstash -f "D:\\nest\\es-logging-example\\logstash.conf"
You also need to modify your config file as follows
path => "D:\\nest\\es-logging-example\\log\\info\\info.log"
Your configuration is wrong, you need to specify which input plugin you are using, which based on what you shared is the file input plugin.
Also, you need to use forward slashes.
Try the following:
input {
file {
path => "D:/nest/es-logging-example/log/info/info.log"
start_position => beginning
}
}

LogStash Configuration issue

I am a novice in the world of LogStash. Just started to learn it. I tried to create a config file called Unhealthy_data.config using data from a similarly named csv file.
The contents of my config file are as below: -
input{
file{
path => "D:/01_Users/LogStash/Unhealthy.csv"
start_position => "beginning"
}
filter{
csv{
separator => ","
columns => ["cluster_name","unhealthy_nodes","userid","applicationid","queue","application_type","impact_host","cluster_utilization","queue_utilization","running_containers","running_memory","elapsed_time","tech_datestamp"]
}
}
output{
elasticsearch{
hosts =>"http://localhost:9200"
index => "unhealthy"
document_type => "unhealthy_data"
}
stdout{}
}
}
The last column "tech_datestamp" is a Date column.
I am unable to load the data and get the error as below: -
C:\ELK\logstash-7.9.1\bin>logstash -f C:\ELK\LogStash\UnhealthyData.config
Sending Logstash logs to C:/ELK/logstash-7.9.1/logs which is now configured via
log4j2.properties
[2020-11-28T07:33:35,924][INFO ][logstash.runner ] Starting Logstash {"
logstash.version"=>"7.9.1", "jruby.version"=>"jruby 9.2.13.0 (2.5.7) 2020-08-03
9a89c94bcc Java HotSpot(TM) 64-Bit Server VM 25.271-b09 on 1.8.0_271-b09 +indy +
jit [mswin32-x86_64]"}
[2020-11-28T07:33:36,158][WARN ][logstash.config.source.multilocal] Ignoring the
'pipelines.yml' file because modules or command line options are specified
[2020-11-28T07:33:37,058][ERROR][logstash.agent ] Failed to execute ac
tion {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :exception=>"L
ogStash::ConfigurationError", :message=>"Expected one of [A-Za-z0-9_-], [ \\t\\r
\\n], \"#\", \"=>\" at line 7, column 6 (byte 131) after input{\r\n\tfile{\r\n\t
\tpath => \"D:/01_Users/LogStash/Unhealthy.csv\"\r\n\t\tstart_posi
tion => \"beginning\"\r\n\t}\r\n\tfilter{\r\n\t\tcsv", :backtrace=>["C:/ELK/logs
tash-7.9.1/logstash-core/lib/logstash/compiler.rb:32:in `compile_imperative'", "
org/logstash/execution/AbstractPipelineExt.java:183:in `initialize'", "org/logst
ash/execution/JavaBasePipelineExt.java:69:in `initialize'", "C:/ELK/logstash-7.9
.1/logstash-core/lib/logstash/java_pipeline.rb:44:in `initialize'", "C:/ELK/logs
tash-7.9.1/logstash-core/lib/logstash/pipeline_action/create.rb:52:in `execute'"
, "C:/ELK/logstash-7.9.1/logstash-core/lib/logstash/agent.rb:357:in `block in co
nverge_state'"]}
[2020-11-28T07:33:37,355][INFO ][logstash.agent ] Successfully started
Logstash API endpoint {:port=>9600}
[2020-11-28T07:33:42,306][INFO ][logstash.runner ] Logstash shut down.
[2020-11-28T07:33:42,328][ERROR][org.logstash.Logstash ] java.lang.IllegalSta
teException: Logstash stopped processing because of an error: (SystemExit) exit
Request
You have not closed your input section before opening your filter section. As a result, the logstash configuration compiler interpets the csv filter as a csv input
Try moving the final } to after the filter section.

Logstash 6.5.4 executing bash script in filter ruby plugin

I am attempting to execute a bash script using Logstash using filter ruby plugin.
My bash code:
#!/bin/bash
name=$1 log_file="/tmp/test.json"
if [[ -n "$name" ]]; then
echo "$1=$( date +%s )" >> ${log_file} else
echo "argument error" fi
My Logstash config:
input {
file{
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
type => "json"
sincedb_write_interval => "1"
discover_interval => "1"
}
}
filter {
if [host] {
ruby { code => '
require "open3"
host = event.get("host")
cmd = "/bin/bash /etc/logstash/conf.d/bash_scripts/execute.sh #{host}"
stdin, stdout, stderr = Open3.popen3(cmd) '
}
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "bash_execute"
}
}
No errors, the events from the file just go and the bash script does not get triggered. I do not really care about any event confirmation or something like that, just want to execute arbitrary bash script that can accept an external argument from the Logstash event.
Can anyone assist?

How to read /var/log/wtmp logs in elasticsearch

I am trying to read the access log s from /var/log/wtmp in elasticsearch
I can read the file when logged into the box by using last -F /var/log/wtmp
I have logstash running and sending logs to elasticsearch, here is logstash conf file.
input {
file {
path => "/var/log/wtmp"
start_position => "beginning"
}
}
output {
elasticsearch {
host => localhost
protocol => "http"
port => "9200"
}
}
what is showing in elasticsearch is
G
Once i opened the file using less , i could only see binary data.
Now logstash cant understand this data.
A logstash file like the following should work fine -
input {
pipe {
command => "/usr/bin/last -f /var/log/wtmp"
}
}
output {
elasticsearch {
host => localhost
protocol => "http"
port => "9200"
}
}
Vineeth's answer is right but the following cleaner config works as well:
input { pipe { command => "last" } }
last /var/log/wtmp and last are exactly the same.
utmp, wtmp, btmp are Unix files that keep track of user logins and logouts. They cannot be read directly because they are not regular text files. However, there is the last command which displays the information of /var/log/wtmp in plain text.
$ last --help
Usage:
last [options] [<username>...] [<tty>...]
I can read the file when logged into the box by using last -F /var/log/wtmp
I doubt that. What the -F flag does:
-F, --fulltimes print full login and logout times and dates
So, last -F /var/log/wtmp will interpret /var/log/wtmp as a username and won't print any login information.
What the -f flag does:
-f, --file <file> use a specific file instead of /var/log/wtmp

Logstash not matching the pattern

I was learning logstash. Have a very simple config file..
input {
file {
path => "D:\b.log"
start_position => beginning
}
}
# The filter part of this file is commented out to indicate that it is
# optional.
filter {
grok {
match => { "message" => "%{LOGLEVEL:loglevel}" }
}
}
output {
stdout { codec => rubydebug }
}
The input file is just this:
INFO
I am running logstash on windows and the command is
logstash -f logstash.conf
I expect the output to be shown on the console to ensure that its working. But logstash produces no output, just the logstash config messages..
D:\Installables\logstash-2.0.0\logstash-2.0.0\bin>logstash -f logstash.conf
io/console not supported; tty will not be manipulated
Default settings used: Filter workers: 2
Logstash startup completed
I have deleted the sincedb file and tried. Is there something that i am missing?
I think this answers your question:
How to force Logstash to reparse a file?
It looks like you are missing the quotes around "beginning" and the other post recommends redirecting sincedb to dev/null. I don't know if there is a windows equivalent for that. I did use that as well, and it worked fine.
As an alternative, what I do now is to configure stdin() as input so that I don't have to worry about anything else.

Resources