Skip to content

Instantly share code, notes, and snippets.

@maxried
Last active December 10, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxried/aee794db69012ad13d38e633465f1ccb to your computer and use it in GitHub Desktop.
Save maxried/aee794db69012ad13d38e633465f1ccb to your computer and use it in GitHub Desktop.
#!/bin/sh
if [ "$#" -eq 0 ] ; then
cat <<EOF
{
"data":
[
{
$(for a in `dmsetup status | grep -E "^.+: [0-9]+ [0-9]+ cache .+$" | cut -d: -f1`; do echo " \"{#DEVICE_NAME}\": \"$a\"" ; done)
}
]
}
EOF
exit 0
elif [ "$#" -ne 1 ] ; then
echo "Usage: $0 <block-device-or-mapper-name> or $0" >&2
exit 1
fi
case $1 in
"/dev/"*) DEVICE=$1 ;;
*) DEVICE="/dev/mapper/$1" ;;
esac
if [ ! -r "$DEVICE" ] ; then
echo "$DEVICE could not be read" >&2
exit 1
fi
if [ ! -b "$DEVICE" ] ; then
echo "$DEVICE is no block device." >&2
exit 1
fi
RAW=$(dmsetup status "$DEVICE" 2> /dev/null)
if [ "$?" -ne 0 ] ; then
echo "$DEVICE could not be analyzed. Try running \"dmsetup status $DEVICE\" to see what's happening." >&2
exit 1
fi
set -- $RAW
if [ "$3" != "cache" ] ; then
echo "That is no cache device, but a $DEVICE_TYPE device." >&2
exit 1
fi
METADATA_BLOCK_SIZE=$4
USED_METADATA_BLOCKS=$(echo "$5" | cut -d/ -f 1)
TOTAL_METADATA_BLOCKS=$(echo "$5" | cut -d/ -f 2)
CACHE_BLOCK_SIZE=$6
USED_CACHE_BLOCKS=$(echo "$7" | cut -d/ -f 1)
TOTAL_CACHE_BLOCKS=$(echo "$7" | cut -d/ -f 2)
READ_HITS=$8
READ_MISSES=$9
WRITE_HITS=${10}
WRITE_MISSES=${11}
DEMOTIONS=${12}
PROMOTIONS=${13}
DIRTY=${14}
cat <<EOF
{
"metadata_block_size": $METADATA_BLOCK_SIZE,
"used_metadata_blocks": $USED_METADATA_BLOCKS,
"total_metadata_blocks": $TOTAL_METADATA_BLOCKS,
"cache_block_size": $CACHE_BLOCK_SIZE,
"used_cache_blocks": $USED_CACHE_BLOCKS,
"total_cache_blocks": $TOTAL_CACHE_BLOCKS,
"read_hits": $READ_HITS,
"read_misses": $READ_MISSES,
"write_hits": $WRITE_HITS,
"write_misses": $WRITE_MISSES,
"demotions": $DEMOTIONS,
"promotions": $PROMOTIONS,
"dirty": $DIRTY,
"needs_check": "$(eval echo "\${$#}")"
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment