From fbd60c2188dc0c345e19baecb64bf71e19df0b2b Mon Sep 17 00:00:00 2001 From: wangdong_cmcc Date: Wed, 27 Nov 2024 18:32:04 +0800 Subject: [PATCH 1/3] refactor: refactoring the check command --- .../syscommand_check_scripts/common_lib.sh | 10 ++- .../test01_hardinfo.sh | 87 +++++++++++++++++++ .../syscommand_check_scripts/test01_lscpu.sh | 56 ------------ .../{test02_uname.sh => test02_softinfo.sh} | 64 +++++++++----- ...{test04_timesync.sh => test03_timesync.sh} | 0 .../test03_version.sh | 53 ----------- 6 files changed, 138 insertions(+), 132 deletions(-) create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh delete mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh rename source/tools/detect/generic/syscommand_check/syscommand_check_scripts/{test02_uname.sh => test02_softinfo.sh} (33%) rename source/tools/detect/generic/syscommand_check/syscommand_check_scripts/{test04_timesync.sh => test03_timesync.sh} (100%) delete mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh index a8122c8a..efd5fd34 100644 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh @@ -7,7 +7,7 @@ LOG_ERROR="[ERROR]" # 日志文件路径 LOG_FILE="/var/log/syscommand_check.log" -# 记录信息日志 +# 打印日志信息 function log_info() { local timestamp=$(date "+%Y-%m-%d %H:%M:%S") echo "${timestamp} ${LOG_INFO} $*" >> "${LOG_FILE}" @@ -18,3 +18,11 @@ function log_error() { local timestamp=$(date "+%Y-%m-%d %H:%M:%S") echo "${timestamp} ${LOG_ERROR} $*" >> "${LOG_FILE}" } + +# 定义分割线长度 +LINE_LENGTH=100 + +# 打印分割线函数 +print_line() { + printf "%-${LINE_LENGTH}s\n" "$1" +} diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh new file mode 100644 index 00000000..e7799ab3 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh @@ -0,0 +1,87 @@ +#!/bin/bash + + +# 获取脚本所在目录 +OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) +source "$OET_PATH/common_lib.sh" + +# 环境准备 +function pre_test() { + OLD_LANG=$LANG + export LANG=en_US.UTF-8 +} + +# 测试执行 +function run_test() { + # 获取系统架构 + log_info "$(print_line "================ System Architecture ================")" + architecture=$(uname -m) + log_info " Architecture: $architecture" + + # 获取CPU信息 + log_info "$(print_line "================ CPU Information ==================")" + cpu_info=$(cat /proc/cpuinfo | grep -E 'model name|cpu cores|cache size|processor' | sort | uniq | sed 's/^/ /') + log_info "$cpu_info" + + # 获取内存信息 + log_info "$(print_line "================ Memory Information ==================")" + memory_info=$(free -h | awk '/^Mem:/ {printf " Total: %s\n Used: %s\n Free: %s\n", $2, $3, $4}') + log_info "$memory_info" + + # 获取磁盘信息 + log_info "$(print_line "================ Disk Information ==================")" + disk_info=$(df -hT | awk 'BEGIN {printf " %-30s %-10s %-10s %-10s %-10s %-10s\n", "Filesystem", "Type", "Size", "Used", "Avail", "Use%"} {print " " $1, $2, $3, $4, $5, $6}' | column -t) + log_info "$disk_info" + + # 获取网络接口信息和带宽及流量信息 + log_info "$(print_line "================ Network Information =================")" + network_info=$(ip -o -4 addr show | awk '{print " " $2, $4}' | grep -v 'lo' | column -t) + log_info "$network_info" + + for interface in $(ip -o link | awk -F': ' '{print $2}' | grep -v 'lo'); do + speed=$(ethtool $interface 2>/dev/null | grep 'Speed:' | awk '{print $2}') + rx_bytes=$(ethtool -S $interface 2>/dev/null | grep 'rx_bytes:' | awk '{print $2}') + tx_bytes=$(ethtool -S $interface 2>/dev/null | grep 'tx_bytes:' | awk '{print $2}') + if [ -n "$speed" ] || [ -n "$rx_bytes" ] || [ -n "$tx_bytes" ]; then + network_details=$(printf " %-10s Speed: %-10s RX Bytes: %-10s TX Bytes: %-10s\n" "$interface" "$speed" "$rx_bytes" "$tx_bytes") + log_info "$network_details" + fi + done + + # 获取显卡信息 + log_info "$(print_line \"================ GPU Information =====================\")" + gpu_info=$(lspci | grep -i 'vga\|3d\|2d' | column -t | sed 's/^/ /') + log_info "$gpu_info" + if command -v nvidia-smi &> /dev/null; then + nvidia_info=$(nvidia-smi | sed 's/^/ /') + log_info "$nvidia_info" + else + log_info " NVIDIA GPU not found or nvidia-smi is not installed." + fi + + # 获取主板信息 + log_info "$(print_line \"================ Motherboard Information =============\")" + motherboard_info=$(dmidecode -t baseboard | grep -E 'Manufacturer|Product|Serial' | column -t | sed 's/^/ /') + log_info "$motherboard_info" + + # 获取BIOS信息 + log_info "$(print_line \"================ BIOS Information ====================\")" + bios_info=$(dmidecode -t bios | grep -E 'Vendor|Version|Release' | column -t | sed 's/^/ /') + log_info "$bios_info" +} + +# 环境清理 +function post_test() { + export LANG=${OLD_LANG} +} + +# 用例调用入口 +function main() { + pre_test + run_test + post_test +} + +# 脚本入口 +main "$@" + diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh deleted file mode 100644 index 7d6be57c..00000000 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -# lscpu - lscpu 是一个非常有用的命令,用于显示有关系统 CPU 架构的详细信息 - -# 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 环境准备 -function pre_test() { - OLD_LANG=$LANG - export LANG=en_US.UTF-8 -} - -# 测试执行 -function run_test() { - # 初始化测试结果变量 - lscpu_passed=false - - # 检查 lscpu 命令 - lscpu_output=$(lscpu | grep -i 'model name' | awk -F':' '{print $2}' | xargs) - lscpu_result=$? - - if [ $lscpu_result -eq 0 ]; then - lscpu_passed=true - #log_info "lscpu 命令: 通过" - log_info "lscpu 命令输出: $lscpu_output" - else - log_error "lscpu 命令: 失败" - log_error "lscpu 命令输出: $lscpu_output" - fi - - #log_info "完成测试!" -} - -# 环境清理 -function post_test() { - export LANG=${OLD_LANG} -} - -# 用例调用入口 -function main() { - pre_test - run_test - post_test - - # 返回测试结果 - if [ "$lscpu_passed" = true ]; then - return 0 - else - return 1 - fi -} - -# 脚本入口 -main "$@" diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh similarity index 33% rename from source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh index a35a80e2..93dd1efe 100644 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh @@ -12,27 +12,54 @@ function pre_test() { export LANG=en_US.UTF-8 } -# 测试执行 -function run_test() { - # 初始化测试结果变量 - uname_passed=false - - # 检查 uname 命令 - uname_output=$(uname -a) - uname_result=$? +# 获取系统内核版本 +function get_kernel_version() { + local kernel_version=$(uname -r) + log_info "Kernel Version: $kernel_version" +} - if [ $uname_result -eq 0 ]; then - uname_passed=true - #log_info "uname 命令: 通过" - log_info "uname 命令输出: $uname_output" +# 获取操作系统版本 +function get_os_version() { + if [ -f /etc/os-release ]; then + . /etc/os-release + log_info "Operating System: $PRETTY_NAME" + elif [ -f /etc/redhat-release ]; then + local os_version=$(cat /etc/redhat-release) + log_info "Operating System: $os_version" else - log_error "uname 命令: 失败" - log_error "uname 命令输出: $uname_output" + log_info "Operating System: Unknown" fi +} + +# 获取bash版本 +function get_bash_version() { + local bash_version=$BASH_VERSION + log_info "Bash Version: $bash_version" +} - #log_info "完成测试!" +# 获取软件版本的通用函数 +function get_software_version() { + local software_name=$1 + local version_command=$2 + if command -v $software_name &> /dev/null; then + local version=$(eval "$version_command" | head -n 1) + log_info "$software_name Version: $version" + else + log_info "$software_name is not installed." + fi } +# 测试执行 +function run_test() { + get_os_version + get_kernel_version + + get_software_version "git" "git --version" + get_software_version "python3" "python3 --version" + get_software_version "java" "java -version 2>&1" + get_software_version "docker" "docker --version 2>&1" +} + # 环境清理 function post_test() { export LANG=${OLD_LANG} @@ -43,13 +70,6 @@ function main() { pre_test run_test post_test - - # 返回测试结果 - if [ "$uname_passed" = true ]; then - return 0 - else - return 1 - fi } # 脚本入口 diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_timesync.sh similarity index 100% rename from source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_timesync.sh diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh deleted file mode 100644 index b38dc4b9..00000000 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash -# 获取操作系统版本信息:/etc/os-release, /etc/redhat-release, /etc/debian_version - -# 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 环境准备 -function pre_test() { - OLD_LANG=$LANG - export LANG=en_US.UTF-8 -} - -# 获取系统版本信息 -function get_system_version() { - if [ -f /etc/os-release ]; then - . /etc/os-release - system_version="$NAME $VERSION" - elif [ -f /etc/redhat-release ]; then - system_version=$(cat /etc/redhat-release) - elif [ -f /etc/debian_version ]; then - system_version="Debian $(cat /etc/debian_version)" - else - system_version="Unknown" - fi - - echo "$system_version" -} - -# 测试执行 -function run_test() { - # 获取系统版本信息 - system_version=$(get_system_version) - log_info "当前系统版本: $system_version" -} - -# 环境清理 -function post_test() { - export LANG=${OLD_LANG} -} - -# 用例调用入口 -function main() { - pre_test - run_test - post_test - - # 返回测试结果 - return 0 -} - -# 脚本入口 -main "$@" -- Gitee From e41ebed7e533b43bf736caddf874c688929b017f Mon Sep 17 00:00:00 2001 From: wangdong_cmcc Date: Wed, 27 Nov 2024 18:32:04 +0800 Subject: [PATCH 2/3] refactor: refactoring the check command --- .../syscommand_check_scripts/common_lib.sh | 10 ++- .../test01_hardinfo.sh | 87 +++++++++++++++++++ .../syscommand_check_scripts/test01_lscpu.sh | 56 ------------ .../{test02_uname.sh => test02_softinfo.sh} | 64 +++++++++----- ...{test04_timesync.sh => test03_timesync.sh} | 0 .../test03_version.sh | 53 ----------- 6 files changed, 138 insertions(+), 132 deletions(-) create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh delete mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh rename source/tools/detect/generic/syscommand_check/syscommand_check_scripts/{test02_uname.sh => test02_softinfo.sh} (33%) rename source/tools/detect/generic/syscommand_check/syscommand_check_scripts/{test04_timesync.sh => test03_timesync.sh} (100%) delete mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh index a8122c8a..efd5fd34 100644 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh @@ -7,7 +7,7 @@ LOG_ERROR="[ERROR]" # 日志文件路径 LOG_FILE="/var/log/syscommand_check.log" -# 记录信息日志 +# 打印日志信息 function log_info() { local timestamp=$(date "+%Y-%m-%d %H:%M:%S") echo "${timestamp} ${LOG_INFO} $*" >> "${LOG_FILE}" @@ -18,3 +18,11 @@ function log_error() { local timestamp=$(date "+%Y-%m-%d %H:%M:%S") echo "${timestamp} ${LOG_ERROR} $*" >> "${LOG_FILE}" } + +# 定义分割线长度 +LINE_LENGTH=100 + +# 打印分割线函数 +print_line() { + printf "%-${LINE_LENGTH}s\n" "$1" +} diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh new file mode 100644 index 00000000..29b0ce7c --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh @@ -0,0 +1,87 @@ +#!/bin/bash + + +# 获取脚本所在目录 +OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) +source "$OET_PATH/common_lib.sh" + +# 环境准备 +function pre_test() { + OLD_LANG=$LANG + export LANG=en_US.UTF-8 +} + +# 测试执行 +function run_test() { + # 获取系统架构 + log_info "$(print_line "================ System Architecture ================")" + architecture=$(uname -m) + log_info " Architecture: $architecture" + + # 获取CPU信息 + log_info "$(print_line "================ CPU Information ==================")" + cpu_info=$(cat /proc/cpuinfo | grep -E 'model name|cpu cores|cache size|processor' | sort | uniq | sed 's/^/ /') + log_info "$cpu_info" + + # 获取内存信息 + log_info "$(print_line "================ Memory Information ==================")" + memory_info=$(free -h | awk '/^Mem:/ {printf " Total: %s\n Used: %s\n Free: %s\n", $2, $3, $4}') + log_info "$memory_info" + + # 获取磁盘信息 + log_info "$(print_line "================ Disk Information ==================")" + disk_info=$(df -hT | awk 'BEGIN {printf " %-30s %-10s %-10s %-10s %-10s %-10s\n", "Filesystem", "Type", "Size", "Used", "Avail", "Use%"} {print " " $1, $2, $3, $4, $5, $6}' | column -t) + log_info "$disk_info" + + # 获取网络接口信息和带宽及流量信息 + log_info "$(print_line "================ Network Information =================")" + network_info=$(ip -o -4 addr show | awk '{print " " $2, $4}' | grep -v 'lo' | column -t) + log_info "$network_info" + + for interface in $(ip -o link | awk -F': ' '{print $2}' | grep -v 'lo'); do + speed=$(ethtool $interface 2>/dev/null | grep 'Speed:' | awk '{print $2}') + rx_bytes=$(ethtool -S $interface 2>/dev/null | grep 'rx_bytes:' | awk '{print $2}') + tx_bytes=$(ethtool -S $interface 2>/dev/null | grep 'tx_bytes:' | awk '{print $2}') + if [ -n "$speed" ] || [ -n "$rx_bytes" ] || [ -n "$tx_bytes" ]; then + network_details=$(printf " %-10s Speed: %-10s RX Bytes: %-10s TX Bytes: %-10s\n" "$interface" "$speed" "$rx_bytes" "$tx_bytes") + log_info "$network_details" + fi + done + + # 获取显卡信息 + log_info "$(print_line "================ GPU Information =====================\")" + gpu_info=$(lspci | grep -i 'vga\|3d\|2d' | column -t | sed 's/^/ /') + log_info "$gpu_info" + if command -v nvidia-smi &> /dev/null; then + nvidia_info=$(nvidia-smi | sed 's/^/ /') + log_info "$nvidia_info" + else + log_info " NVIDIA GPU not found or nvidia-smi is not installed." + fi + + # 获取主板信息 + log_info "$(print_line "================ Motherboard Information =============\")" + motherboard_info=$(dmidecode -t baseboard | grep -E 'Manufacturer|Product|Serial' | column -t | sed 's/^/ /') + log_info "$motherboard_info" + + # 获取BIOS信息 + log_info "$(print_line "================ BIOS Information ====================\")" + bios_info=$(dmidecode -t bios | grep -E 'Vendor|Version|Release' | column -t | sed 's/^/ /') + log_info "$bios_info" +} + +# 环境清理 +function post_test() { + export LANG=${OLD_LANG} +} + +# 用例调用入口 +function main() { + pre_test + run_test + post_test +} + +# 脚本入口 +main "$@" + diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh deleted file mode 100644 index 7d6be57c..00000000 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -# lscpu - lscpu 是一个非常有用的命令,用于显示有关系统 CPU 架构的详细信息 - -# 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 环境准备 -function pre_test() { - OLD_LANG=$LANG - export LANG=en_US.UTF-8 -} - -# 测试执行 -function run_test() { - # 初始化测试结果变量 - lscpu_passed=false - - # 检查 lscpu 命令 - lscpu_output=$(lscpu | grep -i 'model name' | awk -F':' '{print $2}' | xargs) - lscpu_result=$? - - if [ $lscpu_result -eq 0 ]; then - lscpu_passed=true - #log_info "lscpu 命令: 通过" - log_info "lscpu 命令输出: $lscpu_output" - else - log_error "lscpu 命令: 失败" - log_error "lscpu 命令输出: $lscpu_output" - fi - - #log_info "完成测试!" -} - -# 环境清理 -function post_test() { - export LANG=${OLD_LANG} -} - -# 用例调用入口 -function main() { - pre_test - run_test - post_test - - # 返回测试结果 - if [ "$lscpu_passed" = true ]; then - return 0 - else - return 1 - fi -} - -# 脚本入口 -main "$@" diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh similarity index 33% rename from source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh index a35a80e2..93dd1efe 100644 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_softinfo.sh @@ -12,27 +12,54 @@ function pre_test() { export LANG=en_US.UTF-8 } -# 测试执行 -function run_test() { - # 初始化测试结果变量 - uname_passed=false - - # 检查 uname 命令 - uname_output=$(uname -a) - uname_result=$? +# 获取系统内核版本 +function get_kernel_version() { + local kernel_version=$(uname -r) + log_info "Kernel Version: $kernel_version" +} - if [ $uname_result -eq 0 ]; then - uname_passed=true - #log_info "uname 命令: 通过" - log_info "uname 命令输出: $uname_output" +# 获取操作系统版本 +function get_os_version() { + if [ -f /etc/os-release ]; then + . /etc/os-release + log_info "Operating System: $PRETTY_NAME" + elif [ -f /etc/redhat-release ]; then + local os_version=$(cat /etc/redhat-release) + log_info "Operating System: $os_version" else - log_error "uname 命令: 失败" - log_error "uname 命令输出: $uname_output" + log_info "Operating System: Unknown" fi +} + +# 获取bash版本 +function get_bash_version() { + local bash_version=$BASH_VERSION + log_info "Bash Version: $bash_version" +} - #log_info "完成测试!" +# 获取软件版本的通用函数 +function get_software_version() { + local software_name=$1 + local version_command=$2 + if command -v $software_name &> /dev/null; then + local version=$(eval "$version_command" | head -n 1) + log_info "$software_name Version: $version" + else + log_info "$software_name is not installed." + fi } +# 测试执行 +function run_test() { + get_os_version + get_kernel_version + + get_software_version "git" "git --version" + get_software_version "python3" "python3 --version" + get_software_version "java" "java -version 2>&1" + get_software_version "docker" "docker --version 2>&1" +} + # 环境清理 function post_test() { export LANG=${OLD_LANG} @@ -43,13 +70,6 @@ function main() { pre_test run_test post_test - - # 返回测试结果 - if [ "$uname_passed" = true ]; then - return 0 - else - return 1 - fi } # 脚本入口 diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_timesync.sh similarity index 100% rename from source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_timesync.sh diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh deleted file mode 100644 index b38dc4b9..00000000 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash -# 获取操作系统版本信息:/etc/os-release, /etc/redhat-release, /etc/debian_version - -# 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 环境准备 -function pre_test() { - OLD_LANG=$LANG - export LANG=en_US.UTF-8 -} - -# 获取系统版本信息 -function get_system_version() { - if [ -f /etc/os-release ]; then - . /etc/os-release - system_version="$NAME $VERSION" - elif [ -f /etc/redhat-release ]; then - system_version=$(cat /etc/redhat-release) - elif [ -f /etc/debian_version ]; then - system_version="Debian $(cat /etc/debian_version)" - else - system_version="Unknown" - fi - - echo "$system_version" -} - -# 测试执行 -function run_test() { - # 获取系统版本信息 - system_version=$(get_system_version) - log_info "当前系统版本: $system_version" -} - -# 环境清理 -function post_test() { - export LANG=${OLD_LANG} -} - -# 用例调用入口 -function main() { - pre_test - run_test - post_test - - # 返回测试结果 - return 0 -} - -# 脚本入口 -main "$@" -- Gitee From 34b2ffb460d9369d66d0465f84a1db00bf8f4620 Mon Sep 17 00:00:00 2001 From: wangdong_cmcc Date: Wed, 27 Nov 2024 18:50:37 +0800 Subject: [PATCH 3/3] Fix: fix the problem of display --- .../test01_hardinfo.sh | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh index 29b0ce7c..3ede2387 100644 --- a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_hardinfo.sh @@ -1,6 +1,5 @@ #!/bin/bash - # 获取脚本所在目录 OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) source "$OET_PATH/common_lib.sh" @@ -13,30 +12,34 @@ function pre_test() { # 测试执行 function run_test() { - # 获取系统架构 - log_info "$(print_line "================ System Architecture ================")" - architecture=$(uname -m) - log_info " Architecture: $architecture" + # 获取系统架构 + log_info "$(print_line "================ System Architecture ================")" + architecture=$(uname -m) + log_info " Architecture: $architecture" + log_info "" # 获取CPU信息 log_info "$(print_line "================ CPU Information ==================")" cpu_info=$(cat /proc/cpuinfo | grep -E 'model name|cpu cores|cache size|processor' | sort | uniq | sed 's/^/ /') log_info "$cpu_info" + log_info "" # 获取内存信息 log_info "$(print_line "================ Memory Information ==================")" memory_info=$(free -h | awk '/^Mem:/ {printf " Total: %s\n Used: %s\n Free: %s\n", $2, $3, $4}') log_info "$memory_info" + log_info "" # 获取磁盘信息 log_info "$(print_line "================ Disk Information ==================")" - disk_info=$(df -hT | awk 'BEGIN {printf " %-30s %-10s %-10s %-10s %-10s %-10s\n", "Filesystem", "Type", "Size", "Used", "Avail", "Use%"} {print " " $1, $2, $3, $4, $5, $6}' | column -t) - log_info "$disk_info" + log_info "$(df -hT | awk 'BEGIN {printf " %-30s %-10s %-10s %-10s %-10s %-10s\n", "Filesystem", "Type", "Size", "Used", "Avail", "Use%"} {print " " $1, $2, $3, $4, $5, $6}' | column -t | sed '1!b')" + log_info "" # 获取网络接口信息和带宽及流量信息 log_info "$(print_line "================ Network Information =================")" network_info=$(ip -o -4 addr show | awk '{print " " $2, $4}' | grep -v 'lo' | column -t) log_info "$network_info" + log_info "" for interface in $(ip -o link | awk -F': ' '{print $2}' | grep -v 'lo'); do speed=$(ethtool $interface 2>/dev/null | grep 'Speed:' | awk '{print $2}') @@ -47,9 +50,10 @@ function run_test() { log_info "$network_details" fi done + log_info "" # 获取显卡信息 - log_info "$(print_line "================ GPU Information =====================\")" + log_info "$(print_line "================ GPU Information =====================")" gpu_info=$(lspci | grep -i 'vga\|3d\|2d' | column -t | sed 's/^/ /') log_info "$gpu_info" if command -v nvidia-smi &> /dev/null; then @@ -58,16 +62,19 @@ function run_test() { else log_info " NVIDIA GPU not found or nvidia-smi is not installed." fi + log_info "" # 获取主板信息 - log_info "$(print_line "================ Motherboard Information =============\")" + log_info "$(print_line "================ Motherboard Information =============")" motherboard_info=$(dmidecode -t baseboard | grep -E 'Manufacturer|Product|Serial' | column -t | sed 's/^/ /') log_info "$motherboard_info" + log_info "" # 获取BIOS信息 - log_info "$(print_line "================ BIOS Information ====================\")" + log_info "$(print_line "================ BIOS Information ====================")" bios_info=$(dmidecode -t bios | grep -E 'Vendor|Version|Release' | column -t | sed 's/^/ /') log_info "$bios_info" + log_info "" } # 环境清理 @@ -84,4 +91,3 @@ function main() { # 脚本入口 main "$@" - -- Gitee