Advertisement
Biduleohm

ZPool report

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