Splunk Search

RStudio integration with Splunk

mgianola
Explorer

Is there any way to run Splunk queries from the RStudio IDE rather than from within the search bar?

Tags (1)
0 Karma
1 Solution

aljohnson_splun
Splunk Employee
Splunk Employee

The Splunk® SDKs are written on top of the Splunk REST APIs. The intent is to give you broad coverage of the REST API in a language-specific fashion to ease your access to the Splunk engine.

Currently, Splunk has SDKs for these languages:

  • Python
  • Java
  • JavaScript
  • PHP
  • Ruby
  • C#

As you will note, R is not on this list.


however,

All of these are just wrappers around the REST api. You can query the the REST api directly using the RCurl and xml libraries.
In RStudio:

install.packages('RCurl')
install.packages('XML')

Once you've got both of those, you can go ahead and look at the REST api documentation.

To be honest, this is all really ugly with the xml responses and I don't think you want to do it...

library(RCurl)
library(xml)
opts = curlOptions(userpwd="admin:changeme", ssl.verifypeer=FALSE, username="admin", password="changeme")
response <- getURL("https://localhost:8089/servicesNS/admin/search/saved/searches/test", .opts = opts)
parsed <- xmlParse(response)

You get the idea. It's not pretty.

tl;dr

can you? yes. should you? no. use one of the sdks.

look at this python-ease

import splunklib.client as client

HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

# Print installed apps to the console to verify login
for app in service.apps:
    print app.name

View solution in original post

0 Karma

aljohnson_splun
Splunk Employee
Splunk Employee

The Splunk® SDKs are written on top of the Splunk REST APIs. The intent is to give you broad coverage of the REST API in a language-specific fashion to ease your access to the Splunk engine.

Currently, Splunk has SDKs for these languages:

  • Python
  • Java
  • JavaScript
  • PHP
  • Ruby
  • C#

As you will note, R is not on this list.


however,

All of these are just wrappers around the REST api. You can query the the REST api directly using the RCurl and xml libraries.
In RStudio:

install.packages('RCurl')
install.packages('XML')

Once you've got both of those, you can go ahead and look at the REST api documentation.

To be honest, this is all really ugly with the xml responses and I don't think you want to do it...

library(RCurl)
library(xml)
opts = curlOptions(userpwd="admin:changeme", ssl.verifypeer=FALSE, username="admin", password="changeme")
response <- getURL("https://localhost:8089/servicesNS/admin/search/saved/searches/test", .opts = opts)
parsed <- xmlParse(response)

You get the idea. It's not pretty.

tl;dr

can you? yes. should you? no. use one of the sdks.

look at this python-ease

import splunklib.client as client

HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

# Print installed apps to the console to verify login
for app in service.apps:
    print app.name
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...