Kamikaze K
Kamikaze K

Reputation: 191

HEXADECIMAL string to Binary and Binary to decimal in log-stash using ruby filter

I am working with logstash and I am using ruby filter to change HEX to decimal which works fine for one set of data. What I use for this is ;

ruby{
        code => "event.set('xxx', event.get('xx').hex)"
    }

I am looking for something similar that will convert HEX to binary then also convert it to decimal after I grok against patterm.The reason I need to do this is because I am picking uneven bits from the hex values e.g

Hex string - "ABC" BINARY - "101010111100"

Then I will use GROK to only pick the first three bits "101" and convert to decimal field which is the value I want , and also for example the next "010" to another field. I am just after help with the two conversions.Not Grok

Upvotes: 0

Views: 589

Answers (1)

Badger
Badger

Reputation: 4072

You can do the conversions by using to_i and to_s and specifying the base

    mutate { add_field => { "[HexNum]" => "4a" } }
    ruby {
        code => '
            hexNum = event.get("HexNum")
            event.set("binNum", hexNum.to_i(16).to_s(2))
        '
    }

You can then use your grok filter to pick out the parts of [binNum] that you want and put them into a couple of fields, then convert them to decimal

    ruby {
        code => '
            event.set("field1", event.get("field1").to_i(2).to_s)
            event.set("field2", event.get("field2").to_i(2).to_s)
        '
    }

Upvotes: 1

Related Questions