常用脚本cycle介绍

常用脚本cycle介绍

1.理论上兼容cent7,8/rhel7,8/oracle7,8/ubuntu18,19,20/系列,未全部测试
2.使用可视化向导,无需修改脚本,选择要测试的选项以及测试次数即可
3.使用systemctl 控制服务,添加cycle.service用作触发日志收集以及reboot/powercycle,服务在netowrk服务器后启动,启动优先级比rc.local要低,优点是可以解决日收集不全的问题(未实际验证),理论上不会出现日志缺失问题
3.首次运行必须使用root用户执行,服务以系统级别运行(非用户级别),即使系统未设置root登录也可以指向,使用su/sudo指令切换root用户执行即可

缺点:
未加入提示用以终止测试,后期会优化
未加入日志对比功能,后期会添加

已知问题:
lspci 第一次会收集两次,问题未解决(不一定会出现,虚拟机会出现,物理机试过不会出现)
使用了radiolist,防止返回值出现双引号
解决了service无法启动、请求too quickly的问题
新版本未测试

使用方法:
1.将cycle.sh function两个文件放在同一个目录,修改文件权限
2.使用命令行切换root用户后执行cycle使用命令行切换root用户后执行cycle.sh然后根据输入提示即可

正文

以下为配套使用,2个文件,一个cycle.sh,一个function

cycle.sh

!/usr/bin/env bash

source ~/.bashrc
current_path=pwd

chmod 777 -R $

cd ${current_path}
. ${current_path}/function
LOG_FILE=${current_path}/exec.log
TOTAL_COUNT_FILE=${current_path}/TOTAL_COUNT_FILE.log
CURRENT_COUNT_FILE=${current_path}/CURRENT_COUNT_FILE.log

r_file=./$0

alias echo_date='echo $(date +"%Y-%m-%d %X"):'

first_time()
{
OPTION=$(whiptail --title "Cycle Test Tool" --radiolist
"Please Choose your option" 20 70 3
"reboot" "reboot test" OFF
"powercycle" "power cyle test" OFF
"clean" "clean all log"  ON 3>&1 1>&2 2>&3)
exitstatus=$?
 
if [[ ${exitstatus} = 0 ]] && [ ${OPTION} != "clean" ]; then
        TOTAL_COUNT=$(whiptail --title "Cycle Test Tool" --inputbox "Please input test count?" 20 70 3>&1 1>&2 2>&3)
        exitstatus=$?
        if [[ ${exitstatus} = 0 ]]; then
            if (whiptail --title "Test Yes/No Box" --yesno "Do you want to start testing ?" 20 70) then
                echo "You choose Yes. Creating log folder." #后期导出exec.log
                echo ${TOTAL_COUNT} >${TOTAL_COUNT_FILE} && echo "set test count:${TOTAL_COUNT}"
                echo 0 >${CURRENT_COUNT_FILE}
                echo ${OPTION} >${current_path}/option.log
                create_folder
                create_service
                else
                echo "You chose No. Exit cycle test Tool."
            fi
            else
            echo "You chose Cancel."
        fi
elif [[ "${OPTION}" == "clean" ]]; then clear_log
else
    echo "You chose Cancel."
fi
}

main process

if [ -f ${current_path}/TOTAL_COUNT_FILE.log ] &&  [ -f ${current_path}/option.log ] && [ cat ${TOTAL_COUNT_FILE} -ne cat ${CURRENT_COUNT_FILE} ]; then
    if [[ "cat ${current_path}/option.log" == "reboot" ]]; then
      reboot_test
      else
      powercycle_test
    fi
  else
  first_time
fi

function

create_folder()
{

mkdir ${current_path}/01cpu_info/  >/dev/null 2>&1
  mkdir ${current_path}/02ip_info/  >/dev/null 2>&1
  mkdir ${current_path}/03lspci_info/  >/dev/null 2>&1
  mkdir ${current_path}/04memory_info/  >/dev/null 2>&1
  mkdir ${current_path}/05dmesg_info/  >/dev/null 2>&1
  mkdir ${current_path}/06lsblk_info/  >/dev/null 2>&1

if [[ ls ${current_path} |egrep -i "^0" |wc -l == 6 ]]; then
    echo "folders create completed" #后期导出exec.log
    else
    echo "folders create failed"
    exit 0
  fi
}
create_service()
{
  touch /usr/lib/systemd/system/cycle.service && chmod 755 /usr/lib/systemd/system/cycle.service
  cat > /usr/lib/systemd/system/cycle.service <<EOF
[Unit]
Description=cycle service
After=network.target

[Service]
User=root
WorkingDirectory=${current_path}
ExecStart=/usr/bin/env bash ${current_path}/cycle.sh
Restart=on-failure
StartLimitInterval=600

[Install]
WantedBy=multi-user.target
EOF
    
sleep 1
start_service

}

start_service()
{
 if [ -f /usr/lib/systemd/system/cycle.service ]; then
    systemctl daemon-reload && sleep 1
    systemctl enable cycle.service
    systemctl start cycle.service
    /usr/bin/env bash ${current_path}/cycle.sh
 fi
}

reboot_test()
{
    CURRENT_COUNT=cat ${CURRENT_COUNT_FILE} 2> /dev/null
    TOTAL_COUNT=cat ${TOTAL_COUNT_FILE} 2> /dev/null
    if [ ${CURRENT_COUNT} -le ${TOTAL_COUNT} ]
    then
    cpu_info
    ip_info
    lspci_info
    mem_info
    dmesg_info
    lsblk_info
    CURRENT_COUNT=expr ${CURRENT_COUNT} + 1
    echo ${CURRENT_COUNT} > ${CURRENT_COUNT_FILE}
    reboot    
    else
    echo 'done'
    fi
}

powercycle_test()
{
    CURRENT_COUNT=cat ${CURRENT_COUNT_FILE} 2> /dev/null
    TOTAL_COUNT=cat ${TOTAL_COUNT_FILE} 2> /dev/null
    if [ ${CURRENT_COUNT} -le ${TOTAL_COUNT} ]
    then
    cpu_info
    ip_info
    lspci_info
    mem_info
    dmesg_info
    lsblk_info
    CURRENT_COUNT=expr ${CURRENT_COUNT} + 1
    echo ${CURRENT_COUNT} > ${CURRENT_COUNT_FILE}
    ipmitool power cycle    
    else
    echo 'done'
    fi
}
cpu_info()
{

if [ ! -s ${current_path}/01cpu_info/cpu_info_${CURRENT_COUNT}.log ]; then
        {
          lscpu | tee ${current_path}/01cpu_info/cpu_info_${CURRENT_COUNT}.log >/dev/null 2>&1
          
        sleep 2
        } | whiptail --gauge "Collected cpu_info_${CURRENT_COUNT}.log" 6 40 15
        
        fi

}

ip_info()
{

if [ ! -s ${current_path}/02ip_info/ip_info_${CURRENT_COUNT}.log ]; then
        {
          ip a | tee ${current_path}/02ip_info/ip_info_${CURRENT_COUNT}.log >/dev/null 2>&1
          
        sleep 2
        } | whiptail --gauge "Collected ip_info_${CURRENT_COUNT}.log" 6 40 30
        fi

}
lspci_info()
{

if [ ! -s ${current_path}/03lspci_info/lspci_${CURRENT_COUNT}.log ]; then
        {
          for bdf in lspci | grep -i eth | awk -F " " '{print $1}' 
          do    
            lspci -vvv -s $bdf |grep -i "eth|lnk" | tee -a ${current_path}/03lspci_info/lspci_${CURRENT_COUNT}.log  >/dev/null 2>&1
          done            
        sleep 2
        } | whiptail --gauge "Collected lspci_info_${CURRENT_COUNT}.log" 6 40 45
        fi

}
mem_info()
{

if [ ! -s ${current_path}/04memory_info/memory_info_${CURRENT_COUNT}.log ]; then
        {
          free | tee ${current_path}/04memory_info/memory_info_${CURRENT_COUNT}.log >/dev/null 2>&1

sleep 2
        } | whiptail --gauge "Collected memory_info_${CURRENT_COUNT}.log" 6 40 60
        fi

}
dmesg_info()
{

if [ ! -s ${current_path}/05dmesg_info/dmesg_info_${CURRENT_COUNT}.log ]; then
        {
          dmesg |egrep -i 'error|fail|ixgbe|warn' | tee ${current_path}/05dmesg_info/dmesg_info_${CURRENT_COUNT}.log >/dev/null 2>&1
          
        sleep 2
        } | whiptail --gauge "Collected dmesg_info_${CURRENT_COUNT}.log" 6 40 75
        fi

}

lsblk_info()
{

if [ ! -s ${current_path}/06lsblk_info/lsblk_info_${CURRENT_COUNT}.log ]; then
        {
          lsblk | tee ${current_path}/06lsblk_info/lsblk_info_${CURRENT_COUNT}.log >/dev/null 2>&1
         
        sleep 2
        } | whiptail --gauge "Collected lsblk_info_${CURRENT_COUNT}.log" 6 40 90
        fi

}
clear_log()
{
  systemctl stop cycle.service
  rm -f /usr/lib/systemd/system/cycle.service
  systemctl daemon-reload
  rm -rf ${current_path}/0* 
  rm -f ${current_path}/option.log  
  rm -f ${current_path}/TOTAL_COUNT_FILE.log 
  rm -f ${current_path}/CURRENT_COUNT_FILE.log
}

此为Wind

posted @ 2026-10-19 21:06  zuige666  阅读(4)  评论(0)    收藏  举报