Getting Data In

REST and javascript not working

jgauthier
Contributor

Greetings,

I am working with the REST API, and have had some success from the command line.
For instance:

curl -u user:password -k https://s-splunk:8089/services/search/jobs -d"search=savedsearch%20Blocked_Sites"

Works. I can see the job running in the jobs. I can pull the stats, and results as well.

However, when I take this same thing and apply it in application.js, the search does not run.

if (Splunk.util.getCurrentView()=="request_search") {
     $.ajax({
        url: 'https://s-splunk:8089/services/search/jobs', 
        type: 'POST', 
        data: 'search=savedsearch%20Blocked_Sites',
        success: function(){
        var messenger = Splunk.Messenger.System.getInstance();
            messenger.send('info', 'splunk', "Job is running....");
        }
     });
}

It is returning 'success', as the info bar in splunk says, 'Job is running....'.

Am I missing something?

Tags (1)

jgauthier
Contributor

This is not a full fledged answer, but it will be too long for a comment.
I was able to get the username, when I thought about my original question. I can do this like this:

 $.ajax({
    url: '/en-US/app/launcher/home', 
    type: 'GET', 
    success: function(msg){
    var messenger = Splunk.Messenger.System.getInstance();
        var start=msg.indexOf(" ", logged+12)+1;
        var end=msg.indexOf(String.fromCharCode(13), start);
        var username=msg.substring(start,end);
        // I have the username.. now what?
    }
 });

This is exciting. But now what? How can I expose this in a dashboard? Can the XML call a js function? Or otherwise make a variable accessible? Thanks!

0 Karma

araitz
Splunk Employee
Splunk Employee

Understood. The easiest way is to access cherrypy.session['user']['name'] via Python, either in a Mako Template or via a Controller. Since you want to display the user name, I would recommend a template. See ./splunk/share/splunk/search_mrsparkle/modules/nav/AccountBar.html for a good example.

0 Karma

jgauthier
Contributor

Ultimately, I want to use the username in dashboards. It doesn't have to be javascript, but I can't figure out any other way to obtain that from a dashboard.

0 Karma

araitz
Splunk Employee
Splunk Employee

Technically, this should probably be a new question. In any case, user and session information are not exposed to JavaScript. Rather, they are exposed via the cherrypy session object - see http://dev.splunk.com/view/SP-CAAADS2#controller_show. Can you explain why you need the user name via JavaScript?

0 Karma

sideview
SplunkTrust
SplunkTrust

Although there are some exceptions if you can get people to configure their browsers differently, in general you will not be able to talk to the 8089 port from anything that loaded from the 8000 port. Although it's only the port that's different, the browser will treat this as any other cross-domain scripting attempt and it'll block it.

Fortunately there are many better ways to do this sort of thing from Javascript. the Splunk UI contains many classes that it uses to talk to the backend. You can see them here. http://dev.splunk.com/view/app-framework-reference/SP-CAAADQF

In particular, the Search object is probably what you're looking for.
http://dev.splunk.com/view/SP-CAAADQV

Assuming you have two handler functions, onSuccess and onFailure, you'd use it to dispatch a search like this:

var search = new Splunk.Search("foo bar | stats count");
var range = new Splunk.TimeRange("-24h@h","now")
search.setTimeRange(range);
search.dispatchJob(onSuccess, onFailure);

As to what to do with the thing then, well that's another story entirely.... If an instance of Splunk.Jobber exists in the page, then it will notice the dispatch and start polling the backend for progress on the Job, triggering progress and done events as appropriate. On the other hand if the Jobber is not there, then you'll have to write your own polling code to do the same thing.

araitz
Splunk Employee
Splunk Employee

Use /en-US/api/search/jobs to access jobs via the Splunk Web port (just as Splunk Web does), or even better, just use the built in search, jobs, and time_range JS objects that we expose.

0 Karma

jgauthier
Contributor

I think I am very close to solving my ultimate objective.
Which is obtaining the user name of the user logged in so I can customize dashboards based on the user logged in.

http://splunk-base.splunk.com/answers/28869/my-username-in-the-dashboard

Thanks for all the help so far.

0 Karma

jgauthier
Contributor

This is great! But I can't get it to work.
I can review the results of a search using this method:
http://host:8000/en-US/splunkd/search/jobs/1313884823.2/results_preview
Anything else gives me an error:
Error loading stylesheet: A network error occured loading an XSLT stylesheet:
http://host:8000/static/atom.xsl

REST endpoints through 8089 for searching begins with /services. When using /splunkd it does not?

I get that error using either:
http://host:8000/en-US/splunkd/services/authentication/current-context/context
OR
http://host:8000/en-US/splunkd/authentication/current-context/context

0 Karma

sideview
SplunkTrust
SplunkTrust

Well if you're trying to interact with the search API, I'd say the JS objects are still the way to go. If you need to hit other parts of the overall REST API, you should look at the /splunkd proxy. If you go to http://host:port/paths , you will see an intriguing path called /splunkd. Although this may dissappear in future versions, it offers a complete GET access to splunkd's endpoints. If you need to POST however you're out of luck, although I think there's some conf setting to even enable that.

0 Karma

jgauthier
Contributor

That's great! Thanks!
I will try that for searches. The pain behind this is that I am trying to access REST endpoints 😞
Unless there is a javascript module, I will have to do it this way somehow.

0 Karma

sideview
SplunkTrust
SplunkTrust

Indeed. The phrase "no REST for the wicked" certainly springs to mind.

araitz
Splunk Employee
Splunk Employee

Brings up at least two points. There needs to be reference for jobber.js, popup.js, and maybe more (need more time). Also need to be higher level topics such as how events are handled, better know a view's hierarchy, and so on.

Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...