utils.sh.in 2.3 KB

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