check_cpu 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. # Original sauce: https://github.com/episodeiv/icinga-plugins/blob/master/check_cpu.sh
  3. # Shit doesn't seem actively maintained anym0awr so I decided to reuse it and make mein own version
  4. PROGNAME=`basename $0`
  5. VERSION='1.1'
  6. AUTHOR='2020, Wazakindjes'
  7. # Some exit val00s
  8. ST_OK=0
  9. ST_WARN=1
  10. ST_CRIT=2
  11. ST_UNK=3
  12. ST_PARAM=4
  13. ST_NOPROC=5
  14. ST_NOBC=6
  15. # Default interval of uno secund0
  16. interval=1
  17. warn=0
  18. crit=0
  19. print_version() {
  20. echo "$PROGNAME version $VERSION ($AUTHOR)"
  21. }
  22. print_usage() {
  23. echo "Usage:"
  24. echo "$PROGNAME [-i/--interval <seconds>] [-w/--warning <level>] [-c/--critical <level>]"
  25. echo "$PROGNAME --help|--halp|-h"
  26. echo "$PROGNAME --version|-v"
  27. echo ""
  28. echo "Options:"
  29. echo " --interval|-i)"
  30. echo " Defines the pause between the two times /proc/stat is being"
  31. echo " parsed. Higher values could lead to more accurate result."
  32. echo " Default is: 1 second"
  33. echo " --warning|-w)"
  34. echo " Sets a warning level for CPU user. Default is: off"
  35. echo " --critical|-c)"
  36. echo " Sets a critical level for CPU user. Default is: off"
  37. echo " --help|--halp|-h)"
  38. echo " Display help/usage information."
  39. echo " --version|-v)"
  40. echo " Display version information."
  41. }
  42. print_header() {
  43. echo "$PROGNAME is a Nagios/icinga2 plugin to monitor CPU utilisation. It makes"
  44. echo "use of /proc/stat and calculates it through 'bc' rather than"
  45. echo "using another frontend tool like 'iostat' or 'top'."
  46. echo "When using the (optional) warning/critical thresholds all values except"
  47. echo "'idle' are aggregated and compared to the thresholds. There's"
  48. echo "currently no support for warning/critical thresholds for specific"
  49. echo "usage parameters."
  50. }
  51. get_cpuvals() {
  52. tmp1_cpu_user=`grep -m1 '^cpu' /proc/stat | awk '{print $2}'`
  53. tmp1_cpu_nice=`grep -m1 '^cpu' /proc/stat | awk '{print $3}'`
  54. tmp1_cpu_sys=`grep -m1 '^cpu' /proc/stat | awk '{print $4}'`
  55. tmp1_cpu_idle=`grep -m1 '^cpu' /proc/stat | awk '{print $5}'`
  56. tmp1_cpu_iowait=`grep -m1 '^cpu' /proc/stat | awk '{print $6}'`
  57. tmp1_cpu_irq=`grep -m1 '^cpu' /proc/stat | awk '{print $7}'`
  58. tmp1_cpu_softirq=`grep -m1 '^cpu' /proc/stat | awk '{print $8}'`
  59. tmp1_cpu_total=`expr $tmp1_cpu_user + $tmp1_cpu_nice + $tmp1_cpu_sys + $tmp1_cpu_idle + $tmp1_cpu_iowait + $tmp1_cpu_irq + $tmp1_cpu_softirq`
  60. sleep $interval
  61. tmp2_cpu_user=`grep -m1 '^cpu' /proc/stat | awk '{print $2}'`
  62. tmp2_cpu_nice=`grep -m1 '^cpu' /proc/stat | awk '{print $3}'`
  63. tmp2_cpu_sys=`grep -m1 '^cpu' /proc/stat | awk '{print $4}'`
  64. tmp2_cpu_idle=`grep -m1 '^cpu' /proc/stat | awk '{print $5}'`
  65. tmp2_cpu_iowait=`grep -m1 '^cpu' /proc/stat | awk '{print $6}'`
  66. tmp2_cpu_irq=`grep -m1 '^cpu' /proc/stat | awk '{print $7}'`
  67. tmp2_cpu_softirq=`grep -m1 '^cpu' /proc/stat | awk '{print $8}'`
  68. tmp2_cpu_total=`expr $tmp2_cpu_user + $tmp2_cpu_nice + $tmp2_cpu_sys + $tmp2_cpu_idle + $tmp2_cpu_iowait + $tmp2_cpu_irq + $tmp2_cpu_softirq`
  69. diff_cpu_user=`echo "$tmp2_cpu_user - $tmp1_cpu_user" | bc -l`
  70. diff_cpu_nice=`echo "$tmp2_cpu_nice - $tmp1_cpu_nice" | bc -l`
  71. diff_cpu_sys=`echo "$tmp2_cpu_sys - $tmp1_cpu_sys" | bc -l`
  72. diff_cpu_idle=`echo "$tmp2_cpu_idle - $tmp1_cpu_idle" | bc -l`
  73. diff_cpu_iowait=`echo "$tmp2_cpu_iowait - $tmp1_cpu_iowait" | bc -l`
  74. diff_cpu_irq=`echo "$tmp2_cpu_irq - $tmp1_cpu_irq" | bc -l`
  75. diff_cpu_softirq=`echo "$tmp2_cpu_softirq - $tmp1_cpu_softirq" | bc -l`
  76. diff_cpu_total=`echo "$tmp2_cpu_total - $tmp1_cpu_total" | bc -l`
  77. cpu_user=`echo "scale=2; (1000 * $diff_cpu_user / $diff_cpu_total + 5) / 10" | bc -l | sed 's/^\./0./'`
  78. cpu_nice=`echo "scale=2; (1000 * $diff_cpu_nice / $diff_cpu_total + 5) / 10" | bc -l | sed 's/^\./0./'`
  79. cpu_sys=`echo "scale=2; (1000 * $diff_cpu_sys / $diff_cpu_total + 5) / 10" | bc -l | sed 's/^\./0./'`
  80. cpu_idle=`echo "scale=2; (1000 * $diff_cpu_idle / $diff_cpu_total + 5) / 10" | bc -l | sed 's/^\./0./'`
  81. cpu_iowait=`echo "scale=2; (1000 * $diff_cpu_iowait / $diff_cpu_total + 5) /10" | bc -l | sed 's/^\./0./'`
  82. cpu_irq=`echo "scale=2; (1000 * $diff_cpu_irq / $diff_cpu_total + 5) / 10" | bc -l | sed 's/^\./0./'`
  83. cpu_softirq=`echo "scale=2; (1000 * $diff_cpu_softirq / $diff_cpu_total +5) / 10" | bc -l | sed 's/^\./0./'`
  84. cpu_total=`echo "scale=2; (1000 * $diff_cpu_total / $diff_cpu_total + 5) /10" | bc -l | sed 's/^\./0./'`
  85. cpu_usage=`echo "($cpu_user + $cpu_nice + $cpu_sys + $cpu_iowait + $cpu_irq + $cpu_softirq) / 1" | bc`
  86. }
  87. chk_intval() {
  88. if [[ -z $2 ]]; then
  89. echo "Missing value for argument $1"
  90. print_usage
  91. exit $ST_PARAM
  92. fi
  93. # I could just do =~ ^[0-9.]$ for the first condition, but that might not work on older bashes ;];
  94. if [[ ! -z $(echo "$2" | sed 's/[0-9]//g') ]] || [[ $2 -le 0 ]]; then
  95. echo "Invalid value for argument $1: $2 (must be an integer/whole number greater than zero)"
  96. exit $ST_PARAM
  97. fi
  98. }
  99. while test -n "$1"; do
  100. case "$1" in
  101. --help|--halp|-h)
  102. print_version
  103. echo ""
  104. print_header
  105. echo ""
  106. print_usage
  107. exit $ST_UNK
  108. ;;
  109. --version|-v)
  110. print_version
  111. exit $ST_UNK
  112. ;;
  113. --interval|-i)
  114. chk_intval "$1" "$2"
  115. interval=$2
  116. shift
  117. ;;
  118. --warning|-w)
  119. chk_intval "$1" "$2"
  120. warn=$2
  121. shift
  122. ;;
  123. --critical|-c)
  124. chk_intval "$1" "$2"
  125. crit=$2
  126. shift
  127. ;;
  128. *)
  129. echo "Unknown argument: $1"
  130. echo ""
  131. print_usage
  132. exit $ST_UNK
  133. ;;
  134. esac
  135. shift
  136. done
  137. if [[ $crit -gt 0 ]] && [[ $warn -ge $crit ]]; then
  138. echo "Invalid warning/critical thresholds: warning must be lower than the critical level"
  139. exit $ST_UNK
  140. fi
  141. if [[ ! -f /proc/stat ]]; then
  142. echo "/proc/stat doesn't seem to exist, unable to proceed"
  143. exit $ST_NOPROC
  144. fi
  145. if [[ -z $(which bc) ]]; then
  146. echo "Unable to find required 'bc' program, unable to proceed"
  147. exit $ST_NOBC
  148. fi
  149. get_cpuvals
  150. output="user: $cpu_user, nice: $cpu_nice, sys: $cpu_sys, iowait: $cpu_iowait, irq: $cpu_irq, softirq: $cpu_softirq idle: $cpu_idle"
  151. perfdata="'user'=$cpu_user 'nice'=$cpu_nice 'sys'=$cpu_sys 'softirq'=$cpu_softirq 'iowait'=$cpu_iowait 'irq'=$cpu_irq 'idle'=$cpu_idle"
  152. if [[ $crit -gt 0 ]] && [[ $cpu_usage -ge $crit ]]; then
  153. echo "CRITICAL - $output | $perfdata"
  154. exit $ST_CRIT
  155. elif [[ $warn -gt 0 ]] && [[ $cpu_usage -ge $warn ]]; then
  156. echo "WARNING - $output | $perfdata"
  157. exit $ST_WARN
  158. fi
  159. echo "OK - $output | $perfdata"
  160. exit $ST_OK