# Logstash ruby code error

**URL:** https://discuss.elastic.co/t/logstash-ruby-code-error/302030
**Category:** Logstash
**Created:** [April 9, 2022, 3:06pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-error/302030 "2022-04-09T15:06:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![leela](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@leela](https://discuss.elastic.co/u/leela)
#### Post date: [April 9, 2022, 3:06pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-error/302030/1 "2022-04-09T15:06:47Z")

</div>

ruby {  
code =\> "  
byt = event.get('[network][bytes]');  
bytes = byt\*8;  
microsec = ((event.get('[netflow][flow\_end\_sys\_up\_time]'))-(event.get('[netflow][flow\_start\_sys\_up\_time]')))/1000;  
avg = bytes/microsec;  
event.set('netavg\_bps', avg);  
event.set('total\_bytes', bytes);

```
     "

   }

```

For above am getting error..  
like this  
Ruby exception occurred: undefined method `*' for nil:NilClass {:class=>"NoMethodError", :backtrace=>["(ruby filter code):4:in `block in filter\_method.  
if am giving microsec!=0  
Ruby exception occurred: divided by 0 {:class=\>"ZeroDivisionError", :backtrace=\>["(ruby filter code):7:  
for correct code  
can anyone help on this

---

<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: [April 9, 2022, 5:29pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-error/302030/2 "2022-04-09T17:29:34Z")

</div>

> [@leela](#):
>
> byt = event.get('[network][bytes]');  
> bytes = byt\*8;

If an event does not have a [network][bytes] field then this will produce that exception. You need to test it before using it

```
byt = event.get('[network][bytes]');
if byt
    bytes = byt*8;
    ...
end

```

---

<div class="post-metadata">

### Author: ![leela](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@leela](https://discuss.elastic.co/u/leela)
#### Post date: [April 9, 2022, 5:35pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-error/302030/3 "2022-04-09T17:35:37Z")

</div>

> [@Badger](#):
>
> oes not have a [n

thanks..

---

<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: [May 7, 2022, 5:36pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-error/302030/4 "2022-05-07T17:36:36Z")

</div>

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