Dashboards & Visualizations

How to add events into Splunk index from DJango app dashboard

vganjare
Builder

Hi,

I am having a dashboard which is a part of DJango app. On the dashboard, we have a data table. User can click on the data table and can add the comments. We want to store these comments in an index and with specific sourcetype. How can we add new events from dashboard built in django app? Is there any way to use the splunk JS module?

One restriction though, we don't want to hardcode the user credentials in the code. I tried the tutorial http://dev.splunk.com/view/javascript-sdk/SP-CAAAEJ3

But, this requires user credentials (which we want to avoid).

Thanks!!

0 Karma
1 Solution

LukeMurphey
Champion

You can do this using the 'receivers/simple' endpoint. Note that users will need the 'edit_tcp' capability in order to call this endpoint.

Below is some Javascript that illustrates how to call it:

function makeContentBody(fields){

            // Make the content body field
            var content_body = '';

            for (var key in fields) {
                content_body = content_body + key + '=\"' + fields[key] + '\", ';
            }

            return content_body;
}

function makeAnEvent(){

    // Prepare the arguments
    var params = new Object();
    params.index = 'main';
    params.source = 'Ajax made event';
    params.sourcetype = 'my_event';
    params.host = document.domain;
    params.output_mode = 'json';

    var content = new Object();
    content.some_field = "some_value_1";
    content.another_field = "another_value_1";

    var uri = Splunk.util.make_url('/splunkd/__raw/services/receivers/simple');
    uri += '?' + Splunk.util.propToQueryString(params);

    // Fire off the request
    jQuery.ajax({
        url:         uri,
        type:        'POST',
        data:        makeContentBody(content),
        contentType: false,
        processData: false,
        success: function(result) {
            alert("That worked!");
        }
    });
}

// Now make an event
makeAnEvent();

You should see the event when you search for "sourcetype=my_event".

View solution in original post

LukeMurphey
Champion

You can do this using the 'receivers/simple' endpoint. Note that users will need the 'edit_tcp' capability in order to call this endpoint.

Below is some Javascript that illustrates how to call it:

function makeContentBody(fields){

            // Make the content body field
            var content_body = '';

            for (var key in fields) {
                content_body = content_body + key + '=\"' + fields[key] + '\", ';
            }

            return content_body;
}

function makeAnEvent(){

    // Prepare the arguments
    var params = new Object();
    params.index = 'main';
    params.source = 'Ajax made event';
    params.sourcetype = 'my_event';
    params.host = document.domain;
    params.output_mode = 'json';

    var content = new Object();
    content.some_field = "some_value_1";
    content.another_field = "another_value_1";

    var uri = Splunk.util.make_url('/splunkd/__raw/services/receivers/simple');
    uri += '?' + Splunk.util.propToQueryString(params);

    // Fire off the request
    jQuery.ajax({
        url:         uri,
        type:        'POST',
        data:        makeContentBody(content),
        contentType: false,
        processData: false,
        success: function(result) {
            alert("That worked!");
        }
    });
}

// Now make an event
makeAnEvent();

You should see the event when you search for "sourcetype=my_event".

vganjare
Builder

Hi,

This solution will use the session ID from the logged in user. Is it possible to use default admin user session id? Basically, whether this will work or not is dependent on the logged in user. Is there any way to make it independent of the logged in user?

Thanks!!

0 Karma

LukeMurphey
Champion

You could make it independent of the logged-in user but it will be more complicated. To do that, you could have the Javascript write to a lookup file or index and then have a scripted input that runs under the admin user and takes the data from wherever the Javascript put it (lookup or index) and then does something with it.

0 Karma