#!/bin/bash

source /usr/libexec/pstorage/service-lib.sh

check_cluster() {
  local clustername=$1
  for cl in `ls /etc/pstorage/clusters`; do
    if [ $cl == "$clustername" ]
    then
	return 0
    fi
  done;
  return 1
}

get_lvm_devices() {
  local vg_name=$1
  local r=$2
  local devices=`pvs 2>/dev/null | grep $vg_name | awk '{print $1}' | sed -e 's/\/dev\///g' | sed ':a;N;$!ba;s/\n/,/g'` 
  if [ "$devices" != "" ]; then echo "($devices)"; fi
}

find_raid_device() {
  local devname=`echo $1 | sed -e 's/.dev.\(.*[0-9]\+\).*$/\1/g'`
  local result=`cat /proc/mdstat | grep "$devname : " | sed -e 's/ :.*$//g'` 
  echo "$result"
}

get_raid_info() {
  local devname=`echo $1 | sed -e 's/.dev.\(.*[0-9]\+\).*$/\1/g'`
  local devices=`ls /sys/block/$devname/slaves | sed ':a;N;$!ba;s/\n/,/g'`
  local raidtype=`cat /proc/mdstat | grep "$devname : " | sed -e 's/.*\(raid[0-9]\+\).*/\1/g' | sed -e 's/raid/Raid/g'` 
  if [ "$devices" != "" ]; then devices="($devices)"; fi
  echo "$raidtype $devices"
}

print_monitor_status() {
	CLUSTER_NAME="$1"
	DAEMON_DIR="$2"

	PID_DIR=$(get_pid_dir "$DAEMON_DIR")
	PID_FILE="$PID_DIR/monitor.pid"
	pid_file_compat_check
	PID=$(cat "$PID_FILE" 2>/dev/null)

	check_monitor_pid "$PID_FILE" "$DAEMON_DIR"
	case $? in
		1) # program is dead, pid file exists
			echo "dead"
			;;
		3) # program is not running
			echo "stopped"
			;;
		0) # program is running
			pid=`ps -f -C "${type}d" 2>/dev/null | grep "$PID" | awk '{print $2}'` 
			[ -n "$pid" ] &&  pid=" [$pid]"
			s=`/usr/bin/pstorage -c "$CLUSTER_NAME" check-status "$DAEMON_DIR"`
			if [ $? -ne 0 ] 
			then
				echo "failed"
			else
				echo "$s$pid"
			fi
			;;
		4) # failed to read pid file
			echo "error"
			;;
	esac
}

show_path() {
	local cluster_name="$1"
	local path="$2"

	if [ "$LIST_FILE" == "mds.list" ];
	then 
		id=`cat $path/id` 
		t=MDS 
	fi

	if [ "$LIST_FILE" == "cs.list" ];
	then 
		id=`cat $path/control/id`
		t=CS
	fi
	dev=`df $path | grep dev | awk '{ print $1 }'`
	full_dev_path=`readlink -f $dev`
	dev_name=`echo $full_dev_path | sed -e 's/\/dev\///g'`
	
	if [ -d /sys/block/$dev_name/slaves/ ] && [ -n "$(ls -A /sys/block/$dev_name/slaves/)" ]
	then
		if [ "`find_raid_device $dev`" != "" ]
		then
			info=`get_raid_info $dev`
		else
			dev_name=`lvdisplay $dev 2>/dev/null`
			[ -n "$dev_name" ] && dev_name=`echo "$dev_name" | grep 'VG Name' | awk '{print $3}'`
			if [ -n "$dev_name" ]; then 
				full_dev_path=$dev_name
				lvm_devices=`get_lvm_devices $dev_name`
				info="LVM $lvm_devices"
			else
				info="LVM"
			fi
		fi
	else
		physical_root_volume=`echo $dev | sed -e 's/[0-9]//g' | sed -e 's/\/dev\///g'`
		disk_name=`cat /sys/block/$physical_root_volume/device/model`
		info=$disk_name
	fi

	service_status=`print_monitor_status $cluster $path`
	echo "$t!$id!$service_status!$full_dev_path!$info!$path" |awk -F '!' '{printf "%s\t%s\t%-20s\t%-30s %-35s %s\n",$1,$2,$3,$4,$5,$6}'
}


if [ $# == 2 ]; then
	if [ "$2" == "-C" ]; then
		types="cs"
	elif [ "$2" == "-M" ]; then
		types="mds"
	fi
elif [ $# == 1 ]; then
	types="cs mds"
else
	exit 0
fi

cluster=$1
check_cluster $cluster
if [ $? -ne 0 ]; then
	echo "No such cluster: $cluster"
	exit 0
fi

echo "TYPE!ID!STATUS!DEVICE/VOLUME GROUP!DEVICE INFO!PATH" |awk -F '!' '{printf "%s\t%s\t%-20s\t%-30s %-35s %s\n",$1,$2,$3,$4,$5,$6}'
for type in $types; do
	LIST_FILE=$type.list
	for_cluster show_path $cluster 
done;
