Extracting certain fields from Splunk query results - splunk

I want to print the value of a certain field from a set of events that results from running a particular search query. Here's my query:
index=abc "all events that contain this string" sourcetype=prd
Now, this returns certain events that contain a field called traceId. What I want is to extract unique traceIds from the result and print them. Here's the query that I am using currently, but to no avail:
index=abc "all events that contain this string" sourcetype=prd | rex field=_raw "traceId: (?<traceId>.*)"
This query prints all the fields in the event (events are printed as JSON docs.).
Can someone help me with this? I have never worked with Splunk before, so please go easy if the question looks a bit easy.
Thanks!

Answering this without some sample data is almost impossible... still, I think you are getting all data because:
you are not using the fields command to filter your fields of interest. It would go like so: `index=abc "all events that contain this string" sourcetype=prd | rex field=_raw "traceId: (?.*) | fields fiel1, field2, traceId"
your regular expression is greedy, which means traceId field will contain all text from that point to the end of the event. Try to be more specific i.e. \d+ for numeric data or even [^\s]+ for non-blanks.
~HTH

Related

Blank CSV in splunk report

in splunk report, can we get at least header(column name)in attached CSV in autogenerated email, if there are no data in CSV/splunk..
eg. if no data then also it should atleast show header name..
If there are no results, you shouldn't be getting anything sent to you in your Alert/Scheduled Report
Why would you expect to see header rows, when they only "exist" if there are rows of data?
When you run the Report manually, you'll note when there are "no results" there are, well ... "no results"
There is no header information because there is no information
Edit to address comment "there is one requirement from client that they want atleast column name if report is blank"
You need to manually create a "blank" report, then: something similar to this should work:
index=ndx sourcetype=srctp
| fillnull value="-" fieldA, fieldB, fieldC ...
<rest of search before stats>
| stats count by *
<rest of search>
That should mean when you eventually statsout your table, you'll have at least something "in every field" - which means you should always get at least one row (even if it's all dashes)

How to accumulate counts from different searches into one (pie) chart?

I have 5 different searches I am doing in Splunk where I am getting the count of how many results from that search query.
I've had a look at this thread here:
https://answers.splunk.com/answers/757081/pie-chart-with-count-from-different-search-criteri.html
but its not quite working for me, I'm not 100% sure if its what I want.
My search queries all look something like this:
index=A variable="foo" message="Created*" | stats count
index=A variable="foo" message="Deleted*" | stats count
I ideally want to assign each query to a keyword - such as created, deleted, etc, then do a pie chart based on the counts.
The following should be sufficient.
index=A variable="foo" message="Created*" OR message="Deleted*" OR message="<repeat this for any other message types you want>" | stats count by message
If you can provide some more examples of the events you are trying to chart, there may be alternate approaches that can work for you.
This version will extract the key part of the message (Created, Deleted. etc...) into a field called mtype and you can then perform stats on that field.
index=A variable="foo" message="Created*" OR message="Deleted*" OR message="<repeat this for any other message types you want>" | rex field=message "(?<mtype>Created|Deleteted|...)" | stats count by mtype

Splunk only select matching JSON data

I load JSON reports into Splunk and those reports have many arrays. When I search:
source=test| search "behavior.system.processes.process{}.fileactivities.fileCreated.call{}.path"="C:\\Windows*"
I often like to show the matching data. I use table to do so:
source=test| search "behavior.system.processes.process{}.fileactivities.fileCreated.call{}.path"="C:\\Windows*" | table "behavior.system.processes.process{}.fileactivities.fileCreated.call{}.path"
However, the issue is that this shows me all fileCreated of the matching event and not only the one starting with C:\Windows.
How do I filter that?
#joe-jeff
I posted answer on answers.splunk.com. Please check below link.
https://answers.splunk.com/answers/745093/only-select-matching-json-data.html

Query to find the unique code in splunk

can some one suggest a query to send the unique errorcode count.
Example enter image description here 2006
in between the tags(in place of 2006) different codes are printed
i need to query to pull all the unique error codes
You can use the rex command to extract the desired values. It will look something like this:
your_initial_query
| rex field=_raw "<com:errorCode>(?<code>.*)<\/com:errorCode>"
| stats count by code
The second line tells rex to extract everything between the errorCode tags and save that to a field called code. You can then use the stats command to count the number of times a code is seen.

How to move raw data in Splunk with no field assigned to a table?

This might be a really simple question, but I haven't been able to find an answer as of yet. I have some raw data from some events that is for example "(duration 5555ms)" and I want to put that in a "| timechart span=1m count by duration" to create a chart that shows when these events took place and their total duration. There is currently no field set up for duration, it is just raw data. How would I get those numbers into my time chart?
You will first need to extract the value for duration into a field. You will most likely use the regex (rex) function for this.
The exact command you need will depend a lot on your data. But for your example "(duration 5555ms)", this should work assuming the value is always in ms.
| rex field=_raw "\(duration (?<duration>\d+)ms.*"