szaydel
12/18/2017 - 10:28 PM

Capacity, Usage and Quotas on BrickstorOS

Snippets for getting information from ZFS in machine parseable form to help customers with their existing monitoring and data collection systems.

#!/bin/sh
# Copyright 2017 RackTop Systems Inc. and/or its affiliates.
# http://www.racktopsystems.com
#
# The methods and techniques utilized herein are considered TRADE SECRETS
# and/or CONFIDENTIAL unless otherwise noted. REPRODUCTION or DISTRIBUTION
# is FORBIDDEN, in whole and/or in part, except by express written permission
# of RackTop Systems.
#
###############################################################################
# Script: getquota.sh
###############################################################################
# Description: Obtains quota and refquota information for datasets listed in
# the $DATASETS list and reports infromation using machine-parceable CSV.
# The header is expected to print at all times, but there may not always be
# data following it.

DEBUG=0 # Flip this to >0 to enable debugging, not much to debug yet!

[ ${DEBUG} -gt 0 ] && set -o xtrace
ROOT=p01/global # Default root, should be changed based on pool name.
# These are paths relative to ${ROOT}. In other words real path might look
# like this: p01/global/afp/010.
DATASETS=( "${ROOT}/afp/00" "${ROOT}/nfs/00" "${ROOT}/smb/00/02" )
TRIMNAME=1 # If we want to trim all but the last part of zfs path set to 1.

# getDataFromDataset: Queries ZFS and obtains jason-formatted
# information for given dataset which contains only columns of
# interest to us. Data is properly arranged and printed to stdout.
function getDataFromDataset {
    [ ${DEBUG} -gt 0 ] && set -o xtrace
    
    typeset -A a
    dataset=$1
    result=`/usr/racktop/sbin/fsadm -j zp get \
      --fields name,used,avail,refquota,quota \
      -r \
      -f \
      ${dataset}`

    # This will happen if a given dataset name is not valid,
    # or not legal. We don't even want to attempt to print out
    # anything, since really we'll get commas.
    if [ "${result}" == "null" ] ; then return ; fi

    # There are 5 columns in output. We want to create a hash/map
    # of names of those columns to their values.
    for (( i = 0 ; i < 5 ; i++ )) ; do
        name=`jq -r ".[$i].Name" <<< "${result}"`
        value=`jq -r ".[$i].Value" <<< "${result}"`
        if [[ $name == "name" && ${TRIMNAME} -eq 1 ]]; then
            value=`basename ${value}`
        fi
        a[${name}]=${value}
    done

    # Print values in predictable order, separated with commas,
    # terminated with a newline `\n`.
    printf "%s,%s,%s,%s,%s\n" \
        ${a["name"]} ${a["used"]} ${a["avail"]} \
        ${a["refquota"]} ${a["quota"]}
}

echo "NAME,USED,AVAIL,REFQUOTA,QUOTA"
# For each valid dataset listed in the DATASETS list, print out
# a line, CSV-formatted containing quota information.
for ds in ${DATASETS[@]}; do
    getDataFromDataset ${ds}
done