每次登录Shell时使用shell脚本查看Linux上的系统信息
Linux中有几个命令可以获取系统信息,例如处理器信息,制造商名称和序列号等。
您可能需要运行几个命令来收集此信息。
此外,很难记住所有命令及其选项。
相反,您可以编写Shell脚本以根据需要自定义输出。
过去,我们出于各种目的编写了许多bash脚本。
今天,我们提出了一个新的Shell脚本,该脚本在您每次登录Shell时向您显示所需的系统信息。
该脚本分为六个部分,下面有更多详细信息。
第1部分:一般系统信息
第2部分: CPU /内存的当前使用情况
第3部分:磁盘使用率> 80%
第4部分:列表系统WWN详细信息
第5部分: Oracle数据库实例
第6部分:可用的软件包更新
每次登录到Shell时使用Bash脚本查看Linux上的系统信息
每当您登录Shell时,此基本脚本都会将系统信息带到您的终端。
#vi /opt/scripts/system-info.sh
#!/bin/bash
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:tt"`hostname`
echo -e "uptime:ttt"`uptime | awk '{print $3,$4}' | sed 's/,//'`
echo -e "Manufacturer:tt"`cat /sys/class/dmi/id/chassis_vendor`
echo -e "Product Name:tt"`cat /sys/class/dmi/id/product_name`
echo -e "Version:tt"`cat /sys/class/dmi/id/product_version`
echo -e "Serial Number:tt"`cat /sys/class/dmi/id/product_serial`
echo -e "Machine Type:tt"`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:ttt"`uname -r`
echo -e "Architecture:tt"`arch`
echo -e "Processor Name:tt"`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ t]*//'`
echo -e "Active User:tt"`w | cut -d ' ' -f1 | grep -v USER | xargs -n1`
echo -e "System Main IP:tt"`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
脚本准备好后,在基于RHEL的系统CentOS,Oracle Linux和Fedora中的“ .bash_profile”文件末尾添加文件路径。
# 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)
-----------------------------------------------------------------------------
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任