Lista de consulta de servidores para memória / cpu alocados

4

Executando o RHEL 6.2, tentando gravar um script bash para o SSH em uma lista de servidores remotos e gravar as CPUs e a Total Memory em um arquivo, uma linha por host no seguinte formato:

HOSTNAME1    CPUS: 12    MEMORY: 64
HOSTNAME2    CPUS: 08    MEMORY: 12

Aqui está o que eu tenho até agora, ele não funciona totalmente, o que estou executando é a parte em que os SSHs do sistema executam cat /proc/cpuinfo e free .

O script bash seria chamado assim: ./query_host_info.sh <DEST> <USER> <FILE>

O arquivo que lê as listas de hosts é um arquivo de uma linha por hostnames.

#!/bin/bash

# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we don't connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in 'cat $FILE'; do
  echo -n ".";
  CHK='ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
            -l $USER $i "echo success"';
  if [ "success" = $CHK ] >/dev/null 2>&1
  then
    'ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "\
        printf "$i    ";
        echo "'cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}';
        free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*//p''";" >> ${DEST}';
  else
    printf "${i}\tUnable to connect to host\n" >> ${DEST};
  fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
    
por gorelative 06.02.2013 / 22:20

3 respostas

2

Apenas modifiquei seu script:

#!/bin/bash
# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

[[ $# -ne 3 ]] && { echo -e "\nUsage: $0  <User> <ServerList> <LogFile>\n"; exit 1; };

func_ssh() {
    local Ipaddr=$1
    local Cmd="${@:2}"
    local LogIt=${DEST}
    ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $Ipaddr "${Cmd}"
    [[ $? -ne 0 ]] && printf "${Ipaddr}\tUnable to connect to host\n" >> ${LogIt}
}

GetTotalProcs="awk '/processor/{a++} END{print a}'  /proc/cpuinfo"
GetMemoryDetails="free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*//p'"

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for srv in $(< $FILE );
do
    echo -n "."
    A="$( func_ssh $srv $GetTotalProcs )"
    B="$( func_ssh $srv $GetMemoryDetails )"
    echo "${srv} CPU: ${A} MEMORY: ${B}" >> ${DEST}
done

# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
    
por 06.09.2013 / 15:47
0

Você não pode simplesmente executar o script dessa forma?:

% ./query_host_info.sh <DEST> <USER> <FILE> > output.log 

-ou-

% ./query_host_info.sh <DEST> <USER> <FILE> | tee output.log

Além disso, você pode usar uma lista de comandos, agrupar vários comandos nos parêntesis e redirecionar sua saída para um arquivo.

% (ls; ls; ls;) > some_output.log
    
por 06.02.2013 / 22:54
0

Eu descobri .. duas coisas .. SSH não gostou do proc / cpuinfo concatenado e livre, e eu reorganizei como os comandos são executados .. Provavelmente uma maneira muito melhor de fazer isso, mas isso parece funcionar ..

#!/bin/bash

# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in 'cat $FILE'; do
  echo -n ".";
  CHK='ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
            -l $USER $i "echo success"';
  if [ "success" = $CHK ] >/dev/null 2>&1
  then
    A='ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}'"'
    B='ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*//p'"'
    echo "${i} CPU: ${A} MEMORY: ${B}" >> ${DEST}
  else
    printf "${i}\tUnable to connect to host\n" >> ${DEST};
  fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
    
por 06.02.2013 / 23:24