Knowledge Management

Macro optimization

chlima
Explorer

Hi,

I've created a macro to convert IPv6 (IP field) to IPv4 connotation (Ex: 0000000000000000000000FFFF0a0a0a0a > 10.10.10.10)

Is that:

| eval a=tonumber(substr(IP,25,2))
| eval a=tonumber(substr(IP,27,2))
| eval a=tonumber(substr(IP,29,2))
| eval a=tonumber(substr(IP,31,2))
| IP = a+"."+b+"."+c+"."+d

Someone know another method more efficient to do it optimizing the macro execution?

0 Karma
1 Solution

woodcock
Esteemed Legend

You can swap 4 substr commands with a single rex command and that should be very much quicker like this:

| rex field=32BitIP "(?i)(?<JuNkT3Mp_1>[0-9A-F]{2})(?<JuNkT3Mp_2>[0-9A-F]{2})(?<JuNkT3Mp_3>[0-9A-F]{2})(?<JuNkT3Mp_4>[0-9A-F]{2})"
| eval IP = tostring(tonumber(JuNkT3Mp_1,16)) . "."
          . tostring(tonumber(JuNkT3Mp_2,16)) . "." 
          . tostring(tonumber(JuNkT3Mp_3,16)) . "."
          . tostring(tonumber(JuNkT3Mp_4,16))
| fields - JuNkT3Mp*

View solution in original post

0 Karma

woodcock
Esteemed Legend

You can swap 4 substr commands with a single rex command and that should be very much quicker like this:

| rex field=32BitIP "(?i)(?<JuNkT3Mp_1>[0-9A-F]{2})(?<JuNkT3Mp_2>[0-9A-F]{2})(?<JuNkT3Mp_3>[0-9A-F]{2})(?<JuNkT3Mp_4>[0-9A-F]{2})"
| eval IP = tostring(tonumber(JuNkT3Mp_1,16)) . "."
          . tostring(tonumber(JuNkT3Mp_2,16)) . "." 
          . tostring(tonumber(JuNkT3Mp_3,16)) . "."
          . tostring(tonumber(JuNkT3Mp_4,16))
| fields - JuNkT3Mp*
0 Karma

lguinn2
Legend

Why not

IP = tonumber(substr(IP,25,2),16) . "." . tonumber(substr(IP,27,2),16) . "." . tonumber(substr(IP,29,2),16) . "." . tonumber(substr(IP,31,2),16)

although I have no idea if that will actually be faster. It will work, because you have to specify the "16" to get it to convert from hex.

(edited to fix the problem mentioned in the comments - replaced "+" with ".", which is concatenation)

0 Karma

woodcock
Esteemed Legend

4 passes to substr vs. 1 pass to rex in mine (should be 4x faster)

0 Karma

chlima
Explorer

Thanks for the answer lguinn, but it's returning an error.

Error in 'eval' command: Typechecking failed. '+' only takes two strings or two numbers.

So it that i've defined four variables (a,b,c,d).

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...