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
let apps = svc.apps();
let res = await apps.fetch();
console.log(res.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
let files = svc.configurations();
let propsFile = await files.item("props");
let props = await propsFile.fetch();
console.log(props.properties());
configurations: function(namespace) {
return new root.Configurations(this, namespace);
},
Gets the user that is currently logged in.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
The User
.
let user = await service.currentUser();
console.log("Real name: ", user.properties().realname);
currentUser: function(response_timeout) {
var that = this;
let req = this.get(Paths.currentUser, {}, response_timeout).then((response)=>{
let username = response.data.entry[0].content.username;
let user = new root.User(that, username);
return user.fetch({}, response_timeout)
});
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
let firedAlertGroups = svc.firedAlertGroups();
let res = await firedAlertGroups.fetch();
console.log("# of alert groups: " + res.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: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
getJob: function(sid, namespace,response_timeout) {
if (!response_timeout && utils.isNumber(response_timeout)) {
response_timeout = namespace;
namespace = null;
}
let job = new root.Job(this, sid, namespace);
return job.fetch({}, response_timeout);
},
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
let indexes = svc.indexes();
let res = await indexes.fetch();
let index = res.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
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
let jobs = svc.jobs();
let res = await jobs.fetch();
let list = res.list();
for(let 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: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let result = await service.log("A new event", {index: "_internal", sourcetype: "mysourcetype"});
console.log("Submitted event: ", result);
log: function(event, params,response_timeout) {
if (!response_timeout && utils.isNumber(params)) {
response_timeout = params;
params = {};
}
params = params || {};
// If the event is a JSON object, convert it to a string.
if (utils.isObject(event)) {
event = JSON.stringify(event);
}
let path = this.paths.submitEvent;
let method = "POST";
let headers = {"Content-Type": "text/plain"};
let body = event;
let get = params;
let post = {};
let req = this.request(
path,
method,
get,
post,
body,
headers,
response_timeout
).then((response) => {
return 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: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let results = await service.oneshotSearch("search ERROR", {id: "myjob_123"});
console.log("RESULT FIELDS: ", results.fields);
oneshotSearch: function(query, params, namespace,response_timeout) {
if (!response_timeout && utils.isNumber(response_timeout)) {
response_timeout = namespace;
namespace = null;
}
let jobs = new root.Jobs(this, namespace);
return jobs.oneshotSearch(query, params, response_timeout);
},
Parses a search query.
Name | Type | Description |
---|---|---|
query | String | The search query to parse. |
params | Object | An object of options for the parser: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let parse = await service.parse("search index=_internal | head 1");
console.log("Commands: ", parse.commands);
parse: function(query, params,response_timeout) {
if (!response_timeout && utils.isNumber(params)) {
response_timeout = params;
params = {};
}
params = params || {};
params.q = query;
// Pre-9.0 uses GET and v1 endpoint
if (this.disableV2SearchApi()) {
return this.get(Paths.parser, params, response_timeout).then((response) => {
return response.data;
});
}
// Post-9.0 uses POST and v2 endpoint
return this.post(Paths.parserV2, params, response_timeout).then((response) => {
return 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
let savedSearches = svc.savedSearches();
let res = await savedSearches.fetch();
console.log("# Of Saved Searches: " + res.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: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let newJob = await service.search("search ERROR", {id: "myjob_123"});
console.log("CREATED: ", newJob.sid);
search: function(query, params,namespace, response_timeout) {
if (!response_timeout && utils.isNumber(namespace)) {
response_timeout = namespace;
namespace = null;
}
let jobs = new root.Jobs(this, namespace);
return jobs.search(query, params,response_timeout);
},
Gets configuration information about the server.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let info = await service.serverInfo();
console.log("Splunk Version: ", info.properties().version);
serverInfo: function(response_timeout) {
let serverInfo = new root.ServerInfo(this);
return serverInfo.fetch({}, response_timeout);
},
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.
let svc = ...;
let 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
let storagePasswords = svc.storagePasswords();
let res = await storagePasswords.fetch();
console.log("# of Storage Passwords: " + res.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). |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let options = await service.typeahead("index=", 10);
console.log("Autocompletion options: ", options);
typeahead: function(prefix, count, response_timeout) {
let params = {
count: count || 10,
prefix: prefix
};
return this.get(Paths.typeahead, params,response_timeout).then((response) => {
let results = (response.data || {}).results;
return (results || []);
});
},
Gets the Users
collection, which lets you create, list, and retrieve users.
The Users
collection.
// List all usernames
let users = svc.users();
let res = await users.fetch();
let list = res.list();
for(let 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
let views = svc.views();
let res = await views.fetch();
let list = res.list();
for(let 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. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
del: function(path, params,response_timeout) {
var that = this;
let request = function() {
return that.http.del(
that.urlify(path),
that._headers(),
params,
that.timeout,
response_timeout
);
};
return this._requestWrapper(request);
},
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
let owner = namespace.owner || this.owner || "-";
let 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. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
get: function(path, params, response_timeout, isAsync) {
var that = this;
if(isAsync) {
return that.http.get(
that.urlify(path),
that._headers(),
params,
that.timeout,
response_timeout,
true
);
}
else {
let request = function() {
return that.http.get(
that.urlify(path),
that._headers(),
params,
that.timeout,
response_timeout
);
};
return this._requestWrapper(request);
}
},
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. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
post: function(path, params, response_timeout) {
var that = this;
let request = function() {
return that.http.post(
that.urlify(path),
that._headers(),
params,
that.timeout,
response_timeout
);
};
return this._requestWrapper(request);
},
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. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
request: function(path, method, query, post, body, headers,response_timeout) {
var that = this;
let request = function() {
return that.http.request(
that.urlify(path),
{
method: method,
headers: that._headers(headers),
query: query,
post: post,
body: body,
timeout: that.timeout
}
,response_timeout);
};
return this._requestWrapper(request);
},
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) {
let thisVersion = this.version;
if (thisVersion === "default") {
thisVersion = "5.0";
}
let components1 = thisVersion.split(".");
let components2 = otherVersion.split(".");
let numComponents = Math.max(components1.length, components2.length);
for (let i = 0; i < numComponents; i++) {
let c1 = (i < components1.length) ? parseInt(components1[i], 10) : 0;
let c2 = (i < components2.length) ? parseInt(components2[i], 10) : 0;
if (c1 < c2) {
return -1;
} else if (c1 > c2) {
return 1;
}
}
return 0;
},
disableV2SearchApi: function(){
let val;
if(this.instanceType.toLowerCase() == "cloud"){
val = this.versionCompare("9.0.2209");
}else{
val = this.versionCompare("9.0.2")
}
return val < 0;
}
});