How to move nested key value to root level and then rename elements root name
I have 4 core cpu and below is a sample of details for 2 cpu core from the source.
{
"_source": {
"cpu_usage": {
"sys/host-info/2": {
"sys/hostInfo/2/cpuInfo": {
"sys/hostInfo/2/cpuInfo/1": {
"user": 212142779,
"system": 42193851
},
"sys/hostInfo/2/cpuInfo/0": {
"user": 71408201,
"system": 20886608
}
},
"memoryUsed": 2745607080,
"memoryTotal": 6712508416
}
}
},
"fields": {
"cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo.sys/hostInfo/2/cpuInfo/1.system": [
42193851
],
"cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo.sys/hostInfo/2/cpuInfo/0.user": [
71408201
],
"cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo.sys/hostInfo/2/cpuInfo/0.system": [
20886608
],
"cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo.sys/hostInfo/2/cpuInfo/1.user": [
212142779
]
}
}
I would like to move contents under "cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo.sys/hostInfo/2/cpuInfo" to root and then rename "0." into "cpu0." and "1." into "cpu1"
I have tried below logstash filter and I was able move elements into "cpu_usage.sys/host-info/2.sys/hostInfo/2/cpuInfo" into root
filter {
if [cpu_usage] {
ruby {
code => '
event.get("[cpu_usage][sys/host-info/2]").each { |k,v|
event.set(k,v)
}
event.remove("[cpu_usage][sys/host-info/2]")
'
}
}
if [sys/hostInfo/2/cpuInfo] {
ruby {
code => '
event.get("[sys/hostInfo/2/cpuInfo]").each { |k,v|
event.set(k,v)
}
event.remove("[sys/hostInfo/2/cpuInfo]")
'
}
}
}