steelbrain / node-ssh

SSH2 with Promises
MIT License
948 stars 94 forks source link

get real time cpu usage of remote server #415

Open starking8b opened 2 years ago

starking8b commented 2 years ago

hello I am trying to use ssh2 with websocket , so I can get REAL Time cpu usage , this is my code

     conn.connect({
        host: '127.0.0.1',
        port: 22,
        username: 'muhammed',
        password:"Muhbu2020"

    });
    conn.on("ready", () => {
      routerconnection.usagemonitor.channel.to(namespace).emit('ready_to_get_data');

 socket.on("get_live_usage",function(){
      conn.exec('cat /proc/stat', (err, stream) => {
            if (err) throw err;
            stream.on('data', (data) => {
                console.log("recieving data");

                handleMonitorResponse(routerconnection,router_id,data.toString());

            })

        });
 });

    });

actually what isgoing on is when I am emmiting get_live_usage to node js from javascript ,the server start to run a lot of instance and it return a lot of response for one emmit , which making my chart going crazy , did I missed somethings or what , I don't understand , this is my client side code

   socket.on('live_usage',function (recieved) {
        console.log("recieved"+ recieved.load)

        getNewSeries(lastDate, {
            min: 0,
            max: 100
        },recieved)
        resetData()
        chart.updateSeries([{
            data: data
        }])
    });

and this is the getNewSeries function

function getNewSeries(baseval, yrange,recieved) {
console.log(recieved)
    var newDate = baseval + TICKINTERVAL;
    lastDate = newDate

    for(var i = 0; i< data.length - 100; i++) {
        // IMPORTANT
        // we reset the x and y of the data which is out of drawing area
        // to prevent memory leaks
        data[i].x = newDate - XAXISRANGE - TICKINTERVAL
        data[i].y = 0
        console.log("weeeeeeare in for")
    }

        window['status'+admin_id+nas_id] =0;

               cpu = recieved.load

           var iowait = recieved.iowait;
           $('#iobar').attr('style', "width:" + iowait + "%");
           $('#iobar').html(iowait + "%")
           $('#cpubar').attr('style', "width:" + cpu + "%");
           $('#cpubar').html(cpu + "%")
           $('#cpu-usage').html(cpu + "%")

        socket.emit('get_live_usage', detail);

    data.push({
        x: newDate,
        y: cpu
    })
}
steelbrain commented 2 years ago

Hi @starking8b

Thank you so much for reaching out. I would recommend you please censor the password part of the payload. I am confused with the snippet here, it seems to be using callbacks, and this particular package does NOT support callbacks. Are you sure you are using node-ssh and not any other library?

gamedevsam commented 1 year ago

@steelbrain I recommend closing this issue.