19

I want to display Memory usage, Disk Usage and CPU Load in the following format:

Memory Usage: 33/512MB (6%)    
Disk usage: 4.2/20GB (23%) 
CPU Load: 0.01

How do I do that?

3
  • Looks a lot like homework. Grab the data you need from /proc itself or from other commands then format it with awk, perl, or just bash. Mar 11, 2014 at 13:25
  • Welcome to Unix & Linux Stack Exchange! Please always include your OS. Solutions very often depend on the Operating System being used. Are you using Unix, Linux, BSD, OSX, something else? Which version?
    – terdon
    Mar 11, 2014 at 16:26
  • Thanks guys for your help. However, I got my answer here stackoverflow.com/questions/33774260/… Nov 18, 2015 at 8:55

8 Answers 8

46

Try this, it works on my Debian system. The details may vary depending on the implementation of these tools that your OS uses:

#!/bin/sh
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}' 

If you save the above as a script and run it, you will get (example from my system):

$ ./foo.sh 
Memory Usage: 4986/7994MB (62.37%)
Disk Usage: 23/68GB (35%)
CPU Load: 0.78

Note that the script above is giving the disk usage for the / partition. You did not specify what you wanted so I'm guessing that's what you're after.

15
  • 1
    I tried with only awk github.com/rahulinux/scripts/blob/master/sys_info.sh Mar 11, 2014 at 17:31
  • 3
    because it's just your copied ans. :D Mar 12, 2014 at 4:42
  • 1
    the free -m line does give me a error, the percentage was not properly escaped. this is how it should be: free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
    – simon
    Dec 10, 2015 at 21:17
  • 1
    @user1735921 Please ask a question. But first, google how to store the output of a command in a variable, that is trivial. Then, explain what you want to store and what you want to test in the question.
    – terdon
    Mar 16, 2017 at 13:09
  • 1
    @Maveňツ neither, that's not how CPU load works. See scoutapm.com/blog/understanding-load-averages.
    – terdon
    Oct 1, 2021 at 7:51
5

Check atop.

Read this, https://lwn.net/Articles/387202/

See, enter image description here

You can see load of disk / memory / cpu by apps:

enter image description here

For example, disk usage:

enter image description here

atop gathered statistics in the directory /var/log/atop/ in binary format files per days. You could read a man page, there must be a way to extract useful information.

2

There's not a command to display exactly in your particular format, but the following provide some of the base values, which one could build a script around, as an alternative it's also possible to read values from /proc and /sys.

free - quick overview

vmstat 1 shows system performance i/o stats

top dynamic

htop similar to top

1
  • 1
    Throw in my personal favourite, iostat. Mar 11, 2014 at 13:24
0

I think you need nagios help, for all this plugins/srcipts for nagios are already made and you can download and test without nagios installation.

0

If you are using solaris, check this out: showcpucount

This will give you an idea of how to go about writing your code.

2
0

Here is my code for getting CPU and RAM usage:

### 1. get total average CPU usage for the past minute
avg_cpu_use=$(uptime)
# a. split response
IFS=',' read -ra avg_cpu_use_arr <<< "$avg_cpu_use"
# b. find cpu usage
avg_cpu_use=""
for i in "${avg_cpu_use_arr[@]}"; do :
    if [[ $i == *"load average"* ]]; then
        avg_cpu_use=$i
        break       
    fi
done
# c. create response
avg_cpu_use=$(echo ${avg_cpu_use:16})   # Remove "  load average: "
if [[ -z "${avg_cpu_use// }" ]]; then
    avg_cpu_use="CPU: N/A%%"
    exit -1
else
    avg_cpu_use="CPU: ${avg_cpu_use}%%"
fi

### 2. get RAM usage
ram_use=$(free -m)
# a. split response by new lines
IFS=$'\n' read -rd '' -a ram_use_arr <<< "$ram_use"
# b. remove extra spaces
ram_use="${ram_use_arr[1]}"
ram_use=$(echo "$ram_use" | tr -s " ")
# c. split response by spaces
IFS=' ' read -ra ram_use_arr <<< "$ram_use"
# d. get variables
total_ram="${ram_use_arr[1]}"
ram_use="${ram_use_arr[2]}"
# e. create response
ram_use="RAM: ${ram_use}/${total_ram} MB"

echo $avg_cpu_use
echo $ram_use
0

Improving upon vikkp's answer for Solaris CPU usage, as the link seems to have died...

I believe that this is the link now: Oracle Solaris: Show Me the CPU, vCPU, Core Counts and the Socket-Core-vCPU Mapping, which states:

[Replaced old code with new code on 10/03/11]

It should be easy to find this information just by running an OS command. However for some reason it ain't the case as of today. The user must know few details about the underlying hardware and run multiple commands to figure out the exact number of physical processors, cores etc.,

For the benefit of our customers, here is a simple shell script that displays the number of physical processors, cores, virtual processors, cores per physical processor, number of hardware threads (vCPUs) per core and the virtual CPU mapping for all physical processors and cores on a Solaris system (SPARC or x86/x64). This script showed valid output on recent T-series, M-series hardware as well as on some older hardware - Sun Fire 4800, x4600. Due to the changes in the output of cpu_info over the years, it is possible that the script may return incorrect information in some cases. Since it is just a shell script, tweak the code as you like. The script can be executed by any OS user.

However, the formatting of the page seems a little messed up, so here is the script and example output:

% cat showcpucount

#!/bin/bash
/usr/bin/kstat -m cpu_info | egrep "chip_id|core_id|module: cpu_info" > /var/tmp/cpu_info.log
nproc=`(grep chip_id /var/tmp/cpu_info.log | awk '{ print $2 }' | sort -u | wc -l | tr -d ' ')`
ncore=`(grep core_id /var/tmp/cpu_info.log | awk '{ print $2 }' | sort -u | wc -l | tr -d ' ')`
vproc=`(grep 'module: cpu_info' /var/tmp/cpu_info.log | awk '{ print $4 }' | sort -u | wc -l | tr -d ' ')`
nstrandspercore=$(($vproc/$ncore))
ncoresperproc=$(($ncore/$nproc))
speedinmhz=`(/usr/bin/kstat -m cpu_info | grep clock_MHz | awk '{ print $2 }' | sort -u)`
speedinghz=`echo "scale=2; $speedinmhz/1000" | bc`
echo "Total number of physical processors: $nproc"
echo "Number of virtual processors: $vproc"
echo "Total number of cores: $ncore"
echo "Number of cores per physical processor: $ncoresperproc"
echo "Number of hardware threads (strands or vCPUs) per core: $nstrandspercore"
echo "Processor speed: $speedinmhz MHz ($speedinghz GHz)"
# now derive the vcpu-to-core mapping based on above information #
echo -e "\n** Socket-Core-vCPU mapping **"
let linenum=2<br>for ((i = 1; i <= ${nproc}; ++i ))
do
        chipid=`sed -n ${linenum}p /var/tmp/cpu_info.log | awk '{ print $2 }'`
        echo -e "\nPhysical Processor $i (chip id: $chipid):"
        for ((j = 1; j <= ${ncoresperproc}; ++j ))
        do
                let linenum=($linenum + 1)
                coreid=`sed -n ${linenum}p /var/tmp/cpu_info.log | awk '{ print $2 }'`
                echo -e "\tCore $j (core id: $coreid):"
                let linenum=($linenum - 2)
                vcpustart=`sed -n ${linenum}p /var/tmp/cpu_info.log | awk '{ print $4 }'`
                let linenum=(3 * $nstrandspercore + $linenum - 3)
                vcpuend=`sed -n ${linenum}p /var/tmp/cpu_info.log | awk '{ print $4 }'`
                echo -e "\t\tvCPU ids: $vcpustart - $vcpuend"
                let linenum=($linenum + 4)
        done
done
rm /var/tmp/cpu_info.log

# prtdiag | head -1
System Configuration:  Sun Microsystems  sun4u SPARC Enterprise M4000 Server
# ./showcpucount
Total number of physical processors: 4
Number of virtual processors: 32
Total number of cores: 16
Number of cores per physical processor: 4
Number of hardware threads (strands or vCPUs) per core: 2
Processor speed: 2660 MHz (2.66 GHz)
** Socket-Core-vCPU mapping **<br>Physical Processor 1 (chip id: 1024):
        Core 1 (core id: 0):
                vCPU ids: 0 - 1
        Core 2 (core id: 2):
                vCPU ids: 2 - 3
        Core 3 (core id: 4):
                vCPU ids: 4 - 5
        Core 4 (core id: 6):
                vCPU ids: 6 - 7
Physical Processor 2 (chip id: 1032):
        Core 1 (core id: 8):
                vCPU ids: 8 - 9
        Core 2 (core id: 10):
                vCPU ids: 10 - 11
        Core 3 (core id: 12):
                vCPU ids: 12 - 13
        Core 4 (core id: 14):
                vCPU ids: 14 - 15
Physical Processor 3 (chip id: 1040):
        Core 1 (core id: 16):
                vCPU ids: 16 - 17
        Core 2 (core id: 18):
                vCPU ids: 18 - 19
        Core 3 (core id: 20):
                vCPU ids: 20 - 21
        Core 4 (core id: 22):
                vCPU ids: 22 - 23
Physical Processor 4 (chip id: 1048):
        Core 1 (core id: 24):
                vCPU ids: 24 - 25
        Core 2 (core id: 26):
                vCPU ids: 26 - 27
        Core 3 (core id: 28):
                vCPU ids: 28 - 29
        Core 4 (core id: 30):
                vCPU ids: 30 - 31

This may also be useful: How to Find CPU Processor Information in Solaris (Doc ID 1444358.1)

-1

You should be able to display Memory usage, Disk Usage and CPU Load using:

(ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | sort -rnk2)

Not the answer you're looking for? Browse other questions tagged or ask your own question.