Created
May 20, 2018 09:09
-
-
Save iamxy/18b651b021ce822b76ecb1b33b7ed5a8 to your computer and use it in GitHub Desktop.
A shell script to get and show disk space usage of tables in specified database of TiDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Copyright 2018 PingCAP, Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
PROG=${0##*/} | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
DB_HOST="127.0.0.1" | |
DB_PORT="4000" | |
DB_USER="root" | |
DB_PSWD="" | |
DB_SCHE="test" | |
function usage() { | |
cat 1>&2 <<-EOF | |
USAGE: $PROG [OPTIONS] | |
OPTIONS: | |
-h Connect to host of tidb-server. | |
-P Port number to use for connection, built-in default (4000). | |
-u User for login. | |
-p Password to use when connecting to tidb-server. | |
-D Database to use. | |
EOF | |
} | |
function err_exit() { | |
echo "ERROR: $1" 1>&2 | |
exit 1 | |
} | |
function pre_checks() { | |
which mysql >/dev/null 2>&1 || err_exit "mysql client not installed" | |
which curl >/dev/null 2>&1 || err_exit "curl not found" | |
which printf >/dev/null 2>&1 || err_exit "printf not found" | |
} | |
pre_checks | |
# main from here | |
while getopts ":h:P:u:D:p:" opt; do | |
case "$opt" in | |
h ) DB_HOST=$OPTARG;; | |
P ) DB_PORT=$OPTARG;; | |
u ) DB_USER=$OPTARG;; | |
D ) DB_SCHE=$OPTARG;; | |
p ) DB_PSWD="-p$OPTARG";; | |
\? ) usage; err_exit "Unknown option: -$OPTARG";; | |
: ) | |
if [[ $OPTARG == "p" ]]; then | |
echo -e "Enter password: \c" | |
read PSWD | |
DB_PSWD="-p$PSWD" | |
else | |
usage; err_exit "Missing option arugment for -$OPTARG" | |
fi | |
;; | |
* ) usage; exit 1;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
DB_VERSION=$(echo "select version()" | mysql -h $DB_HOST -P $DB_PORT -u $DB_USER $DB_PSWD -D $DB_SCHE | sed '1d') | |
[[ -z $DB_VERSION ]] && err_exit "connect to tidb-server($DB_HOST $DB_PORT $DB_USER $DB_SCHE) failed" | |
printf "## TiDB Version: %s\n" $DB_VERSION | |
printf "## DB Name: %s\n" $DB_SCHE | |
printf "\n%-48s %14s\n" "TABLE" "SPACE USAGE" | |
echo "-----------------------------------------------------------------" | |
show_tables=$(echo "show tables" | mysql -h $DB_HOST -P $DB_PORT -u $DB_USER $DB_PSWD -D $DB_SCHE | sed '1d') | |
for tbl in $show_tables; do | |
usage=$(curl -s http://$DB_HOST:10080/tables/$DB_SCHE/$tbl/disk-usage) | |
printf "%-48s %14s\n" $tbl "$usage MB" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment