Advertisement
DaywalkerGLXVR6

ZFS check script

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