utils.sh.in 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #! /bin/sh
  2. STATE_OK=0
  3. STATE_WARNING=1
  4. STATE_CRITICAL=2
  5. STATE_UNKNOWN=3
  6. STATE_DEPENDENT=4
  7. if test -x /usr/bin/printf; then
  8. ECHO=/usr/bin/printf
  9. else
  10. ECHO=echo
  11. fi
  12. print_revision() {
  13. echo "$1 v$2 (@PACKAGE@ @VERSION@)"
  14. $ECHO "@WARRANTY@" | sed -e 's/\n/ /g'
  15. }
  16. support() {
  17. $ECHO "@SUPPORT@" | sed -e 's/\n/ /g'
  18. }
  19. #
  20. # check_range takes a value and a range string, returning successfully if an
  21. # alert should be raised based on the range.
  22. #
  23. check_range() {
  24. local v range yes no err decimal start end cmp match
  25. v="$1"
  26. range="$2"
  27. # whether to raise an alert or not
  28. yes=0
  29. no=1
  30. err=2
  31. # regex to match a decimal number
  32. decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
  33. # compare numbers (including decimals), returning true/false
  34. cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
  35. # returns successfully if the string in the first argument matches the
  36. # regex in the second
  37. match() { echo "$1" | grep -E -q -- "$2"; }
  38. # make sure value is valid
  39. if ! match "$v" "^$decimal$"; then
  40. echo "${0##*/}: check_range: invalid value" >&2
  41. unset -f cmp match
  42. return "$err"
  43. fi
  44. # make sure range is valid
  45. if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
  46. echo "${0##*/}: check_range: invalid range" >&2
  47. unset -f cmp match
  48. return "$err"
  49. fi
  50. # check for leading @ char, which negates the range
  51. if match $range '^@'; then
  52. range=${range#@}
  53. yes=1
  54. no=0
  55. fi
  56. # parse the range string
  57. if ! match "$range" ':'; then
  58. start=0
  59. end="$range"
  60. else
  61. start="${range%%:*}"
  62. end="${range#*:}"
  63. fi
  64. # do the comparison, taking positive ("") and negative infinity ("~")
  65. # into account
  66. if [ "$start" != "~" ] && [ "$end" != "" ]; then
  67. if cmp "$start <= $v" && cmp "$v <= $end"; then
  68. unset -f cmp match
  69. return "$no"
  70. else
  71. unset -f cmp match
  72. return "$yes"
  73. fi
  74. elif [ "$start" != "~" ] && [ "$end" = "" ]; then
  75. if cmp "$start <= $v"; then
  76. unset -f cmp match
  77. return "$no"
  78. else
  79. unset -f cmp match
  80. return "$yes"
  81. fi
  82. elif [ "$start" = "~" ] && [ "$end" != "" ]; then
  83. if cmp "$v <= $end"; then
  84. unset -f cmp match
  85. return "$no"
  86. else
  87. unset -f cmp match
  88. return "$yes"
  89. fi
  90. else
  91. unset -f cmp match
  92. return "$no"
  93. fi
  94. }