# Ruby Script in Logstash :: Enters a NULL into the Data?

**URL:** <https://discuss.elastic.co/t/ruby-script-in-logstash-enters-a-null-into-the-data/255727>\
**Category:** Logstash\
**Tags:** docker\
**Created:** [November 17, 2020, 3:41pm UTC](https://discuss.elastic.co/t/ruby-script-in-logstash-enters-a-null-into-the-data/255727 "2020-11-17T15:41:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![redapplesonly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/redapplesonly/32/57700_2.png) [@redapplesonly](https://discuss.elastic.co/u/redapplesonly)\
**Post date:** [November 17, 2020, 3:41pm UTC](https://discuss.elastic.co/t/ruby-script-in-logstash-enters-a-null-into-the-data/255727/1 "2020-11-17T15:41:54Z")

</div>

Hi Logstash Jedi Masters,

I’m running Logstash 7.7.1 in the Docker container. (Yes, I know I need to upgrade) In my Logstash config file, I’m running a snippet of Ruby code:

```
  ruby {
    init => "
      require 'socket'
    "

    code => '
      socket = TCPSocket.new("192.168.3.1", 12345)
      socket.write (event.to_hash).to_s
      readable = IO.select [socket], nil, nil, 0.1
      if readable then
        response = socket.recv(1000)
      else
        response = "UNAVAILABLE"
      end
      event.set("nDPI_Application", response)
      socket.close
    ' 
  }

```

When a data record arrives in my Logstash, the code is supposed to do the following: Open a TCP socket to a remote host, send the entire event to the host, then wait 100 msec to get a response back. The remote host should send back a single string. If the remote host replies in time, the host’s response is added to a new field called `RemoteHostInfo`. However, if no response is received, then Logstash should populate the `RemoteHostInfo` field with “_ **UNAVAILABLE** _”

(I am not a Ruby coder, and the code has been pasted together with a lot of trial-and-error.)

My Logstash exports all data to an Elasticsearch instance, so I can monitor the final data records there. The good news is that when the remote host is available, I can see that the above Ruby code works great. However, when the remote host is down, I see this in the records:

```
sql> select HostA, HostB, RemoteHostInfo, sum( totalBytes )
> from \"myIndex\"
> where \"@timestamp\" >= NOW()- INTERVAL 10 MINUTES
> group by HostA, HostB, RemoteHostInfo;
  HostA | HostB | RemoteHostInfo | sum( totalBytes )
------------+------------+----------------+------------------
10.10.10.50 |10.10.10.100|null |1378304.0
10.10.10.51 |10.10.10.110|null |4.95383552E9
10.10.10.52 |10.10.10.120|null |4.843754496E9
10.10.10.53 |10.10.10.130|null |5.076754432E9
10.10.10.54 |10.10.10.140|null |2.46971392E8
10.10.10.55 |10.10.10.150|null |1.53665536E8 

```

Hmm. So when the remote host is not reachable, Logstash is populating the `RemoteHostInfo` field with _ **NULL** _, not “_ **UNAVAILABLE** _”. That’s not necessarily a bad thing, but it does create data automation problems further downstream in my pipeline.

What I can’t tell is if this is a Ruby problem or a Logstash problem. And unfortunately, I don’t know how to troubleshoot the Ruby code when its within the Docker container. Any tips? 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:** [December 15, 2020, 3:41pm UTC](https://discuss.elastic.co/t/ruby-script-in-logstash-enters-a-null-into-the-data/255727/2 "2020-12-15T15:41:54Z")

</div>

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