我已经创建了下面的脚本来从jelastapi中提取统计信息,这样我就可以在一段时间内收集资源统计信息。最终目标是将数据记录到电子表格中。
下面是我的代码,它处理身份验证,然后向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();
        });
    }



})();

最佳答案

您被限制为一个并发登录会话。登录名由IP/用户代理固定。
如果需要创建多个并发登录会话,可以尝试在每个会话中使用唯一的UA以避免冲突。

10-07 16:53