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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.
awk
github.com/rahulinux/scripts/blob/master/sys_info.sh
– Rahul Patil
Mar 11 '14 at 17:31
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
– simon
Dec 10 '15 at 21:17
Check atop.
Read this, https://lwn.net/Articles/387202/
See,
You can see load of disk / memory / cpu by apps:
For example, disk usage:
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.
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
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.
If you are using solaris, check this out: showcpucount
This will give you an idea of how to go about writing your code.
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
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)