mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-28 08:51:44 +00:00
Add --version and --help to pt-sift.
This commit is contained in:
509
bin/pt-sift
509
bin/pt-sift
@@ -4,15 +4,54 @@
|
||||
# See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal
|
||||
# notices and disclaimers.
|
||||
|
||||
usage() {
|
||||
if [ "${OPT_ERR}" ]; then
|
||||
echo "Error: $OPT_ERR" >&2
|
||||
fi
|
||||
echo "Usage: pt-sift FILE|PREFIX|DIRECTORY" >&2
|
||||
echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
|
||||
# ###########################################################################
|
||||
# log_warn_die package
|
||||
# This package is a copy without comments from the original. The original
|
||||
# with comments and its test file can be found in the Bazaar repository at,
|
||||
# lib/bash/log_warn_die.sh
|
||||
# t/lib/bash/log_warn_die.sh
|
||||
# See https://launchpad.net/percona-toolkit for more information.
|
||||
# ###########################################################################
|
||||
|
||||
|
||||
set -u
|
||||
|
||||
PTFUNCNAME=""
|
||||
PTDEBUG="${PTDEBUG:-""}"
|
||||
EXIT_STATUS=0
|
||||
|
||||
ts() {
|
||||
TS=$(date +%F-%T | tr ':-' '_')
|
||||
echo "$TS $*"
|
||||
}
|
||||
|
||||
info() {
|
||||
[ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
|
||||
}
|
||||
|
||||
log() {
|
||||
[ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
|
||||
}
|
||||
|
||||
warn() {
|
||||
[ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
|
||||
EXIT_STATUS=1
|
||||
}
|
||||
|
||||
die() {
|
||||
ts "$*" >&2
|
||||
EXIT_STATUS=1
|
||||
exit 1
|
||||
}
|
||||
|
||||
_d () {
|
||||
[ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
|
||||
}
|
||||
|
||||
# ###########################################################################
|
||||
# End log_warn_die package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# tmpdir package
|
||||
# This package is a copy without comments from the original. The original
|
||||
@@ -54,6 +93,402 @@ rm_tmpdir() {
|
||||
# End tmpdir package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# parse_options package
|
||||
# This package is a copy without comments from the original. The original
|
||||
# with comments and its test file can be found in the Bazaar repository at,
|
||||
# lib/bash/parse_options.sh
|
||||
# t/lib/bash/parse_options.sh
|
||||
# See https://launchpad.net/percona-toolkit for more information.
|
||||
# ###########################################################################
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
set -u
|
||||
|
||||
ARGV="" # Non-option args (probably input files)
|
||||
EXT_ARGV="" # Everything after -- (args for an external command)
|
||||
HAVE_EXT_ARGV="" # Got --, everything else is put into EXT_ARGV
|
||||
OPT_ERRS=0 # How many command line option errors
|
||||
OPT_VERSION="" # If --version was specified
|
||||
OPT_HELP="" # If --help was specified
|
||||
PO_DIR="" # Directory with program option spec files
|
||||
|
||||
usage() {
|
||||
local file="$1"
|
||||
|
||||
local usage="$(grep '^Usage: ' "$file")"
|
||||
echo $usage
|
||||
echo
|
||||
echo "For more information, 'man $TOOL' or 'perldoc $file'."
|
||||
}
|
||||
|
||||
usage_or_errors() {
|
||||
local file="$1"
|
||||
|
||||
if [ "$OPT_VERSION" ]; then
|
||||
local version=$(grep '^pt-[^ ]\+ [0-9]' "$file")
|
||||
echo "$version"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ "$OPT_HELP" ]; then
|
||||
usage "$file"
|
||||
echo
|
||||
echo "Command line options:"
|
||||
echo
|
||||
perl -e '
|
||||
use strict;
|
||||
use warnings FATAL => qw(all);
|
||||
my $lcol = 20; # Allow this much space for option names.
|
||||
my $rcol = 80 - $lcol; # The terminal is assumed to be 80 chars wide.
|
||||
my $name;
|
||||
while ( <> ) {
|
||||
my $line = $_;
|
||||
chomp $line;
|
||||
if ( $line =~ s/^long:/ --/ ) {
|
||||
$name = $line;
|
||||
}
|
||||
elsif ( $line =~ s/^desc:// ) {
|
||||
$line =~ s/ +$//mg;
|
||||
my @lines = grep { $_ }
|
||||
$line =~ m/(.{0,$rcol})(?:\s+|\Z)/g;
|
||||
if ( length($name) >= $lcol ) {
|
||||
print $name, "\n", (q{ } x $lcol);
|
||||
}
|
||||
else {
|
||||
printf "%-${lcol}s", $name;
|
||||
}
|
||||
print join("\n" . (q{ } x $lcol), @lines);
|
||||
print "\n";
|
||||
}
|
||||
}
|
||||
' "$PO_DIR"/*
|
||||
echo
|
||||
echo "Options and values after processing arguments:"
|
||||
echo
|
||||
(
|
||||
cd "$PO_DIR"
|
||||
for opt in *; do
|
||||
local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)"
|
||||
eval local varvalue=\$$varname
|
||||
if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then
|
||||
if [ "$varvalue" -a "$varvalue" = "yes" ];
|
||||
then varvalue="TRUE"
|
||||
else
|
||||
varvalue="FALSE"
|
||||
fi
|
||||
fi
|
||||
printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}"
|
||||
echo
|
||||
done
|
||||
)
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ $OPT_ERRS -gt 0 ]; then
|
||||
echo
|
||||
usage "$file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
option_error() {
|
||||
local err="$1"
|
||||
OPT_ERRS=$(($OPT_ERRS + 1))
|
||||
echo "$err" >&2
|
||||
}
|
||||
|
||||
parse_options() {
|
||||
local file="$1"
|
||||
shift
|
||||
|
||||
ARGV=""
|
||||
EXT_ARGV=""
|
||||
HAVE_EXT_ARGV=""
|
||||
OPT_ERRS=0
|
||||
OPT_VERSION=""
|
||||
OPT_HELP=""
|
||||
PO_DIR="$PT_TMPDIR/po"
|
||||
|
||||
if [ ! -d "$PO_DIR" ]; then
|
||||
mkdir "$PO_DIR"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Cannot mkdir $PO_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -rf "$PO_DIR"/*
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Cannot rm -rf $PO_DIR/*" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
_parse_pod "$file" # Parse POD into program option (po) spec files
|
||||
_eval_po # Eval po into existence with default values
|
||||
|
||||
if [ $# -ge 2 ] && [ "$1" = "--config" ]; then
|
||||
shift # --config
|
||||
local user_config_files="$1"
|
||||
shift # that ^
|
||||
local IFS=","
|
||||
for user_config_file in $user_config_files; do
|
||||
_parse_config_files "$user_config_file"
|
||||
done
|
||||
else
|
||||
_parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf"
|
||||
fi
|
||||
|
||||
_parse_command_line "${@:-""}"
|
||||
}
|
||||
|
||||
_parse_pod() {
|
||||
local file="$1"
|
||||
|
||||
cat "$file" | PO_DIR="$PO_DIR" perl -ne '
|
||||
BEGIN { $/ = ""; }
|
||||
next unless $_ =~ m/^=head1 OPTIONS/;
|
||||
while ( defined(my $para = <>) ) {
|
||||
last if $para =~ m/^=head1/;
|
||||
chomp;
|
||||
if ( $para =~ m/^=item --(\S+)/ ) {
|
||||
my $opt = $1;
|
||||
my $file = "$ENV{PO_DIR}/$opt";
|
||||
open my $opt_fh, ">", $file or die "Cannot open $file: $!";
|
||||
print $opt_fh "long:$opt\n";
|
||||
$para = <>;
|
||||
chomp;
|
||||
if ( $para =~ m/^[a-z ]+:/ ) {
|
||||
map {
|
||||
chomp;
|
||||
my ($attrib, $val) = split(/: /, $_);
|
||||
print $opt_fh "$attrib:$val\n";
|
||||
} split(/; /, $para);
|
||||
$para = <>;
|
||||
chomp;
|
||||
}
|
||||
my ($desc) = $para =~ m/^([^?.]+)/;
|
||||
print $opt_fh "desc:$desc.\n";
|
||||
close $opt_fh;
|
||||
}
|
||||
}
|
||||
last;
|
||||
'
|
||||
}
|
||||
|
||||
_eval_po() {
|
||||
local IFS=":"
|
||||
for opt_spec in "$PO_DIR"/*; do
|
||||
local opt=""
|
||||
local default_val=""
|
||||
local neg=0
|
||||
local size=0
|
||||
while read key val; do
|
||||
case "$key" in
|
||||
long)
|
||||
opt=$(echo $val | sed 's/-/_/g' | tr '[:lower:]' '[:upper:]')
|
||||
;;
|
||||
default)
|
||||
default_val="$val"
|
||||
;;
|
||||
"short form")
|
||||
;;
|
||||
type)
|
||||
[ "$val" = "size" ] && size=1
|
||||
;;
|
||||
desc)
|
||||
;;
|
||||
negatable)
|
||||
if [ "$val" = "yes" ]; then
|
||||
neg=1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Invalid attribute in $opt_spec: $line" >&2
|
||||
exit 1
|
||||
esac
|
||||
done < "$opt_spec"
|
||||
|
||||
if [ -z "$opt" ]; then
|
||||
echo "No long attribute in option spec $opt_spec" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $neg -eq 1 ]; then
|
||||
if [ -z "$default_val" ] || [ "$default_val" != "yes" ]; then
|
||||
echo "Option $opt_spec is negatable but not default: yes" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $size -eq 1 -a -n "$default_val" ]; then
|
||||
default_val=$(size_to_bytes $default_val)
|
||||
fi
|
||||
|
||||
eval "OPT_${opt}"="$default_val"
|
||||
done
|
||||
}
|
||||
|
||||
_parse_config_files() {
|
||||
|
||||
for config_file in "${@:-""}"; do
|
||||
test -f "$config_file" || continue
|
||||
|
||||
while read config_opt; do
|
||||
|
||||
echo "$config_opt" | grep '^[ ]*[^#]' >/dev/null 2>&1 || continue
|
||||
|
||||
config_opt="$(echo "$config_opt" | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')"
|
||||
|
||||
[ "$config_opt" = "" ] && continue
|
||||
|
||||
if ! [ "$HAVE_EXT_ARGV" ]; then
|
||||
config_opt="--$config_opt"
|
||||
fi
|
||||
|
||||
_parse_command_line "$config_opt"
|
||||
|
||||
done < "$config_file"
|
||||
|
||||
HAVE_EXT_ARGV="" # reset for each file
|
||||
|
||||
done
|
||||
}
|
||||
|
||||
_parse_command_line() {
|
||||
local opt=""
|
||||
local val=""
|
||||
local next_opt_is_val=""
|
||||
local opt_is_ok=""
|
||||
local opt_is_negated=""
|
||||
local real_opt=""
|
||||
local required_arg=""
|
||||
local spec=""
|
||||
|
||||
for opt in "${@:-""}"; do
|
||||
if [ "$opt" = "--" -o "$opt" = "----" ]; then
|
||||
HAVE_EXT_ARGV=1
|
||||
continue
|
||||
fi
|
||||
if [ "$HAVE_EXT_ARGV" ]; then
|
||||
if [ "$EXT_ARGV" ]; then
|
||||
EXT_ARGV="$EXT_ARGV $opt"
|
||||
else
|
||||
EXT_ARGV="$opt"
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "$next_opt_is_val" ]; then
|
||||
next_opt_is_val=""
|
||||
if [ $# -eq 0 ] || [ $(expr "$opt" : "\-") -eq 1 ]; then
|
||||
option_error "$real_opt requires a $required_arg argument"
|
||||
continue
|
||||
fi
|
||||
val="$opt"
|
||||
opt_is_ok=1
|
||||
else
|
||||
if [ $(expr "$opt" : "\-") -eq 0 ]; then
|
||||
if [ -z "$ARGV" ]; then
|
||||
ARGV="$opt"
|
||||
else
|
||||
ARGV="$ARGV $opt"
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
|
||||
real_opt="$opt"
|
||||
|
||||
if $(echo $opt | grep '^--no[^-]' >/dev/null); then
|
||||
local base_opt=$(echo $opt | sed 's/^--no//')
|
||||
if [ -f "$PT_TMPDIR/po/$base_opt" ]; then
|
||||
opt_is_negated=1
|
||||
opt="$base_opt"
|
||||
else
|
||||
opt_is_negated=""
|
||||
opt=$(echo $opt | sed 's/^-*//')
|
||||
fi
|
||||
else
|
||||
if $(echo $opt | grep '^--no-' >/dev/null); then
|
||||
opt_is_negated=1
|
||||
opt=$(echo $opt | sed 's/^--no-//')
|
||||
else
|
||||
opt_is_negated=""
|
||||
opt=$(echo $opt | sed 's/^-*//')
|
||||
fi
|
||||
fi
|
||||
|
||||
if $(echo $opt | grep '^[a-z-][a-z-]*=' >/dev/null 2>&1); then
|
||||
val="$(echo $opt | awk -F= '{print $2}')"
|
||||
opt="$(echo $opt | awk -F= '{print $1}')"
|
||||
fi
|
||||
|
||||
if [ -f "$PT_TMPDIR/po/$opt" ]; then
|
||||
spec="$PT_TMPDIR/po/$opt"
|
||||
else
|
||||
spec=$(grep "^short form:-$opt\$" "$PT_TMPDIR"/po/* | cut -d ':' -f 1)
|
||||
if [ -z "$spec" ]; then
|
||||
option_error "Unknown option: $real_opt"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
required_arg=$(cat "$spec" | awk -F: '/^type:/{print $2}')
|
||||
if [ "$required_arg" ]; then
|
||||
if [ "$val" ]; then
|
||||
opt_is_ok=1
|
||||
else
|
||||
next_opt_is_val=1
|
||||
fi
|
||||
else
|
||||
if [ "$val" ]; then
|
||||
option_error "Option $real_opt does not take a value"
|
||||
continue
|
||||
fi
|
||||
if [ "$opt_is_negated" ]; then
|
||||
val=""
|
||||
else
|
||||
val="yes"
|
||||
fi
|
||||
opt_is_ok=1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$opt_is_ok" ]; then
|
||||
opt=$(cat "$spec" | grep '^long:' | cut -d':' -f2 | sed 's/-/_/g' | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
if grep "^type:size" "$spec" >/dev/null; then
|
||||
val=$(size_to_bytes $val)
|
||||
fi
|
||||
|
||||
eval "OPT_$opt"="'$val'"
|
||||
|
||||
opt=""
|
||||
val=""
|
||||
next_opt_is_val=""
|
||||
opt_is_ok=""
|
||||
opt_is_negated=""
|
||||
real_opt=""
|
||||
required_arg=""
|
||||
spec=""
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
size_to_bytes() {
|
||||
local size="$1"
|
||||
echo $size | perl -ne '%f=(B=>1, K=>1_024, M=>1_048_576, G=>1_073_741_824, T=>1_099_511_627_776); m/^(\d+)([kMGT])?/i; print $1 * $f{uc($2 || "B")};'
|
||||
}
|
||||
|
||||
# ###########################################################################
|
||||
# End parse_options package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# Global variables
|
||||
# ###########################################################################
|
||||
@@ -106,11 +541,6 @@ main() {
|
||||
# prefix. The outcome of this block of code should be that BASEDIR is the
|
||||
# directory where the files live, without a trailing slash; and PREFIX is
|
||||
# either empty or a timestamp, such as "2011_02_08_16_58_07".
|
||||
if [ $# -gt 1 ]; then
|
||||
OPT_ERR="Specify only one PREFIX or DIR"
|
||||
usage
|
||||
fi
|
||||
|
||||
if [ $# -eq 1 ]; then
|
||||
if [ -d "$1" ]; then
|
||||
BASEDIR="$1"
|
||||
@@ -147,9 +577,6 @@ main() {
|
||||
fi
|
||||
done
|
||||
|
||||
# Make a secure tmpdir.
|
||||
mk_tmpdir
|
||||
|
||||
# We need to generate a list of timestamps, and ask the user to choose one if
|
||||
# there is no PREFIX yet. NOTE: we rely on the "-df" files here.
|
||||
(
|
||||
@@ -580,15 +1007,31 @@ main() {
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
rm_tmpdir
|
||||
}
|
||||
|
||||
# Execute the program if it was not included from another file. This makes it
|
||||
# possible to include without executing, and thus test.
|
||||
if [ "${0##*/}" = "$TOOL" ] \
|
||||
|| [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
|
||||
main "${@:-""}"
|
||||
|
||||
mk_tmpdir
|
||||
|
||||
parse_options "$0" "${@:-""}"
|
||||
if [ -z "$OPT_HELP" -a -z "$OPT_VERSION" ]; then
|
||||
if [ $# -gt 1 ]; then
|
||||
option_error "Specify only one PREFIX or DIR"
|
||||
fi
|
||||
fi
|
||||
usage_or_errors "$0"
|
||||
po_status=$?
|
||||
if [ $po_status -ne 0 ]; then
|
||||
[ $OPT_ERRS -gt 0 ] && exit 1
|
||||
exit 0
|
||||
fi
|
||||
|
||||
main "${@:-""}"
|
||||
|
||||
rm_tmpdir
|
||||
fi
|
||||
|
||||
# ############################################################################
|
||||
@@ -644,47 +1087,47 @@ you can use are as follows:
|
||||
|
||||
=over
|
||||
|
||||
=item d
|
||||
=item * d
|
||||
|
||||
Sets the action to start the L<pt-diskstats> tool on the sample's disk
|
||||
performance statistics.
|
||||
|
||||
=item i
|
||||
=item * i
|
||||
|
||||
Sets the action to view the first INNODB STATUS sample in less.
|
||||
|
||||
=item m
|
||||
=item * m
|
||||
|
||||
Displays the first 4 samples of SHOW STATUS counters side by side with the
|
||||
L<pt-mext> tool.
|
||||
|
||||
=item n
|
||||
=item * n
|
||||
|
||||
Summarizes the first sample of netstat data in two ways: by originating host,
|
||||
and by connection state.
|
||||
|
||||
=item j
|
||||
=item * j
|
||||
|
||||
Select the next timestamp as the active sample.
|
||||
|
||||
=item k
|
||||
=item * k
|
||||
|
||||
Select the previous timestamp as the active sample.
|
||||
|
||||
=item q
|
||||
=item * q
|
||||
|
||||
Quit the program.
|
||||
|
||||
=item 1
|
||||
=item * 1
|
||||
|
||||
Sets the action for each sample to the default, which is to view a summary
|
||||
of the sample.
|
||||
|
||||
=item 0
|
||||
=item * 0
|
||||
|
||||
Sets the action to just list the files in the sample.
|
||||
|
||||
=item *
|
||||
=item * *
|
||||
|
||||
Sets the action to view all of the sample's files in the less program.
|
||||
|
||||
@@ -692,7 +1135,17 @@ Sets the action to view all of the sample's files in the less program.
|
||||
|
||||
=head1 OPTIONS
|
||||
|
||||
This tool does not have any command-line options.
|
||||
=over
|
||||
|
||||
=item --help
|
||||
|
||||
Show help and exit.
|
||||
|
||||
=item --version
|
||||
|
||||
Show version and exit.
|
||||
|
||||
=back
|
||||
|
||||
=head1 ENVIRONMENT
|
||||
|
||||
|
Reference in New Issue
Block a user