Cpu Usage Percent from Linux Server

overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu

You can use top and/or vmstat from the procps package.
Use vmstat -s to get the amount of RAM on your machine (optional), and
then use the output of top to calculate the memory usage percentages.

%Cpu(s):  3.8 us,  2.8 sy,  0.4 ni, 92.0 id,  1.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 24679620 total, 1705524 free, 7735748 used, 15238348 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 16161296 avail Mem

You can also do this for relatively short output:

watch '/usr/bin/top -b | head -4 | tail -2'

A shell pipe that calculates the current RAM usage periodically is

watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s   total: %s  => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"

(CPU + Swap usages were filtered out here.)

This command prints every 5 seconds:

Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u...  wb3: Wed Nov 21 13:51:49 2018

used: 8349560 total: 24667856 => RAM Usage: 33.8%

Retrieve CPU usage percentage

A simple awk could help you here(considering that you want to print only the numbers of sy column).

vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'

NOTE: I have used vmstat 1 10 to perform 10 times vmstat command on server and then I am printing the $(NF-3) value which is 4th value from last.

java server cpu usage monitor in linux

Maybe there is a misunderstanding of the pcpu value. From man ps

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.

If the process starts and has a high CPU utilization, then the %CPU value is high. When the same process later has a low CPU utilization or is in sleep state this value will decrease. %CPU represents the cputime/realtime ratio.

As a small example

class Loop {
public static void main(String...args) throws Exception {
long l = 0;
for (int i = 0; i >= 0; i++) {
for (int j = 0; j < 50; j++) {
l++;
}
}
System.out.println("will sleep now");
Thread.sleep(30_000);
}
}

Start in one session following command, which will monitor the process in an interval of one second.

watch -n 1 ps -C java -o pcpu,state,cputime,etimes

Now run in another session the example code.

As long the Java code is inside the foo-loops and has not printed will sleep now the value of %CPU is really high (around 100). When the Java process reach the Thread.sleep the value of %CPU will constantly be decreasd (in this example).

How to get the memory and cpu usage of a remote server?

After searching online and combining a few answers from other questions on stackflow. I get the following solution.

Solution

On your local computer, you might want to have the following bash script, named, say, usage_ssh

START=1
END=3
date
for i in $(seq $START $END)
do
printf '=%.0s' {1..50};
printf '\n'
echo myservery$i
ssh myserver$i -o LogLevel=QUIET -t "~/bin/usage"
done
printf '=%.0s' {1..50};
printf '\n'
printf 'CPU Load: \n'
printf 'First Field\tprocesses per processor\n'
printf 'Second Filed\tidling percentage in last 5 minutes\n'
printf '\n'
printf '\n'

On your remote server, you should have the following bash script named usage. This script should be located in ~/bin.

free -m | awk 'NR==2{printf "Memory Usage\t%s/%sMB\t\t%.2f%\n", $3, $2, $3/$2*100}';
top -n 1 | grep load | awk '{printf "CPU Load\t%.2f\t\t\t%.2f\n", $(NF-2), $(NF-1)}';

Explanation

The idea is that You will call the use ssh -t <your command> to run executable on your remote file and get the output on the screen of your local computer.

Output

Sat Mar 28 10:32:34 CDT 2020
==================================================
myserver1
Memory Usage 47418/48254MB 98.27%
CPU Load 0.01 0.02
==================================================
myserver2
Memory Usage 47421/48254MB 98.27%
CPU Load 0.01 0.02
==================================================
myserver3
Memory Usage 4300/84541MB 5.09%
CPU Load 0.02 0.02
==================================================
CPU Load:
First Field processes per processor
Second Filed idling percentage in last 5 minutes

Accurate calculation of CPU usage given in percentage in Linux?

According the htop source code, my assumptions looks like they are valid:

(see static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) function at LinuxProcessList.c)

// Guest time is already accounted in usertime
usertime = usertime - guest; # As you see here, it subtracts guest from user time
nicetime = nicetime - guestnice; # and guest_nice from nice time
// Fields existing on kernels >= 2.6
// (and RHEL's patched kernel 2.4...)
unsigned long long int idlealltime = idletime + ioWait; # ioWait is added in the idleTime
unsigned long long int systemalltime = systemtime + irq + softIrq;
unsigned long long int virtalltime = guest + guestnice;
unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;

And so, from fields listed in the first line of /proc/stat: (see section 1.8 at documentation)

     user    nice   system  idle      iowait irq   softirq  steal  guest  guest_nice
cpu 74608 2520 24433 1117073 6176 4054 0 0 0 0

Algorithmically, we can calculate the CPU usage percentage like:

PrevIdle = previdle + previowait
Idle = idle + iowait

PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal
NonIdle = user + nice + system + irq + softirq + steal

PrevTotal = PrevIdle + PrevNonIdle
Total = Idle + NonIdle

# differentiate: actual value minus the previous one
totald = Total - PrevTotal
idled = Idle - PrevIdle

CPU_Percentage = (totald - idled)/totald


Related Topics



Leave a reply



Submit