概述
以 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脚本用法:nt安装tsh %s installnt卸载tsh %s uninstallnt卸载tsysinfo uninstallnn执行安装操作会把脚本自身复制到 /usr/local/bin/sysinfo ,并添加到 /etc/profile 中n每次登录即可执行该脚本,也可以执行 sysinfo 命令n" $0 $0
exit 1
fi
# 对齐格式
format2c=" %-20s%-20sn"
format3c=" %-20s%-20s%-20sn"
line1="------------------------------------------------------------------------"
line2="========================================================================"
NOW=`date "+%F %T"`
ME=`whoami`
# 表头 + 分割线
printf "nHello ${ME},nnThe system information of Host [${HOSTNAME}] at ${NOW} is:nn${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
最后
以上就是重要悟空为你收集整理的Bash 实现 Linux 版 sysinfo 获取系统信息的全部内容,希望文章能够帮你解决Bash 实现 Linux 版 sysinfo 获取系统信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复