Gets an instance of the Applications
collection, which allows you to list installed apps and retrieve information about them.
Gets an instance of the Applications
collection, which allows you to list installed apps and retrieve information about them.
The Applications
collection.
// List installed apps
var apps = svc.apps();
apps.fetch(function(err) { console.log(apps.list()); });
apps: function() {
return new root.Applications(this);
},
Gets an instance of the Configurations
collection, which lets you create, list, and retrieve configuration (CONF) files.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing). |
The Configurations
collection.
// List all properties in the 'props.conf' file
var files = svc.configurations();
files.item("props", function(err, propsFile) {
propsFile.fetch(function(err, props) {
console.log(props.properties());
});
});
configurations: function(namespace) {
return new root.Configurations(this, namespace);
},
Gets the user that is currently logged in.
Name | Type | Description |
---|---|---|
callback | Function | A function to call with the user instance: |
service.currentUser(function(err, user) {
console.log("Real name: ", user.properties().realname);
});
currentUser: function(callback) {
callback = callback || function() {};
var that = this;
var req = this.get(Paths.currentUser, {}, function(err, response) {
if (err) {
callback(err);
}
else {
var username = response.data.entry[0].content.username;
var user = new root.User(that, username);
user.fetch(function() {
if (req.wasAborted) {
return; // aborted, so ignore
}
else {
callback.apply(null, arguments);
}
});
}
});
return req;
},
Gets an instance of the Indexes
collection, which lets you create, list, and update indexes.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing). |
The Indexes
collection.
// Check if we have an _internal index
var indexes = svc.configurations();
indexes.fetch(function(err, indexes) {
var index = indexes.item("_internal");
console.log("Was index found: " + !!index);
// `index` is an Index object.
});
indexes: function(namespace) {
return new root.Indexes(this, namespace);
},
Constructor for splunkjs.Service
.
Name | Type | Description |
---|---|---|
http | splunkjs.Http | An instance of a |
params | Object | A dictionary of optional parameters: |
A new splunkjs.Service
instance.
init: function() {
this._super.apply(this, arguments);
// We perform the bindings so that every function works
// properly when it is passed as a callback.
this.specialize = utils.bind(this, this.specialize);
this.apps = utils.bind(this, this.apps);
this.configurations = utils.bind(this, this.configurations);
this.indexes = utils.bind(this, this.indexes);
this.savedSearches = utils.bind(this, this.savedSearches);
this.jobs = utils.bind(this, this.jobs);
this.users = utils.bind(this, this.users);
this.currentUser = utils.bind(this, this.currentUser);
this.views = utils.bind(this, this.views);
},
Gets an instance of the Jobs
collection, which lets you create, list, and retrieve search jobs.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing). |
The Jobs
collection.
// List all job IDs
var jobs = svc.jobs();
jobs.fetch(function(err, jobs) {
var list = jobs.list();
for(var i = 0; i < list.length; i++) {
console.log("Job " + (i+1) + ": " + list[i].sid);
}
});
jobs: function(namespace) {
return new root.Jobs(this, namespace);
},
Logs an event to Splunk.
Name | Type | Description |
---|---|---|
event | String | The text for this event. |
params | Object | A dictionary of parameters for indexing: |
callback | Function | A function to call when the event is submitted: |
service.log("A new event", {index: "_internal", sourcetype: "mysourcetype"}, function(err, result) {
console.log("Submitted event: ", result);
});
log: function(event, params, callback) {
if (!callback && utils.isFunction(params)) {
callback = params;
params = {};
}
callback = callback || function() {};
params = params || {};
var path = this.paths.submitEvent;
var method = "POST";
var headers = {"Content-Type": "text/plain"};
var body = event;
var get = params;
var post = {};
var req = this.request(
path,
method,
get,
post,
body,
headers,
function(err, response) {
if (err) {
callback(err);
}
else {
callback(null, response.data);
}
}
);
return req;
}
});
Creates a oneshot search job from a given search query and parameters.
Name | Type | Description |
---|---|---|
query | String | The search query. |
params | Object | A dictionary of properties for the job. For details, see the POST search/jobs endpoint in the REST API documentation. |
namespace | Object | Namespace information (owner, app, sharing). |
callback | Function | A function to call with the results of the job: |
service.oneshotSearch("search ERROR", {id: "myjob_123"}, function(err, results) {
console.log("RESULT FIELDS": results.fields);
});
oneshotSearch: function(query, params, namespace, callback) {
if (!callback && utils.isFunction(namespace)) {
callback = namespace;
namespace = null;
}
var jobs = new root.Jobs(this, {}, namespace);
return jobs.oneshotSearch(query, params, callback);
},
Parses a search query.
Name | Type | Description |
---|---|---|
query | String | The search query to parse. |
params | Object | An object of options for the parser. For details, see the GET search/parser endpoint in the REST API documentation. |
callback | Function | A function to call with the parse info: |
service.parse("search index=_internal | head 1", function(err, parse) {
console.log("Commands: ", parse.commands);
});
parse: function(query, params, callback) {
if (!callback && utils.isFunction(params)) {
callback = params;
params = {};
}
callback = callback || function() {};
params = params || {};
params.q = query;
return this.get(Paths.parser, params, function(err, response) {
if (err) {
callback(err);
}
else {
callback(null, response.data);
}
});
},
Gets an instance of the SavedSearches
collection, which lets you create, list, and update saved searches.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing). |
The SavedSearches
collection.
// List all # of saved searches
var savedSearches = svc.savedSearches();
savedSearches.fetch(function(err, savedSearches) {
console.log("# Of Saved Searches: " + savedSearches.list().length);
});
savedSearches: function(namespace) {
return new root.SavedSearches(this, namespace);
},
Creates an asyncronous search job with a given search query and parameters.
Name | Type | Description |
---|---|---|
query | String | The search query. |
params | Object | A dictionary of properties for the job. For details, see the POST search/jobs endpoint in the REST API documentation. |
namespace | Object | Namespace information (owner, app, sharing). |
callback | Function | A function to call with the created job: |
service.search("search ERROR", {id: "myjob_123"}, function(err, newJob) {
console.log("CREATED": newJob.sid);
});
search: function(query, params, namespace, callback) {
if (!callback && utils.isFunction(namespace)) {
callback = namespace;
namespace = null;
}
var jobs = new root.Jobs(this, {}, namespace);
return jobs.search(query, params, callback);
},
Gets configuration information about the server.
Name | Type | Description |
---|---|---|
callback | Function | A function to call with the server info: |
service.serverInfo(function(err, info) {
console.log("Splunk Version: ", info.properties().version);
});
serverInfo: function(callback) {
callback = callback || function() {};
var serverInfo = new root.ServerInfo(this);
return serverInfo.fetch(callback);
},
Creates a specialized version of the current Service
instance for a specific owner and app.
Name | Type | Description |
---|---|---|
owner | String | The owner of the specialized service. |
app | String | The app of the specialized service. |
The specialized Service
instance.
var svc = ...;
var newService = svc.specialize("myuser", "unix");
specialize: function(owner, app) {
return new Service(this.http, {
scheme: this.scheme,
host: this.host,
port: this.port,
username: this.username,
password: this.password,
owner: owner,
app: app,
sessionKey: this.sessionKey,
version: this.version
});
},
Provides auto-complete suggestions for search queries.
Name | Type | Description |
---|---|---|
prefix | String | The query fragment to autocomplete. |
count | Number | The number of options to return (optional). |
callback | Function | A function to call with the autocompletion info: |
service.typeahead("index=", 10, function(err, options) {
console.log("Autocompletion options: ", options);
});
typeahead: function(prefix, count, callback) {
if (!callback && utils.isFunction(count)) {
callback = count;
count = 10;
}
callback = callback || function() {};
var params = {
count: count || 10,
prefix: prefix
};
return this.get(Paths.typeahead, params, function(err, response) {
if (err) {
callback(err);
}
else {
var results = (response.data || {}).results;
callback(null, results || []);
}
});
},
Gets an instance of the Users
collection, which lets you create, list, and retrieve users.
The Users
collection.
// List all usernames
var users = svc.users();
users.fetch(function(err, users) {
var list = users.list();
for(var i = 0; i < list.length; i++) {
console.log("User " + (i+1) + ": " + list[i].properties().name);
}
});
users: function() {
return new root.Users(this);
},
Gets an instance of the Views
collection, which lets you create, list, and retrieve views (custom UIs built in Splunk's app framework).
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing). |
The Views
collection.
// List all views
var views = svc.views();
views.fetch(function(err, views) {
var list = views.list();
for(var i = 0; i < list.length; i++) {
console.log("View " + (i+1) + ": " + list[i].properties().name);
}
});
views: function(namespace) {
return new root.Views(this, namespace);
},
Performs a DELETE request.
Name | Type | Description |
---|---|---|
path | String | The path of the DELETE request. |
params | Object | The query parameters for this request. |
callback | Function | The function to call when the request is complete: |
del: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.del(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Converts a partial path to a fully-qualified path, and if necessary includes the owner and app prefixes.
Name | Type | Description |
---|---|---|
path | String | Partial path |
namespace | String | The namespace context, as 'owner/app'. |
Fully qualified path
fullpath: function(path, namespace) {
namespace = namespace || {};
if (utils.startsWith(path, "/")) {
return path;
}
// If we don't have an app name (explicitly or implicitly), we default to /services/
if (!namespace.app && !this.app && namespace.sharing !== root.Sharing.SYSTEM) {
return "/services/" + path;
}
// Get the app and owner, first from the passed in namespace, then the service,
// finally defaulting to wild cards
var owner = namespace.owner || this.owner || "-";
var app = namespace.app || this.app || "-";
namespace.sharing = (namespace.sharing || "").toLowerCase();
// Modify the owner and app appropriately based on the sharing parameter
if (namespace.sharing === root.Sharing.APP || namespace.sharing === root.Sharing.GLOBAL) {
owner = "nobody";
}
else if (namespace.sharing === root.Sharing.SYSTEM) {
owner = "nobody";
app = "system";
}
return utils.trim("/servicesNS/" + owner + "/" + app + "/" + path);
},
Performs a GET request.
Name | Type | Description |
---|---|---|
path | String | The path of the GET request. |
params | Object | The query parameters for this request. |
callback | Function | The function to call when the request is complete: |
get: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.get(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Performs a POST request.
Name | Type | Description |
---|---|---|
path | String | The path of the POST request. |
params | Object | The query parameters for this request. |
callback | Function | The function to call when the request is complete: |
post: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.post(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Performs a request.
Name | Type | Description |
---|---|---|
path | String | The request URL (with any query parameters already appended and encoded). |
method | String | The HTTP method (can be |
headers | Object | An object of headers for this request. |
body | Object | The body parameters for this request. |
callback | Function | The function to call when the request is complete: |
request: function(path, method, query, post, body, headers, callback) {
var that = this;
var request = function(callback) {
return that.http.request(
that.urlify(path),
{
method: method,
headers: that._headers(headers),
query: query,
post: post,
body: body,
timeout: 0
},
callback
);
};
return this._requestWrapper(request, callback);
}
});