Monitorando processos, CPU e memória

Publicado por eli marques junior (última atualização em 21/08/2013)

[ Hits: 32.251 ]


Download (versão 2)

Simples script para monitorar processos, CPU e memória...
Apenas para brincar e aprender...

A função CPU foi copiada de "by Paul Colby (, no rights reserved ;)"

Para executá-lo basta dar permissão:


$ chmod u+x

E executá-lo:

$ ./


Versões atualizadas deste script

Versão 2 - Enviado por eli marques junior em 20/08/2013

Changelog: Uso:

$ ./ -t "time for sleep" -c "cpu crit" -p "process crit"


$ ./ -t 60 -c 80 -p 10


Esconder código-fonte

# [email protected]

trap 'echo -e "\n\t\t`tput blink`{FONTE}33[44;1;37mNão é possivel cancelar o script $0{FONTE}33[0m`tput sgr0`\n"; sleep 1; ' 2
function process(){

    ps -eo pid,%cpu,user,args | grep -v CPU > FILE.txt
    exec 3<&0
    exec 0<$FILE
    # read $FILE using the file descriptors
    while read line

        cpu=`echo $line | awk '{print $2}' | cut -d. -f1`
        process=`echo $line | awk '{print $4}'`

        if (( $cpu >= $1 )); then
        echo -e "{FONTE}33[41;1;37m$quant_proc - Process: $process, Uso CPU: $cpu%, Status: CRIT{FONTE}33[0m"

                #output=(0, $quant_proc)
                #echo ${output[@]}

            #quant_proc=`expr $quant_proc + 1`

    exec 0<&3

        return ${quant_proc[0]}

function cpu(){
    # by Paul Colby (, no rights reserved ;)

        CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
        unset CPU[0]                          # Discard the "cpu" prefix.
        IDLE=${CPU[4]}                        # Get the idle CPU time.

        # Calculate the total CPU time.
        for VALUE in "${CPU[@]}"; do
            let "TOTAL=$TOTAL+$VALUE"

        # Calculate the CPU usage since we last checked.
        let "DIFF_IDLE=$IDLE-$PREV_IDLE"
        let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
        #echo -en "\rCPU: $DIFF_USAGE%  \b\b"

        # Remember the total and idle CPU times for the next check.

        # Wait before checking again.

function memoria(){
    center=$(expr `tput cols` \/ 2 - 25)
    var=(`free -m | grep -i Mem:`)

        var_per=`expr 100 \* ${var[2]} \/ ${var[1]}`
        if (( $var_per >= 88 )); then

    printf "%$center.s|----------------------------------------|\n"
    printf "%$center.s|{FONTE}33[44;1;37m             MEMORIA EM MB              {FONTE}33[0m|\n"
    printf "%$center.s|{FONTE}33[44;1;37m------------|-------------|-------------{FONTE}33[0m|\n"
    printf "%$center.s|{FONTE}33[44;1;37m   Total    |    Used     |    Livre    {FONTE}33[0m|\n"
    printf "%$center.s|------------|-------------|-------------|\n"
    printf "%$center.s|{FONTE}33[46;1;37m   %4.4s     |   $red%4.4s$nred      |    %4.4s     {FONTE}33[m|\n" "" "${var[1]}" "${var[2]}" "${var[3]}"
    printf "%$center.s|------------|-------------|-------------|\n"

function chama_tudo(){

    while true; do

        printf "%*s\n" $(tput cols) "ELI MARQUES JUNIOR"
        #Chamando a Funcao teste

            if (($DIFF_USAGE >= 10 )); then
                 printf "Uso CPU: `tput blink`{FONTE}33[41;1;37m%2.2s%%{FONTE}33[0m`tput sgr0`" $DIFF_USAGE
                 printf "Uso CPU: %2.2s%%" $DIFF_USAGE

        printf "\nProcess Crit:\n"
        #Chamando a Funcao process
        process $1

            echo "----------------------------------------------------------------------------------------"

        while (($count <= $2)); do

        sleep 1
                echo -en "\rTime refresh: $count{FONTE}33[179C"

            tput cup 0 0; tput ed
        #printf "{FONTE}33[1K{FONTE}33[H";


if [ "-c $2" == "$1 $2" ] && [ "-t $4" == "$3 $4" ]; then
       printf "{FONTE}33[2J{FONTE}33[H";
       chama_tudo $2 $4
       echo -e "\t\t\t$0 -c \"arg crit\" -t \"time for sleep\""

Scripts recomendados

CkDeps - Análise de Dependências e Busca por Pacotes Instalados

Shell scrip de monitoramento de rede

Backup da database do gerenciador de pacotes RPM


Enviar gráficos 3D pela janela de chat no Kopete


[1] Comentário enviado por mrpawloski em 25/03/2014 - 12:18h

Particularmente prefiro o utilitário "nmon" que faz isso e mais...

É só baixar e executar, já vem pronto.

This systems administrator, tuner, benchmark tool gives you a huge amount of important performance information in one go. It can output the data in two ways

On screen (console, telnet, VNC, putty or X Windows) using curses for low CPU impact which is updated once every two seconds. You hit single characters on you keyboard to enable/disable the various sorts of data.
You can display the CPU, memory, network, disks (mini graphs or numbers), file systems, NFS, top processes, resources (Linux version & processors) and on Power micro-partition information.
For lots of examples, see the "Screen shots" from the left menu.
As you can see on the left lmon12e now in colour
Save the data to a comma separated file for analysis and longer term data capture.
Use this together with nmon Analyser Excel 2000 spreadsheet, which loads the nmon output file and automatically creates dozens of graphs ready for you to study or write performance reports.
Filter this data, add it to a rrd database (using an excellent freely available utility called rrdtool). This graphs the data to .gif or .png files plus generates the webpage .html file and you can then put the graphs directly on a website automatically on AIX with no need of a Windows based machine.
Directly put the data into a rrd database or other database for your own analysis

Contribuir com comentário


Site hospedado pelo provedor RedeHost.
Linux banner
Linux banner





Top 10 do mês