查询服务器列表以获取分配的内存/CPU

查询服务器列表以获取分配的内存/CPU

运行 RHEL 6.2,尝试编写 bash 脚本通过 SSH 连接到远程服务器列表,并将 CPU 和总内存写入文件,每个主机一行,格式如下:

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

这是我到目前为止所拥有的,它不能完全工作,我正在运行的是系统 SSH 运行的部分cat /proc/cpuinfofree.

bash 脚本的调用方式如下:./query_host_info.sh <DEST> <USER> <FILE>

它从中读取主机列表的文件是每行一个主机名文件。

#!/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]*\) .*/\1/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

答案1

刚刚修改了你的脚本:

#!/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]*\) .*/\1/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

答案2

你不能像这样运行脚本吗?:

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

-或者-

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

此外,您可以使用命令列表,将一堆命令包装在括号中,并将其输出重定向到文件。

% (ls; ls; ls;) > some_output.log

答案3

我想通了..有两件事..SSH不喜欢连接的proc/cpuinfo和free,我重新组织了命令的运行方式..可能是一种更好的方法,但这似乎有效..

#!/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]*\) .*/\1/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

相关内容