Splunk Search

Creating a simple chart from extracting specific string

philallen1
Path Finder

Hi

This should be nice and easy for you lot.

I have an application producing thousands of logs a day. In some of these logs there will be the phrase: "IncomeData SCV link" and some of the other logs there will be the phrase: "IncomeData started in". The two phrases will never be in the same log.
They are also in different positions within the log, so I think field extractions is out of the question)

I want to simply produce a column chart like below, that counts how many logs have the first phrase in and how many have the second phrase in.

I use the below search to pull out all the logs, but I can't finish it off to produce the chart.

sourcetype="UserLogs" Username=* "IncomeData SCV link" OR "IncomeData started in"

alt text

Tags (4)
1 Solution

_d_
Splunk Employee
Splunk Employee

The basic unit of performing statistics in splunk is called a field. In your case you need to make those phrases (or more precisely, the presence of those phrases in events) into fields before you can build charts/tables. Here is one way of doing it:

sourcetype="UserLogs" Username=* "IncomeData SCV link" OR "IncomeData started in" | rex "(?<phrase>IncomeData SCV link)" | rex "(?<phrase>IncomeData started in)" | stats count by phrase

View solution in original post

philallen1
Path Finder

Yes, that's exactly right. It's the count of events.

0 Karma

jtrucks
Splunk Employee
Splunk Employee

I would do it like this (with one rex):

sourcetype="UserLogs" Username=* "IncomeData SCV link" OR "IncomeData started in" | rex field=_raw ".*\s(?<phrase>IncomeData (SCV link|started in))\s.*" | stats count by phrase

This is slightly more efficient and on large data volumes will be more efficient (faster).

--
Jesse Trucks
Minister of Magic

_d_
Splunk Employee
Splunk Employee

Actually, the backtracking due to (two!) greedy quantifiers (.*) as well as the alternation (|) within the capture group make that a very inefficient regex. But, it does work.

0 Karma

somesoni2
Revered Legend

You can try this

sourcetype="userlogs" UserName=* "IncomeData SCV link" | stats count as "IncomeData SCV link" | appendcols [search sourcetype="userlogs" UserName=* "IncomeData started in" | stats count as "IncomeData started in"]

_d_
Splunk Employee
Splunk Employee

The basic unit of performing statistics in splunk is called a field. In your case you need to make those phrases (or more precisely, the presence of those phrases in events) into fields before you can build charts/tables. Here is one way of doing it:

sourcetype="UserLogs" Username=* "IncomeData SCV link" OR "IncomeData started in" | rex "(?<phrase>IncomeData SCV link)" | rex "(?<phrase>IncomeData started in)" | stats count by phrase

philallen1
Path Finder

This worked perfectly - thank you

0 Karma

jtrucks
Splunk Employee
Splunk Employee

By "how many logs" do you mean a count of events? so in your UserLogs sourcetype, say you had 1000 entries with "IncomeData SCV link" and 1500 entries with "IncomeData started in" you want the chart like you showed to reflet those numbers?

--
Jesse Trucks
Minister of Magic
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...