#!/bin/bash

today=`date +%y%m%d_%H%M`
stat_file=/etc/zabbix/scripts/log/ceph_$today
err_log=/etc/zabbix/scripts/log/ceph.$$

#/etc/zabbix/cc.sh > $err_log #err
ceph health detail > $err_log #OK
err_msg=`head -1 $err_log`


##############
#  Function  #
##############
log_copy()
{
	cat $err_log > $stat_file
}

repair_pg()
{
	pg=`grep "is active+clean+inconsistent" $stat_file | awk '{print $2}'`
	echo "pg repair $pg, $err_msg"
	echo "PG Repair Start" >> $stat_file
	ceph pg repair $pg 2>> $stat_file
}


###############
# Error Check #
###############
if [ `echo $err_msg | egrep -c "HEALTH_OK"` -eq 1 ] ;then
	echo HEALTH_OK
else
	log_copy
# imsi
	if [ `echo $err_msg | grep -c "pgs inconsistent"` -gt 0 ]; then
		repair_pg
	elif [ `echo $err_msg | grep -c "osds are down"` -gt 0 ]; then
		echo $err_msg
	elif [ `echo $err_msg | grep -c "mons down"` -gt 0 ]; then
		echo $err_msg
	#elif [ `echo $err_msg | grep -c "noout flag(s) set"` -gt 0 ]; then
	#	echo HEALTH_OK
	else
		sed ':a;N;$!ba;s/\n/ /g' $stat_file
	fi;

fi;

rm -rf $err_log
