Getting Data In

how to sum the machine OS in a JSON file

garumaru
Explorer

Hi Guys,

I have a JSON file for OS type in some cluster like below:

{
"clusterA": ubuntu,
"clusterA": ubuntu,
"clusterA": rhel5,
"clusterA": sles11,
"clusterB": sles11,
"clusterB": sles11,
"clusterB": ubuntu,
"clusterC": centos,
"clusterC": ubuntu
...
}

I'd like sum the OS type for each cluster, like in above sample, 2 ubuntu in clusterA, 1 rhel5 in clusterA etc.
Would you please kindly help out?

Thank you!

0 Karma

vnravikumar
Champion

Hi

Check this

| makeresults 
| eval json="{\"clusterA\": ubuntu,
 \"clusterA\": ubuntu,
 \"clusterA\": rhel5,
 \"clusterA\": sles11,
 \"clusterB\": sles11,
 \"clusterB\": sles11,
 \"clusterB\": ubuntu,
 \"clusterC\": centos,
 \"clusterC\": ubuntu}" 
| eval json=replace(json,"\"|{|\s|}","") 
| makemv delim="," json 
| mvexpand json 
| eval temp=split(json,":"),cluster=mvindex(temp,0),os=mvindex(temp,1) 
| stats count by cluster,os
0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="{\"clusterA\": ubuntu,
\"clusterA\": ubuntu,
\"clusterA\": rhel5,
\"clusterA\": sles11,
\"clusterB\": sles11,
\"clusterB\": sles11,
\"clusterB\": ubuntu,
\"clusterC\": centos,
\"clusterC\": ubuntu}"
| rex mode=sed "s/: (\w+)/: \"\1\"/g"
| spath
| foreach cluster* [ eval <<FIELD>> = mvjoin('<<FIELD>>' , ",")]
| untable _time cluster OS
| eval OS = split(OS,",")
| mvexpand OS
| stats count by OS cluster
| sort cluster
0 Karma
Get Updates on the Splunk Community!

Leveraging Detections from the Splunk Threat Research Team & Cisco Talos

  Now On Demand  Stay ahead of today’s evolving threats with the combined power of the Splunk Threat Research ...

New in Splunk Observability Cloud: Automated Archiving for Unused Metrics

Automated Archival is a new capability within Metrics Management; which is a robust usage & cost optimization ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...