Splunk Search

stats count issue

krusovice
Path Finder

Hello,

I hit a problem in the query below. I believed I'm not allow to form the stat count 2 times in the query. The result of this query result 0 which is not the right answer expected. How can I achieve to get the % failed from the result of the regex?

index=* 
 | rex field=_raw "Request Name\=(?<request_name>[\w+]+)" 
 | rex field=_raw "Request type.*Id\=(?<>[toy\w+]+).*Type\=Toy"
 | rex field=_raw "Request type.*Id\=(?<>[play\w+]+).*Type\=Play"
 | rex field=_raw "Failed.*FailId\=(?<fail_count>[\w+]+)"
 | stats count(request_name) as total, count(faild_count) as failed
 | eval percentage=round(((failed/total)*100),2)."%"
 | stats count(request_name) by "Total Request", count(toy) as "Toy Count", count(play) as "Play Count", count(failed_count) as "Failed Count", values(percentage) as "% Failed" by date_hour

My Expected table should look like this:

date_hour      Total Request      Toy Count     Play Count       Failed Count      % Failed
  1                 50               30            10               10                20%
Tags (2)
0 Karma
1 Solution

p_gurav
Champion

Can you try:

index=* 
  | rex field=_raw "Request Name\=(?<request_name>[\w+]+)" 
  | rex field=_raw "Request type.*Id\=(?<>[toy\w+]+).*Type\=Toy"
  | rex field=_raw "Request type.*Id\=(?<>[play\w+]+).*Type\=Play"
  | rex field=_raw "Failed.*FailId\=(?<fail_count>[\w+]+)"
  | stats count(request_name) as "Total Request", count(toy) as "Toy Count", count(play) as "Play Count", count(failed_count) as "Failed Count" by date_hour
  | eval percentage=round((('Failed Count'/'Total Request')*100),2)."%"

View solution in original post

0 Karma

p_gurav
Champion

Can you try:

index=* 
  | rex field=_raw "Request Name\=(?<request_name>[\w+]+)" 
  | rex field=_raw "Request type.*Id\=(?<>[toy\w+]+).*Type\=Toy"
  | rex field=_raw "Request type.*Id\=(?<>[play\w+]+).*Type\=Play"
  | rex field=_raw "Failed.*FailId\=(?<fail_count>[\w+]+)"
  | stats count(request_name) as "Total Request", count(toy) as "Toy Count", count(play) as "Play Count", count(failed_count) as "Failed Count" by date_hour
  | eval percentage=round((('Failed Count'/'Total Request')*100),2)."%"
0 Karma

krusovice
Path Finder

It's works. Learning something new today.
Thanks 🙂

0 Karma

niketn
Legend

@krusovice , if possible you should also have single Regular Expression to fetch request_name, toy, play and failed_count from raw data, instead of performing rex extraction four times. Refer to .conf 2017 session on Beyond Regular Regular Expressions by @cpetterborg

Eventually you should convert regular expression based extractions as Fields Knowledge Object for easy maintenance and reuse.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...

Purpose in Action: How Splunk Is Helping Power an Inclusive Future for All

At Cisco, purpose isn’t a tagline—it’s a commitment. Cisco’s FY25 Purpose Report outlines how the company is ...

[Upcoming Webinar] Demo Day: Transforming IT Operations with Splunk

Join us for a live Demo Day at the Cisco Store on January 21st 10:00am - 11:00am PST In the fast-paced world ...