我创建了以下脚本来从Jelastic api中提取统计数据,这样我就可以在一段时间内收集资源统计信息。最终目标是将数据记录到电子表格中。
以下是我的代码,用于处理身份验证,然后向GetSumStats
发送请求。
如果我运行代码,有些时候会返回结果。
{
iops_used: 0,
duration: 3600,
cpumhz: 7,
start: '',
disk: 7857,
mem: 725212,
cpu: 24002,
capacity: 9,
net: { in_int: 96004, out_int: 96004, in_ext: 9181, out_ext: 9395 },
chanksused: 7,
nodeid: 'sum'
}
但有时候请求会因错误而失败。
{ result: 702,
source: 'JEL',
error: 'not authenticated (different session key)',
stats: [] }
这是时间问题还是已知问题?也许剧本太快了,API还不知道会话ID?这就是我引入setTimeout
的原因var sites = require('./sites.json').sites,
credentials = require('./credentials.json'),
Client = require('node-rest-client').Client,
util = require('./util.js');
(function () {
"use strict";
var client = new Client();
var session;
login();
function login() {
var args = {
parameters: {
appid: sites[2].appId,
login: credentials.email,
password: credentials.password
}
};
client.registerMethod("login", "https://app.j.hostapi.co.uk/1.0/users/authentication/rest/signin", "GET");
client.methods.login(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
session = data.session;
console.log(session);
// Tried to pause here in case it was too quick
setTimeout(function() {
getSumStats();
}, 3000);
});
}
function logout() {
var args = {
parameters: {
appid: sites[2].appId,
session: session
}
};
client.registerMethod("logout", "https://app.j.hostapi.co.uk/1.0/users/authentication/rest/signout", "GET");
client.methods.logout(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
console.log(data);
// raw response
//console.log(response);
});
}
// Failure here
function getSumStats() {
var args = {
parameters: {
domain: sites[2].domain,
session: session,
duration: 3600
}
};
client.registerMethod("getSumStats", "https://app.j.hostapi.co.uk/1.0/environment/control/rest/getsumstat", "GET");
client.methods.getSumStats(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
console.log(data.stats);
logout();
});
}
})();
答案 0 :(得分:3)
您只能使用1个并发登录会话。登录由IP /用户代理固定。
如果您需要创建多个并发登录会话,可以尝试在每个会话中使用唯一的UA以避免冲突。