shell脚本编写--服务器资源信息简要展示
1.背景
展示一些简单的服务器信息
2.脚本
#!/usr/bin/env bash # ----------------------------------------------------------------- # -------- 创建日期:2022-01 -------- # -------- 作者:ywjia -------- # -------- 邮箱:duxingren201955@163.com -------- # ----------------------------------------------------------------- # 打印log-详细日志-install_all.log function Print_info_log() { echo -e "\e[1;35m INFO:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1" | tee -a /install_all.log } # 打印log-错误日志-install_all.log function Print_error_log() { echo -e "\e[1;38m ERROR:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1" | tee -a /install_all.log } # 打印log-进度日志-install_show.log function Print_show_log() { echo -e "\e[1;35m INFO:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1" | tee -a /install_show.log } function Calc_disk() { calc_total_size=0 calc_array=$@ for disksize in ${calc_array[@]}; do [ "${disksize}" == "0" ] && disksize=0 || disksize_t=$(echo ${disksize:0:${#disksize}-1}) [ "$(echo ${disksize:(-1)})" == "K" ] && disksize=0 [ "$(echo ${disksize:(-1)})" == "M" ] && disksize=$(awk 'BEGIN{printf "%.f", '$disksize_t' / 1024}') [ "$(echo ${disksize:(-1)})" == "T" ] && disksize=$(awk 'BEGIN{printf "%.f", '$disksize_t' * 1024}') [ "$(echo ${disksize:(-1)})" == "G" ] && disksize=${disksize_t} calc_total_size=$(awk 'BEGIN{printf "%.f", '$calc_total_size' + '$disksize'}') done echo ${calc_total_size} } function Get_system_info() { cpu_name=$(awk -F ":" '/model name/ {name=$2} END {print name}' /proc/cpuinfo) cpu_cores=$(awk -F":" '/processor/ {num++} END {print num}' /proc/cpuinfo) cpu_mhz=$(awk -F":" '/cpu MHz/ {cpu_mhz=$2} END {print cpu_mhz}' /proc/cpuinfo) memory_total=$(free -m | awk '/Mem/{print $2}') memory_used=$(free -m | awk '/Mem/{print $3}') #memory_free=$(free -m | awk '/Mem/{print $4}') memory_buff=$(free -m | awk '/Mem/{print $6}') memory_available=$(free -m | awk '/Mem/{print $7}') swap_total=$(free -m | awk '/Swap/{print $2}') swap_used=$(free -m | awk '/Swap/{print $3}') #swap_free=$(free -m | awk '/Swap/{print $4}') running_time=$(awk '{days=$1/86400;hours=($1%86400)/3600;mins=($1%3600)/60} {printf("%d days %d hour %d min\n",days,hours,mins)}' /proc/uptime) load_average=$(w | awk '/load average/{print $(NF-2),$(NF-1),$(NF) }') system_version=$(awk '{print $1" "$4}' /etc/redhat-release) system_arch=$(uname -m) system_kernel=$(uname -r) disk_size=($(LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem|\/boot' | awk '{print $2}')) disk_usedsize=($(LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem|\/boot' | awk '{print $3}')) disk_total_size=$(Calc_disk ${disk_size[@]}) disk_total_usedsize=$(Calc_disk ${disk_usedsize[@]}) virtual_info=$(dmesg) 2>/dev/null [[ "$virtual_info" =~ "VMware Virtual Platform" ]] && virtual_type="VMware" || virtual_type="none" } function Print_system_info() { Print_info_log " CPU 类型 Model : $cpu_name " Print_info_log " CPU 核心信息 CoreInfo : $cpu_cores Cores $cpu_mhz MHz $system_arch " Print_info_log " 虚拟化类型 virtual_info : $virtual_type " Print_info_log " 系统版本 OS_Version : $system_version " Print_info_log " 系统内核 Kernel : $system_kernel " Print_info_log " 磁盘统计 Store : $disk_total_usedsize GB / $disk_total_size GB " Print_info_log " 内存统计 Memory : $memory_used MB / $memory_total MB ($memory_buff MB Buff $memory_available MB available)" Print_info_log " 交换分区统计 SWAP : $swap_used MB / $swap_total MB " Print_info_log " 系统运行时间 Uptime : $running_time " Print_info_log " 系统平均负载 Load Average : $load_average " } #主函数 function main() { Get_system_info Print_system_info } #------------start--------- main #------------end-----------
---------------------------------------采遍所有的坑,让别人无坑可踩---------------------------------------