Splunk Search

How do I use the eval command to calculate error rates and show them as percentages?

moizmmz
Path Finder

Hello,

I am creating a dashboard in which I am displaying total logins, successful logins, failed logins, error rates for each failed login, and percentages of failed logins.

Where:

Total Logins say count is 1000
Successful logins where status=200 say count=800
Failed logins is when status!=200 say count = 200

i.e., when status=401(say count = 100) or 204 (say count = 50) or 404 (say count = 50)

I first need to calculate the error rate for each failed login. So say error rate for status =401. I know the math i.e., 50/1000 where 50 is count for 401 and 100 is the total logins. But, I don't know how to use the eval command very well. Pls help!!

Also, I need to calculate the percentage of failed logins i.e., (200/1000)*100. Again, need help with the eval command. Pls help!!

Tags (2)
0 Karma
1 Solution

kmaron
Motivator
index=akamai cp=654130 login 
| eventstats count (status) as Total 
| where status!=200 
| stats count(status) as Error, values(Total) as Total by status 
| eval error_rate= Error/Total 
| fields error_rate status

View solution in original post

0 Karma

CryoHydra
Path Finder

Check if this helps.

your search
| stats count(eval(status="200")) as success count(eval(status!=200) as failed count(eval(status) as total_logon

| eval success_rate =(success/total_logon)*100 , failure_rate=(failed/total_logon)*100

0 Karma

moizmmz
Path Finder

Nope it gives an error 😞

0 Karma

CryoHydra
Path Finder

now try, if it is successful accept the answer.

My previous search was intended for each failed connection from a client, now i changed for over all summary.

0 Karma

kmaron
Motivator
index=akamai cp=654130 login 
| eventstats count (status) as Total 
| where status!=200 
| stats count(status) as Error, values(Total) as Total by status 
| eval error_rate= Error/Total 
| fields error_rate status
0 Karma

moizmmz
Path Finder

I just ran this and it worked perfectly!!'

Marry me 😛

0 Karma

kmaron
Motivator

great!! I'll convert this to an answer then you can accept it.

0 Karma

kmaron
Motivator
eval Error_Rate = (error_count/login_count)*100
eval Percent_Failed = (failed_count/login_count)*100
0 Karma

moizmmz
Path Finder

Its not working. Problem is that I cant use count in eval.

For example: Here Total_logins is count(status)

so I wrote: eval Total_logins=count(status), but it returns an error. Pls help!

0 Karma

Vijeta
Influencer

Try this

yourquery| streamstats count(status) as Total|stats count(eval(Status!=200)) as Error by Status| eval Error_Rate = (Error/Total)*100

0 Karma

moizmmz
Path Finder

This doesn't work 😞 Help pls!

0 Karma

Vijeta
Influencer

Try this

 index=akamai cp=654130 login| eventstats count (status) as Total|where status!=200| stats count(status) as Error, values(Total) as Total by status| eval error_rate= Error/Total
0 Karma

moizmmz
Path Finder

Well... the result displays the status, total_logins and number of errors. However, I want to display only the error rate and the status

0 Karma

Vijeta
Influencer

You can add |fields error_rate status at the end of query

0 Karma

moizmmz
Path Finder

But there's no value for error_rate. It doesn't seem to be calculating it. I'm sorry for all the trouble 😞

0 Karma

kmaron
Motivator
Try this:

index=akamai cp=654130 login 
| eval error_ind = if(status=200,0,1) 
| stats count(status) as Total, sum(error_ind) as Errors by status 
| eval error_rate= (Errors/Total)*100
| fields error_rate status
0 Karma

kmaron
Motivator

@moizmmz did this help you at all?

0 Karma

Vijeta
Influencer

@moizmmz what does your event look like and what are field names for status? Each event means a login?

0 Karma

moizmmz
Path Finder

Here's my query:

index=akamai cp=654130 login| stats count (status)

This basically gives me the total logins.

Next, status=200 gives me successful logins and status!=200 gives me failed logins.

0 Karma

kmaron
Motivator

You need to use stats in order to get your counts. I assumed you already had those counts.

0 Karma

moizmmz
Path Finder

hmm.. I still dont understand how to get them. pls help!!

For example: how would I get total_logins?

0 Karma
Get Updates on the Splunk Community!

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

A Guide To Cloud Migration Success

As enterprises’ rapid expansion to the cloud continues, IT leaders are continuously looking for ways to focus ...