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