1.背景
大多数情况下,我们希望在登录到Linux服务器后能够直观地看到服务器的各种参数信息,比如CPU、内存、硬盘等的占用信息,方便进行系统管理。比如像openEuler系统自带的登录信息展示:
目前已经有部分软件已经可以在登录的时候,直接展示系统详情,比如XTerminal,FinalShell等工具,如果需要自定义展示特定的内容,可以自行实现。
2.系统信息脚本1
我将openEuler的脚本从系统中拷贝出来,供大家修改使用,脚本名称:system_info.sh
/bin/bash
Copyright (c) [2019] Huawei Technologies Co., Ltd.
generic-release is licensed under the Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
PURPOSE.
See the Mulan PSL v2 for more details.
Welcome
welcome=$(uname -r)
Memory
memory_total=$(cat /proc/meminfo | awk ‘/^MemTotal:/ {printf($2)}’)
memory_free=$(cat /proc/meminfo | awk ‘/^MemFree:/ { printf($2)}’)
buffers=$(cat /proc/meminfo | awk ‘/^Buffers:/ { printf($2)}’)
cached=$(cat /proc/meminfo | awk ‘/^Cached:/ { printf($2)}’)
sreclaimable=$(cat /proc/meminfo | awk ‘/^SReclaimable:/ { printf($2)}’)
swap_total=$(cat /proc/meminfo | awk ‘/^SwapTotal:/ { printf($2)}’)
swap_free=$(cat /proc/meminfo | awk ‘/^SwapFree:/ { printf($2)}’)
if [ $memory_total -gt 0 ]
then
memory_usage=echo "scale=1; ($memory_total - $memory_free - $buffers - $cached - $sreclaimable) * 100.0 / $memory_total" |bc
memory_usage=”${memory_usage}%”
else
memory_usage=0.0%
fi
Swap memory
if [ $swap_total -gt 0 ]
then
swap_mem=echo "scale=1; ($swap_total - $swap_free) * 100.0 / $swap_total" |bc
swap_mem=”${swap_mem}%”
else
swap_mem=0.0%
fi
Usage
usageof=$(df -h / | awk ‘/\// {print $(NF-1)}’)
System load
load_average=$(awk ‘{print $1}’ /proc/loadavg)
WHO I AM
whoiam=$(whoami)
Time
time_cur=$(date)
Processes
processes=$(ps aux | wc -l)
Users
user_num=$(users | wc -w)
Ip address
ip_pre=””
if [ -x “/sbin/ip” ]
then
ip_pre=$(/sbin/ip a | grep inet | grep -v “127.0.0.1” | grep -v inet6 | awk ‘{print $2}’)
fi
echo -e “\n”
echo -e “Welcome to $welcome\n”
echo -e “System information as of time: \t$time_cur\n”
echo -e “System load: \t\033[0;33;40m$load_average\033[0m”
echo -e “Processes: \t$processes”
echo -e “Memory used: \t$memory_usage”
echo -e “Swap used: \t$swap_mem”
echo -e “Usage On: \t$usageof”
for line in $ip_pre
do
ip_address=${line%/*}
echo -e “IP address: \t$ip_address”
done
echo -e “Users online: \t$user_num”
if [ “$whoiam” == “root” ]
then
echo -e “\n”
else
echo -e “To run a command as administrator(user \”root\”),use \”sudo \”.”
fi
这里脚本实现展示了操作系统信息、系统负载信息、内存信息、交换内存信息、硬盘使用信息和在线用户数等。可以在此基础上增加自己需要的功能。登录后实现效果如图,
3.系统信息脚本2
这个脚本从网上搜集而来,并在此基础上增加了公网IP显示功能。在使用前需要安装bc命令:
yum -y install bc
并将以下内容的脚本保存为system_info1.sh
!/bin/bash
clear
LogFileName=polling.$(date +%F-%T)
EchoFormat=$(for (( i=0;i<30;i++ ));do echo -n "=";done)
系统信息
SystemInfo(){
printf “${EchoFormat} 系统信息 ${EchoFormat}\n”
printf “系统类型: %-10s\n” $(uname -a| awk ‘{print $NF}’)
printf "系统版本: %-10s\n" "$(cat /etc/redhat-release)"
printf “内核信息: %-10s\n” $(uname -r)
printf "主机名: %-10s\n" $(uname -n)
printf “编码格式: %-10s\n” ${LANG}
printf "系统当前时间: %-10s %-10s\n" $(date +%F) $(date +%T)
printf "系统运行负载: %-4s %-1s\n" $(uptime | awk -F: '{print $5 }'|awk -F, '{print $1,"%"}')
printf "系统运行天数: %-10s\n" $(uptime |awk '{print $3}')
printf "在线用户人数: %-3s\n" $(w|tail -n +3|wc -l)
printf “SELinux: %-10s\n” $(grep “SELINUX=[d|e|p]” /etc/selinux/config |awk -F= ‘{print $2}’)
echo -e "最后一次修改时间: $(uptime -p)"
echo -e “IP地址: $(hostname -I | cut -d’ ‘ -f1)”
echo -e "Cpu处理器: $(lscpu | grep "Model name:" | sed 's/Model name:\s*//')"
echo -e “内存空间: $(free -h | awk ‘/^Mem:/ { print $3 “/” $2 }’)”
echo -e "交换空间: $(free -h | awk '/^Swap:/ { print $3 "/" $2 }')"
}
CPU信息
CpuInfo(){
MemonyId=$(top -b -n1|awk ‘NR==3’|awk -F, ‘{print $4}’| cut -c 1-5)
MemonyUse=$(echo "100-${MemonyId}" |bc)
printf "${EchoFormat} CPU信息 ${EchoFormat}\n"
printf "逻辑CPU核数: %-3s\n" $(grep "processor" /proc/cpuinfo|sort -u|wc -l)
printf “物理CPU核数: %-3s\n” $(grep “physical id” /proc/cpuinfo |sort -u|wc -l)
printf "CPU架构: %-3s\n" $(uname -m)
printf “CPU设置型号: %-3s\n” “$(grep “model name” /proc/cpuinfo |awk -F: ‘{print $2}’|sort -u|cut -c 2-50)”
echo -e "CPU 1分钟负载: `awk '{printf "%15s",$1}' /proc/loadavg`"
echo -e "CPU 5分钟负载: `awk '{printf "%15s",$2}' /proc/loadavg`"
echo -e "CPU10分钟负载: `awk '{printf "%15s",$3}' /proc/loadavg`"
printf "使用CPU占比: %-1s %-1s\n" ${MemonyUse} %
printf “空闲CPU占比: %-1s %-1s\n” ${MemonyId} %
printf “占用CPU Top10信息:\n\n”
ps -eo user,pid,pcpu,pmem,args –sort=-pcpu |head -n 10
}
Memory信息
MemoryInfo(){
printf “${EchoFormat} 内存信息 ${EchoFormat}\n”
printf “总共内存: %-1s\n” $(free -mh|awk “NR==2″|awk ‘{print $2}’)
printf "使用内存: %-1s\n" $(free -mh|awk "NR==2"|awk '{print $3}')
printf "剩余内存: %-1s\n" $(free -mh|awk "NR==2"|awk '{print $4}')
printf "内存使用占比: %-1s %-1s\n" $(free | grep -i mem |awk '{print $6/$2*100}'|cut -c1-5) %
printf “占用内存排名前10的soft:\n\n”
ps -eo user,pid,pcpu,pmem,args –sort=-pmem |head -n 10
}
磁盘使用量排序:
Disk_Info() {
printf “${EchoFormat} 各分区使用率 ${EchoFormat}\n”
df -h
echo
}
Swap信息
SwapInfo(){
printf “${EchoFormat} Swap信息 ${EchoFormat}\n”
printf “Swap总大小: %-1s\n” $(free -mh|awk “NR==3″|awk ‘{print $2}’)
printf "已用Swap: %-1s\n" $(free -mh|awk "NR==3"|awk '{print $3}')
printf "可用Swap: %-1s\n" $(free -mh|awk "NR==3"|awk '{print $4}')
}
网络信息
NetworkInfo(){
printf “${EchoFormat} 网络信息 ${EchoFormat}\n”
printf “IP地址: %-1s %-1s %-1s %-1s\n” $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk ‘{print $2}’|tr -d “addr:”)
printf "网关: %-1s %-1s %-1s %-1s\n" $(ifconfig -a|grep "netmask"|grep -v 127.0.0.1|awk '{print $4}')
printf "DNS: %-1s %-1s %-1s %-1s %-1s\n" $(grep "nameserver" /etc/resolv.conf | awk '{print $2}')
if (ping -c2 -w2 www.baidu.com &>/dev/null);then
printf "网络是否连通: %s\n" 是
printf "公网IP地址: %s\n" $(curl -s ip.sb)
else
printf "网络是否连通: %s\n" 否
fi
}
docker检查
DockerInfo(){
printf "${EchoFormat} docker运行情况 ${EchoFormat}\n"
printf "当前正在运行的容器:\n $(docker ps --format "{{.Names}}")\n"
printf "当前没有运行的容器:\n $(docker ps --format "{{.Names}}"|grep Exited)\n"
}
直接登录后打印
SystemInfo
CpuInfo
MemoryInfo
Disk_Info
SwapInfo
NetworkInfo
登录后进入查看模式
All(){
SystemInfo
CpuInfo
MemoryInfo
Disk_Info
SwapInfo
NetworkInfo
}
All >${LogFileName};less ${LogFileName}
效果展示:
4.脚本使用
可以把脚本放置在/etc/profile.d目录下,即可在开机时自动调用执行。
往期推荐:
部署私有化大模型ollama,造一个私人小秘书
小米路由器安装插件,实现软路由功能
部署一款路由兼较NAS系统-iStoreOS
华为openEuler OS下搭建ftp全过程-踩坑版
声明:文中观点不代表本站立场。本文传送门:http://eyangzhen.com/420288.html