本文目录导读:

我来提供一个监控RAID状态的脚本方案,涵盖常见的RAID卡类型。
通用RAID监控脚本
#!/bin/bash
# RAID状态监控脚本
# 支持:LSI/MegaRAID, HP Smart Array, 软件RAID(mdadm)
LOG_FILE="/var/log/raid_monitor.log"
EMAIL="admin@example.com"
HOSTNAME=$(hostname)
# 日志函数
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
# 发送告警邮件
send_alert() {
subject="[RAID ALERT] $HOSTNAME - $1"
echo "$2" | mail -s "$subject" "$EMAIL"
}
# 检查MR RAID卡 (MegaRAID/LSI)
check_megaraid() {
if command -v storcli &>/dev/null; then
log_message "检查 MegaRAID RAID 状态..."
# 获取所有RAID卡信息
controllers=$(storcli show all | grep "Number of Controllers" | awk '{print $NF}')
for ((c=0; c<controllers; c++)); do
# 检查RAID卷状态
vd_status=$(storcli /c$c/vall show | grep "State")
if echo "$vd_status" | grep -qi "degraded\|failed\|critical"; then
log_message "警告: RAID控制器$c 的虚拟磁盘状态异常!"
send_alert "RAID状态异常" "控制器$c 虚拟磁盘异常:\n$vd_status"
fi
# 检查物理磁盘状态
pd_status=$(storcli /c$c/eall/sall show | grep "State")
if echo "$pd_status" | grep -qi "failed\|missing"; then
log_message "警告: RAID控制器$c 存在故障磁盘!"
send_alert "磁盘故障" "控制器$c 磁盘故障:\n$pd_status"
fi
# 检查BBU状态
bbu_status=$(storcli /c$c/bbu show | grep "Battery")
if echo "$bbu_status" | grep -qi "failed\|disconnected"; then
log_message "警告: 控制器$c BBU状态异常!"
fi
done
elif command -v megaraid-status &>/dev/null; then
log_message "使用megaraid-status检查..."
megaraid-status | grep -qi "degraded\|failed\|offline"
if [ $? -eq 0 ]; then
log_message "RAID状态异常!"
fi
fi
}
# 检查HP Smart Array
check_hp_raid() {
if command -v hpssacli &>/dev/null; then
log_message "检查 HP Smart Array RAID 状态..."
# 列出所有控制器
controllers=$(hpssacli ctrl all show status | grep "Smart Array")
# 检查每个控制器状态
hpssacli ctrl all show status | while read line; do
if echo "$line" | grep -qi "failed\|degraded\|critical"; then
log_message "警告: $line"
send_alert "HP RAID异常" "$line"
fi
done
# 检查逻辑驱动器状态
hpssacli ctrl slot=0 ld all show status | while read line; do
if echo "$line" | grep -qi "failed\|degraded"; then
log_message "逻辑驱动器异常: $line"
fi
done
elif command -v hpacucli &>/dev/null; then
log_message "使用hpacucli检查..."
hpacucli ctrl all show status
fi
}
# 检查软件RAID (mdadm)
check_software_raid() {
if command -v mdadm &>/dev/null; then
log_message "检查软件RAID状态..."
# 检查所有md设备
for md in /dev/md*; do
if [ -b "$md" ]; then
md_status=$(mdadm --detail "$md" | grep "State")
raid_device=$(basename "$md")
if echo "$md_status" | grep -qi "degraded\|failed\|recovering"; then
log_message "警告: $raid_device 状态异常 - $md_status"
send_alert "软件RAID异常" "$raid_device: $md_status"
fi
# 检查磁盘状态
failed_disks=$(mdadm --detail "$md" | grep "faulty\|failed\|removed")
if [ -n "$failed_disks" ]; then
log_message "警告: $raid_device 存在故障磁盘:\n$failed_disks"
fi
fi
done
# 检查/proc/mdstat
if grep -qi "failed\|degraded" /proc/mdstat 2>/dev/null; then
log_message "检测到RAID异常!"
cat /proc/mdstat
fi
fi
}
# 检查硬件RAID卡工具是否存在
check_tools() {
tools_found=0
# 检测可用工具
for tool in storcli megaraid-status hpssacli hpacucli mdadm; do
command -v $tool &>/dev/null && {
log_message "找到工具: $tool"
tools_found=$((tools_found + 1))
}
done
if [ $tools_found -eq 0 ]; then
log_message "未找到任何RAID监控工具!"
return 1
fi
return 0
}
# 主函数
main() {
log_message "=== 开始RAID状态检查 ==="
# 检查工具是否存在
check_tools || {
log_message "已安装的监控工具:"
dpkg -l | grep -i "storcli\|megaraid\|hpssacli\|hpacucli\|mdadm" 2>/dev/null
exit 1
}
# 分别检查不同类型的RAID
check_megaraid
check_hp_raid
check_software_raid
log_message "=== RAID状态检查完成 ==="
}
# 执行主函数
main
简化版监控脚本
#!/bin/bash
# 简化版RAID监控脚本
LOG="/var/log/raid_check.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$DATE] RAID状态检查开始" >> $LOG
# 检查软件RAID
if [ -f /proc/mdstat ]; then
if grep -qi "degraded\|failed\|recovering" /proc/mdstat; then
echo "警告: 软件RAID异常" >> $LOG
cat /proc/mdstat >> $LOG
fi
fi
# 尝试检查硬件RAID
for cmd in "storcli /c0/vall show" "megaraid-status" "hpssacli ctrl all show status"; do
if command -v ${cmd%% *} &>/dev/null; then
eval $cmd 2>/dev/null | grep -qi "failed\|degraded\|critical"
if [ $? -eq 0 ]; then
echo "警告: 硬件RAID异常" >> $LOG
eval $cmd >> $LOG 2>/dev/null
fi
fi
done
echo "[$DATE] 检查完成" >> $LOG
Zabbix集成版本
#!/bin/bash
# Zabbix集成的RAID监控脚本
case "$1" in
discovery)
# 发现所有RAID设备
echo '{"data":['
first=1
# 发现软件RAID
for md in /dev/md*; do
if [ -b "$md" ]; then
[ $first -ne 1 ] && echo ","
echo "{\"{#RAID_NAME}\":\"$(basename $md)\",\"{#RAID_TYPE}\":\"software\"}"
first=0
fi
done
# 发现硬件RAID
if command -v storcli &>/dev/null; then
for ctrl in $(storcli show all | grep -E "^\s+[0-9]+:" | awk '{print $1}'); do
[ $first -ne 1 ] && echo ","
echo "{\"{#RAID_NAME}\":\"controller$ctrl\",\"{#RAID_TYPE}\":\"hardware\"}"
first=0
done
fi
echo ']}'
;;
status)
# 检查指定RAID状态
raid_name=$2
raid_type=$3
if [ "$raid_type" = "software" ]; then
mdadm --detail /dev/$raid_name 2>/dev/null | grep "State" | awk '{print $2}'
else
# 在此添加硬件RAID状态获取逻辑
echo "unknown"
fi
;;
count)
# 统计异常RAID数量
count=0
# 检查软件RAID
if [ -f /proc/mdstat ]; then
count=$((count + $(grep -c "degraded\|failed" /proc/mdstat 2>/dev/null)))
fi
echo $count
;;
*)
echo "Usage: $0 {discovery|status name type|count}"
exit 1
;;
esac
使用说明
安装依赖
Ubuntu/Debian:
# 安装软件RAID工具 apt-get install mdadm # 安装MegaRAID工具 # 从Broadcom官网下载storcli wget https://docs.broadcom.com/docs/.../storcli.deb dpkg -i storcli.deb # 安装HP工具 apt-get install hpssacli # 或从HP官网下载
CentOS/RHEL:
# 安装软件RAID工具 yum install mdadm # 安装MegaRAID工具 rpm -ivh storcli.rpm # 安装HP工具 yum install hpssacli
设置定时任务
# 添加到crontab,每30分钟检查一次 crontab -e # 添加以下行: */30 * * * * /path/to/raid_monitor.sh
配置邮件告警
# 安装邮件工具 apt-get install mailutils # Debian/Ubuntu yum install mailx # CentOS/RHEL # 配置SSMTP(如果使用外部邮件服务器) apt-get install ssmtp # 编辑 /etc/ssmtp/ssmtp.conf
监控指标说明
| 状态 | 说明 | 严重级别 |
|---|---|---|
| Optimal/正常 | RAID运行正常 | 正常 |
| Degraded/降级 | 有磁盘故障但仍可运行 | 警告 |
| Failed/失败 | RAID失效 | 严重 |
| Recovering/恢复 | 正在进行重建 | 信息 |
最佳实践
- 定期检查:建议每15-30分钟检查一次
- 日志轮转:配置logrotate管理日志文件
- 多重告警:邮件 + SNMP + 系统日志
- 自动化修复:对有热备盘的RAID,自动触发重建
- 性能监控:同时监控IO性能,预测磁盘故障
这个脚本提供了完整的RAID监控方案,可以根据实际环境选择合适的版本使用。