Advertisement
Guest User

check_disk.sh

a guest
Mar 3rd, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.30 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15.  
  16. PROGNAME=`basename $0`
  17. VERSION="version 0.93"
  18. AUTHOR="by Radu MARINESCU [email protected]"
  19. #############################
  20. # Program paths. If left empty the script tries to find the programs
  21. # in the PATH. Fill in the correct paths only if needed.
  22. #############################
  23. #usually /bin/awk or /usr/bin/awk
  24. AWK=""
  25. #usually /bin/grep or /usr/bin/grep
  26. GREP=""
  27. #usually /bin/df
  28. DF=""
  29. #############################
  30.  
  31. if [ ${#AWK} == 0 ] #the variable AWK is not defined; trying to find awk in the PATH
  32. then
  33.     AWK=`which awk 2>&1`
  34.     AWK_EXISTS=$?
  35.     if [ "$AWK_EXISTS" != 0 ] || [ ${#AWK} == 0 ] #awk is not in the PATH or the variable AWK is empty
  36.     then
  37.         echo "Error! Can't find awk."
  38.         exit 3
  39.     fi 
  40. fi
  41.  
  42. if [ ${#GREP} == 0 ] #the variable GREP is not defined; trying to find grep in the PATH
  43. then
  44.     GREP=`which grep 2>&1`
  45.     GREP_EXISTS=$?
  46.     if [ "$GREP_EXISTS" != 0 ] || [ ${#GREP} == 0 ] #grep is not in the PATH or the variable GREP is empty
  47.     then
  48.         echo "Error! Can't find grep."
  49.         exit 3
  50.     fi 
  51. fi
  52.  
  53. if [ ${#DF} == 0 ] #the variable DF is not defined; trying to find df in the PATH
  54. then
  55.     DF=`which df 2>&1`
  56.     DF_EXISTS=$?
  57.     if [ "$DF_EXISTS" != 0 ] || [ ${#DF} == 0 ] #df is not in the PATH or the variable DF is empty
  58.     then
  59.         echo "Error! Can't find df."
  60.         exit 3
  61.     fi 
  62. fi
  63.  
  64.  
  65. #############################
  66. # Functions
  67. #############################
  68.  
  69. print_version() {
  70.     echo "$VERSION $AUTHOR"
  71. }
  72.  
  73.  
  74. print_help() {
  75. print_version $PROGNAME $VERSION
  76. echo ""
  77. echo "$PROGNAME is a Nagios plugin used to check disk used space by"
  78. echo "processing the output of \"df\" command. It runs on UNIX, Linux"
  79. echo "and BSD platforms and reports the following performance data:"
  80. echo "- total disk space (Bytes)"
  81. echo "- currently used disk space (Bytes)"
  82. echo "- currently used disk space (%)"
  83. echo " "
  84. echo "$PROGNAME [-v] [-h] [-w UsedSpaceWarning] [-c UsedSpaceCritical] [-p Partition]"
  85. echo " "
  86. echo "Options:"
  87. echo "  --version|-v)"
  88. echo "    prints the program version"
  89. echo "  --help|-h)"
  90. echo "    prints this help information"
  91. echo "  -w)"
  92. echo "    warning threshold (in percents without % sign) for used space"
  93. echo "  -c)"
  94. echo "    critical threshold (in percents without % sign) for used space"
  95. echo "  -p)"
  96. echo "    disk partition to check"
  97. echo " "
  98. exit 3
  99. }
  100.  
  101.  
  102. # float number comparison
  103. function fcomp() {
  104.     $AWK -v n1=$1 -v n2=$2 'BEGIN{ if (n1<=n2) exit 0; exit 1}'
  105. }
  106.  
  107.  
  108. #formats bytes => KBytes, MBytes, GBytes, TBytes
  109. function btokmgt() {
  110.     if [ $1 -lt 1024 ] #Bytes
  111.     then
  112.         echo "${1}B"
  113.     elif [ $1 -lt 1048576 ] #KBytes
  114.     then
  115.         echo "$1" | $AWK '{printf "%.1fKB", $1/1024}'
  116.     elif [ $1 -lt 1073741824 ] #MBytes
  117.     then
  118.         echo "$1" | $AWK '{printf "%.1fMB", $1/1048576}'
  119.     elif [ $1 -lt 1099511627776 ] #GBytes
  120.     then
  121.         echo "$1" | $AWK '{printf "%.1fGB", $1/1073741824}'
  122.     elif [ $1 -lt 1125899906842624 ] #TBytes
  123.     then
  124.         echo "$1" | $AWK '{printf "%.1fTB", $1/1099511627776}'
  125.     fi
  126. }
  127. #############################
  128.  
  129.  
  130. if [ $# -lt 1 ]; then
  131.     print_help
  132.     exit 3
  133. fi
  134.  
  135. while test -n "$1"; do
  136.     case "$1" in
  137.         --help|-h)
  138.             print_help
  139.             exit 3
  140.             ;;
  141.         --version|-v)
  142.             print_version $PROGNAME $VERSION
  143.             exit 3
  144.             ;;
  145.         -w)
  146.             WarnSpace=$2
  147.             shift
  148.             ;;
  149.         -c)
  150.             CritSpace=$2
  151.             shift
  152.             ;;
  153.         -p)
  154.             Partition=$2
  155.             shift
  156.             ;;
  157.         *)
  158.             echo "Unknown argument: $1"
  159.             print_help
  160.             exit 3
  161.             ;;
  162.     esac
  163.     shift
  164. done
  165.  
  166. if [ ${#Partition} == 0 ]
  167.     then
  168.         echo "Error! You must specify a partition to check! Example: /var or / or /home"
  169.         exit 3
  170. fi
  171.  
  172. if fcomp $WarnSpace 0
  173. then
  174.     WarnSpace=0
  175. fi
  176. if fcomp 100 $WarnSpace
  177. then
  178.     WarnSpace=100
  179. fi
  180.  
  181. if fcomp $CritSpace 0
  182. then
  183.     CritSpace=0
  184. fi
  185. if fcomp 100 $CritSpace
  186. then
  187.     CritSpace=100
  188. fi
  189.  
  190. if fcomp $CritSpace $WarnSpace
  191. then
  192.     WarnSpace=$CritSpace
  193. fi
  194.  
  195.  
  196. USEDTXT=`$DF -P -B 1 $Partition 2>&1`
  197. if [ $? != 0 ]
  198. then
  199.     echo "Error! Disk partition $Partition can't be checked. Does it exist?"
  200.     exit 3
  201. fi
  202.  
  203. SpaceTxt=`echo "$USEDTXT" | $GREP "${Partition}\$"`
  204. SpaceTotal=`echo "$SpaceTxt" | $AWK '{print $2}'`
  205. SpaceUsed=`echo "$SpaceTxt" | $AWK '{print $3}'`
  206. SpaceFree=`echo "$SpaceTxt" | $AWK '{print $4}'`
  207. SpaceUsedProc=`echo "$SpaceTxt" | $AWK '{printf "%.1f", $3*100/$2}'`
  208. SpaceFreeProc=`echo "$SpaceTxt" | $AWK '{printf "%.1f", $4*100/$2}'`
  209.  
  210. WarnSpaceAbs=`echo "$SpaceTotal $WarnSpace" | $AWK '{printf "%d", $1*$2/100}'`
  211. CritSpaceAbs=`echo "$SpaceTotal $CritSpace" | $AWK '{printf "%d", $1*$2/100}'`
  212.  
  213. SpaceTotal_F=`btokmgt $SpaceTotal`
  214. SpaceUsed_F=`btokmgt $SpaceUsed`
  215. SpaceFree_F=`btokmgt $SpaceFree`
  216.  
  217.  
  218. PerfData="'used space'=${SpaceUsed}B;${WarnSpaceAbs};${CritSpaceAbs};0;${SpaceTotal} 'used space (pct.)'=${SpaceUsedProc}%;${WarnSpace};${CritSpace};0;100"
  219.  
  220. if fcomp $SpaceUsedProc $WarnSpace
  221. then
  222.     echo "OK; $Partition: total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
  223.     exit 0
  224. fi
  225.  
  226. if fcomp $WarnSpace $SpaceUsedProc && fcomp $SpaceUsedProc $CritSpace
  227. then
  228.     echo "LowDiskSpace WARNING: $Partition total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%>${WarnSpace}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
  229.     #echo "LowDiskSpace WARNING: $Partition total ${SpaceTotal_F}, used ${SpaceUsed_F},  free ${SpaceFree_F} "
  230.     #echo "LowDiskSpace WARNING: Low space"
  231.     exit 1
  232. fi
  233.  
  234. if fcomp $CritSpace $SpaceUsedProc
  235. then
  236.     echo "CRITICAL; $Partition: total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%>${CritSpace}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
  237.     exit 2
  238. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement