This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
MySQL_Script_Utils.pm
211 lines (159 loc) · 4.73 KB
/
MySQL_Script_Utils.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (c) 2008 Yahoo! Inc. All rights reserved. The copyrights
# embodied in the content of this file are licensed by Yahoo! Inc.
# under the BSD (revised) open source license
package MySQL_Script_Utils;
use strict;
use utf8;
use Exporter;
use Getopt::Long qw/ :config no_ignore_case /;
use vars qw/ $DEBUG $HELP $USER $PASS $HOST $PORT %DEFAULT_OPTIONS
$PASSWORD_ON @ISA @EXPORT $DEFAULT_OPTIONS_STRING
$LOGIN_PATH
/;
@ISA = qw/ Exporter /;
@EXPORT = qw/ $HOST &parse_options &print_debug &mysql_call
&format_number &format_percent &format_memory
&format_microseconds
$DEFAULT_OPTIONS_STRING
/;
$DEBUG = 0;
$HELP = 0;
$PASS = '';
warn "'mysql' binary not found in your \$PATH\n" if !`which mysql`;
$DEFAULT_OPTIONS_STRING = " [-d] [-?] [--login-path=<login_path>] [-u <user> [-p [<pass>]]] [-h <host>] [-P <port>]";
my %DEFAULT_OPTIONS = (
'help|?' => \$HELP,
'debug|d' => \$DEBUG,
'login-path=s' => \$LOGIN_PATH,
'host|h=s' => \$HOST,
'P=i' => \$PORT,
'user|u=s' => \$USER,
'p:s' => \$PASS,
);
sub print_debug {
return if( !$DEBUG );
my( $string ) = @_;
print STDERR "DEBUG: $string\n";
}
sub raw_format_number {
my( $units, $num, $sig, $max_len, $debug ) = @_;
$sig = 0 if( $sig < 0 );
print "Num: $num\n" if $debug;
foreach my $factor( sort keys %$units ) {
my $raw = $num / $factor;
my $string = sprintf( "%." . $sig . "f%s", $raw, $units->{$factor} );
print "Trying factor: $factor, $raw => $string\n" if $debug;
if( $raw != 0 and length( $string ) <= $max_len + $sig ) {
print "\tThese are our units\n" if $debug;
my $left = $max_len - length( $string );
if( $left < 0 ) {
print "\tcan we pare down the sig?\n" if $debug;
# Return a pared down $sig or what we've got (may not fit in $max_len)
$sig > 0 ?
return &raw_format_number( $units, $num, $sig - 1, $max_len, $debug ) :
return $string;
} elsif( $left > 1 and $factor ne 1 ) {
print "\tadd some decimal places\n" if $debug;
# Add some decimal places
my $decimal = $left - 1;
return sprintf( "%." . $decimal . "f" . $units->{$factor}, $raw );
} else {
print "\tas is\n" if $debug;
return $string;
}
}
# Else, try the next smaller factor
}
# if we get here, we have no factor
my $string = sprintf( "%." . $sig . "f", $num );
print "Using $string\n" if $debug;
if( length( $string ) <= $max_len ) {
return $string;
} else {
$sig > 0 ?
return &raw_format_number( $units, $num, $sig - 1, $max_len, $debug ) :
return $string;
}
}
sub format_number {
my %units = (
1 => '',
1000 => 'k',
1000000 => 'm',
1000000000 => 'g'
);
return &raw_format_number( \%units, @_ );
}
sub format_memory {
my %units = (
1 => 'b',
1024 => 'K',
1048576 => 'M',
1073741824 => 'G',
1099511627776 => 'T'
);
return &raw_format_number( \%units, @_ );
}
# Takes microsecons
sub format_microseconds {
my %units = (
1000000000 => 'ks',
1000000 => 's',
1000 => 'ms',
1 => 'µs',
);
return &raw_format_number( \%units, @_ );
}
sub format_percent {
my( $top, $bottom ) = @_;
return 0 if( $bottom == 0 );
my $raw = sprintf( "%.1f", ($top / $bottom) * 100 );
while( length( $raw ) > 4 ) {
chop $raw;
}
if( $raw =~ m/\.$/ ) {
chop $raw;
$raw = ' ' . $raw;
}
return "$raw%";
}
sub parse_options {
my( $options ) = @_;
$PASSWORD_ON = grep( /^-p$/, @ARGV );
my $opt_res = GetOptions( (%DEFAULT_OPTIONS, %$options) );
return 0 if( not $opt_res or $HELP );
return 1;
}
sub mysql_call {
my ( $sql, $host, $port ) = @_;
$host = $HOST unless defined $host;
$port = $PORT unless defined $port;
# Prompt for a password the first time we need it, and only if -p was
# given on the command line (could be passwordless)
if( $PASSWORD_ON and $PASS eq '' ) {
print "Password: ";
system "stty -echo"; $PASS =<STDIN>;
system "stty echo";
$PASS =~ s/\s+$//g; # filter training whitespace
print "\n";
}
my $command = "mysql";
if( defined( $LOGIN_PATH )) {
$command .= ' --login-path=' . $LOGIN_PATH if defined( $LOGIN_PATH );
# No other options should matter
} else {
$command .= ' --user=' . $USER if defined $USER;
$command .= ' \'--password=' . $PASS . '\'' if $PASS ne '';
$command .= ' --host=' . $host if defined $host;
$command .= ' --port=' . $port if defined $port;
}
$command .= " 2>&1";
&print_debug( "echo \"$sql\" | $command" );
my @output = grep( !/^Warning/, (`echo "$sql" | $command`));
my $rc = $? >> 8;
if( $rc ) {
die "Error accessing mysql\n";
}
return \@output;
}
1;