Splunk Search

regex to extract part of the variable

damucka
Builder

Hello,

I need help with regex. I have the following string under the Tracefile variable in my search:

    /usr/sap/BWP/HDB02/ls5926/trace/DB_BWP/indexserver_ls5926.30240.crashdump.20181108-093323.134243.trc

My search looks as follows:

| metadata type=sources index=mlbso 
| rename totalCount as "Log Entries" source as "Tracefile" firstTime as "First Event" lastTime as "Last Event" recentTime as "Last Update" 
| fieldformat "Log Entries"=tostring('Log Entries', "commas") 
| fieldformat "First Event"=strftime('First Event', "%c") 
| fieldformat "Last Event"=strftime('Last Event', "%c") 
| fieldformat "Last Update"=strftime('Last Update', "%c") 
| search Tracefile=CASE("*BWP*crashdump*") AND Tracefile!="/usr/sap/trans*" 
| sort - "Last Update" 
| head 1

Which basically gives back the Tracefile above as an output. So far so good.
Now, I would like to calculate the elapsed time since the last crash, which would be basically something like:

now() - 20181108-093323

How would I extract the "20181108-093323" from the Tracefile variable, convert it to the datetime, make a time delta and output it in the reasonable form of dd:hh:mm:ss?

Kind Regards,
Kamil

Tags (1)
0 Karma
1 Solution

harsmarvania57
Ultra Champion

Hi @damucka,

Please try below search, in below search if you do not want to convert diff seconds to days, hours, minutes then remove | eval crashtime=tostring(diff, "duration") from below query

| metadata type=sources index=mlbso 
 | rename totalCount as "Log Entries" source as "Tracefile" firstTime as "First Event" lastTime as "Last Event" recentTime as "Last Update" 
 | fieldformat "Log Entries"=tostring('Log Entries', "commas") 
 | fieldformat "First Event"=strftime('First Event', "%c") 
 | fieldformat "Last Event"=strftime('Last Event', "%c") 
 | fieldformat "Last Update"=strftime('Last Update', "%c") 
 | search Tracefile=CASE("*BWP*crashdump*") AND Tracefile!="/usr/sap/trans*" 
| rex field=Tracefile "crashdump\.(?<crash_time>\d+\-\d+)\."
| eval crash_time_epoch=strptime(crash_time, "%Y%m%d-%H%M%S")
| eval diff = now() - crash_time_epoch
| eval crashtime=tostring(diff, "duration")
| sort - "Last Update"
| head 1

Below is run anywhere search which is generating correct result.

| makeresults
| eval Tracefile="/usr/sap/BWP/HDB02/ls5926/trace/DB_BWP/indexserver_ls5926.30240.crashdump.20181108-093323.134243.trc"
| rex field=Tracefile "crashdump\.(?<crash_time>\d+\-\d+)\."
| eval crash_time_epoch=strptime(crash_time, "%Y%m%d-%H%M%S")
| eval diff = now() - crash_time_epoch
| eval crashtime=tostring(diff, "duration")

View solution in original post

0 Karma

harsmarvania57
Ultra Champion

Hi @damucka,

Please try below search, in below search if you do not want to convert diff seconds to days, hours, minutes then remove | eval crashtime=tostring(diff, "duration") from below query

| metadata type=sources index=mlbso 
 | rename totalCount as "Log Entries" source as "Tracefile" firstTime as "First Event" lastTime as "Last Event" recentTime as "Last Update" 
 | fieldformat "Log Entries"=tostring('Log Entries', "commas") 
 | fieldformat "First Event"=strftime('First Event', "%c") 
 | fieldformat "Last Event"=strftime('Last Event', "%c") 
 | fieldformat "Last Update"=strftime('Last Update', "%c") 
 | search Tracefile=CASE("*BWP*crashdump*") AND Tracefile!="/usr/sap/trans*" 
| rex field=Tracefile "crashdump\.(?<crash_time>\d+\-\d+)\."
| eval crash_time_epoch=strptime(crash_time, "%Y%m%d-%H%M%S")
| eval diff = now() - crash_time_epoch
| eval crashtime=tostring(diff, "duration")
| sort - "Last Update"
| head 1

Below is run anywhere search which is generating correct result.

| makeresults
| eval Tracefile="/usr/sap/BWP/HDB02/ls5926/trace/DB_BWP/indexserver_ls5926.30240.crashdump.20181108-093323.134243.trc"
| rex field=Tracefile "crashdump\.(?<crash_time>\d+\-\d+)\."
| eval crash_time_epoch=strptime(crash_time, "%Y%m%d-%H%M%S")
| eval diff = now() - crash_time_epoch
| eval crashtime=tostring(diff, "duration")
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...