Advertisement
Biduleohm

ZPool report

Mar 4th, 2015
4,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.01 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ### Parameters ###
  4. logfile="/tmp/zpool_report.tmp"
  5. email="your_email@gmail.com"
  6. subject="ZPool Status Report for FreeNAS"
  7. pools="freenas-boot tank"
  8. usedWarn=75
  9. usedCrit=90
  10. scrubAgeWarn=30
  11. warnSymbol="?"
  12. critSymbol="!"
  13.  
  14. ### Set email headers ###
  15. (
  16.     echo "To: ${email}"
  17.     echo "Subject: ${subject}"
  18.     echo "Content-Type: text/html"
  19.     echo "MIME-Version: 1.0"
  20.     echo -e "\r\n"
  21. ) > "$logfile"
  22.  
  23. ### Set email body ###
  24. echo "<pre style=\"font-size:14px\">" >> "$logfile"
  25.  
  26. ###### summary ######
  27. (
  28.     echo ""
  29.     echo "########## ZPool status report summary for all pools ##########"
  30.     echo ""
  31.     echo "+--------------+--------+------+------+------+----+--------+------+-----+"
  32.     echo "|Pool Name     |Status  |Read  |Write |Cksum |Used|Scrub   |Scrub |Last |"
  33.     echo "|              |        |Errors|Errors|Errors|    |Repaired|Errors|Scrub|"
  34.     echo "|              |        |      |      |      |    |Bytes   |      |Age  |"
  35.     echo "+--------------+--------+------+------+------+----+--------+------+-----+"
  36. ) >> "$logfile"
  37. for pool in $pools; do
  38.     status="$(zpool list -H -o health "$pool")"
  39.     errors="$(zpool status "$pool" | egrep "(ONLINE|DEGRADED|FAULTED|UNAVAIL|REMOVED)[ \t]+[0-9]+")"
  40.    readErrors=0
  41.    for err in $(echo "$errors" | awk '{print $3}'); do
  42.        if echo "$err" | egrep -q "[^0-9]+"; then
  43.            readErrors=1000
  44.            break
  45.        fi
  46.        readErrors=$((readErrors + err))
  47.    done
  48.    writeErrors=0
  49.    for err in $(echo "$errors" | awk '{print $4}'); do
  50.        if echo "$err" | egrep -q "[^0-9]+"; then
  51.            writeErrors=1000
  52.            break
  53.        fi
  54.        writeErrors=$((writeErrors + err))
  55.    done
  56.    cksumErrors=0
  57.    for err in $(echo "$errors" | awk '{print $5}'); do
  58.        if echo "$err" | egrep -q "[^0-9]+"; then
  59.            cksumErrors=1000
  60.            break
  61.        fi
  62.        cksumErrors=$((cksumErrors + err))
  63.    done
  64.    if [ "$readErrors" -gt 999 ]; then readErrors=">1K"; fi
  65.    if [ "$writeErrors" -gt 999 ]; then writeErrors=">1K"; fi
  66.    if [ "$cksumErrors" -gt 999 ]; then cksumErrors=">1K"; fi
  67.    used="$(zpool list -H -p -o capacity "$pool")"
  68.    scrubRepBytes="N/A"
  69.    scrubErrors="N/A"
  70.    scrubAge="N/A"
  71.    if [ "$(zpool status "$pool" | grep "scan" | awk '{print $2}')" = "scrub" ]; then
  72.        scrubRepBytes="$(zpool status "$pool" | grep "scan" | awk '{print $4}')"
  73.        scrubErrors="$(zpool status "$pool" | grep "scan" | awk '{print $8}')"
  74.        scrubDate="$(zpool status "$pool" | grep "scan" | awk '{print $15"-"$12"-"$13"_"$14}')"
  75.        scrubTS="$(date -j -f "%Y-%b-%e_%H:%M:%S" "$scrubDate" "+%s")"
  76.        currentTS="$(date "+%s")"
  77.        scrubAge=$((((currentTS - scrubTS) + 43200) / 86400))
  78.    fi
  79.    if [ "$status" = "FAULTED" ] \
  80.    || [ "$used" -gt "$usedCrit" ] \
  81.    || ( [ "$scrubErrors" != "N/A" ] && [ "$scrubErrors" != "0" ] )
  82.    then
  83.        symbol="$critSymbol"
  84.    elif [ "$status" != "ONLINE" ] \
  85.    || [ "$readErrors" != "0" ] \
  86.    || [ "$writeErrors" != "0" ] \
  87.    || [ "$cksumErrors" != "0" ] \
  88.    || [ "$used" -gt "$usedWarn" ] \
  89.    || [ "$scrubRepBytes" != "0" ] \
  90.    || [ "$(echo "$scrubAge" | awk '{print int($1)}')" -gt "$scrubAgeWarn" ]
  91.    then
  92.        symbol="$warnSymbol"
  93.    else
  94.        symbol=" "
  95.    fi
  96.    (
  97.        printf "|%-12s %1s|%-8s|%6s|%6s|%6s|%3s%%|%8s|%6s|%5s|\n" \
  98.        "$pool" "$symbol" "$status" "$readErrors" "$writeErrors" "$cksumErrors" \
  99.        "$used" "$scrubRepBytes" "$scrubErrors" "$scrubAge"
  100.    ) >> "$logfile"
  101. done
  102. (
  103.    echo "+--------------+--------+------+------+------+----+--------+------+-----+"
  104.    echo ""
  105.    echo ""
  106. ) >> "$logfile"
  107.  
  108. ###### for each pool ######
  109. for pool in $pools; do
  110.    (
  111.        echo ""
  112.        echo "########## ZPool status report for ${pool} ##########"
  113.         echo ""
  114.         zpool status -v "$pool"
  115.         echo ""
  116.         echo ""
  117.     ) >> "$logfile"
  118. done
  119.  
  120. echo "</pre>" >> "$logfile"
  121.  
  122. ### Send report ###
  123. sendmail -t < "$logfile"
  124. rm "$logfile"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement