3ware_tool.sh 4.35 KB
Newer Older
Chris Layton's avatar
Chris Layton committed
1 2 3
#!/bin/sh

# Author : Chris Layton <linux@misterx.org> 
4 5
# Purpose : To make interaction with 3ware raid cards easier and to also allow easy scripting of write cache functions. 
# Usage : See USAGE Below
Chris Layton's avatar
Chris Layton committed
6 7 8 9 10 11 12 13 14 15
# Requires :  tw_cli (Make sure to get the right one for your card and card's code base!)
# Disclaimer : This is a armed and dangerous program...use at your own risk!
# License : GPL V3

# Exit Codes 
# 0 - All is well..in theory 
# 1 - Something went wrong...really it did...
# 2 - No arguments given on CLI! 
# 3 - Error turning on writecache
# 4 - Error turning off writecache
16 17 18 19
# 5 - tw_cli is not installed!
# 6 - tw_cli show diag FAILED. Your controller might have a issue or might not work with this feature. 
# 7 - tw_cli dpmstat command FAILED.
# 8 - dpmstat was not set correct (on|off) and usage was displayed.
Chris Layton's avatar
Chris Layton committed
20
# 9 - dpmstat error on setting (on|off)
Chris Layton's avatar
Chris Layton committed
21 22

TWCLI=/usr/sbin/tw_cli
Chris Layton's avatar
Chris Layton committed
23 24 25
LOGGER=$(which logger)
SYSLOG="$LOGGER -t LSI"
#SYSLOG="echo -e"
Chris Layton's avatar
Chris Layton committed
26
CONTROLLER=$($TWCLI show | awk '/c./ {print $1}')
27
VERBOSE="0"
Chris Layton's avatar
Chris Layton committed
28 29
USAGE=" 
  Usage (-v must always be first if used) : \n 
30
 -v verbose \n  
Chris Layton's avatar
Chris Layton committed
31 32 33
 -W Turn on Writecache if off \n 
 -w turn off Writecache if on \n 
 -s drive status \n 
34 35 36
 -c controller status  \n 
 -d detailed controller information  \n
 -D (on|off) set state of dpmstat \n
37
 -p Show phy (link speed, Device type, SAS information, and others) \n
38
 -P (inst│ra│ext) dpmstat Performance Monitor (inst = Instantaneous| ra = Running Average | ext = Extended Drive Statistics)  \n
Chris Layton's avatar
Chris Layton committed
39 40
 -h This usage \n 
"
Chris Layton's avatar
Chris Layton committed
41 42 43 44 45 46 47

# Functions 

turn_on_writecache () {
for i in $($TWCLI /$CONTROLLER show unitstatus | egrep -o 'u[0-9]'); do
    case $($TWCLI /$CONTROLLER/$i show wrcache | cut -d= -f2 | sed 's/^[ \t]*//;/^$/d') in
	on)
48
	$SYSLOG "WriteCache is on for /$CONTROLLER/$i"
Chris Layton's avatar
Chris Layton committed
49 50
	;;
	off)
51
	$SYSLOG "WriteCache is off for /$CONTROLLER/$i ..turning on"
Chris Layton's avatar
Chris Layton committed
52 53 54
	$TWCLI /$CONTROLLER/$i set wrcache=on quiet
	;;
	*)
Chris Layton's avatar
Chris Layton committed
55
	$SYSLOG "ERROR: 3ware_tool ran into a unexpected error turning on writecache" && exit 3
Chris Layton's avatar
Chris Layton committed
56 57 58 59 60 61 62 63 64 65
	;;
    esac
done
}


turn_off_writecache () {
for i in $($TWCLI /$CONTROLLER show unitstatus | egrep -o 'u[0-9]'); do
    case $($TWCLI /$CONTROLLER/$i show wrcache | cut -d= -f2 | sed 's/^[ \t]*//;/^$/d') in
        on)
66
        $SYSLOG "WriteCache is on for /$CONTROLLER/$i turning off"
Chris Layton's avatar
Chris Layton committed
67 68 69
	$TWCLI /$CONTROLLER/$i set wrcache=off quiet
        ;;
        off)
70
        $SYSLOG "WriteCache is already off for /$CONTROLLER/$i"
Chris Layton's avatar
Chris Layton committed
71 72
        ;;
        *)
Chris Layton's avatar
Chris Layton committed
73
        $SYSLOG "ERROR : 3ware_tool ran into a unexpected error turning off writecache" && exit 4
Chris Layton's avatar
Chris Layton committed
74 75 76 77 78 79
        ;;
    esac
done
}

show_drive_status () {
80

Chris Layton's avatar
Chris Layton committed
81
for i in $($TWCLI /$CONTROLLER show unitstatus | egrep -o 'u[0-9]'); do
82 83 84 85 86
    if [[ $VERBOSE -eq 1 ]] ; then
        $TWCLI /$CONTROLLER/$i show
    else
        $TWCLI /$CONTROLLER/$i show status
    fi
Chris Layton's avatar
Chris Layton committed
87
done
88

Chris Layton's avatar
Chris Layton committed
89 90 91 92
}

show_controller_status () {

93 94
if [[ $VERBOSE -eq 1 ]] ; then 
	$TWCLI /$CONTROLLER show all
Chris Layton's avatar
Chris Layton committed
95
else
96 97
	$TWCLI /$CONTROLLER show unitstatus	
fi
Chris Layton's avatar
Chris Layton committed
98 99 100

}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
show_phy () {

    $TWCLI /$CONTROLLER show phy

}

show_perf () {

$TWCLI /$CONTROLLER show dpmstat type=$OPTARG

if [[ $? -ne 0 ]] ; then
        echo "ERROR : $TWCLI /$CONTROLLER show dpmstat type=$OPTARG failed!"  && exit 7
fi

}

117 118
show_detailed_status () {

119 120 121 122 123
if [[ $VERBOSE -eq 1 ]] ; then
    $TWCLI show diag
else
    $TWCLI show diag | awk '/###/ {sub("###",""); print $0}'
fi
124 125

if [[ $? -ne 0 ]] ; then
126
	$SYSLOG "ERROR : $TWCLI show diag FAILED. Might be a controller issue!"  && exit 6 
127 128 129 130
fi

}

131 132 133 134 135 136 137 138 139 140 141 142 143
set_dpmstat () {

if [[ $OPTARG =~ (on|off) ]] ; then 
    $TWCLI /$CONTROLLER set dpmstat=$OPTARG 
else
    echo -e $USAGE && exit 8 
fi 

if [[ $? -ne 0 ]] ; then
        $SYSLOG "ERROR : Setting DPMSTAT Failed make sure you used (on|off) correctly !"  && exit 9
fi

}
144

Chris Layton's avatar
Chris Layton committed
145 146 147
# Make sure TWI_CLI is installed

if ! [[ $(which tw_cli) ]] ; then
148
	$SYSLOG "ERROR : tw_cli is not installed ! Program exiting!" && exit 5
Chris Layton's avatar
Chris Layton committed
149 150 151 152 153
fi


# Process the arguments

Chris Layton's avatar
Chris Layton committed
154
[[ ! $1 ]] && echo -e $USAGE && exit 0
Chris Layton's avatar
Chris Layton committed
155

156
while getopts vWwscD:dphP: arg; do
Chris Layton's avatar
Chris Layton committed
157
    case $arg in
158 159 160
        v)
            VERBOSE="1"
            ;;
Chris Layton's avatar
Chris Layton committed
161 162 163 164 165 166 167 168 169 170 171 172
	W) 
	    turn_on_writecache
	    ;;
	w)
	    turn_off_writecache
	    ;;
	s)
	    show_drive_status
	    ;;
	c)
	    show_controller_status
            ;;
173 174
	D)
	    set_dpmstat
Chris Layton's avatar
Chris Layton committed
175
	    ;;
176 177 178
        d)
            show_detailed_status
           ;;
179 180 181 182 183 184
	p) 
	    show_phy
	   ;;
	P)
	    show_perf
	   ;;
Chris Layton's avatar
Chris Layton committed
185 186 187 188 189 190 191 192 193 194 195
        h)
            echo -e $USAGE
            exit 0
	    ;;
        *)
            echo -e $USAGE
            exit 2
            ;;
	esac
done

196