Ceph PG自动巡检修复脚本

#!/bin/bash
 
health_state=$(ceph -s | grep health | awk '{print $2}')
inconsistent_state=$(ceph -s | grep inconsistent)
damaged_pg=$(ceph health detail | grep -A1 PG_DAMAGED | tail -1 | awk '{print $2}')
damaged_info=$(ceph health detail | grep -A1 PG_DAMAGED | tail -1)
 
cmd_dir=$(
    cd $(dirname $0)
    pwd
)

cd $cmd_dir
date >>ceph-pg-repair.log
case $health_state in
"HEALTH_OK")
    echo "Cluster HEALTH_OK!" >>ceph-pg-repair.log
    ;;
 
"HEALTH_WARN")
    echo "Cluster HEALTH_WARN!" >>ceph-pg-repair.log
    ;;
 
"HEALTH_ERR")
    if [ -z "$inconsistent_state" ]; then
        echo "Cluster is not a consistency check problem!" >>ceph-pg-repair.log
    else
        if [ -z "$damaged_pg" ]; then
            echo "No PG with inconsistent found, please check manually!" >>ceph-pg-repair.log
        else
	    echo $damaged_info >>ceph-pg-repair.log
            ceph pg repair $damaged_pg
            if [ $? != 0 ]; then
                echo "Repair error, please check manually!" >>ceph-pg-repair.log
            fi
	    echo "The repaired pg is $damaged_pg" >>ceph-pg-repair.log
        fi
    fi
    ;;
 
*)
    echo "Unknown state, please check manually!" >>ceph-pg-repair.log
    ;;
esac
echo "------------------------------------------------------------" >>ceph-pg-repair.log
posted @ 2022-02-10 14:24  wanghongwei-dev  阅读(94)  评论(0)    收藏  举报