Ruby exception occurred: undefined method `*' for nil:NilClass

I have a filter that is as follows:

	event.set('[json][event][packetloss1]', event.get('[packets-received]') / event.get('[packets-sent]') * 100)
	event.set('[json][event][packetlosspercentage]', 100 - event.get('[json][event][packetloss1]'))

What I am trying to do is calculate the % of packets that didn't make it ( % of Packets Lost) but no matter the figures input in Packets Sent / Received, packetloss1 is always equals to 0 and I'm unsure why that is

I suspect [packets-received] and [packets-sent] are integers. Try

ruby {
    code => '
        received = event.get("[packets-received]")
        sent = event.get("[packets-sent]")
        if sent and received
            event.set("[json][event][packetlosspercentage]", 100 * (1.0 -  received.to_f / sent.to_f))
        end
    '
}

Thanks Badger,

I had some errors at first so had to tweak it a little bit but got this working:

event.set('[received]', event.get('[packets-received]'))
event.set('[sent]', event.get('[packets-sent]'))
event.set('[json][event][packetlosspercentage]', 100 * (1.0 -  event.get('[received]').to_f / event.get('[sent]').to_f))

Only the output is still the same in the Logstash console -

[ERROR][logstash.filters.ruby    ][main] Ruby exception occurred: undefined method `*' for nil:NilClass

I can see in the event that comes into Elastic that received is 6000 and sent is 7265 but im having trouble getting this filter to perform those calculations - and yes it is an integer

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