forked from splunk/splunk-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
204 lines (184 loc) · 8.07 KB
/
Copy pathsearch.js
File metadata and controls
204 lines (184 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2011 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
(function() {
var splunkjs = require('../../index');
var Class = splunkjs.Class;
var utils = splunkjs.Utils;
var Async = splunkjs.Async;
var options = require('./cmdline');
var print = require('util').print;
var FLAGS_CREATE = [
"search", "earliest_time", "latest_time", "now", "time_format",
"exec_mode", "search_mode", "rt_blocking", "rt_queue_size",
"rt_maxblocksecs", "rt_indexfilter", "id", "status_buckets",
"max_count", "max_time", "timeout", "auto_finalize_ec", "enable_lookups",
"reload_macros", "reduce_freq", "spawn_process", "required_field_list",
"rf", "auto_cancel", "auto_pause"
];
var createService = function(options) {
return new splunkjs.Service({
scheme: options.scheme,
host: options.host,
port: options.port,
username: options.username,
password: options.password,
version: options.version
});
};
var search = function(service, options, callback) {
// Extract the options we care about and delete them
// the object
var query = options.search;
var isVerbose = options.verbose;
var count = options.count || 0;
var mode = options.mode || "row";
delete options.search;
delete options.verbose;
delete options.count;
delete options.mode;
Async.chain([
// Create a search
function(done) {
service.search(query, options, done);
},
// Poll until the search is complete
function(job, done) {
Async.whilst(
function() { return !job.properties().isDone; },
function(iterationDone) {
job.fetch(function(err, job) {
if (err) {
callback(err);
}
else {
// If the user asked for verbose output,
// then write out the status of the search
var properties = job.properties();
if (isVerbose) {
var progress = (properties.doneProgress * 100.0) + "%";
var scanned = properties.scanCount;
var matched = properties.eventCount;
var results = properties.resultCount;
var stats = "-- " +
progress + " done | " +
scanned + " scanned | " +
matched + " matched | " +
results + " results";
print("\r" + stats + " ");
}
Async.sleep(1000, iterationDone);
}
});
},
function(err) {
if (isVerbose) {
print("\r");
}
done(err, job);
}
);
},
// Once the search is done, get the results
function(job, done) {
job.results({count: count, json_mode: mode}, done);
},
// Print them out (as JSON), and cancel the job
function(results, job, done) {
process.stdout.write(JSON.stringify(results));
job.cancel(done);
}
],
function(err) {
callback(err);
}
);
};
var oneshotSearch = function(service, options, callback) {
var query = options.search;
delete options.search;
// Oneshot searches don't have a job associated with them, so we
// simply execute it and print out the results.
service.oneshotSearch(query, options, function(err, results) {
if (err) {
callback(err);
}
else {
console.log(JSON.stringify(results));
callback();
}
});
};
exports.main = function(argv, callback) {
splunkjs.Logger.setLevel("NONE");
callback = callback || function(err) {
if (err) {
console.log(err);
}
};
var cmdline = options.create();
cmdline.name = "search";
cmdline.description("Create a search and print the results to stdout");
cmdline.option("--verbose", "Output job progress as we wait for completion");
cmdline.option("--count <count>", "How many results to fetch");
cmdline.option("--mode <mode>", "Row or column mode [row|column]");
// For each of the flags, add an option to the parser
var flags = FLAGS_CREATE;
var required_flags = ["search"];
for(var i = 0; i < flags.length; i++) {
var required = required_flags.indexOf(flags[i]) >= 0;
var option = "<" + flags[i] + ">";
cmdline.option("--" + flags[i] + " " + option, "", undefined, required);
}
cmdline.on('--help', function(){
console.log(" Examples:");
console.log(" ");
console.log(" Create a regular search:");
console.log(" > node search.js --search 'search index=_internal | head 10'");
console.log(" ");
console.log(" Create a oneshot search:");
console.log(" > node search.js --search 'search index=_internal | head 10' --exec_mode oneshot");
console.log(" ");
console.log(" Create a regular search and only return 10 results:");
console.log(" > node search.js --search 'search index=_internal | head 20' --count 10");
console.log(" ");
console.log(" Create a regular search and output the progress while the search is running");
console.log(" > node search.js --search 'search index=_internal | head 20' --verbose");
console.log(" ");
});
cmdline.parse(argv);
var service = createService(cmdline.opts);
service.login(function(err, success) {
if (err || !success) {
callback("Error logging in");
return;
}
delete cmdline.username;
delete cmdline.password;
delete cmdline.scheme;
delete cmdline.host;
delete cmdline.port;
delete cmdline.namespace;
delete cmdline.version;
if (cmdline.opts.exec_mode === "oneshot") {
oneshotSearch(service, cmdline.opts, callback);
}
else {
search(service, cmdline.opts, callback);
}
});
};
if (module === require.main) {
exports.main(process.argv);
}
})();