raid-battery 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/sh
  2. # {{{ Variables
  3. DEVICE=
  4. SCAN=0
  5. ATTRIBUTE=
  6. ZBX_DISCOVERY_OUTPUT=0
  7. # }}}
  8. # {{{ usage()
  9. usage() {
  10. cat <<EOH
  11. USAGE: $(basename "$0") -d DEVICE
  12. Get RAID Battery status value for a device
  13. Options:
  14. -d Battery device path
  15. -a Attribute (status, temperature, 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_batteries()
  22. scan_megaraid_batteries() {
  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. printf "megaraid /c%s\n" "${i}"
  29. done
  30. fi
  31. fi
  32. }
  33. # }}}
  34. # {{{ scan_batteries()
  35. scan_batteries() {
  36. scan_megaraid_batteries
  37. }
  38. # }}}
  39. # {{{ list_batteries()
  40. list_batteries() {
  41. ZBX="${1}"
  42. if [ "${ZBX}" = 1 ]; then
  43. printf '{"data":['
  44. fi
  45. scan_batteries | \
  46. while read type device ; do
  47. if [ "${ZBX}" = 1 ]; then
  48. printf '{'
  49. printf '"{#DEVICE}":"%s",' "${device}"
  50. printf '"{#TYPE}":"%s"' "${type}"
  51. printf '},'
  52. else
  53. printf "${device}|${type}\n"
  54. fi
  55. done | sed 's/},$/}/'
  56. if [ "${ZBX}" = 1 ]; then
  57. printf ']}'
  58. fi
  59. }
  60. # }}}
  61. # {{{ main()
  62. while getopts "d:a:Ss" OPT ; do
  63. case "$OPT" in
  64. \?|h)
  65. usage
  66. exit 0
  67. ;;
  68. d)
  69. DEVICE="$OPTARG"
  70. ;;
  71. a)
  72. ATTRIBUTE="$OPTARG"
  73. ;;
  74. S)
  75. SCAN=1
  76. ;;
  77. s)
  78. SCAN=1
  79. ZBX_DISCOVERY_OUTPUT=1
  80. ;;
  81. esac
  82. done
  83. if [ "${SCAN}" = 1 ]; then
  84. list_batteries "${ZBX_DISCOVERY_OUTPUT}"
  85. exit 0
  86. fi
  87. if [ -z "${DEVICE}" ] ; then
  88. printf "[ERR] device is not defined\n"
  89. usage
  90. exit 1
  91. fi
  92. if [ -z "${ATTRIBUTE}" ] ; then
  93. printf "[ERR] attribute is not defined\n"
  94. usage
  95. exit 1
  96. fi
  97. result=0
  98. case "${ATTRIBUTE}" in
  99. status)
  100. status=
  101. for d in ${DEVICE}/cv ${DEVICE}/bbu ; do
  102. status=$(storcli "${d}" show all | sed -r -n 's/^(Battery |)State\s+([a-z]+)\s*$/\2/pi')
  103. if [ -n "${status}" ]; then
  104. break
  105. fi
  106. done
  107. if [ "${status}" != "Optimal" ]; then
  108. result=1
  109. fi
  110. ;;
  111. temperature)
  112. for d in ${DEVICE}/cv ${DEVICE}/bbu ; do
  113. status=$(storcli "${d}" show all | sed -r -n 's/^Temperature\s+([0-9]+)\s+C\s*$/\1/pi')
  114. if [ -n "${status}" ]; then
  115. break
  116. fi
  117. done
  118. if [ -z "${status}" ]; then
  119. result=1
  120. else
  121. result="${status}"
  122. fi
  123. ;;
  124. *)
  125. printf "[ERR] unsupported attribute '%s'\n" "${ATTRIBUTE}"
  126. usage
  127. exit 1
  128. esac
  129. printf "${result}\n"
  130. exit 0
  131. # }}}
  132. # vim: foldmethod=marker foldlevel=0 foldenable