3

As we know that for sysadmins uses commands top for cpu usage, free -m for memory, df -lh for hdd space. I want to cut just usage details of cpu, memory, and Hdd.

I need to make three commands and put it in bash script that print used details of cpu usage (used % average all cores in multi core cou), disk usage (only / partition %) , Ram (only used MB in %) as shown below.

Desired output:

CPU: 70
RAM: 45
HDD: 10
2
  • 1
    Too many undefined parameters. Is CPU usage per-core? Does your definition of RAM usage include cache/buffers? What if there are multiple drives?
    – Chris Down
    Mar 26, 2013 at 6:01
  • No,I need cpu average all core, Ram only used, no cached buffer, and I need only / partition for my systems.
    – KK Patel
    Mar 26, 2013 at 6:04

3 Answers 3

10

Try this :

#!/bin/bash     
echo CPU: `top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'` 
FREE_DATA=`free -m | grep Mem` 
CURRENT=`echo $FREE_DATA | cut -f3 -d' '`
TOTAL=`echo $FREE_DATA | cut -f2 -d' '`
echo RAM: $(echo "scale = 2; $CURRENT/$TOTAL*100" | bc)
echo HDD: `df -lh | awk '{if ($6 == "/") { print $5 }}' | head -1 | cut -d'%' -f1`

Edit:

    [root@dev ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg_dev-lv_root
                           64G   31G   30G  51% /
    tmpfs                1004M  112K 1004M   1% /dev/shm
    /dev/sda1             485M   30M  430M   7% /boot


[root@dev ~]# top -b -n1
top - 13:39:49 up 8 days,  4:59,  3 users,  load average: 0.00, 0.01, 0.00
Tasks: 135 total,   1 running, 134 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.2%us,  0.5%sy,  0.0%ni, 98.0%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2055668k total,  1957592k used,    98076k free,   155640k buffers
Swap:  4128760k total,    83644k used,  4045116k free,   718168k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                      
 1250 root      20   0 15028 1080  808 R  5.5  0.1   0:00.04 top                          
    1 root      20   0 19336 1176  976 S  0.0  0.1   0:01.80 init                         
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd                     
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                  
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0                  
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                   
    6 root      20   0     0    0    0 S  0.0  0.0   0:00.15 events/0                     
    7 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cpuset                       
    8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper                      
    9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns                        
   10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr                    
   11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                           
   12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 sync_supers                  
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default                  
   14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                
   15 root      20   0     0    0    0 S  0.0  0.0   0:00.45 kblockd/0                    
   16 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid                       
   17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                 
   18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug                
   19 root      20   0     0    0    0 S  0.0  0.0   4:02.29 ata/0                        
   20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux                      
   21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd                
   22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd                        
   23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kseriod                      
   25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd                   
   26 root      20   0     0    0    0 S  0.0  0.0   0:07.66 kswapd0                      
   27 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd                         
   28 root      39  19     0    0    0 S  0.0  0.0   0:06.30 khugepaged                   
   29 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/0                        
   30 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/0   
8
  • Thank You friend. Ram work perfectly CPU , HDD doesn't show.
    – KK Patel
    Mar 26, 2013 at 8:00
  • Please post df -lh output and top -b -n1 header.
    – GHugo
    Mar 26, 2013 at 8:02
  • output of df -l [root@dev ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_dev-lv_root 64G 31G 30G 51% / tmpfs 1004M 112K 1004M 1% /dev/shm /dev/sda1 485M 30M 430M 7% /boot
    – KK Patel
    Mar 26, 2013 at 8:03
  • For HDD, try this one : df -lh | awk '{if ($6 == "/") { print $5 }}' | head -1 | cut -d'%' -f1
    – GHugo
    Mar 26, 2013 at 8:09
  • No luck. Nothing in output
    – KK Patel
    Mar 26, 2013 at 8:13
0

Building on @GHugo's, I replaced only the CPU section because it was giving me nonsense results. This may work for the CPU part if you have Python with numpy and psutil installed:

PY_COMMAND="import psutil;import numpy;print(round(numpy.array(psutil.cpu_percent(percpu=True)).mean(),1))"
echo CPU: $( python -c "${PY_COMMAND}" )
0

Percentage of RAM used: free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'

Percentage of disk spce used : df -h | awk '$NF=="/"{printf "%s\t\t", $5}'

CPU % used : top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}'

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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