TIPS & TRICKS TIPS & TRICKS

The Splunk SDKs for Ruby and C# are now in Beta

The Splunk SDKs for Ruby and C# have reached Beta! Developers familiar with Ruby and C#/.NET can now easily leverage their existing skills to integrate data and functionality from Splunk with other applications across the enterprise, letting the entire organization get more value out of Splunk. Do you have an existing reporting app or customer support system that would benefit from being able to search and display data from Splunk? Want to build a .NET or Ruby app powered by Splunk data?  Then these SDKs are for you. As Beta releases these SDKs are now fully supported, customers with support contracts are covered for any questions about the Splunk SDKs for C# and Ruby.

Let’s take a look at some sample Ruby code …

Connect to Splunk

require 'splunk-sdk-ruby'
service = Splunk::connect(:scheme=>"https", :host=>"localhost", :port=>8089, :username=>"admin", :password=>"changeme")

Run a oneshot search and print the results

stream = service.create_oneshot("search index=_internal | head 10")
reader = Splunk::ResultsReader.new(stream)
reader.each do |result|
  puts result
end

Run an export and print the results

stream = service.create_export("search index=_internal | head 10")
reader = Splunk::ResultsReader.new(stream)
reader.each do |result|
  puts result
end

Write events into Splunk

main = service.indexes["main"]

# Using the simple receiver endpoint
main.submit("This is a test event.")

# Using the streaming receiver endpoint
socket = main.attach()
begin
  socket.write("The first event.\r\n")
  socket.write("The second event.\r\n")
ensure
  socket.close()
end

Now let’s take a look at some sample C# code …

Connect to Splunk

// Define the context of the Splunk service
ServiceArgs svcArgs = new ServiceArgs();
svcArgs.Host = "localhost";
svcArgs.Port = 8089;

// Create a Service instance and log in
Service service = new Service(svcArgs);
service = service.Login("admin", "changeme");

List Splunk objects, e.g. list of apps installed on Splunk

foreach (var app in service.GetApplications().Values)
{
    Console.WriteLine(app.Name);

    // Write a seperator between the name and the description of an app.
    Console.WriteLine(Enumerable.Repeat('-', app.Name.Length).ToArray());

    Console.WriteLine(app.Description);
    Console.WriteLine();
}

Run a search and display results

var jobs = service.GetJobs();
var job = jobs.Create("search index=_internal | head 10");
while (!job.IsDone)
{
    Thread.Sleep(1000);
}

var outArgs = new Args
{
    { "output_mode", "json" },
};

using (var stream = job.Results(outArgs))
{
    using (var rr = new ResultsReaderJson(stream))
    {
        foreach (var map in rr)
        {
            System.Console.WriteLine("EVENT:");
            foreach (string key in map.Keys)
            {
                System.Console.WriteLine("   " + key + " -> " + map[key]);
            }
        }
    }
}

Write events into Splunk

var args = new Args
{
    { "source", "splunk-sdk-tests" },
    { "sourcetype", "splunk-sdk-test-event" }
};

Receiver receiver = new Receiver(service);

// Submit to default index using the simple receiver endpoint
receiver.Submit(args, "Hello World from C# SDK!");
Jon Rooney
Posted by Jon Rooney

Jon Rooney is head of product marketing at Splunk. He joined Splunk in 2012, having formerly served as Splunk’s senior director of ITOA solutions marketing and as director of developer marketing. In his five years as a Splunker, Jon has spearheaded Splunk’s developer outreach, given many presentations on the Splunk developer platform, Splunk of DevOps and Splunk for ITOps, and led the IT Ops marketing team. Before Splunk, he worked in marketing and product management at Microsoft for a number of developer platform offerings (Windows Azure, Silverlight, ASP.NET), served at a handful of now-departed startups in San Francisco and stockpiled air miles as a consultant at Accenture back when it was still Andersen Consulting. Growing up in the Philadelphia area, his exposure to mass market red licorice was limited to Twizzlers. Red Vines didn’t cross his path until he moved West in his early 20s. In Jon's words, "I was raised right."

Join the Discussion