GNU/Linux >> Belajar Linux >  >> Linux

Daftar Permintaan Server Untuk Memori/cpu Dialokasikan?

Menjalankan RHEL 6.2, mencoba menulis skrip bash ke SSH ke daftar server jarak jauh, dan menulis CPU dan Memori Total ke file, satu baris per host dalam format berikut:

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

Inilah yang saya miliki sejauh ini, itu tidak berfungsi sepenuhnya, yang saya jalankan adalah bagian di mana sistem SSH menjalankan cat /proc/cpuinfo dan free .

Skrip bash akan dipanggil seperti ini:./query_host_info.sh <DEST> <USER> <FILE>

File tempat membaca daftar host adalah file nama host satu per baris.

#!/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 hostn" >> ${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

Jawaban yang Diterima:

Baru saja memodifikasi skrip Anda :

#!/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 hostn" >> ${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

Linux
  1. 80 Alat Pemantauan Linux untuk SysAdmins

  2. Daftar Aplikasi &Sumber Daya yang Luar Biasa untuk OS dasar

  3. Matriks izin untuk Server Cloud

  1. Mengapa Windows10 VM lambat di OpenStack?

  2. 17 Contoh Perintah hpacucli untuk Linux di Server HP

  3. cara memeriksa ukuran tumpukan yang dialokasikan untuk jvm oleh linux

  1. Cara Memeriksa Beban Server di Server Windows

  2. Linux – Batasi Penggunaan Memori Untuk Proses Linux Tunggal?

  3. Apa Arti Karakter Khusus Dalam Echo {a..z}?