Getting Data In
Highlighted

REST and javascript not working

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)
Highlighted

Re: REST and javascript not working

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.

Highlighted

Re: REST and javascript not working

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.

Highlighted

Re: REST and javascript not working

SplunkTrust
SplunkTrust

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

Highlighted

Re: REST and javascript not working

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
Highlighted

Re: REST and javascript not working

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
Highlighted

Re: REST and javascript not working

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
Highlighted

Re: REST and javascript not working

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
Highlighted

Re: REST and javascript not working

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
Highlighted

Re: REST and javascript not working

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