AKIWEB
AKIWEB

Reputation: 19612

Shell script to display - Memory usage, Disk Usage and CPU Load?

I want to diplay the 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

So I started using the below script to do that but somehow it gives me an error

#!/bin/bash

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)}' 

Below is the error I get -

awk: run time error: not enough arguments passed to printf("Memory Usage: %s/%sMB (%.2f%)
")
        FILENAME="-" FNR=2 NR=2

I am running ubuntu 12.04. Any idea what's wrong?

Update:-

After running my below shell script -

#!/bin/bash

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)}' 

I got below output -

Memory Usage: 128504/128896MB (99.70%)
Disk Usage: 3/48GB (8%)
CPU Load: 0.40

Does CPU Load looks ok? What does 0.40 means? And what number I should say will be high for CPU Load?

Upvotes: 4

Views: 15472

Answers (1)

William Pursell
William Pursell

Reputation: 212228

Try escaping the %:

printf "Memory Usage: %s/%sMB (%.2f%%)\n"

Upvotes: 3

Related Questions