raid 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/sh
  2. # {{{ Variables
  3. DEVICE=
  4. SCAN=0
  5. TYPE=
  6. ZBX_DISCOVERY_OUTPUT=0
  7. # }}}
  8. # {{{ usage()
  9. usage() {
  10. cat <<EOH
  11. USAGE: $(basename "$0") -d DEVICE
  12. Get RAID status value for a device
  13. Options:
  14. -d Device path
  15. -t Raid type (mdraid, megaraid, etc.)
  16. -S Scan devices and display list
  17. -s Scan devices and display list (Zabbix discovery output)
  18. EOH
  19. }
  20. # }}}
  21. # {{{ scan_megaraid_devices()
  22. scan_megaraid_devices() {
  23. if which storcli >/dev/null 2>&1 ; then
  24. ctrlcount=$(storcli show ctrlcount| sed -n 's/^Controller Count = //p')
  25. if [ "${ctrlcount}" -gt 0 ]; then
  26. ctrlcount=$(( ${ctrlcount} - 1))
  27. for i in $(seq 0 ${ctrlcount}) ; do
  28. storcli /c${i}/vall show|sed -r -n 's/^[0-9]+\/([0-9+]+)\s+RAID[0-9]+\s+[a-z]+\s+.*$/\1/pi' | \
  29. while read vd ; do
  30. printf "megaraid /c%s/v%s\n" "${i}" "${vd}"
  31. done
  32. done
  33. fi
  34. fi
  35. }
  36. # }}}
  37. # {{{ scan_mdraid_devices()
  38. scan_mdraid_devices() {
  39. if which mdadm >/dev/null 2>&1 ; then
  40. mdadm --detail --scan | sed -r -n 's/^ARRAY\s+(.*)\s+metadata=.*$/mdraid \1/p'
  41. fi
  42. }
  43. # }}}
  44. # {{{ scan_devices()
  45. scan_devices() {
  46. scan_megaraid_devices
  47. scan_mdraid_devices
  48. }
  49. # }}}
  50. # {{{ list_raid_devices()
  51. list_raid_devices() {
  52. ZBX="${1}"
  53. if [ "${ZBX}" = 1 ]; then
  54. printf '{"data":['
  55. fi
  56. scan_devices | \
  57. while read type device ; do
  58. if [ "${ZBX}" = 1 ]; then
  59. printf '{'
  60. printf '"{#DEVICE}":"%s",' "${device}"
  61. printf '"{#TYPE}":"%s"' "${type}"
  62. printf '},'
  63. else
  64. printf "${device}|${type}\n"
  65. fi
  66. done | sed 's/},$/}/'
  67. if [ "${ZBX}" = 1 ]; then
  68. printf ']}'
  69. fi
  70. }
  71. # }}}
  72. # {{{ main()
  73. while getopts "d:t:Ss" OPT ; do
  74. case "$OPT" in
  75. \?|h)
  76. usage
  77. exit 0
  78. ;;
  79. d)
  80. DEVICE="$OPTARG"
  81. ;;
  82. t)
  83. TYPE="$OPTARG"
  84. ;;
  85. S)
  86. SCAN=1
  87. ;;
  88. s)
  89. SCAN=1
  90. ZBX_DISCOVERY_OUTPUT=1
  91. ;;
  92. esac
  93. done
  94. if [ "${SCAN}" = 1 ]; then
  95. list_raid_devices "${ZBX_DISCOVERY_OUTPUT}"
  96. exit 0
  97. fi
  98. if [ -z "${DEVICE}" ] ; then
  99. printf "[ERR] device is not defined\n"
  100. usage
  101. exit 1
  102. fi
  103. if [ -z "${TYPE}" ] ; then
  104. printf "[ERR] raid type is not defined\n"
  105. usage
  106. exit 1
  107. fi
  108. result=0
  109. case "${TYPE}" in
  110. mdraid)
  111. status="$(mdadm --detail "${DEVICE}" 2>/dev/null | sed -r -n 's/^\s+State\s*:\s*([^ ]+)\s*$/\1/p')"
  112. if [ "${status}" != 'clean' ]; then
  113. result=1
  114. fi
  115. ;;
  116. megaraid)
  117. status="$(storcli "${DEVICE}" show 2>/dev/null | sed -r -n 's/^.*RAID[0-9]+\s+([a-z]+)\s+.*$/\1/pi')"
  118. if [ "${status}" != 'Optl' ]; then
  119. result=1
  120. fi
  121. ;;
  122. *)
  123. printf "[ERR] unsupported raid type '%s'\n" "${TYPE}"
  124. usage
  125. exit 1
  126. esac
  127. printf "${result}\n"
  128. exit 0
  129. # }}}
  130. # vim: foldmethod=marker foldlevel=0 foldenable