# Logstash filtering on eventId in ruby

**URL:** <https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117>\
**Category:** Logstash\
**Created:** [June 11, 2019, 7:48am UTC](https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117 "2019-06-11T07:48:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dries\_vanherpe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dries_vanherpe/32/47838_2.png) [@dries\_vanherpe](https://discuss.elastic.co/u/dries_vanherpe)\
**Post date:** [June 11, 2019, 7:48am UTC](https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117/1 "2019-06-11T07:48:52Z")

</div>

So im trying to filter with the ruby filter plugin in Logstash. Im pretty new to ruby and Logstash but I have tried some code and it doesn't seem to work. It looks like it won't go inside the CSV.foreach. I want to give a weight to certain event ids in the csv.  
this is my logstash.conf :

```
input {
  beats {
      port => 5000
    }
}

filter {
  ruby {

    code =>
    "
    require 'csv'
    content = File.read('/usr/share/logstash/CSV/DC.csv')
    csv = CSV.parse(content, headers:true)
    id = event.get('winlog.event_id')
    csv.each do |row|
        if row['Event ID'] == id
            event.set('weigth', row['Weigth'])
        end
    end
    if event.get('Weigth').nil?
        event.set('Weigth', 0)
    end
 "
  }
}
output {
   elasticsearch {
		hosts => "elasticsearch:9200"
		user => #######
		password => ######
	}
}

```

my csv looks like this :

Event ID;Weight  
4672;100  
4625;80  
4648;80  
4657;80  
4657;80

The result I get is something like this where weight is standard 0 :

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/4/6495d7cd91e69ef41558e5503bd90b191cf48f84.png)

---

<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:** [June 11, 2019, 9:42am UTC](https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117/2 "2019-06-11T09:42:16Z")

</div>

Your csv has a column 'Weight' but your configuration is looking for 'Weigth'.

---

<div class="post-metadata">

**Author:** ![dries\_vanherpe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dries_vanherpe/32/47838_2.png) [@dries\_vanherpe](https://discuss.elastic.co/u/dries_vanherpe)\
**Post date:** [June 11, 2019, 1:03pm UTC](https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117/3 "2019-06-11T13:03:10Z")

</div>

solved it

```
require 'csv'
content = File.read('/usr/share/logstash/CSV/DC.csv')
csv = CSV.parse(content ,col_sep:';', headers:true)
id = event.get('[winlog][event_id]')
csv.each do |row|
    if row[0].to_s == id.to_s
        event.set('weigth', row[1])
    end
end
if event.get('weigth').nil?
    event.set('weigth', 0)
end 
"
```

---

<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:** [July 9, 2019, 1:03pm UTC](https://discuss.elastic.co/t/logstash-filtering-on-eventid-in-ruby/185117/4 "2019-07-09T13:03:12Z")

</div>

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