Bash 實現 Linux 版 sysinfo 獲取系統資訊

一條老萌新發表於2020-10-30

以 CentOS 7 為基礎寫的,有想法的同學可以自行完善或提交PR。

執行結果如下圖:

Code China https://codechina.csdn.net/weixin_45919616/sysinfo/

Github  https://github.com/taills/sysinfo

程式查詢了以下內容進行展示:

  • 當前使用者名稱
  • 主機名
  • 當前主機時間
  • 核心版本
  • CPU 名稱
  • 處理器數量
  • CPU 負載
  • 記憶體負載
  • 開機時長
  • 當前登入使用者數
  • 當前程式數量
  • Nvidia GPU 名稱
  • Nvidia 驅動版本
  • Nvidia CUDA 版本
  • Nvidia cuDNN 版本
  • 磁碟空間使用情況
  • 網路卡介面名稱、MAC、IP地址

使用

下載

#!/bin/bash
#########################################################
# Function :sysinfo for Linux                           #
# Platform :CentOS 7                                    #
# Version  :1.0                                         #
# Date     :2020-10-30                                  #
# Author   :taills                                      #
# Contact  :taills@qq.com                               #
# Github   :https://github.com/taills/sysinfo           #
#########################################################
if [ -n "$1" ] ; then
    if [ `id -u` -ne 0 ];then
        echo "請使用 root 使用者執行該指令碼進行安裝/解除安裝操作"
        exit 1
    fi
    if [ "$1" = "install" ] ;then
        if [ "$0" = "/usr/local/bin/sysinfo" ] ;then
            echo "騷年,已經安裝過了! /usr/local/bin/sysinfo 已存在,可直接使用。"
            exit 0
        fi
        /usr/bin/cp $0 /usr/local/bin/sysinfo
        chmod +x /usr/local/bin/sysinfo
        echo /usr/local/bin/sysinfo >>/etc/profile
        exit 0
    fi
    if [ "$1" = "uninstall" ] ;then
        rm -f /usr/local/bin/sysinfo
        sed -i '/^\/usr\/local\/bin\/sysinfo/d' /etc/profile
        exit 0
    fi
    printf "\n指令碼用法:\n\t安裝\tsh %s install\n\t解除安裝\tsh %s uninstall\n\t解除安裝\tsysinfo uninstall\n\n執行安裝操作會把指令碼自身複製到 /usr/local/bin/sysinfo ,並新增到 /etc/profile 中\n每次登入即可執行該指令碼,也可以執行 sysinfo 命令\n" $0 $0
    exit 1
fi

# 對齊格式
format2c=" %-20s%-20s\n"
format3c=" %-20s%-20s%-20s\n"

line1="------------------------------------------------------------------------"
line2="========================================================================"

NOW=`date  "+%F %T"`
ME=`whoami`
# 表頭 + 分割線
printf "\nHello ${ME},\n\nThe system information of Host [${HOSTNAME}] at ${NOW} is:\n\n${line2}\n${format2c}${line1}\n" " Name" "Value"
# 核心

printf "$format2c" "Kernel Version" $(uname -r)


# CPU
cat /proc/cpuinfo  |grep 'model name' |sort -u |awk -v F="$format2c" -F ': ' '{printf F,"CPU",$2}'
# processor count
PROCESSOR_COUNT=$(cat /proc/cpuinfo  |grep processor |wc -l)
printf "$format2c" "Processors" "$PROCESSOR_COUNT"


#Cpu load
load1=`cat  /proc/loadavg  |  awk  '{print $1}' `
load5=`cat  /proc/loadavg  |  awk  '{print $2}' `
load15=`cat  /proc/loadavg  |  awk  '{print $3}' `

printf  "$format2c" "System Load" "$load1, $load5, $load15"

#System uptime
uptime=`cat  /proc/uptime  |  cut  -f1 -d.`
upDays=$((uptime /60/60/24 ))
upHours=$((uptime /60/60 %24))
upMins=$((uptime /60 %60))
upSecs=$((uptime%60))
up_lastime=`date  -d  "$(awk -F. '{print $1}' /proc/uptime) second ago"  +"%Y-%m-%d %H:%M:%S" `
  
#Memory Usage
mem_usage=`free  -m |  awk  '/Mem:/{total=$2} /buffers\/cache/ {used=$3} END {printf("%3.2f%%",used/total*100)}' `
swap_usage=`free  -m |  awk  '/Swap/{printf "%.2f%",$3/$2*100}' `

printf "$format2c" "Memory Usage" "$mem_usage"   
printf "$format2c" "Swap Usage" "$swap_usage"
 

# Uptime
printf  "$format2c" "System Uptime" "$upDays days $upHours hours $upMins min $upSecs sec"

#User
users=`users  |  wc  -w`
printf  "$format2c" "Login Users" $users 

#Processes
processes=`ps  aux |  wc  -l`
printf  "$format2c" "Processes"   $processes


if [ -f /usr/bin/nvidia-smi ]; then
    # Nvidia GPU
    /usr/bin/nvidia-smi  -q|grep 'Product Name' | awk -v F="$format2c" -F ': ' '{printf F,"GPU Product Name",$2}'
    # Nvidia Driver
    /usr/bin/nvidia-smi  -q|grep 'Driver Version' | awk -v F="$format2c" -F ': ' '{printf F,"Driver Version",$2}'
    # CUDA Version
    /usr/bin/nvidia-smi  -q|grep 'CUDA Version' | awk -v F="$format2c" -F ': ' '{printf F,"CUDA Version",$2}'
fi


# cuDNN
CUDNN_LIB_PATH=/usr/lib64/libcudnn.so
if [ -f "$CUDNN_LIB_PATH" ]; then
    readlink -f $CUDNN_LIB_PATH | awk -F 'libcudnn.so.' -v F="$format2c" '{printf F,"cuDNN Version",$2}'
fi

echo
echo $line2
#System fs usage
Filesystem=$( df  -h |  awk  '/^\/dev/{print $6}' )
printf "$format2c"  "Mount Point" "Usage"
echo $line1
for  f  in  $Filesystem
do
     df  -h| awk -v A="$format2c" -v B=$f '{if($NF==B) printf A,B,$3" ("$5")"}'
done

echo 
echo $line2
#Interfaces
INTERFACES=$(ip -4 ad |  grep  'state '  |  awk  -F ":"  '!/^[0-9]*: ?lo/ {print $2}' )
printf "$format3c" "Interface" "MAC" "IP Address"
echo $line1
for  i  in  $INTERFACES
do
    MAC=$(ip ad show dev $i |  grep  "link/ether"  |  awk  '{print $2}' ) 
    ip ad show dev $i |  awk -v A="$format3c" -v B="$i" -v C="$MAC" '/inet/{printf A,B,C,$2}'
done

該指令碼支援安裝到 /usr/local/bin/ 目錄下,以便日常使用,安裝後會寫入 /etc/profile ,在登入時會展示執行結果,實現動態的 /etc/motd 。

 

安裝

sh sysinfo.sh install

 

解除安裝

sh sysinfo.sh uninstall

或者

sysinfo uninstall

相關文章