-
Notifications
You must be signed in to change notification settings - Fork 0
/
mext2
executable file
·251 lines (214 loc) · 6.49 KB
/
mext2
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
#!/bin/bash
# This program is part of Aspersa (http://code.google.com/p/aspersa/)
################################################################################
# mext2 - Tool to see columnized output of mysqladmin. See "mext". This is a
# version of "mext" that doesn't rely on some system-specific things. But it is
# less convenient than "mext" for some purposes, too.
#
# @author Istvan Podor <[email protected]>
################################################################################
usage() {
echo -en "Usage: $0 c=N i=N [r=true] [ma=/path/to/mysqladmin and options]\n"
echo -en " c=N Count: number of iterations to make.\n"
echo -en " i=N Sleep between iterations.\n"
echo -en " r=[true|false] Relative: subtract each column from the previous column.\n"
echo -en " \"ma=[command]\" mysqladmin command and parameters.\n\n"
echo -en "mext columnizes repeated output from mysqladmin extended.\n"
echo -en "Example: 'mext c=3 i=5 r=true \"ma=/usr/bin/mysql50/bin/mysqladmin ext -S /var/sock/mysql.sock\"'\n"
echo -en "Pay attention for the quotation marks (\") before and after the mysqladmin definition!\n"
exit 1
}
if [ -z "$1" ]; then
usage;
fi
# predef some variables
FILE=/tmp/mext_temp_file; # work file prefix
NUM=0; # Counter
REL=0; # subtract columns from each other?
STOP=0; # how many times to run
SLEEP=0; # how many seconds to sleep between
c=0; # counts
i=0; # sleeps
ma="" # myadm
COL="" #empty
IS_DEF='' # if mysqladmin defined
MAXT=0 # max runtime
rm -f $FILE*; #clean up on startup
# exporting definied arguments
export_arguments() {
# if mysqladmin is defined, that means it should run with special arguments
# ma argument cannot be exported that easy because of the space and other special characters
# this is why this part needed
IS_DEF=`echo $@ | grep -ic mysqladmin`
ALL_ARG=$#
# if there is mysqladmin in the command line, stop exporting arguments before the last element
if [[ IS_DEF -ge 1 ]] || [ "$4" == "" ];then
STOP_ARG_PARS=`expr $# - 1`
else
STOP_ARG_PARS=$ALL_ARG
fi
# This while exports each argument
CNT=0
ARG_ARR=( $@ )
while [[ $CNT -lt $STOP_ARG_PARS ]];do
export "${ARG_ARR[$CNT]}"
let CNT=CNT+1
done
#lets start from the back to find mysqladmin...
# this part is only needed to avoid throwing of errors if ma= undef
ALL_MAX_ARG=4
SEE_IF_4_IS_MADM=`echo $4 | cut -d'=' -f2 | cut -d' ' -f1 | grep -ic mysql`
if [[ $SEE_IF_4_IS_MADM -ne 1 ]]; then
export "$3"
#need to pass the test?
if [ "$4" != "" ];then export "$4"; fi
else
export "$4"
fi
SLEEP=$i
STOP=$c
COL=$r
}
# Pre-condition checks
pre_cond_checks() {
# if sleep time not given or >5min, exit
if [[ $SLEEP -eq 0 ]] || [[ $SLEEP -gt 360 ]];then
echo "Invalid sleep time given, should be beetween 1-360!"
usage;
fi
if [[ $STOP -lt 1 ]]; then
echo "You don't want me to run no times?.. Weird.."
usage;
fi
#this isn't that beauty, but this is just a bash script and we need to handle tests somehow
case "$COL" in
truetest)
REL="1"
IS_DEF=5
;;
falsetest)
REL="0"
IS_DEF=5
;;
true)
REL="1"
;;
false)
REL="0"
;;
esac
## MAX RUNTIME = STOPS * SLEEP +2seconds safedelay
let MAXT=STOP*SLEEP+1
}
#re-architecturing
find_mysqladmin() {
if [[ $IS_DEF -lt 1 ]];then
ma=`which mysqladmin`
if [[ $? -gt 0 ]]; then
echo "Can't find mysqladmin!"
exit 1
fi
fi
}
proceed_output() {
if [[ $IS_DEF -gt 1 ]];then
$1 | grep -v '+' | grep -v Variable_name | sed 's/|//g' >${FILE}large &
else
$1 ext -c$2 -i$3 | grep -v '+' | grep -v Variable_name | sed 's/|//g' >${FILE}large &
fi
}
organize_output() {
cat ${FILE}large | while read line; do
if [ "$line" == "" ]; then
NUM=`expr $NUM + 1`;
echo "" > $FILE$NUM;
fi
echo "$line" >> $FILE$NUM;
done
}
# daycare to take care of the processes living in the background
daycare() {
# if it runs for too many times, we have to stop him whatever he is doing ..
JOBS=`jobs -r |grep -c .`
while [[ $JOBS -gt 0 ]]; do
CURR_DATE=`date +%s`
let CURR_DATE=CURR_DATE-START_PROCEED
if [[ $CURR_DATE -lt $MAXT ]];then
sleep 1
JOBS=`jobs -r | grep -c .`;
else
echo -en "\nForce quitting of mysqladmin..."
killall -9 mysqladmin 1>/dev/null 2>/dev/null
echo -en "done\n"
break;
fi
done
}
seq_couz_fun() {
START=$1
STOP=$2
RETVAL=( $START )
if [[ $START -lt $STOP ]];then
#go for it
while [[ $START -le $STOP ]]; do
echo -en "$START "
let START=START+1
# RETVAL=( ${ARR[*]} $START)
done
else
RETVAL=1
fi
return $RETVAL
}
format_and_print_output() {
# Count how many files there are and prepare to format the output
SPEC="%-33s %13d"
AWKS=""
NUM=`ls $FILE* |grep -v large| wc -l`;
# The last file will be empty...
NUM=`expr $NUM - 3`;
if [[ `cat ${FILE}large | wc -l` -lt 1 ]];then
echo "Something goes wrong! Result file EMPTY!"
exit 1
fi
# Join each file with the next file, joining on the first field. Build a printf
# spec and awk spec at the same time.
COUNTARR=( `seq_couz_fun 0 $NUM` )
for i in `echo ${COUNTARR[*]}`; do
NEXTFILE=`expr $i + 1`;
join $FILE$i $FILE${NEXTFILE} | grep . > $FILE;
# Find the max length of the [numeric only] values in the file so we know how
# wide to make the columns
MAXLEN=`awk '{print $2}' $FILE${NEXTFILE} | grep -v '[^0-9]' | awk '{print length($1)}' | sort -rn | head -n1`
mv $FILE $FILE${NEXTFILE};
SPEC="$SPEC %${MAXLEN}d";
if [ "$REL" = "1" ]; then
AWKS="$AWKS, \$`expr $i + 3` - \$`expr $i + 2`";
else
AWKS="$AWKS, \$`expr $i + 3`";
fi
done
# Print output
AWKCMD="printf(\"$SPEC\n\", \$1, \$2$AWKS);";
awk "{$AWKCMD}" $FILE`expr $NUM + 1`;
}
###### WORKFLOW ######
if [ -z "$1" ]; then usage; fi
# export each arguents, export used to become more platform independent
export_arguments "$1" "$2" "$3" "$4";
# lets see if everything defined well
pre_cond_checks;
# lets see where is it
find_mysqladmin;
#this defines the timestamp when we started
START_PROCEED=`date +%s`
# call mysqladmin
proceed_output "$ma" "$STOP" "$SLEEP"
# young child goes to daycare, old got killed
daycare;
# lets prepare the result for printing
organize_output;
# and now, give it to them! :)
format_and_print_output;
# Remove all temporary files.
#rm -f $FILE*;