CPU和交换次数的脚本

CPU和交换次数的脚本

本文介绍了获取内存,CPU和交换次数的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的脚本,该脚本正在运行,但似乎太慢了,我还不熟悉Shell,因为它仍在学习过程中,因此没有太多的想法使它变得更聪明.

I have a below script which is working but seems too slow, I'm not expert on the shell as still in learning process hence not getting much idea to make it more smarter.

对此表示感谢.

我的代码:

#!/bin/bash
CurrntTime=$(date +'%m/%d/%Y %T')

read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS
echo -e  "\n"
printf "%s %-38s  %s\n" "Server_Name                         CPU  Memory  Swap"
printf '%s\n' "-------------------------------------------------------"

for server in $(cat /home/user1/mem)
do
cpu_info=$(sshpass -e ssh -q -t  $server sudo grep processor /proc/cpuinfo | awk 'NF==3{count++} END {printf count}')
mem_info=$(sshpass -e ssh -q -t $server sudo free -g | awk '/Mem:/{printf $2}')
swap_info=$(sshpass -e ssh -q -t $server sudo free -g | awk '/Swap:/{printf $2}')
printf "%s %-38s  %s\n" "$server          $cpu_info   $mem_info     $swap_info"

done | tee -a  2>/dev/null

脚本输出:

$ ./cpu-memory-count.sh
Please Enter password below:

Server_Name                          CPU  Memory  Swap
-------------------------------------------------------
udc0150.exmapl.udc.com          8   15     2
udc0196.exmapl.udc.com          8   15     2
udc0193.exmapl.udc.com          8   15     2
udc0160.exmapl.udc.com          8   15     2
udc0146.exmapl.udc.com          1   15     2

注意:我不会将输出保存到任何文件中,而只是将其打印在控制台上.

Note: I am not saving output to any of the file rather just getting them printed on the console.

由于安全原因,我们无法使用 ansible key-baed 访问,因此我将 sshpass 与ssh一起使用成为 sudo ,需要root特权才能获取信息.

Due to some security reasons we are not able to use ansible and key-baed access hence i used sshpass along with ssh need to become sudo where it required root privileges to get the information.

推荐答案

  1. 请勿使用 sshpass .而是将私钥复制到远程位置.参见 ssh-copy-id .
  2. 请勿为每个服务器创建3个单独的连接.为每个服务器创建一个连接.
  3. 并行连接到服务器.
  4. 不使用终端功能时不要分配psuedoterminal ...
  5. 我认为没有理由使用 sudo 来访问/proc/cpuinfo free . grep 具有 -c 选项.还有 nproc .
  6. 删除无用的 |发球-a .
  7. 不要重新发明轮子,而要使用工具-研究 ansible 和类似的自动化实用程序.
  1. Do not use sshpass. Instead copy private key to remote locations. see ssh-copy-id.
  2. Do not create 3 separate connections to each server. Create one connection per server.
  3. Connect to servers in parallel.
  4. Do not allocate psuedoterminal when you do not use terminal features...
  5. I see no reason to use sudo to get to /proc/cpuinfo nor to free. grep has -c option. And there's nproc.
  6. Remove useless | tee -a from the end.
  7. Do not reinvent the wheel and use a tool - research ansible and similar automation utilities.

也就是说,我可以看到一个函数,将其导出,然后从 xargs 内部运行 bash .GNU xargs 具有 -P 选项可以并行运行内容:

That said, I could see a function, export it, then run bash from inside xargs. GNU xargs has -P option to run stuff in parallel:

work() {
   server=$1
   # ONE connection
   tmp=$(ssh "$server" bash -c 'nproc; free -g')
   # parsing later
   cpu_info=$(<<<"$tmp" awk 'NR==1')
   mem_info=$(<<<"$tmp" awk '/Mem:/{printf $2}')
   swap_info=$(<<<"$tmp" awk '/Swap:/{printf $2}')
   # outputting
   printf "%-40s %5s %5s %5s\n" "$server" "$cpu_info" "$mem_info" "$swap_info"
}
export -f work
< /home/user1/mem xargs -P0 -n1 -d'\n' bash -c 'work "$@"' _

</home/user1/mem 将文件/home/user1/mem 的内容重定向到命令的标准输入.该命令是 xargs . xargs 从标准输入中读取用换行符分隔的 -d'\ n'数据,并将 -n1 传递给一个参数到随后的命令.因此它执行 bash -c'work'$ @'''_< the_line> 每行. -P0 使其同时执行.

< /home/user1/mem redirects the content of the file /home/user1/mem to standard input of the command. The command is xargs. xargs reads -d'\n' data separates with newlines (lines...) from the standard input, and passes -n1 one argument to the command that followed. So it executes bash -c 'work "$@"' _ <the_line> on each line. -P0 makes it do that concurrently.

man bash 中我们可以阅读:

和:

bash -c'work'$ @'中_< the_line> :

  • 命令 bash 用4个参数执行- -c work"$ @" _ 和行内容
  • bash看到 -c
    • 下一个参数 work"$ @" 是要执行的脚本
    • "$ @" 将扩展为"$ 1""$ 2".. .
    • 实际上,这里每次都只有1个自变量,因此无论如何"$ @" 都将等于"$ 1" .
    • command bash is executed with 4 arguments - -c work "$@" _ and the line content
    • bash sees -c
      • the next argument work "$@" is the script to execute
      • "$@" will expand to "$1" "$2" ...
      • Actually, here there will be only 1 argument each time, so "$@" is going to be just equal to "$1" anyway.

      这篇关于获取内存,CPU和交换次数的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:20