Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CPU usage via SSH

I want to retrieve the CPU utilization percentage via SSH and I have tried the command "top" but it will not let me.

I am using CentOS 6.

I tried this code

$connection = ssh2_connect("IP", PORT);
ssh2_auth_password($connection, "root", "PASS");
$stream = ssh2_exec($connection, "top");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);

// Whichever of the two below commands is listed first will receive its appropriate output.  The second command receives nothing
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);

// Close the streams        
fclose($errorStream);
fclose($stream);

But its every time give me an error: Output: Error: TERM environment variable not set.

I'm using PHP.

like image 920
user3120926 Avatar asked Dec 16 '22 03:12

user3120926


2 Answers

Thanks everyone but I managed. I did this command:

top -b -n 10 -d.2 | grep 'Cpu' |  awk 'NR==3{ print($2)}'
like image 73
user3120926 Avatar answered Dec 27 '22 23:12

user3120926


you can use

top -n 1
mpstat
iostat
like image 39
Tharanga Abeyseela Avatar answered Dec 27 '22 23:12

Tharanga Abeyseela