# \[ruby\] filter configuration is invalid

**URL:** https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958
**Category:** Logstash
**Created:** [September 18, 2019, 9:10am UTC](https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958 "2019-09-18T09:10:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hdma](https://avatars.discourse-cdn.com/v4/letter/h/ebca7d/32.png) [@hdma](https://discuss.elastic.co/u/hdma)
#### Post date: [September 18, 2019, 9:10am UTC](https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958/1 "2019-09-18T09:10:54Z")

</div>

Hello  
I tried to create a ruby's filter to permit logging messaage only when I have mounted a partition. This is the conf file:  
filter {  
ruby {  
code =\> 'if File.readlines("/proc/mounts").any?{ |line| line.split(' ')[0] != "/dev/drbd0" }  
logger.info("drbd is not mounted. I drop every event")  
event.cancel  
end'  
}  
}  
output {  
if "wm" in [tags] {  
file {  
codec =\> line { format =\> "%{message}" }  
path =\> "/drbd/web-logs/wm.log"  
}  
}  
if "wm\_pf" in [tags] {  
file {  
codec =\> line { format =\> "%{message}" }  
path =\> "/drbd/promo-logs/wm\_pf.log"  
}  
}  
}

This is the log:  
[2019-09-18T11:06:50,489][INFO][logstash.agent] Successfully started Logstash API endpoint {:port=\>9600}  
[2019-09-18T11:06:55,367][INFO][logstash.runner] Logstash shut down.  
[2019-09-18T11:07:18,494][INFO][logstash.runner] Starting Logstash {"logstash.version"=\>"7.3.0"}  
[2019-09-18T11:07:20,100][ERROR][logstash.agent] Failed to execute action {:action=\>LogStash::PipelineAction::Create/pipeline\_id:main, :exception=\>"LogStash::ConfigurationError", :message=\>"Expected one of #, =\> at line 14, column 2 (byte 279) after filter {\n\truby {\n\t\tcode =\> 'if File.readlines("\/proc\/mounts").any?{ |line| line.split(' ')[0] != "\/dev\/drbd0" }\n\t\t\t\tlogger.info("drbd is not mounted. I drop every event")\n\t\t\t\tevent.cancel\n\t\t\tend'\n\t", :backtrace=\>["/usr/share/logstash/logstash-core/lib/logstash/compiler.rb:41:in `compile_imperative'", "/usr/share/logstash/logstash-core/lib/logstash/compiler.rb:49:in`compile\_graph'", "/usr/share/logstash/logstash-core/lib/logstash/compiler.rb:11:in `block in compile_sources'", "org/jruby/RubyArray.java:2577:in`map'", "/usr/share/logstash/logstash-core/lib/logstash/compiler.rb:10:in `compile_sources'", "org/logstash/execution/AbstractPipelineExt.java:151:in`initialize'", "org/logstash/execution/JavaBasePipelineExt.java:47:in `initialize'", "/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:24:in`initialize'", "/usr/share/logstash/logstash-core/lib/logstash/pipeline\_action/create.rb:36:in `execute'", "/usr/share/logstash/logstash-core/lib/logstash/agent.rb:325:in`block in converge\_state'"]}  
[2019-09-18T11:07:20,434][INFO][logstash.agent] Successfully started Logstash API endpoint {:port=\>9600}

If I test from command line:  
bin/logstash --path.settings="/etc/logstash/" -e "filter { ruby { code =\> ' if File.readlines("/proc/mounts").any?{ |line| line.split(' ')[0] != "/dev/drbd0" } event.cancel end' } }" -t  
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.  
WARNING: An illegal reflective access operation has occurred  
WARNING: Illegal reflective access by com.headius.backport9.modules.Modules (file:/usr/share/logstash/logstash-core/lib/jars/jruby-complete-9.2.7.0.jar) to field java.io.FileDescriptor.fd  
WARNING: Please consider reporting this to the maintainers of com.headius.backport9.modules.Modules  
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations  
WARNING: All illegal access operations will be denied in a future release  
Thread.exclusive is deprecated, use Thread::Mutex  
Sending Logstash logs to /var/log/logstash which is now configured via log4j2.properties  
[2019-09-18T11:10:12,516][WARN][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified  
[2019-09-18T11:10:13,106][FATAL][logstash.runner] The given configuration is invalid. Reason: Expected one of #, =\> at line 3, column 126 (byte 201) after filter { ruby { code =\> ' if File.readlines(/proc/mounts).any?{ |line| line.split(' ')[0] != /dev/drbd0 } event.cancel end'  
[2019-09-18T11:10:13,122][ERROR][org.logstash.Logstash] java.lang.IllegalStateException: Logstash stopped processing because of an error: (SystemExit) exit

Please, can I help me?

Thank you

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [September 18, 2019, 12:49pm UTC](https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958/2 "2019-09-18T12:49:36Z")

</div>

> [@hdma](#):
>
> line.split(' ')

You should use double quotes or escape the single quotes here.

The error message is telling you it does not know what to do with the quoted string that follows the code option

```
')[0] != "/dev/drbd0" } ... end'

```

---

<div class="post-metadata">

### Author: ![hdma](https://avatars.discourse-cdn.com/v4/letter/h/ebca7d/32.png) [@hdma](https://discuss.elastic.co/u/hdma)
#### Post date: [September 18, 2019, 3:09pm UTC](https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958/3 "2019-09-18T15:09:18Z")

</div>

> [@Badger](#):
>
> line.split(' ')

Resolved. This is the final configuration:  
filter {  
ruby {  
code =\> '  
if File.readlines("/proc/mounts").any?{ |line| line.split(" ")[0] == "/dev/drbd0" }  
logger.info("drbd is mounted.");  
else  
logger.info("drbd is not mounted. I drop every event");  
event.cancel;  
end  
'  
}  
}  
output {  
if "wm" in [tags] {  
#elasticsearch {  
# hosts =\> ["[http://0.0.0.0:9200](http://0.0.0.0:9200)"]  
# index =\> "%{[@metadata][beat]}-%{+YYYY.MM.dd}"  
# document\_type =\> "%{[@metadata][type]}"  
#}  
file {  
codec =\> line {  
format =\> "%{message}"  
}  
path =\> "/drbd/web-logs/wm.log"  
}  
}  
if "wm\_pf" in [tags] {  
#elasticsearch {  
# hosts =\> ["[http://0.0.0.0:9200](http://0.0.0.0:9200)"]  
# index =\> "%{[@metadata][beat]}-%{+YYYY.MM.dd}"  
# document\_type =\> "%{[@metadata][type]}"  
#}  
file {  
codec =\> line {  
format =\> "%{message}"  
}  
path =\> "/drbd/promo-logs/wm\_pf.log"  
}  
}  
}

Thank you.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 16, 2019, 3:09pm UTC](https://discuss.elastic.co/t/ruby-filter-configuration-is-invalid/199958/4 "2019-10-16T15:09:25Z")

</div>

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