From 7d03439897da98123e0067ebd738953f35c98554 Mon Sep 17 00:00:00 2001 From: wangdong_cmcc Date: Fri, 8 Nov 2024 19:16:46 +0800 Subject: [PATCH] update: change syscommand_check directory and help info --- rpm/sysak-build-nodep-aarch64.sh | 2 +- rpm/sysak-build-nodep.sh | 2 +- rpm/sysak-build.sh | 2 +- source/tools/bclinux/Makefile | 1 - source/tools/bclinux/checkcommand/README.md | 2 - .../bclinux/checkcommand/checkcommand.sh | 69 ----------- .../tools/bclinux/checkcommand/common_lib.sh | 117 ------------------ .../generic/syscommand_check}/Makefile | 2 +- .../detect/generic/syscommand_check/README.md | 18 +++ .../syscommand_check/syscommand_check.sh | 68 ++++++++++ .../syscommand_check_scripts/clear.sh | 13 ++ .../syscommand_check_scripts/common_lib.sh | 20 +++ .../syscommand_check_clear.sh | 13 ++ .../syscommand_check_scripts}/test01_lscpu.sh | 18 ++- .../syscommand_check_scripts}/test02_uname.sh | 23 ++-- .../test03_version.sh | 53 ++++++++ 16 files changed, 215 insertions(+), 208 deletions(-) delete mode 100644 source/tools/bclinux/Makefile delete mode 100644 source/tools/bclinux/checkcommand/README.md delete mode 100644 source/tools/bclinux/checkcommand/checkcommand.sh delete mode 100644 source/tools/bclinux/checkcommand/common_lib.sh rename source/tools/{bclinux/checkcommand => detect/generic/syscommand_check}/Makefile (47%) create mode 100644 source/tools/detect/generic/syscommand_check/README.md create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check.sh create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/clear.sh create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/syscommand_check_clear.sh rename source/tools/{bclinux/checkcommand => detect/generic/syscommand_check/syscommand_check_scripts}/test01_lscpu.sh (57%) rename source/tools/{bclinux/checkcommand => detect/generic/syscommand_check/syscommand_check_scripts}/test02_uname.sh (55%) create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh diff --git a/rpm/sysak-build-nodep-aarch64.sh b/rpm/sysak-build-nodep-aarch64.sh index 2c712b6b..ef51f213 100755 --- a/rpm/sysak-build-nodep-aarch64.sh +++ b/rpm/sysak-build-nodep-aarch64.sh @@ -47,7 +47,7 @@ mkdir -p \$RPM_BUILD_ROOT/usr/lib/systemd/system/ #/bin/cp -f $BUILD_DIR/.sysak_components/tools/dist/app/collector/plugin.yaml \$RPM_BUILD_ROOT/etc/sysak/ /bin/cp $SOURCE_DIR/rpm/sysak.service \$RPM_BUILD_ROOT/usr/lib/systemd/system/ /bin/cp $SOURCE_DIR/rpm/sysak_server.conf \$RPM_BUILD_ROOT/usr/local/sysak/ -/bin/cp -rf $SOURCE_DIR/source/tools/bclinux/checkcommand/*.sh \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ +/bin/cp -rf $SOURCE_DIR/source/tools/detect/generic/syscommand_check/*.sh \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ %preun diff --git a/rpm/sysak-build-nodep.sh b/rpm/sysak-build-nodep.sh index 48f19b81..3df5a30d 100755 --- a/rpm/sysak-build-nodep.sh +++ b/rpm/sysak-build-nodep.sh @@ -50,7 +50,7 @@ mkdir -p \$RPM_BUILD_ROOT/usr/lib/systemd/system/ /bin/cp -f $BUILD_DIR/.sysak_components/tools/dist/app/etc/* \$RPM_BUILD_ROOT/etc/sysak/ /bin/cp $SOURCE_DIR/rpm/sysak.service \$RPM_BUILD_ROOT/usr/lib/systemd/system/ /bin/cp $SOURCE_DIR/rpm/sysak_server.conf \$RPM_BUILD_ROOT/usr/local/sysak/ -/bin/cp -rf $SOURCE_DIR/source/tools/bclinux/checkcommand/*.sh \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ +/bin/cp -rf $SOURCE_DIR/source/tools/detect/generic/syscommand_check/syscommand_check_scripts \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ %preun systemctl stop sysak diff --git a/rpm/sysak-build.sh b/rpm/sysak-build.sh index 23806654..f6e356df 100755 --- a/rpm/sysak-build.sh +++ b/rpm/sysak-build.sh @@ -45,7 +45,7 @@ mkdir -p \$RPM_BUILD_ROOT/usr/lib/systemd/system/ /bin/cp $SOURCE_DIR/rpm/sysak.service \$RPM_BUILD_ROOT/usr/lib/systemd/system/ /bin/cp -f $BUILD_DIR/.sysak_components/tools/monitor/sysakmon.conf \$RPM_BUILD_ROOT/usr/local/sysak/ /bin/cp $SOURCE_DIR/rpm/sysak_server.conf \$RPM_BUILD_ROOT/usr/local/sysak/ -/bin/cp -rf $SOURCE_DIR/source/tools/bclinux/checkcommand/*.sh \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ +/bin/cp -rf $SOURCE_DIR/source/tools/detect/generic/syscommand_check/syscommand_check_scripts \$RPM_BUILD_ROOT/usr/local/sysak/.sysak_components/tools/ %preun diff --git a/source/tools/bclinux/Makefile b/source/tools/bclinux/Makefile deleted file mode 100644 index 6a58b3a5..00000000 --- a/source/tools/bclinux/Makefile +++ /dev/null @@ -1 +0,0 @@ -include $(SRC)/mk/sub.mk diff --git a/source/tools/bclinux/checkcommand/README.md b/source/tools/bclinux/checkcommand/README.md deleted file mode 100644 index c58ed89a..00000000 --- a/source/tools/bclinux/checkcommand/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# 功能说明 -系统指令支持 diff --git a/source/tools/bclinux/checkcommand/checkcommand.sh b/source/tools/bclinux/checkcommand/checkcommand.sh deleted file mode 100644 index 6ef35291..00000000 --- a/source/tools/bclinux/checkcommand/checkcommand.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/bash - -# 创建日期: 2024-10-15 - -# 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 日志文件路径 -LOG_FILE="/var/log/checkcommand.log" - -# 重定向输出到日志文件 -exec 6>&1 -exec 7>&2 -exec 1>>"$LOG_FILE" -exec 2>>"$LOG_FILE" - -# 定义要执行的测试脚本列表 -TEST_SCRIPTS=("test01_lscpu.sh" "test02_uname.sh") - -# 记录总测试结果 -total_result=0 - -# 执行单个测试脚本的函数 -function run_single_test() { - local script=$1 - local script_path="$OET_PATH/$script" - - #LOG_INFO "开始执行测试脚本: $script_path" - - # 源入测试脚本并调用 main 函数 - source "$script_path" && main "$@" - - # 检查测试脚本的执行结果 - local result=$? - if [ $result -ne 0 ]; then - LOG_ERROR "The result of running the $script_path script is $result" - total_result=$((total_result + 1)) - else - LOG_INFO "The result of running the $script_path script is 0" - fi -} - -# 主函数,执行所有测试脚本 -function main() { - #LOG_INFO "开始执行所有测试脚本..." - - for script in "${TEST_SCRIPTS[@]}"; do - run_single_test "$script" - done - - # 恢复标准输出和标准错误 - exec 1>&6 - exec 2>&7 - exec 6>&- - exec 7>&- - - # 检查总测试结果 - if [ $total_result -eq 0 ]; then - echo "所有测试脚本均已执行,查看日志详情/var/log/checkcommand.log" - else - echo "有 $total_result 个测试脚本执行失败" - fi - - #LOG_INFO "所有测试脚本执行完毕" -} - -# 脚本入口 -main "$@" diff --git a/source/tools/bclinux/checkcommand/common_lib.sh b/source/tools/bclinux/checkcommand/common_lib.sh deleted file mode 100644 index 77db6fd3..00000000 --- a/source/tools/bclinux/checkcommand/common_lib.sh +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/bash - -# 日志文件路径 -LOG_FILE="/var/log/checkcommand.log" - -function LOG_INFO() { - message=$1 - echo "[INFO] $message" >> "$LOG_FILE" -} - -function LOG_ERROR() { - message=$1 - echo "[ERROR] $message" >> "$LOG_FILE" -} - -function LOG_WARN() { - message=$1 - echo "[WARN] $message" >> "$LOG_FILE" -} - -function LOG_DEBUG() { - message=$1 - echo "[DEBUG] $message" >> "$LOG_FILE" -} - -# 结果检查 -function CHECK_RESULT() { - local actual_result=$1 - local expect_result=${2-0} - local mode=${3-0} - local error_log=$4 - local exit_mode=${5-0} - local exec_result=0 - - if [ -z "$actual_result" ]; then - LOG_ERROR "Missing actual error code." - return 1 - fi - - if ! [[ "$mode" =~ ^[0-9]+$ ]]; then - LOG_ERROR "Invalid mode value: $mode" - return 1 - fi - - if ! [[ "$exit_mode" =~ ^[0-9]+$ ]]; then - LOG_ERROR "Invalid exit_mode value: $exit_mode" - return 1 - fi - - if [ "$actual_result" != "$expect_result" ]; then - [ -n "$error_log" ] && LOG_ERROR "$error_log" - ((exec_result++)) - LOG_ERROR "${BASH_SOURCE[1]} line ${BASH_LINENO[0]}" - if [ $exit_mode -eq 1 ]; then - exit 1 - fi - fi - - return 0 -} - -# 执行用例 -function CASE_RESULT() { - local case_result=$1 - local exec_result=0 - - if [ -z "$exec_result" ]; then - if [ $case_result -eq 0 ]; then - LOG_INFO "succeed to execute the case." - exec_result="" - exit 0 - else - LOG_ERROR "failed to execute the case." - exit $case_result - fi - fi - - if [ $exec_result -gt 0 ]; then - LOG_ERROR "failed to execute the case." - exit $exec_result - fi - - LOG_INFO "succeed to execute the case." - exit $exec_result -} - -# 默认测试后清理 -function POST_TEST_DEFAULT() { - LOG_INFO "$0 post_test" -} - -# 主入口 -function main() { - local input_path="$0" - local file_name=$(basename "$input_path" .sh) - local relative_path=${input_path#*local_sh/} - local log_path="/var/log/checkcommand/${relative_path:-$file_name}" - - mkdir -p "$log_path" - exec 6>&1 - exec 7>&2 - exec >>"$log_path"/"$(date +%Y-%m-%d-%T)".log 2>&1 - - trap '[[ $(type -t post_test) ]] && post_test || POST_TEST_DEFAULT' EXIT INT HUP TERM - - if ! rpm -q expect >/dev/null 2>&1; then - if dnf -y install expect; then - LOG_INFO "Succeed to install expect." - else - LOG_ERROR "Failed to install expect." - fi - fi - - [[ $(type -t config_params) ]] && config_params - [[ $(type -t pre_test) ]] && pre_test - [[ $(type -t run_test) ]] && run_test && CASE_RESULT $? -} diff --git a/source/tools/bclinux/checkcommand/Makefile b/source/tools/detect/generic/syscommand_check/Makefile similarity index 47% rename from source/tools/bclinux/checkcommand/Makefile rename to source/tools/detect/generic/syscommand_check/Makefile index a1231550..3c8922dc 100644 --- a/source/tools/bclinux/checkcommand/Makefile +++ b/source/tools/detect/generic/syscommand_check/Makefile @@ -1,2 +1,2 @@ -target := checkcommand +target := syscommand_check include $(SRC)/mk/sh.mk diff --git a/source/tools/detect/generic/syscommand_check/README.md b/source/tools/detect/generic/syscommand_check/README.md new file mode 100644 index 00000000..9ea27ce7 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/README.md @@ -0,0 +1,18 @@ +# 功能说明 +A tool for "health checking" after operating system upgrade +系统指令健康巡检 + +使用方法: + +查看帮助信息 +# sysak syscommand_ciheck --help 或者 -h + +执行系统指令健康巡检 +# sysak syscommand_check + +详细日志输出 +/var/log/syscommand_check.log + +清理 +# sysak syscommand_check clear + diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check.sh b/source/tools/detect/generic/syscommand_check/syscommand_check.sh new file mode 100644 index 00000000..6a2899df --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +SCRIPTS_PATH="/usr/local/sysak/.sysak_components/tools/syscommand_check_scripts" + +function run_tests() { + test_scripts=($(ls test*.sh 2>/dev/null)) + + if [ ${#test_scripts[@]} -eq 0 ]; then + log_error "No test scripts found in the current directory." + echo "No test scripts found in the current directory." + exit 1 + fi + + for script in "${test_scripts[@]}"; do + result=$(bash "$script") + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo "The result of running the $script script is $exit_code" + else + echo "The result of running the $script script is $exit_code" + fi + done +} + +function print_help() { + echo "Usage: sysak syscommand_check [options]" + echo "" + echo "Options:" + echo " clear Run the clear.sh script in the current directory" + echo " --help, -h Show this help message and exit" + echo "" + echo "Examples:" + echo " sysak syscommand_check Perform the default operation (copy files)" + echo " sysak syscommand_check clear Run the clear.sh script" +} + +function check_parameter (){ + if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + print_help + exit 0 + fi + + if [ "$1" = "clear" ]; then + if [ -f "./clear.sh" ]; then + sh clear.sh + else + echo "Error: clear.sh not found in the current directory." + exit 1 + fi + fi +} + +# 主函数入口 +main() { + check_parameter "$1" + cd $SCRIPTS_PATH || { echo "Error: Failed to change directory to $SCRIPTS_PATH"; exit 1; } + if [ ! -f "$SCRIPTS_PATH/common_lib.sh" ]; then + echo "Error: $SCRIPTS_PATH/common_lib.sh not found." + exit 1 + fi + + source "$SCRIPTS_PATH/common_lib.sh" + run_tests +} + +# 脚本入口 +main "$@" diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/clear.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/clear.sh new file mode 100644 index 00000000..ff558276 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/clear.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +LOG_FILE="/var/log/syscommand_check.log" + +if [ -f "$LOG_FILE" ]; then + if rm -f "$LOG_FILE"; then + echo "File $LOG_FILE has been successfully deleted." + else + echo "Failed to delete file $LOG_FILE." + fi +else + echo "File $LOG_FILE does not exist." +fi 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 new file mode 100644 index 00000000..a8122c8a --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/common_lib.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# 日志级别定义 +LOG_INFO="[INFO]" +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}" +} + +# 记录错误日志 +function log_error() { + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") + echo "${timestamp} ${LOG_ERROR} $*" >> "${LOG_FILE}" +} diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/syscommand_check_clear.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/syscommand_check_clear.sh new file mode 100644 index 00000000..ff558276 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/syscommand_check_clear.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +LOG_FILE="/var/log/syscommand_check.log" + +if [ -f "$LOG_FILE" ]; then + if rm -f "$LOG_FILE"; then + echo "File $LOG_FILE has been successfully deleted." + else + echo "Failed to delete file $LOG_FILE." + fi +else + echo "File $LOG_FILE does not exist." +fi diff --git a/source/tools/bclinux/checkcommand/test01_lscpu.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh similarity index 57% rename from source/tools/bclinux/checkcommand/test01_lscpu.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh index 8066c398..7d6be57c 100644 --- a/source/tools/bclinux/checkcommand/test01_lscpu.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test01_lscpu.sh @@ -1,6 +1,6 @@ -#!/usr/bin/bash +#!/bin/bash -# 创建日期: 2024-10-15 +# lscpu - lscpu 是一个非常有用的命令,用于显示有关系统 CPU 架构的详细信息 # 获取脚本所在目录 OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) @@ -18,11 +18,19 @@ function run_test() { lscpu_passed=false # 检查 lscpu 命令 - lscpu &>/dev/null - CHECK_RESULT $? 0 0 "lscpu 命令执行失败" - if [ $? -eq 0 ]; then + 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 "完成测试!" } # 环境清理 diff --git a/source/tools/bclinux/checkcommand/test02_uname.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh similarity index 55% rename from source/tools/bclinux/checkcommand/test02_uname.sh rename to source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh index 70255912..a35a80e2 100644 --- a/source/tools/bclinux/checkcommand/test02_uname.sh +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test02_uname.sh @@ -1,6 +1,6 @@ -#!/usr/bin/bash +#!/bin/bash -# 创建日期: 2024-10-15 +# uname - 用于显示系统的内核和其他相关信息。用于了解当前系统的基本信息,包括操作系统、内核版本、主机名等 # 获取脚本所在目录 OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) @@ -18,16 +18,19 @@ function run_test() { uname_passed=false # 检查 uname 命令 - if command -v uname > /dev/null 2>&1; then - uname -a >/dev/null - local result=$? - CHECK_RESULT $result 0 0 "uname -a 命令执行失败" - if [ $? -eq 0 ]; then - uname_passed=true - fi + uname_output=$(uname -a) + uname_result=$? + + if [ $uname_result -eq 0 ]; then + uname_passed=true + #log_info "uname 命令: 通过" + log_info "uname 命令输出: $uname_output" else - CHECK_RESULT 1 0 0 "uname 命令未找到" + log_error "uname 命令: 失败" + log_error "uname 命令输出: $uname_output" fi + + #log_info "完成测试!" } # 环境清理 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 new file mode 100644 index 00000000..b38dc4b9 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test03_version.sh @@ -0,0 +1,53 @@ +#!/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