check_rpi_temp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. PROGNAME=`basename $0`
  3. VERSION='1.0'
  4. AUTHOR='2020, Wazakindjes'
  5. # Some exit val00s
  6. ST_OK=0
  7. ST_WARN=1
  8. ST_CRIT=2
  9. ST_UNK=3
  10. ST_NOVC=4
  11. GENCMD_DEFAULT='/opt/vc/bin/vcgencmd'
  12. # RPIs can get plenty hawt but that's cool ;;]];
  13. warn=50
  14. crit=70
  15. print_version() {
  16. echo "$PROGNAME version $VERSION ($AUTHOR)"
  17. }
  18. print_usage() {
  19. echo "Usage:"
  20. echo "$PROGNAME [-w/--warning <level>] [-c/--critical <level>]"
  21. echo "$PROGNAME --help|--halp|-h"
  22. echo "$PROGNAME --version|-v"
  23. echo ""
  24. echo "Options:"
  25. echo " --warning|-w)"
  26. echo " Sets a warning level, default is $warn"
  27. echo " --critical|-c)"
  28. echo " Sets a critical level, default is $crit"
  29. echo " --help|--halp|-h)"
  30. echo " Display help/usage information."
  31. echo " --version|-v)"
  32. echo " Display version information."
  33. }
  34. print_header() {
  35. echo "$PROGNAME is a Nagios/icinga2 plugin to monitor a Raspberry Pi's temperature."
  36. echo "It makes use of vcgencmd which is usually available as $GENCMD_DEFAULT."
  37. }
  38. chk_intval() {
  39. if [[ -z $2 ]]; then
  40. echo "Missing value for argument $1"
  41. print_usage
  42. exit $ST_PARAM
  43. fi
  44. # I could just do =~ ^[0-9.]$ for the first condition, but that might not work on older bashes ;];
  45. if [[ ! -z $(echo "$2" | sed 's/[0-9]//g') ]] || [[ $2 -le 0 ]]; then
  46. echo "Invalid value for argument $1: $2 (must be an integer/whole number greater than zero)"
  47. exit $ST_PARAM
  48. fi
  49. }
  50. while test -n "$1"; do
  51. case "$1" in
  52. --help|--halp|-h)
  53. print_version
  54. echo ""
  55. print_header
  56. echo ""
  57. print_usage
  58. exit $ST_UNK
  59. ;;
  60. --version|-v)
  61. print_version
  62. exit $ST_UNK
  63. ;;
  64. --warning|-w)
  65. chk_intval "$1" "$2"
  66. warn=$2
  67. shift
  68. ;;
  69. --critical|-c)
  70. chk_intval "$1" "$2"
  71. crit=$2
  72. shift
  73. ;;
  74. *)
  75. echo "Unknown argument: $1"
  76. echo ""
  77. print_usage
  78. exit $ST_UNK
  79. ;;
  80. esac
  81. shift
  82. done
  83. if [[ $warn -ge $crit ]]; then
  84. echo "Invalid warning/critical thresholds: warning must be lower than the critical level"
  85. exit $ST_UNK
  86. fi
  87. gencmd=$(which vcgencmd)
  88. if [[ -z $gencmd ]]; then
  89. gencmd="$GENCMD_DEFAULT"
  90. fi
  91. if [[ ! -f $gencmd || ! -x $gencmd ]]; then
  92. echo "vcgencmd not found or not executable: $gencmd"
  93. exit $ST_NOVC
  94. fi
  95. tempout=$(sudo "$gencmd" measure_temp 2>&1)
  96. temp=$(echo "$tempout" | cut -d '=' -f2 | cut -d "'" -f1)
  97. if [[ -z $temp || ! -z $(echo "$temp" | sed 's/[0-9.]//g') ]]; then
  98. echo "Unable to run/parse 'measure_temp', output was:"
  99. echo "$tempout"
  100. exit $ST_CRIT
  101. fi
  102. output="temp: $temp"
  103. perfdata="'temp'=$temp"
  104. tempround=$(echo "$temp" | sed 's/\..*//')
  105. if [[ $tempround -ge $crit ]]; then
  106. echo "CRITICAL - $output exceeds $crit | $perfdata"
  107. exit $ST_CRIT
  108. elif [[ $tempround -ge $warn ]]; then
  109. echo "WARNING - $output exceeds $warn | $perfdata"
  110. exit $ST_WARN
  111. fi
  112. echo "OK - $output | $perfdata"
  113. exit $ST_OK