简体   繁体   中英

How to calculate disk space on remote server and store values using ssh2 & node.js?

Currently I'm using SSH2 and node.js to monitor disk space and partitions over remote server. And printing output to console. But the output is in string format so I couldn't store free space / total space value in any variable. Current I'm running df -k command using con.exec in node.js(ssh2) to run command over remote servers using credentials.

  1. So How would it would be possible(store disk space in variable).
  2. My Final task is to check disk space over remote server and generate alert if it is < 25%.
  3. And also for HTML part I'm using AngularJs. So I've to give a button over web page to force check and for generating alert for users.

I've added the output format I'm receiving over console(both) below

Shell 控制台输出 浏览器控制台输出

Use df -h | grep sd df -h | grep sd to select only the row of the HDD's main partition.

After that, in data content of result array (see your picture, first item of array "data":"RESULT") change the sequence of space to one space. For example after Filesystem in your uploaded picture there are 4 spaces, replace them with one space.

In the final string you sill have a result like this: /dev/sda1 609G 202G 376G 35% /

Now, you should split this string whit space char. Resulting in the array: item[0] is Filesystem item[1] is total space item[2] is used space item[3] is available space item[4] is used space in percent form item[5] is mounted on path

Very very important note : I suggest you do not use this methodology, because it is NOT secure (look at your picture).

Anyone can find it by tracing with a firebug !

Try this , if you are using linux system there is command "df -h | grep sd" to find out disk space i am sure there must be command for windows also
"child_process" module in js helps to run system commands . to find disk space details use following code .

let command = `df -h | grep sd`;
child_process.exec(command, (err, stdout, stdin) => {
    if (err) {
        console.log("Err", err)

    }
    console.log("Out", stdout.split(/[ ,]+/));

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM