Bash 指令碼實現每次登入到 Shell 時可以檢視 Linux 系統資訊

Magesh Maruthamuthu發表於2019-12-05

Linux 中有很多可以檢視系統資訊如處理器資訊、生產商名字、序列號等的命令。你可能需要執行多個命令來收集這些資訊。同時,記住所有的命令和他們的選項也是有難度。

你可以寫一個 shell 指令碼 基於你的需求來自定義顯示的資訊。

以前我們出於不同的目的需要寫很多個 bash 指令碼

現在我們寫一個新的 shell 指令碼,在每次登入到 shell 時顯示需要的系統資訊。

這個j指令碼有 6 部分,細節如下:

  1. 通用系統資訊
  2. CPU/記憶體當前使用情況
  3. 硬碟使用率超過 80%
  4. 列出系統 WWN 詳情
  5. Oracle DB 例項
  6. 可更新的包

我們已經基於我們的需求把可能需要到的資訊加到了每個部分。之後你可以基於自己的意願修改這個指令碼。

這個j指令碼需要用到很多工具,其中大部分我們之前已經涉及到了。

你可以參照以前文章,瞭解工具詳情。

如果你想為這個指令碼增加其他的資訊,請在評論去留下你的需求,以便我們幫助你。

Bash 指令碼實現每次登入到 Shell 時可以檢視 Linux 系統資訊

這個指令碼會在你每次登入 shell 時把系統資訊列印到 terminal。

# vi /opt/scripts/system-info.sh
#!/bin/bash
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:\t\t"`hostname`
echo -e "uptime:\t\t\t"`uptime | awk '{print $3,$4}' | sed 's/,//'`
echo -e "Manufacturer:\t\t"`cat /sys/class/dmi/id/chassis_vendor`
echo -e "Product Name:\t\t"`cat /sys/class/dmi/id/product_name`
echo -e "Version:\t\t"`cat /sys/class/dmi/id/product_version`
echo -e "Serial Number:\t\t"`cat /sys/class/dmi/id/product_serial`
echo -e "Machine Type:\t\t"`vserver=$(lscpu | grep Hypervisor | wc -l); if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi`
echo -e "Operating System:\t"`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`
echo -e "Kernel:\t\t\t"`uname -r`
echo -e "Architecture:\t\t"`arch`
echo -e "Processor Name:\t\t"`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
echo -e "Active User:\t\t"`w | cut -d ' ' -f1 | grep -v USER | xargs -n1`
echo -e "System Main IP:\t\t"`hostname -I`
echo ""
echo -e "-------------------------------CPU/Memory Usage------------------------------"
echo -e "Memory Usage:\t"`free | awk '/Mem/{printf("%.2f%"), $3/$2*100}'`
echo -e "Swap Usage:\t"`free | awk '/Swap/{printf("%.2f%"), $3/$2*100}'`
echo -e "CPU Usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' |  awk '{print $0}' | head -1`
echo ""
echo -e "-------------------------------Disk Usage >80%-------------------------------"
df -Ph | sed s/%//g | awk '{ if($5 > 80) print $0;}'
echo ""

echo -e "-------------------------------For WWN Details-------------------------------"
vserver=$(lscpu | grep Hypervisor | wc -l)
if [ $vserver -gt 0 ]
then
echo "$(hostname) is a VM"
else
cat /sys/class/fc_host/host?/port_name
fi
echo ""

echo -e "-------------------------------Oracle DB Instances---------------------------"
if id oracle >/dev/null 2>&1; then
/bin/ps -ef|grep pmon
then
else
echo "oracle user does not exist on $(hostname)"
fi
echo ""

if (( $(cat /etc/*-release | grep -w "Oracle|Red Hat|CentOS|Fedora" | wc -l) > 0 ))
then
echo -e "-------------------------------Package Updates-------------------------------"
yum updateinfo summary | grep 'Security|Bugfix|Enhancement'
echo -e "-----------------------------------------------------------------------------"
else
echo -e "-------------------------------Package Updates-------------------------------"
cat /var/lib/update-notifier/updates-available
echo -e "-----------------------------------------------------------------------------"
fi

把上面指令碼內容儲存到一個檔案 system-info.sh,之後新增可執行許可權:

# chmod +x ~root/system-info.sh

當指令碼準備好後,把指令碼檔案的路徑加到 .bash_profile 檔案末尾(紅帽系列的系統:CentOS、Oracle Linux 和 Fedora):

# echo "/root/system-info.sh" >> ~root/.bash_profile

執行以下命令,來讓修改的內容生效:

# source ~root/.bash_profile

對於 Debian 系統的系統,你可能需要把檔案路徑加到 .profile 檔案中:

# echo "/root/system-info.sh" >> ~root/.profile

執行以下命令使修改生效:

# source ~root/.profile

你以前執行上面 source 命令時可能見過類似下面的輸出。從下次開始,你在每次登入 shell 時會看到這些資訊。當然,如果有必要你也可以隨時手動執行這個指令碼。

-------------------------------System Information---------------------------
Hostname:            daygeek-Y700
uptime:              1:20 1
Manufacturer:        LENOVO
Product Name:        80NV
Version:             Lenovo ideapad Y700-15ISK
Serial Number:       AA0CMRN1
Machine Type:        Physical
Operating System:    Manjaro Linux
Kernel:              4.19.80-1-MANJARO
Architecture:        x86_64
Processor Name:      Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Active User:         daygeek renu thanu
System Main IP:      192.168.1.6 192.168.122.1

-------------------------------CPU/Memory Usage------------------------------
Memory Usage:     37.28%
Swap Usage:       0.00%
CPU Usage:        15.43%

-------------------------------Disk Usage >80%-------------------------------
Filesystem      Size  Used Avail Use Mounted on
/dev/nvme0n1p1  217G  202G  4.6G  98 /
/dev/loop0      109M  109M     0 100 /var/lib/snapd/snap/odrive-unofficial/2
/dev/loop1       91M   91M     0 100 /var/lib/snapd/snap/core/6405
/dev/loop2       90M   90M     0 100 /var/lib/snapd/snap/core/7713

-------------------------------For WWN Details-------------------------------
CentOS8.2daygeek.com is a VM

-------------------------------Oracle DB Instances---------------------------
oracle user does not exist on CentOS8.2daygeek.com

-------------------------------Package Updates-------------------------------
    13 Security notice(s)
         9 Important Security notice(s)
         3 Moderate Security notice(s)
         1 Low Security notice(s)
    35 Bugfix notice(s)
     1 Enhancement notice(s)
-----------------------------------------------------------------------------

via: https://www.2daygeek.com/bash-shell-script-view-linux-system-information/

作者:Magesh Maruthamuthu 選題:lujun9972 譯者:lxbwolf 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

Bash 指令碼實現每次登入到 Shell 時可以檢視 Linux 系統資訊

訂閱“Linux 中國”官方小程式來檢視

相關文章