forked from jayjanssen/myq_gadgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL_Script_Utils.pm
251 lines (200 loc) · 6.46 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# 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 Exporter;
use Getopt::Long qw/ :config no_ignore_case /;
use vars qw/ $DEBUG $HELP $USER $PASS $HOST $PORT $SOCKET %DEFAULT_OPTIONS
$PASSWORD_ON @ISA @EXPORT $DEFAULT_OPTIONS_STRING
/;
@ISA = qw/ Exporter /;
@EXPORT = qw/ $HOST &parse_options &print_debug &mysql_call
&format_number &format_percent &format_memory
$DEFAULT_OPTIONS_STRING
/;
$DEBUG = 0;
$HELP = 0;
$HOST = '';
die "'mysql' binary not found in your \$PATH\n" if !`which mysql`;
$DEFAULT_OPTIONS_STRING = " [-d] [-?] [-u user [-p [pass]]] [-h host] [-P <port>] [-S socket]";
my %DEFAULT_OPTIONS = (
'help|?' => \$HELP,
'debug|d' => \$DEBUG,
'host|h=s' => \$HOST,
'P=i' => \$PORT,
'socket|S=s' => \$SOCKET,
'user|u=s' => \$USER,
'p:s' => \$PASS,
);
sub print_debug {
return if( !$DEBUG );
my( $string ) = @_;
print STDERR "DEBUG: $string\n";
}
sub format_number {
my( $num, $sig, $max_len, $debug ) = @_;
# return 0 if( $num <= 0 );
my $format = "%." . $sig . "f";
my $raw_kilo = $num / 1000;
my $raw_mega = $raw_kilo / 1000;
my $raw_giga = $raw_mega / 1000;
my $kilo = sprintf( $format, $raw_kilo );
my $mega = sprintf( $format, $raw_mega );
my $giga = sprintf( $format, $raw_giga );
my $one = sprintf( $format, $num );
print "$giga, $mega, $kilo, $one\n" if( $debug );
if( $raw_giga >= 1 ) {
if( length( $giga ) < $max_len ) {
return $giga . 'g';
} else {
$sig > 0 ?
return &format_number( $num, $sig - 1, $max_len, $debug ) :
return $giga . 'g';
}
} elsif( $raw_mega >= 1 ) {
if( length( $mega ) < $max_len ) {
return $mega . 'm';
} else {
$sig > 0 ?
return &format_number( $num, $sig - 1, $max_len, $debug ) :
return $mega . 'm';
}
} elsif( $raw_kilo >= 1 ) {
if( length( $kilo ) < $max_len ) {
return $kilo . 'k';
} else {
$sig > 0 ?
return &format_number( $num, $sig - 1, $max_len, $debug ) :
return $kilo . 'k';
}
} else {
if( length( $one ) <= $max_len ) {
return $one;
} else {
$sig > 0 ?
return &format_number( $num, $sig - 1, $max_len, $debug ) :
return $one;
}
}
}
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 format_memory {
my( $num, $sig, $max_len, $debug ) = @_;
return 0 if( $num <= 0 );
my $format = "%." . $sig . "f";
my $raw_kilo = $num / (2**10);
my $raw_mega = $num / (2**20) ;
my $raw_giga = $num / (2**30);
my $raw_tera = $num / (2**40);
my $kilo = sprintf( $format, $raw_kilo );
my $mega = sprintf( $format, $raw_mega );
my $giga = sprintf( $format, $raw_giga );
my $tera = sprintf( $format, $raw_tera );
my $one = sprintf( $format, $num );
print "$tera, $giga, $mega, $kilo, $one\n" if( $debug );
if( $raw_tera >= 1 ) {
if( length( $tera ) < $max_len ) {
return $tera . 'T';
} else {
$sig > 0 ?
return &format_memory( $num, $sig - 1, $max_len, $debug ) :
return $tera . 'T';
}
} elsif( $raw_giga >= 1 ) {
if( length( $giga ) < $max_len ) {
return $giga . 'G';
} else {
$sig > 0 ?
return &format_memory( $num, $sig - 1, $max_len, $debug ) :
return $giga . 'G';
}
} elsif( $raw_mega >= 1 ) {
if( length( $mega ) < $max_len ) {
return $mega . 'M';
} else {
$sig > 0 ?
return &format_memory( $num, $sig - 1, $max_len, $debug ) :
return $mega . 'M';
}
} elsif( $raw_kilo >= 1 ) {
if( length( $kilo ) < $max_len ) {
return $kilo . 'K';
} else {
$sig > 0 ?
return &format_memory( $num, $sig - 1, $max_len, $debug ) :
return $kilo . 'K';
}
} else {
if( length( $one ) <= $max_len ) {
return $one;
} else {
$sig > 0 ?
return &format_memory( $num, $sig - 1, $max_len, $debug ) :
return $one;
}
}
}
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, $user, $pass, $host, $port, $socket ) = ('', '', '', '', '', '' );
( $sql, $host, $port, $socket ) = @_;
# 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";
}
if( $host eq '' ) {
$host = $HOST;
}
if( $port eq '' ) {
$port = $PORT;
}
if( $socket eq '') {
$socket = $SOCKET;
}
if( $user eq '' ) {
$user = $USER;
}
if( $pass eq '' ) {
$pass = $PASS;
}
my ( $connect_str, $user_str, $pass_str, $host_str, $port_str, $socket_str ) = ('', '', '', '', '', '', '' );
$user_str = ' --user=' . $user if( $user ne '' );
$pass_str = ' \'--password=' . $pass . '\'' if( $pass ne '' );
$host_str = ' --host=' . $host if( $host ne '' );
$port_str = ' --port=' . $port if( $port ne '' );
$socket_str = ' --socket=' . $socket if( $socket ne '' );
$connect_str = $socket_str;
$connect_str = join "", $user_str, $pass_str, $host_str, $port_str if( $socket eq '' );
&print_debug( "echo \"$sql\" | mysql $connect_str" );
my @output = `echo "$sql" | mysql $connect_str`;
my $rc = $? >> 8;
if( $rc ) {
die "Error accessing mysql\n";
}
return \@output;
}
1;