RAID.tst: use screen(1) so that promiseutil(8) has a valid tty
[nagcollect.git] / client / lib / tests / RAID.tst
1 # NagCollect -- Nagios Data Collector for Passive Checks
2 # Copyright (c)2009-2011 Alexander Barton, alex@barton.de
3
4 SERVICE="RAID_p"
5
6 if [ `uname` = "Darwin" ]; then
7         tmp=`mktemp "/tmp/$$.XXXX"`
8         tmp2=`mktemp "/tmp/$$-2.XXXX"`
9         LC_ALL=C diskutil checkRAID >"$tmp" 2>/dev/null
10         if [ $? -eq 0 ]; then
11                 # Apple Software-RAID detected
12                 statusString1=`grep "^Status:" "$tmp" | uniq`
13                 status=`echo $statusString1 | sed -e 's/Status: //g'`
14                 statusString=`grep "^Status:" "$tmp"`
15                 statusText=`echo $statusString | sed -e 's/Status: //g' | sed -e 's/ /, /g'`
16                 if [ "$status" != "Online" ]; then
17                         STATUS=2
18                         TEXT="$TEXT SW-RAID is $statusText."
19                 else
20                         [ -z "$STATUS" ] && STATUS=0
21                         TEXT="$TEXT SW-RAID status is good."
22                 fi
23         fi
24         LC_ALL=C raidutil list status 2>/dev/null | grep "RAID " >"$tmp"
25         if [ $? -eq 0 ]; then
26                 # Apple Hardware-RAID detected
27                 declare -i good=0
28                 declare -i bad=0
29                 status=""
30                 while read x; do
31                         v=`echo "$x" | cut -d' ' -f1`
32                         s=`echo "$x" | cut -b67- | sed -e 's/://g'`
33                         s2=`echo "$x" | cut -b78-`
34                         [ -n "$status" ] \
35                                 && status="$status, $v:$s" \
36                                 || status="$v:$s"
37                         [ "$s2" = "Good" ] && good=$good+1 || bad=bad+1
38                 done <"$tmp"
39                 if [ $bad -eq 0 -a $good -ge 1 ]; then
40                         [ -z "$STATUS" ] && STATUS=0
41                         TEXT="$TEXT Apple HW-RAID is good: $status."
42                 elif [ $bad -gt 0 ]; then
43                         STATUS=2
44                         TEXT="$TEXT Apple HW-RAID status: $status."
45                 else
46                         [ -z "$STATUS" -o "$STATUS" = "1" ] && STATUS=1
47                         TEXT="$TEXT  Apple HW-RAID status is unknown ($status)!?"
48                 fi
49         fi
50         LC_ALL=C screen -D -m sh -c "promiseutil -C array -a list -v >$tmp2"
51         grep -A8 '^LdId' "$tmp2" | grep '^[0-9]' >"$tmp"
52         if [ $? -eq 0 ]; then
53                 # Promise Hardware-RAID detected
54                 declare -i good=0
55                 declare -i bad=0
56                 status=""
57                 while read x; do
58                         v=`echo "$x" | awk "{ print \\$2 }"`
59                         z=`echo "$x" | awk "{ print \\$3 }"`
60                         s=`echo "$x" | awk "{ print \\$4 }"`
61                         [ -n "$status" ] \
62                                 && status="$status, $v($z):$s" \
63                                 || status="$v($z):$s"
64                         [ "$s" = "OK" ] && good=$good+1 || bad=bad+1
65                 done <"$tmp"
66                 if [ $bad -eq 0 -a $good -ge 1 ]; then
67                         [ -z "$STATUS" ] && STATUS=0
68                         TEXT="$TEXT Promise HW-RAID is good: $status."
69                 elif [ $bad -gt 0 ]; then
70                         STATUS=2
71                         TEXT="$TEXT Promise HW-RAID status: $status."
72                 else
73                         [ -z "$STATUS" -o "$STATUS" = "1" ] && STATUS=1
74                         TEXT="$TEXT  Promise HW-RAID status is unknown ($status)!?"
75                 fi
76         fi
77         rm -f "$tmp" "$tmp2"
78 elif [ `uname` = "Linux" -a -r /proc/mdstat ]; then
79         status=$(cat /proc/mdstat | grep -E "(^md|^      [0-9])" \
80          | while read info1; do
81                 read info2
82                 dev=`echo $info1 | cut -d' ' -f1`
83                 stat=`echo $info2 | cut -d'[' -f 3 | cut -d']' -f1`
84                 echo -n " $dev:$stat"
85         done)
86         if [ -n "$status" ]; then
87                 if [ "`echo $status | tr -d 'md0123456789: U'`" = "" ]; then
88                         [ -z "$STATUS" ] && STATUS=0
89                         TEXT="$TEXT SW-RAID is good: $status."
90                 else
91                         STATUS=2
92                         TEXT="$TEXT SW-RAID status: $status."
93                 fi
94         fi
95 fi
96
97 if [ -n "$TEXT" ]; then
98         case "$STATUS" in
99                 0)      TEXT="OK -${TEXT}"; ;;
100                 1)      TEXT="WARNING -${TEXT}"; ;;
101                 2)      TEXT="ERROR -${TEXT}"; ;;
102                 *)      TEXT="UNKNOWN -${TEXT}"; ;;
103         esac
104 fi