我正在尝试从Splunk中获取一些信息,以便我可以使用它。我在登录时成功,但后来我无法做任何事情。我试图让我保存的搜索和工作无济于事。这是我用来登录并尝试获取已保存搜索的代码。
var splunkjs = require('splunk-sdk');
var service = new splunkjs.Service({username: "****", password: "*****", host: "dllprdspl01.dl.prd.p.******.com", port: 8089, scheme: "https", version: 5});
service.login(function(err, success) {
if (err) {
throw err;
}
console.log("Login was successful: " + success);
var mySavedSearches = service.savedSearches();
mySavedSearches.fetch(function(err, mySavedSearches) {
console.log("There are " + mySavedSearches.list().length + " saved searches");
var savedSearchColl = mySavedSearches.list();
for(var i = 0; i < savedSearchColl.length; i++) {
var search = savedSearchColl[i];
console.log(i + ": " + savedSearchColl[i].name);
console.log(" Query: " + savedSearchColl[i].properties().search + "\n");
}
});
});
我在登录时回来成功,但当我尝试列出我保存的搜索时,我收到此错误:
TypeError: Cannot call method 'list' of undefined
登录后,我已将服务打印到控制台,我看不出任何错误。
我已经能够使用以下代码连接并获取会话密钥:
var splunkjs = require('splunk-sdk');
var xml2js = require('xml2js');
var context = new splunkjs.Context({username: "****", password: "****", host: "****.com", port: 8089, scheme: "https", version: 5});
var sessionKey;
var key = context.login(function(err, success){
if(err){
throw err;
}
// console.log("Login was successful " + success);
var keyXml = key.req.res.body;
var parser = new xml2js.Parser();
parser.parseString(keyXml, function(err, result){
sessionKey = result.response.sessionKey[0];
});
context.sessionKey = sessionKey;
console.log(context);
context.get('services/saved/searches/hamm_test_mn7', function(err, response){
console.get(response);
})
});
如果我可以获得会话密钥,那么我应该登录到splunk。我将如何从那里开始完成我保存的搜索。
答案 0 :(得分:3)
通过在Splunk工作的Neeraj的帮助,我能够连接到Splunk并获得我保存的搜索。我没注意到我必须在我的Splunk服务器上安装xml2json。这是doc页面,用于解释所有需要发生的事情。然后我不得不将我的版本号更改为4.3而不是像上面那样5。然后连接的一切,我能够像我想的那样得到我保存的搜索。使用原始代码,我似乎正在连接,但实际上我被返回一个空的服务对象。这是我用来确保连接的代码。当你看到会话密钥时,你知道Splunk正在和你说话。
var splunkjs = require('splunk-sdk');
var service = new splunkjs.Service({username: "admin",password: "changeme",host: "localhost",port:8089,scheme: "https",version: 4.3});
service.login(function (err, success) {
if (err) console.log("Error logging in", err);
else console.log(service.sessionKey);
});
答案 1 :(得分:1)
令人惊讶的是,您可以登录,但它会在保存的搜索中窒息。我们怎么看在抓取过程中是否抛出错误。请尝试以下代码,如果您在控制台中看到任何内容,请告诉我。
var splunkjs = require('splunk-sdk');
var service = new splunkjs.Service({username: "****", password: "*****", host: "dllprdspl01.dl.prd.p.******.com", port: 8089, scheme: "https", version: 5});
service.login(function(err, success) {
if (err) {
throw err;
}
console.log("Login was successful: " + success);
var mySavedSearches = service.savedSearches();
mySavedSearches.fetch(function(err, mySavedSearches) {
if (err) {
console.log("Error in fetching savedSearches:", err);
throw err;
}
console.log("There are " + mySavedSearches.list().length + " saved searches");
var savedSearchColl = mySavedSearches.list();
for(var i = 0; i < savedSearchColl.length; i++) {
var search = savedSearchColl[i];
console.log(i + ": " + savedSearchColl[i].name);
console.log(" Query: " + savedSearchColl[i].properties().search + "\n");
}
});
});