Splunk Search

How to compare the counts of certain fields to find a ratio in a search?

flytrinity
Engager

We have a server access log with format like below.

10:30:30.269 HostID=1001, ClientConnectionStatus=Connecting,...
10:30:32.269 HostID=1004, ClientConnectionStatus=Connecting,...
10:30:33.269 HostID=1004, ClientConnectionStatus=Connected,...
10:30:50.269 HostID=1001, ClientConnectionStatus=Connecting,...
10:31:10.269 HostID=1001, ClientConnectionStatus=Connecting,...
10:31:11.269 HostID=1001, ClientConnectionStatus=Connected,...

So a host is trying to connect to server and if it fails it will re-try it every 20sec (in this example, host 1001 has tried three times and eventually connected at 3rd attempt while host 1004 was able to connect by its 1st attempt). We'd like to have a search/report to count the ratio of connected/connecting attempts per HostID, for example 1001 is 1/3 and 1004 is 1 in this case. Was wondering what's the best way of constructing this?

Thank you!

Tags (3)
1 Solution

gkanapathy
Splunk Employee
Splunk Employee
sourcetype=mydata 
| stats count(eval(ClientConnectionStatus=="Connecting")) AS connecting_count
        count(eval(ClientConnectionStatus=="Connected")) AS connected_count
  by HostID
| eval ratio=connected_count/connecting_count

View solution in original post

gkanapathy
Splunk Employee
Splunk Employee
sourcetype=mydata 
| stats count(eval(ClientConnectionStatus=="Connecting")) AS connecting_count
        count(eval(ClientConnectionStatus=="Connected")) AS connected_count
  by HostID
| eval ratio=connected_count/connecting_count

flytrinity
Engager

Grt thanks for the quick answer, gkanapathy! I will have a try when i back to office later. Thanks very much.

0 Karma

upmangaurav
Explorer

Hi Gerald, I am getting the second count(here 'connected_count') as 0 with a similar query. Could you please suggest an alternative to this approach if you have any in mind?

0 Karma
Get Updates on the Splunk Community!

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...