Monitorando processos, CPU e memória
Publicado por eli marques junior (última atualização em 21/08/2013)
[ Hits: 35.105 ]
Download 1377011720.top_of_eli.sh (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 (http://colby.id.au), no rights reserved ;)"
Para executá-lo basta dar permissão:
Ex.:
$ chmod u+x top_of_eli.sh
E executá-lo:
$ ./top_of_eli.sh
Versão 2 - Enviado por eli marques junior em 20/08/2013
Changelog: Uso:
$ ./top_of_eli.sh -t "time for sleep" -c "cpu crit" -p "process crit"
Exemplo:
$ ./top_of_eli.sh -t 60 -c 80 -p 10
Download 1377011720.top_of_eli.sh
#!/bin/bash # ELI MARQUES JUNIOR # eli.marquesjunior@gmail.com # 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 process_number=0 function process(){ quant_proc=0 FILE="FILE.txt" 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 do cpu=`echo $line | awk '{print $2}' | cut -d. -f1` process=`echo $line | awk '{print $4}'` if (( $cpu >= $1 )); then ((quant_proc++)) 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[@]} fi #quant_proc=`expr $quant_proc + 1` done exec 0<&3 return ${quant_proc[0]} } PREV_TOTAL=0 PREV_IDLE=0 function cpu(){ # by Paul Colby (http://colby.id.au), 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. TOTAL=0 for VALUE in "${CPU[@]}"; do let "TOTAL=$TOTAL+$VALUE" done # Calculate the CPU usage since we last checked. let "DIFF_IDLE=$IDLE-$PREV_IDLE" let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" 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. PREV_TOTAL="$TOTAL" PREV_IDLE="$IDLE" # 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 red="{FONTE}33[0m{FONTE}33[41;1;37m" nred="{FONTE}33[0m{FONTE}33[46;1;37m" else red="" nred="" fi 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 memoria cpu if (($DIFF_USAGE >= 10 )); then printf "Uso CPU: `tput blink`{FONTE}33[41;1;37m%2.2s%%{FONTE}33[0m`tput sgr0`" $DIFF_USAGE else printf "Uso CPU: %2.2s%%" $DIFF_USAGE fi printf "\nProcess Crit:\n" #Chamando a Funcao process process $1 #contando=$? echo "----------------------------------------------------------------------------------------" count=0 while (($count <= $2)); do sleep 1 ((count++)) echo -en "\rTime refresh: $count{FONTE}33[179C" done process_number=$contando tput cup 0 0; tput ed #printf "{FONTE}33[1K{FONTE}33[H"; done } if [ "-c $2" == "$1 $2" ] && [ "-t $4" == "$3 $4" ]; then printf "{FONTE}33[2J{FONTE}33[H"; chama_tudo $2 $4 else echo -e "\t\t\t$0 -c \"arg crit\" -t \"time for sleep\"" fi
Verifica os artigos destaques da VOL
SlackBuild para empacotar o Firefox (binário) pro Slackware
Padronização de usuários em vários micros
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Aplicativo simples para gravar tela
Quebra de linha na data e hora no Linux Mint
trocar linhas [RESOLVIDO] (11)
Firefox não abre em usuário não administradores (2)
Ubuntu com problemas no áudio (1)