bash script que imprime o uso da cpu, diskusage, uso de ram

3

Como sabemos, o comando sysadmins usa comandos top para uso da CPU, free -m para memória, df -lh para o espaço hdd. Eu quero cortar apenas detalhes de uso de cpu, memória e HDD.

Eu preciso fazer três comandos e colocá-lo no script bash que imprime os detalhes usados do uso da CPU (usado% média de todos os núcleos em vários núcleos), uso de disco (somente /% de partição), Ram (MB somente usado em% ) como mostrado abaixo.

Saída desejada:

CPU: 70
RAM: 45
HDD: 10
    
por Ketan Patel 26.03.2013 / 06:37

2 respostas

8

Tente isto:

#!/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'

Editar:

    [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   
    
por 26.03.2013 / 08:56
0

Criando o GHugo, substituí apenas a seção da CPU porque estava me dando resultados absurdos. Isso pode funcionar para a parte da CPU se você tiver o Python com numpy e psutil instalado:

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