Linux服务器信息检测脚本
本帖最后由 SuiFenPX 于 2021-7-13 17:30 编辑由于日常工作中经常有了解某个服务器状态信息的需求,而往往这些服务器上并没有相关的状态检测工具(如zabbix等)。因此为解决当前问题故编写了此脚本,近多次迭代目前功能较为完善,故分享出来供大家讨论交流。脚本运行结果如下图所示。
#!/bin/bash
:<<!
功能:Linux服务器资源检查脚本。
1、此脚本可检查目标服务器基础信息、CPU信息、内存信息、磁盘信息、网络信息。
2、此脚本仅适用于单机服务器,未设计查看远程服务器信息功能。
3、此脚本适用于符合LSB标准的系统。
版本:1.4
作者:SuiFenPX
邮件:suifenpx@qq.com
日期:2021-07-12
!
# 颜色函数
function color_schema() {
#色彩 黑红绿黄蓝洋红青白
# 前景色 3031323334 35 3637
# 背景色 4041424344 45 4647
#
# 规则 \033[前景色;背景色m
high_color="\033[1;31m"
low_color="\033[1;34m"
medium_color="\033[1;35m"
glimmer="\033[5m"
default_color="\033[0m"
}
# 依赖命令检测函数
function bc_check() {
color_schema
bc_check=`find / -name bc`
if [ -z ${bc_check} ]; then
starting
echo -e "\n${high_color}\t\t${glimmer}bc命令不存在,请安装bc后再运行此脚本!${default_color}\n"
endding
exit
fi
}
# 服务器基础信息检查函数
function server_basics_message() {
local os_name=`grep "PRETTY_NAME" /etc/os-release | cut -d= -f2 | cut -d\" -f2`
local kernel_release=`uname -r`
local os_hostname=`hostname`
local now_time=`date +%Y-%m-%d\ %H:%M:%S`
local sys_lang=`echo $LANG`
echo -e "系 统:\t${os_name}"
echo -e "内 核:\t${kernel_release}"
echo -e "主 机 名:\t${os_hostname}"
echo -e "系统语系: \t${sys_lang}"
echo -e "当前时间:\t${now_time}"
}
# CPU基础信息检查函数
function cpu_message() {
color_schema
echo -e -n "--------------------------------CPU 信息--------------------------------\n"
local physical_id=`grep "physical id" /proc/cpuinfo | sort | uniq | wc -l`
local cpu_cores=`grep "cpu cores" /proc/cpuinfo | uniq | awk '{print $4}'`
local siblings=`grep "siblings" /proc/cpuinfo | uniq | awk '{print $3}'`
local processor=`grep "processor" /proc/cpuinfo | wc -l`
local cpu_availing=`top -n1 -b | grep "%Cpu(s)" | cut -d, -f4 | cut -c 1-5 | sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
local cpu_using=`awk 'BEGIN{printf "%0.2f%%",100-'${cpu_availing}'}'`
local cpu_using_tmp=`awk 'BEGIN{printf "%0.2f",100-'${cpu_availing}'}'`
if [ ${cpu_cores} -eq ${siblings} ]; then
local is_use_hyper_threading="否"
else
local is_use_hyper_threading="是"
fi
echo -e "物理CPU总数: \t${physical_id}"
echo -e "逻辑CPU总数: \t${processor}"
echo -e "每颗物理CPU的核心数:\t${cpu_cores}"
echo -e "是否使用超线程技术: \t${is_use_hyper_threading}"
if [ $(echo "${cpu_using_tmp} < "33.33" " | bc) = "1" ]; then
echo -e "CPU使用率: \t${low_color}${cpu_using}${default_color}"
elif [ $(echo "${cpu_using_tmp} > "66.66" " | bc) = "1" ]; then
echo -e "CPU使用率: \t${high_color}${cpu_using}\t\t${glimmer}CPU使用率较高,请留意!${default_color}"
else
echo -e "CPU使用率: \t${medium_color}${cpu_using}${default_color}"
fi
}
# 内存基本信息检查函数
function mem_message() {
color_schema
echo -e -n "--------------------------------内存信息--------------------------------\n"
local mem_total_tmp=`free | grep "Mem" | awk '{print $2}'`
local mem_used_tmp=`free | grep "Mem" | awk '{print $3}'`
local mem_free_tmp=`free | grep "Mem" | awk '{print $4}'`
local mem_total=`awk 'BEGIN{printf "%0.2f",'${mem_total_tmp}'/1024/1024}'`
local mem_used=`awk 'BEGIN{printf "%0.2f",'${mem_used_tmp}'/1024/1024}'`
local mem_avail=`awk 'BEGIN{printf "%0.2f",'${mem_total}'-'${mem_used}'}'`
local mem_free=`awk 'BEGIN{printf "%0.2f",'${mem_free_tmp}'/1024/1024}'`
local mem_using=`awk 'BEGIN{printf "%0.2f%%",'${mem_used}'/'${mem_total}'*100}'`
local mem_using_tmp=`awk 'BEGIN{printf "%0.2f",'${mem_used}'/'${mem_total}'*100}'`
local swap_total_tmp=`free | grep "Swap" | awk '{print $2}'`
local swap_total=`awk 'BEGIN{printf "%0.2f",'${swap_total_tmp}'/1024/1024}'`
local is_exist_swap="Unknow"
if [ ${swap_total} = "0.00" ]; then
local is_exist_swap="No"
else
local swap_used_tmp=`free | grep "Swap" | awk '{print $3}'`
local swap_free_tmp=`free | grep "Swap" | awk '{print $4}'`
local swap_used=`awk 'BEGIN{printf "%0.2f",'${swap_used_tmp}'/1024/1024}'`
local swap_avail=`awk 'BEGIN{printf "%0.2f",'${swap_total}'-'${swap_used}'}'`
local swap_free=`awk 'BEGIN{printf "%0.2f",'${swap_free_tmp}'/1024/1024}'`
local swap_using=`awk 'BEGIN{printf "%0.2f%%",'${swap_used}'/'${swap_total}'*100}'`
local swap_using_tmp=`awk 'BEGIN{printf "%0.2f",'${swap_used}'/'${swap_total}'*100}'`
fi
echo -e "物理内存:"
echo -e "\t总量:\t${mem_total}G"
echo -e "\t使用:\t${mem_used}G"
echo -e "\t剩余:\t${mem_avail}G"
echo -e "\tFree态:${mem_free}G"
if [ $(echo "${mem_using_tmp} < "33.33" " | bc ) = "1" ]; then
echo -e "\t使用率:${low_color}${mem_using}${default_color}"
elif [ $(echo "${mem_using_tmp} > "66.66" " | bc ) = "1" ]; then
echo -e "\t使用率:${high_color}${mem_using}\t\t\t${glimmer}物理内存使用率较高,请留意!${default_color}"
else
echo -e "\t使用率:${medium_color}${mem_using}${default_color}"
fi
if [ ${is_exist_swap} = "No" ]; then
echo -e "该服务器无交换分区!"
else
echo -e "交换分区:"
echo -e "\t总量:\t${swap_total}G"
echo -e "\t使用:\t${swap_used}G"
echo -e "\t剩余:\t${swap_avail}G"
echo -e "\tFree态:${swap_free}G"
if [ $(echo "${swap_using_tmp} < "33.33" " | bc) = "1" ]; then
echo -e "\t使用率:${low_color}${swap_using}${default_color}"
elif [ $(echo "${swap_using_tmp} > "66.66" " | bc ) = "1" ]; then
echo -e "\t使用率:${high_color}\t\t\t${glimmer}Swap使用率较高,请留意!${swap_using}${default_color}"
else
echo -e "\t使用率:${medium_color}${swap_using}${default_color}"
fi
fi
}
# 磁盘基本信息检查函数
function disk_message() {
color_schema
echo -e -n "--------------------------------磁盘信息--------------------------------\n"
local disk_dev_list=`fdisk -l | grep " /dev" | awk '{print $2}' | sed 's/:/ /g' | sed 's/:/ /g' | awk '{print $1}'`
for disk_dev in ${disk_dev_list}; do
local partition_list=`df | grep "${disk_dev}" | awk '{print $1}'`
local partition_sum=`df | grep "${disk_dev}" | awk '{print $1}' | wc -l`
local disk_dev_size=`fdisk -l | grep " ${disk_dev}" | sed 's/:/ /g' | sed 's/:/ /g' | sed 's/,/ /g' | sed 's/,/ /g' | awk '{print $3,$4}'`
echo -e "磁盘名称:\t${disk_dev}\t\t磁盘大小:${disk_dev_size}"
if [ ${partition_sum} = "0" ]; then
echo -e " 未找到磁盘设备${disk_dev}的挂载点!"
else
for partition in ${partition_list}; do
local mount_point=`df | grep "${partition}" | awk '{print $6}'`
local mount_size_sum=`df | grep "${partition}" | awk '{printf "%0.2f", $2/1024/1024}'`
local mount_size_used=`df | grep "${partition}" | awk '{printf "%0.2f", $3/1024/1024}'`
local mount_size_avail=`df | grep "${partition}" | awk '{printf "%0.2f", $4/1024/1024}'`
local mount_size_using=`awk 'BEGIN{printf "%0.2f%%",'${mount_size_used}'/'${mount_size_sum}'*100}'`
local mount_size_using_tmp=`awk 'BEGIN{printf "%0.2f",'${mount_size_used}'/'${mount_size_sum}'*100}'`
echo -e " 挂载点:\t${mount_point}"
echo -e "\t总量:\t${mount_size_sum}G"
echo -e "\t使用:\t${mount_size_used}G"
echo -e "\t剩余:\t${mount_size_avail}G"
if [ $(echo "${mount_size_using_tmp} < "33.33" " | bc) = "1" ]; then
echo -e "\t使用率:${low_color}${mount_size_using}${default_color}"
elif [ $(echo "${mount_size_using_tmp} > "66.66" " | bc) = "1" ]; then
echo -e "\t使用率:${high_color}${mount_size_using}\t\t\t${glimmer}磁盘使用率较高,请留意!${default_color}"
else
echo -e "\t使用率:${medium_color}${mount_size_using}${default_color}"
fi
done
fi
done
}
# 网络基本信息检查函数
function network_message() {
echo -e -n "------------------------------物理网卡信息------------------------------\n"
local physical_network_card_list=`ls /sys/class/net/ | grep -v "\`ls /sys/devices/virtual/net/\`"`
for physical_network_card in ${physical_network_card_list}; do
local ipaddress_and_prefix=`ip addr | grep -A10 "${physical_network_card}" | awk '/inet/' | awk 'NR==1{print}' | awk '{print $2}'`
local gateway=`ip addr | grep -A10 "${physical_network_card}" | awk '/inet/' | awk 'NR==1{print}' | awk '{print $4}'`
if [ -z ${ipaddress_and_prefix} ]; then
local ipaddress_and_prefix="未查询到${physical_network_card}网卡的IP地址!"
fi
if [ -z ${gateway} ]; then
local gateway="未查询到${physical_network_card}网卡的网关地址!"
fi
echo -e "网卡名称:\t${physical_network_card}"
echo -e " I P 地址:\t${ipaddress_and_prefix}"
echo -e " 网关地址:\t${gateway}"
done
}
# 脚本启动时标识
function starting() {
echo -e -n "---------------------------服务器信息检测脚本---------------------------\n"
}
# 脚本结束时标识
function endding() {
echo -e "LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1WZXJzaW9uOjEuNC0tLS1CeTogU3VpRmVuUFgtLS0t" | base64 -d
echo -e -n "\n"
}
# 判断脚本是否以root权限运行
function permissions_judgment() {
color_schema
if [ `id -u` -ne "0" ]; then
starting
echo -e "\n${high_color}\t\t${glimmer}脚本未以root权限执行,请使用root权限执行此脚本!${default_color}\n"
endding
exit
fi
}
# 主函数
function main() {
clear
permissions_judgment
bc_check
starting
server_basics_message
cpu_message
mem_message
disk_message
network_message
endding
}
# 运行
main
【注意事项】
1、此脚本由shell编写,适用于以bash作为默认shell的服务器,其他解释器并未进行测试。
2、由于脚本在运行时会检测磁盘、CPU等信息,因此脚本需使用root权限执行。
3、脚本在使用时会依赖bc命令,请确保服务器上有此工具。
4、请勿将此脚本用于任何商业用途。 本帖最后由 SuiFenPX 于 2021-7-13 17:33 编辑
排版我调整合适了,现在应该不会有因为排版格式的报错了。 学习了,谢谢楼主,楼主的shell脚本写这么厉害,是怎么练成的 我在UOS上运行不了 感谢,收藏! 脚本排版太乱,根本没法看,也运行不了 楼主,这个shell运行报错,报错如下: 警告: 立即文档在第 2 行被文件结束符分隔 (需要“!”) 第二行报错怎么改? Licae 发表于 2021-7-13 14:58
楼主,这个shell运行报错,报错如下: 警告: 立即文档在第 2 行被文件结束符分隔 (需要“!”)
遇到同样的问题。。。。 我也是,手动删除第二行后,会接着报错 :
find: ‘/proc/1560/task/1560/net’: Invalid argument
find: ‘/proc/1560/net’: Invalid argument
find: ‘/proc/1590/task/1590/net’: Invalid argument
find: ‘/proc/1590/net’: Invalid argument
find: ‘/proc/881390/task/881390/net’: Invalid argument
find: ‘/proc/881390/net’: Invalid argument
./getSystemInfo.sh:行16: 未预期的符号 `then' 附近有语法错误
./getSystemInfo.sh:行16: ` if [ $(echo "${cpu_using_tmp} < "33.33" " | bc) = "1" ]; then echo -e "CPU使用率: \t${low_color}${cpu_using}${default_color}" elif [ $(echo "${cpu_using_tmp} > "66.66" " | bc) = "1" ]; then echo -e "CPU使用率: \t${high_color}${cpu_using}\t\t${glimmer}CPU使用率较高,请留意!${default_color}" else echo -e "CPU使用率: \t${medium_color}${cpu_using}${default_color}" fi}'
排班问题,运行不了