Constructor for splunkjs.Service
Constructor for splunkjs.Service
Name | Type | Description |
---|---|---|
http | splunkjs.Http | An instance of a |
params | Object | Dictionary of optional parameters: scheme, host, port, username, password, owner, app, sessionKey |
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);
},
Get an instance of the Users collection
The Users collection allows you to list users, create new ones, get a specific user, etc.
// 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);
},
Get an instance of the Views collection
The Views collection allows you to list views, create new ones, get a specific user, etc.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing) |
// 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);
},
Log an event to splunk
Name | Type | Description |
---|---|---|
event | String | The text for this event |
params | Object | A dictionary of parameters for indexing: index, host, host_regex, source, sourcetype |
callback | Function | A callback when the event was 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 + "?" + Http.encode(params);
var method = "POST";
var headers = {};
var body = event;
var req = this.request(path, method, headers, body, function(err, response) {
if (err) {
callback(err);
}
else {
callback(null, response.data);
}
});
return req;
}
});
Create an asyncronous search job
Create a search job using the specified query and parameters.
Name | Type | Description |
---|---|---|
query | String | The search query |
params | Object | A dictionary of properties for the job. |
namespace | Object | Namespace information (owner, app, sharing) |
callback | Function | A callback 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);
},
Get an instance of the Applications collection
The Applications collection allows you to list installed applications, create new ones, etc.
// List installed apps
var apps = svc.apps();
apps.fetch(function(err) { console.log(apps.list()); });
apps: function() {
return new root.Applications(this);
},
Get an instance of the Indexes collection
The Indexes collection allows you to list indexes, create new indexes, update indexes, etc.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing) |
// 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);
},
Parse a search string
Name | Type | Description |
---|---|---|
query | String | The search query to parse |
params | Object | An object of options for the parser |
callback | Function | A callback 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);
}
});
},
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 {
callback(null, response.data);
}
});
},
Get the server info
Name | Type | Description |
---|---|---|
callback | Function | A callback 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);
},
Get the current user
Get the current logged in user
Name | Type | Description |
---|---|---|
callback | Function | A callback 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;
},
Create a oneshot search job
Create a oneshot search job using the specified query and parameters.
Name | Type | Description |
---|---|---|
query | String | The search query |
params | Object | A dictionary of properties for the job. |
namespace | Object | Namespace information (owner, app, sharing) |
callback | Function | A callback 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);
},
Get an instance of the SavedSearches collection
The SavedSearches collection allows you to list saved searches, create new ones, update a saved search, etc.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing) |
// 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);
},
Get an instance of the Configurations collection
The Configurations collection allows you to list configuration files, create new files, get specific files, etc.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing) |
// 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);
},
Create a more specialized clone of this service
This will create a more specialized version of the current Service
instance, which is useful in cases where a specific owner or app need to be specified.
Name | Type | Description |
---|---|---|
owner | String | The specialized owner of the new service |
app | String | The specialized app of the new sevice |
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
});
},
Get an instance of the Jobs collection
The Jobs collection allows you to list jobs, create new ones, get a specific job, etc.
Name | Type | Description |
---|---|---|
namespace | Object | Namespace information (owner, app, sharing) |
// 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);
},
Perform a request
Name | Type | Description |
---|---|---|
path | String | URL to request (with any query parameters already appended and encoded) |
method | String | HTTP method (one of GET | POST | DELETE) |
headers | Object | Object of headers for this request |
body | Object | Body of parameters for this request |
callback | Function | Callback for when the request is complete: |
request: function(path, method, headers, body, callback) {
var that = this;
var request = function(callback) {
return that.http.request(
that.urlify(path),
{
method: method,
headers: that._headers(headers),
body: body,
timeout: 0
},
callback
);
};
return this._requestWrapper(request, callback);
}
});
Perform a POST request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Body parameters for this request |
callback | Function | Callback for 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);
},
Perform a DELETE request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Query parameters for this request |
callback | Function | Callback for 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);
},
Perform a GET request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Query parameters for this request |
callback | Function | Callback for 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);
},
Convert partial paths to fully qualified ones
Convert any partial path into a full path containing the full owner and app prefixes if necessary
Name | Type | Description |
---|---|---|
path | String | Partial 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);
},