利用shell脚本查询linux机器运行的cpu及内存信息

   查看Linux版本的cpu信息除了dstat,iostat,nmon等工具进行监测,也可以通过脚本进行测试验证。

  我这里直接在root下创建一个文件夹  mkdir -scripts

 

 接下来进行编辑shell脚本:进入到scripts目录下直接执行vi  cpu_mem_info.sh

接下来赋予可执行的权限:chmod +x cpu_mem_info.sh 

 

#! /bin/bash
# Display the CPU and Memory information of the current machine.
# Edited on Dec 25, 2020
phy_cpu_total=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
phy_cpu_per_core=`cat /proc/cpuinfo | grep "cpu cores" | uniq | awk -F ":" '{print $2}'`
logi_cpu_total=`cat /proc/cpuinfo | grep "processor" | wc -l`
cpu_name=`cat /proc/cpuinfo | grep "model name" | uniq | awk -F ":" '{print $2}'`
memory_k=`cat /proc/meminfo | head -1 | awk -F ":" '{print $2}' | awk '{print $1}'`
memory_g=`awk 'BEGIN{printf "%.2f\n",'$memory_k'/'1048576'}'`
swap_k=`cat /proc/meminfo | grep "SwapTotal:" | awk -F ":" '{print $2}' | awk '{print $1}'`
swap_g=`awk 'BEGIN{printf "%.2f\n",'$swap_k'/'1048576'}'`

echo -e "\n物理CPU总数为 ${phy_cpu_total} 个。"
echo -e "每颗物理CPU的核心数为 ${phy_cpu_per_core} 个。"
echo -e "逻辑CPU的总数为 ${logi_cpu_total} 个。"
echo -e "CPU型号为 ${cpu_name} 。"
echo -e "物理内存总数大约为 ${memory_g} GB。"
echo -e "虚拟内存总数大约为 ${swap_g} GB。\n"

 

 

posted @ 2020-11-24 17:03  晨起的太阳  阅读(1995)  评论(0编辑  收藏  举报