I have mutated it by splitting on new line and now i can access msg[0] ....
like
msg[0] -> CPU=9% TotalMem=12288 MB FreeMem=3895 MB AMQMemoryPercentUsage=0%
msg[1] -> n0-kkk size=11 inFlight=3 enqRate=22 deqRate=22
msg[2] -> n1-xxx size=12 inFlight=4 enqRate=4 deqRate=7
I want to have a for loop on this 'msg' array from array index 1 to n, so i don't have to hard code the index and read each value in this loop. I want to read each array item e.g.
n0-kkk size=11 inFlight=3 enqRate=22 deqRate=22
and process it further like break the first word and label it as 'queue-name',
and i want to load all array item in a list of map.
It tried to us kv filter but it combine all values from same field in an array like
size = 11,12,...
inFlight= 3,4,...
whereas i want to get each array entry as a map
like
{
I would use an approach like this. Find a regexp with two capture groups, one to capture the key, one to capture the value, use .scan to get an array of arrays, then for each array use event.set to add fields to the event.
.scan is a method of the String class that repeatedly matches a regexp against that string. In your case [msg] is an array, so you need something more like
code => '
# Split multiline field into lines, removing newlines
m = event.get("msg").lines(chomp: true)
m.each { |x|
# See if there is a prefix
firstWord = x.match(/^([^ ]+) /)
if firstWord[1] =~ /=/
prefix = ""
else
prefix = "#{firstWord[1]}-"
end
matches = x.scan(/([a-zA-Z]+)=([^ ]+) /)
# matches is an array of arrays, one per match. For example
# [["CPU", "9%"], ["TotalMem", "12288"], ["FreeMem", "3895"]]
matches.each { |y|
event.set("#{prefix}#{y[0]}", y[1])
}
}
'
you could rewrites the matches.each loop to do that.
If you want to include the " MB" in the memory numbers, so that you can use a bytes filter to convert them, then you would need to use a more complex regexp with a lookahead assertion.
You could try event.set("#{prefix}#{y[0]}", y[1].to_i) but I am not sure that works if y[1] is not just a number, so you might need something more like
if y[1] == y[1].to_i.to_s
event.set("#{prefix}#{y[0]}", y[1].to_i)
else
event.set("#{prefix}#{y[0]}", y[1])
end
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.