Gets the Applications
collection, which allows you to list installed apps and retrieve information about them.
Gets 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 the Configurations
collection, which lets you create, list, and retrieve configuration (.conf) files.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
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: |
The User
.
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 the DataModels
collection, which lets you create, list, and retrieve data models.
dataModels: function(namespace) {
return new root.DataModels(this, namespace);
},
Gets the FiredAlertGroupCollection
collection, which lets you list alert groups.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
The FiredAlertGroupCollection
collection.
// List all # of fired alert groups
var firedAlertGroups = svc.firedAlertGroups();
firedAlertGroups.fetch(function(err, firedAlertGroups) {
console.log("# of alert groups: " + firedAlertGroups.list().length);
});
firedAlertGroups: function(namespace) {
return new root.FiredAlertGroupCollection(this, namespace);
},
A convenience method to get a Job
by its sid.
Name | Type | Description |
---|---|---|
sid | String | The search ID for a search job. |
namespace | Object | Namespace information: |
callback | Function | A function to call with the created job: |
getJob: function(sid, namespace, callback) {
if (!callback && utils.isFunction(namespace)) {
callback = namespace;
namespace = null;
}
var job = new root.Job(this, sid, namespace);
return job.fetch({}, callback);
},
Gets the Indexes
collection, which lets you create, list, and update indexes.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
The Indexes
collection.
// Check if we have an _internal index
var indexes = svc.indexes();
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);
this.firedAlertGroups = utils.bind(this, this.firedAlertGroups);
this.dataModels = utils.bind(this, this.dataModels);
},
Gets the Jobs
collection, which lets you create, list, and retrieve search jobs.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
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,Object | The text for this event, or a JSON object. |
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 || {};
// If the event is a JSON object, convert it to a string.
if (utils.isObject(event)) {
event = JSON.stringify(event);
}
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 from a given search query and optional parameters.
Name | Type | Description |
---|---|---|
query | String | The search query. |
params | Object | A dictionary of properties for the search: |
namespace | Object | Namespace information: |
callback | Function | A function to call with the results of the search: |
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: |
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 the SavedSearches
collection, which lets you create, list, and update saved searches.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
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 a search job with a given search query and optional parameters, including exec_mode
to specify the type of search:
Use exec_mode=normal
to return a search job ID immediately (default). Poll for completion to find out when you can retrieve search results.
Use exec_mode=blocking
to return the search job ID when the search has finished.
To run a oneshot search, which does not create a job but rather returns the search results, use Service.oneshotSearch
.
Name | Type | Description |
---|---|---|
query | String | The search query. |
params | Object | A dictionary of properties for the job. For a list of available parameters, see Search job parameters on Splunk Developer Portal. |
namespace | Object | Namespace information: |
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 namespace context.
Name | Type | Description |
---|---|---|
owner | String | The Splunk username, such as "admin". A value of "nobody" means no specific user. The "-" wildcard means all users. |
app | String | The app context for this resource (such as "search"). The "-" wildcard means all apps. |
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
});
},
Gets the StoragePasswords
collection, which lets you create, list, and update storage passwords.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information: |
The StoragePasswords
collection.
// List all # of storage passwords
var storagePasswords = svc.storagePasswords();
storagePasswords.fetch(function(err, storagePasswords) {
console.log("# of Storage Passwords: " + storagePasswords.list().length);
});
storagePasswords: function(namespace) {
return new root.StoragePasswords(this, namespace);
},
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 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 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: |
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 REST endpoint path of the DELETE request. |
params | Object | The entity-specific 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,
that.timeout,
callback
);
};
return this._requestWrapper(request, callback);
},
Converts a partial path to a fully-qualified path to a REST endpoint, and if necessary includes the namespace owner and app.
Name | Type | Description |
---|---|---|
path | String | The partial path. |
namespace | String | The namespace, in the format "owner/app". |
The 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/" + encodeURIComponent(owner) + "/" + encodeURIComponent(app) + "/" + path);
},
Performs a GET request.
Name | Type | Description |
---|---|---|
path | String | The REST endpoint path of the GET request. |
params | Object | The entity-specific parameters for this request. |
callback | Function | The function to call when the request is complete: |
get: function(path, params, callback, isAsync) {
var that = this;
if(isAsync) {
return that.http.get(
that.urlify(path),
that._headers(),
params,
that.timeout,
null,
true
);
}
else {
var request = function(callback) {
return that.http.get(
that.urlify(path),
that._headers(),
params,
that.timeout,
callback
);
};
return this._requestWrapper(request, callback);
}
},
Performs a POST request.
Name | Type | Description |
---|---|---|
path | String | The REST endpoint path of the POST request. |
params | Object | The entity-specific 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,
that.timeout,
callback
);
};
return this._requestWrapper(request, callback);
},
Issues an arbitrary HTTP request to the REST endpoint path segment.
Name | Type | Description |
---|---|---|
path | String | The REST endpoint path segment (with any query parameters already appended and encoded). |
method | String | The HTTP method (can be |
query | Object | The entity-specific parameters for this request. |
post | Object | A dictionary of POST argument that will get form encoded. |
body | Object | The body of the request, mutually exclusive with |
headers | Object | Headers 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: that.timeout
},
callback
);
};
return this._requestWrapper(request, callback);
},
Compares the Splunk server's version to the specified version string. Returns -1 if (this.version < otherVersion), 0 if (this.version == otherVersion), 1 if (this.version > otherVersion).
Name | Type | Description |
---|---|---|
otherVersion | String | The other version string, for example "5.0". |
versionCompare: function(otherVersion) {
var thisVersion = this.version;
if (thisVersion === "default") {
thisVersion = "5.0";
}
var components1 = thisVersion.split(".");
var components2 = otherVersion.split(".");
var numComponents = Math.max(components1.length, components2.length);
for (var i = 0; i < numComponents; i++) {
var c1 = (i < components1.length) ? parseInt(components1[i], 10) : 0;
var c2 = (i < components2.length) ? parseInt(components2[i], 10) : 0;
if (c1 < c2) {
return -1;
} else if (c1 > c2) {
return 1;
}
}
return 0;
}
});