首页 > 其他分享 >磁盘坏道检测脚本

磁盘坏道检测脚本

时间:2023-09-26 16:27:12浏览次数:24  
标签:脚本 20s grep 坏道 rpm timeout 磁盘 disk RPM

磁盘坏道检测脚本

说明: hp服务器有一定概率执行命令会卡主,

#!/bin/bash
# utf-8
# 说明: 其他型号服务器获取raid信息通过MegaCli
# 说明: HP服务器获取raid信息通过hpacucli

# 安装包信息定义
HP_SSACLI_COMMAND_RPM_NAME="hpacucli-9.40-12.0.x86_64.rpm"
HP_SSACLI_COMMAND_RPM_NAME2="hpssacli-2.30-6.0.x86_64.rpm"
LIB_UTILS_RPM_NAME="Lib_Utils-1.00-09.noarch.rpm"
OTHER_MEGACLI_COMMAND_RPM_NAME="MegaCli-8.02.21-1.noarch.rpm"

# 其他基础常量定义
DOWNLOAD_BASE_URL="http://xxxxxx/CentOS/app/xxxx"
RPM_BACKAGE_STORAGE_LOCATION="/tmp"
CORRECT_DISK_DESCRIPTION="all disk is ok"
BAD_DISK_DESCRIPTION="disk has error"

# 安装相关依赖
function install_dependency(){
  yum install dmidecode* -y &>/dev/null
}

function install_rpm() {
  case $1 in
  "HP")
    if [ ! -f ${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME} ]; then
        timeout 60s wget "${DOWNLOAD_BASE_URL}/${HP_SSACLI_COMMAND_RPM_NAME}" -O ${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME} &>/dev/null
    fi

    if [ ! -f ${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME2} ]; then
        timeout 60s wget "${DOWNLOAD_BASE_URL}/${HP_SSACLI_COMMAND_RPM_NAME2}" -O ${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME2} &>/dev/null
    fi

    cd ${RPM_BACKAGE_STORAGE_LOCATION}
    rpm -Uvh "${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME}" &>/dev/null
    rpm -Uvh "${RPM_BACKAGE_STORAGE_LOCATION}/${HP_SSACLI_COMMAND_RPM_NAME2}" &>/dev/null
    ;;
  "Other")
    if [ ! -f ${RPM_BACKAGE_STORAGE_LOCATION}/${LIB_UTILS_RPM_NAME} ]; then
      timeout 60s wget "${DOWNLOAD_BASE_URL}/${LIB_UTILS_RPM_NAME}" -O ${RPM_BACKAGE_STORAGE_LOCATION}/${LIB_UTILS_RPM_NAME} &>/dev/null
    fi
    if [ ! -f ${RPM_BACKAGE_STORAGE_LOCATION}/${OTHER_MEGACLI_COMMAND_RPM_NAME} ]; then
      timeout 60s wget "${DOWNLOAD_BASE_URL}/${OTHER_MEGACLI_COMMAND_RPM_NAME}" -O ${RPM_BACKAGE_STORAGE_LOCATION}/${OTHER_MEGACLI_COMMAND_RPM_NAME} &>/dev/null
    fi

    cd ${RPM_BACKAGE_STORAGE_LOCATION}
    rpm -Uvh "${RPM_BACKAGE_STORAGE_LOCATION}/${LIB_UTILS_RPM_NAME}" &>/dev/null
    rpm -Uvh "${RPM_BACKAGE_STORAGE_LOCATION}/${OTHER_MEGACLI_COMMAND_RPM_NAME}" &>/dev/null
    ln -s /opt/MegaRAID/MegaCli/MegaCli64 /bin/MegaCli64 2>/dev/null
    ln -s /opt/MegaRAID/MegaCli/MegaCli64 /sbin/MegaCli64 2>/dev/null
    ;;
  *)
    :
    ;;
  esac
}

# 其他服务器类型获取raid信息
function other_server_hardware_health_check() {
  #echo 'start check server '
  server_ip=$(timeout 20s ifconfig | grep Bcast | grep 'inet addr:' | grep -v '127.0.0.1' | head -n 1 | awk '{print $2}' | awk -F: '{print $2}')
  disk_usage_percent=$(timeout 20s df -h | grep /export | grep -v 'mnt' | awk '{print $5}')
  raid_level=$(timeout 20s MegaCli64 -LDInfo -Lall -aALL | grep 'RAID Level')
  Current_Access_Policy=$(timeout 20s MegaCli64 -LDInfo -Lall -aALL | grep 'Current Cache Policy')
  disk_bad_sectors=$(timeout 20s MegaCli64 -pdlist -aALL | grep -E "Media Error|Other Error" | awk -F ': ' '{print($2)}' | sort -r | head -n 1)

  Firmware_State=$(timeout 20s MegaCli64 -pdlist -aALL | grep -E 'Slot Number|Firmware state' | grep -B 1 'Firmware state: [^O]')

  if [ -z "$Firmware_State" ]; then
    Firmware_State="other_server_Firmware_State_NULL"
  fi

  Predictive_Failure_Count=$(timeout 20s MegaCli64 -PDList -aALL | grep -E 'Slot Number|Predictive Failure Count:' | grep -B 1 'Predictive Failure Count: [^0]')

  if [ -z "$Predictive_Failure_Count" ]; then
    Predictive_Failure_Count="other_server_Predictive_Failure_Count_NULL"
  fi

  local disk_bad_info=$(timeout 20s MegaCli64 -pdlist -aALL | grep -E "Slot Number|Media Error|Other Error" | grep -E 'Count: [^0]|Slot Number' | grep 'Error' -B1)
  BBU_status=$(timeout 20s MegaCli64 -LDInfo -Lall -aALL | grep 'Current Cache Policy')
  Media_Type=$(timeout 30s MegaCli64 -PDList -aAll | grep 'Media Type' | sort -u | sed 's/      //g')
  Media_Size=$(timeout 30s MegaCli64 -PDList -aAll | grep 'Raw Size' | sort -u | sed 's/      //g')
  Media_Type=$Media_Type' '$Media_Size
  raid_Memory=$(timeout 20s MegaCli64 -Cfgdsply -aALL | grep -E 'Memory' | head -1 | sed 's/      //g' | sed 's/Memory/CacheSize/g')
  disk_bad_sectors=$([ ${disk_bad_sectors} -gt 0 ] && echo -n "${BAD_DISK_DESCRIPTION}" || echo -n "${CORRECT_DISK_DESCRIPTION}")
  # 结果print
  if [[ "$disk_bad_sectors" == "disk has error" ]]; then
#    echo "check_disk_disk_bad_sectors: "$disk_bad_sectors | sed 'N;s/\n//g'
    echo "$disk_bad_info" | sed 'N;s/\n//g'
  fi
}

# 需要前置进行rpm库的初始化保证后续执行rpm包安装的时候不出现问题
function rpm_localdb_rebuild(){
  /bin/rm -f /var/lib/rpm/.dbenv.lock
  /bin/rm -f /var/lib/rpm/.rpm.lock
  /bin/rm -rf /var/lib/rpm/__db.00*
  /bin/rm -f /var/run/yum.pid
  rpm --rebuilddb
}

# HP服务器健康检查
function hp_server_hardware_health_check() {
  #has_command=$(rpm -qa 2>/dev/null | grep -i 'hpssacli' | wc -l)
  #echo 'start check DBserver '
  server_ip=$(timeout 20s ifconfig | grep Bcast | grep 'inet addr:' | grep -v '127.0.0.1' | head -n 1 | awk '{print $2}' | awk -F: '{print $2}')
  disk_usage_percent=$(timeout 20s df -h | grep /export | grep -v 'mnt' | awk '{print $5}')
  raid_level=$(timeout 20s hpssacli ctrl slot=0 ld all show | grep RAID | awk -F ',' '{print $2}')
  Current_Access_Policy=$(timeout 20s hpssacli ctrl all show config detail | grep -i cache | grep 'Wait for Cache Room' | awk -F ':' '{print $2}')
  disk_bad_sectors=$(timeout 20s hpacucli ctrl slot=${raid_slot_id} physicaldrive all show | grep 'physicaldrive' | grep -v 'OK' | wc -l | sed 'N;s/\n//g')
  Firmware_State=$(timeout 20s hpssacli ctrl all show config | grep physicaldrive | awk -F ',' '{print $2 $3 $4}' | sort | uniq -c)
  Predictive_Failure_Count="HP_server_Predictive_Failure_Count_NULL"
  local disk_bad_info=$(timeout 20s hpacucli ctrl slot=${raid_slot_id} physicaldrive all show | grep 'physicaldrive' | grep -v 'OK')
  BBU_status=$(timeout 20s hpacucli ctrl all show config detail | grep -i 'No-Battery Write Cache')
  Media_Type=$(timeout 20s hpacucli ctrl slot=${raid_slot_id} physicaldrive all show | grep 'physicaldrive' | awk -F ',' '{print $2 $3 $4}' | sort -u | sed 'N;s/\n//g' | sed 's/      //g')
  raid_Memory=$(timeout 20s hpacucli ctrl all show config detail | grep -i cache | grep 'Total Cache Size' | sed 'N;s/\n//g' | sed 's/   //g')
  # disk sector judge
  disk_bad_sectors=$([ "$disk_bad_sectors" = 0 ] && echo -n "${CORRECT_DISK_DESCRIPTION}" || echo -n "${BAD_DISK_DESCRIPTION}")
  # response print
  if [[ "$disk_bad_sectors" == "disk has error" ]]; then
    #echo "check_disk_disk_bad_sectors: "$disk_bad_sectors | sed 'N;s/\n//g'
    echo "$disk_bad_info" | sed 'N;s/\n//g'
  fi
}

function main() {

  #server_vendor=`dmidecode -s system-manufacturer|sed '/^#/d'|awk '{print $1}'`
  local is_docker=$(ps -ef | grep '/sbin/init' | wc -l)

  if [[ "$is_docker" == "1" ]]; then
    echo "server is docker,return"
    exit 0
  fi
  # 重建rpm本地db
  rpm_localdb_rebuild
  # 安装依赖
  install_dependency
  # 服务器类型信息
  local server_vendor=$(dmidecode -s system-manufacturer | sed '/^#/d' | awk '{print $1}')

  case $server_vendor in
  "HP"|"Hewlett-Packard")
    install_rpm "HP"
    hp_server_hardware_health_check
    ;;
  *)
    install_rpm "Other"
    other_server_hardware_health_check
    ;;
  esac
}

main

标签:脚本,20s,grep,坏道,rpm,timeout,磁盘,disk,RPM
From: https://www.cnblogs.com/PythonOrg/p/17730329.html

相关文章

  • 磁盘扩容与缩减(lvm逻辑卷)
    磁盘的动态扩容和缩减原创 运维家 运维家 2023-09-2508:02 发表于北京收录于合集#磁盘2个#linux59个 主旨在日常运维过程中,经常会出现磁盘爆满,不足以维持未来业务量,或者磁盘太大,造成资源浪费的情况,这种情况下最好的方式就是采用磁盘的动态扩容和缩减。LVM......
  • LDAP性能测试脚本开发
    LDAP是什么?LDAP是轻量目录访问协议,英文全称是LightweightDirectoryAccessProtocol,一般都简称位LDAP。它是基于X.500标准的,但更简单并且可以根据需要定制。与X.500不同,LDAP支持TCP/IP协议,这对访问Internet来说是必需的。LDAP的核心规范在RFC中都有定义,所有与LDAP相关的RFC都可以......
  • 欧拉系统、CentOS系统、Linux 系统。。。初始化磁盘,设置动态扩容
    欧拉系统、CentOS系统、Linux系统。。。初始化磁盘,设置动态扩容初始化磁盘,设置动态扩容登录root用户查看磁盘fdisk-l查看磁盘格式化磁盘,将磁盘设置成动态扩容格式fdisk/dev/vdc创建分区fdisk-l查看到/dev/vdc磁盘依次输入np回车回车回车t......
  • 客户端自助加域脚本
    计算机重命名:Set-ExecutionPolicyRemoteSigned-Force$ID=Read-Host("请输入您的工号:")$New_Name="BJ-"+$ID$Old_Name=hostnameRename-Computer-ComputerName$Old_Name-NewName$New_Name-Force$ws=New-Object-ComObjectWScript.Shell$......
  • Vmware Workstation 16 Pro 创建共享磁盘
    图形界面创建共享磁盘第一台创建共享磁盘首先打开已经安装好系统的虚拟机,点击编辑虚拟机设置,弹出如下窗口:点击添加,选择硬盘,点击下一步:默认选择scsi,点击下一步。选择创建新虚拟磁盘。点击下一步:分配磁盘空间大小,选择立即分配磁盘所有空间,将虚拟磁盘存储为单个文件。命名磁盘名称及......
  • shell脚本中的EOF是什么
    概述在Shell脚本中,EOF(EndofFile)是一个特殊标记,用于指示一段文本的开始和结束位置。但它并不是Shell脚本中的关键字或保留字。您可以自由选择EOF之前的标记,只需确保开始和结束标记匹配即可。基本上<<EOF告诉shell您将输入多行字符串,直到“标记”EOF。您可以根据需要命名此标......
  • Applescript脚本实现全自动无痕检测手机号码是否注册iMessage的原理
    一、检测数据的两种方式:1.人工筛选,将要验证的号码输出到文件中,以逗号分隔。再将文件中的号码粘贴到iMessage客户端的地址栏,iMessage客户端会自动逐个检验该号码是否为iMessage账号,检验速度视网速而定。红色表示不是iMessage账号,蓝色表示iMessage账号。2.编写脚本控制Macos/iphon......
  • 磁盘占用率100% 的优化方案
    1.禁用不必要的系统服务右键点击此电脑,打开管理打开服务寻找SysMain右键属性=》禁用=》停止=》应用SysMain的作用:当开机后,windows会加载大量的应用程序预加载到内存中,会在后台预加载数据(如果是旧版本windows,服务名称为:SuperFetch或DiagTrack)Windows的search......
  • 用python脚本计算某一个文件的行数
    python可以统计文件的行数,你相信吗?不管你信不信反正我信了。下面我们来看一下python怎样统计文件的行数,代码很简单,我也做了注释,很简单的实现。。。123456789101112131415161718192021#_*_coding:utf_8#引入sys和系统os模块import sys import os '''计算某一个文件的行数'''def......
  • bash的start脚本
    #!/bin/bash#这里可替换为你自己的执行程序,其他代码无需更改APP_NAME=gateway-1.0-SNAPSHOT.jarINSTALL_PATH=/home/server/immp/gateway#使用说明,用来提示输入参数usage(){echo"Usage:sh脚本名.sh[start|stop|restart|status]"exit1}#检查程序是否在运......