Advertisement
DaywalkerGLXVR6

ZFS check script

Aug 8th, 2013
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.71 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. echo "---------------------------------------------"
  4.  
  5. PIDFILE=/var/run/zfs/zfs_check.pid
  6. EXPECTET_RESULT="all pools are healthy"
  7. BAD_OUTPUT_FILE=/tmp/zfs_broken.log
  8. IFS=";"
  9.  
  10. ZPOOL=/sbin/zpool
  11.  
  12. send_mail() {
  13.         echo "Sending mail to " $1
  14.         echo $($ZPOOL status) | /usr/bin/mail -s $2 $1
  15. }
  16.  
  17. if [ -e $PIDFILE ]
  18. then
  19.          PIDC=$(/bin/cat $PIDFILE)
  20.          echo "ZFS check is already running!       PID: "$PIDC
  21.          echo "Please use \"kill $PIDC\" to stop the Process (if you want to (because it hung,ect...))"
  22.          echo "And remove the PID File by hand ($PIDFILE)"
  23.          echo
  24. else
  25.          echo "Creating PID file"
  26.          PID=$(echo $$)
  27.          echo $PID > $PIDFILE
  28.          RESULT=$($ZPOOL status -x)
  29.          if [ "$RESULT" = "$EXPECTET_RESULT" ]
  30.          then
  31.                  #echo "Everything OK"
  32.                  echo $RESULT
  33.  
  34.                   if [ -e $BAD_OUTPUT_FILE ]
  35.                   then
  36.                           echo "Normal working conditions reached."
  37.                           echo "Removing bad output file..."
  38.                           rm $BAD_OUTPUT_FILE
  39.  
  40.               set -- $MAILADDR
  41.                           for addr in $@
  42.                           do
  43.                                    send_mail $addr "ZFS normal working conditions restored on $(hostname)"
  44.                           done
  45.  
  46.                   fi
  47.          else
  48.                  if [ -e $BAD_OUTPUT_FILE ]
  49.                  then
  50.                          echo "Already send something..."
  51.              echo "Updating output log"
  52.                          date > $BAD_OUTPUT_FILE
  53.                          $ZPOOL status >> $BAD_OUTPUT_FILE
  54.                  else
  55.                          echo "SOMETHING IS WRONG!"
  56.                          date > $BAD_OUTPUT_FILE
  57.                          $ZPOOL status >> $BAD_OUTPUT_FILE
  58.  
  59.                          set -- $MAILADDR
  60.                          for addr in $@
  61.                          do
  62.                                   send_mail $addr "ZFS ERROR on $(hostname)"
  63.                          done
  64.                  fi
  65.          fi
  66.  
  67.         echo "Removing PID file"
  68.         /bin/rm $PIDFILE
  69.  
  70. fi
  71.  
  72. #---additional steps:
  73. #Create a folder (or change the above path in /var/run/)
  74. #mkdir /var/run/zfs/
  75. #
  76. #chmod +x /PATH/TO/THIS/SCRIPT.sh
  77. #
  78. #crontab -e
  79. ##ZFS Disk failure check
  80. #*   *  *  *  *  /PATH/TO/THIS/SCRIPT.sh > /dev/null 2>&1
  81. #
  82. #---be sure that the user who executes the script can access the folder /var/run for PID file creation
  83. #---or just comment it out, if you don't want to crate a PID file
  84. #---zpool should obviously also be accessible for this user :)
  85. #
  86. #last update 2013-08-16
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement