Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
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 '13 at 6:01
    
No,I need cpu average all core, Ram only used, no cached buffer, and I need only / partition for my systems. –  K.K Patel Mar 26 '13 at 6:04
add comment

1 Answer 1

up vote 3 down vote accepted

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   
share|improve this answer
    
Thank You friend. Ram work perfectly CPU , HDD doesn't show. –  K.K Patel Mar 26 '13 at 8:00
    
Please post df -lh output and top -b -n1 header. –  GHugo Mar 26 '13 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 –  K.K Patel Mar 26 '13 at 8:03
    
For HDD, try this one : df -lh | awk '{if ($6 == "/") { print $5 }}' | head -1 | cut -d'%' -f1 –  GHugo Mar 26 '13 at 8:09
    
No luck. Nothing in output –  K.K Patel Mar 26 '13 at 8:13
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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