-
Notifications
You must be signed in to change notification settings - Fork 8
/
slashmon
executable file
·445 lines (374 loc) · 10.7 KB
/
slashmon
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/perl
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2004 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
use File::Basename;
use Getopt::Std;
use Slash::Utility;
use Slash::DB;
use Time::HiRes;
use strict;
use vars qw(
$PROGNAME $VERSION
%opts
$virtuser $width $height $sleep $min_pl_time
$weed_out_images
@abbreviations %abbreviations
$slashdb $constants $logdb
@db
$time_last $maxlog_last $maxcid_last
$time_first $maxlog_first $maxcid_first
$time_new $maxlog_new $maxcid_new
$queries $pages $comments
$queries_total $pages_total $comments_total
$loadavg $loadavg_sum $num_cycles
$lines_since_header
);
($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
init();
run();
exit 0;
############################################################
sub init {
$PROGNAME = basename($0);
usage('Options used incorrectly') unless getopts('hviu:w:r:s:t:', \%opts);
usage() if $opts{h};
version() if $opts{v};
$weed_out_images = $opts{i} ? 1 : 0;
$virtuser = $opts{u} || 'slash';
$width = $opts{w} || 106;
$height = $opts{r} || 20;
$sleep = $opts{s} || 10;
$min_pl_time = defined($opts{t}) ? $opts{t} : 1;
createEnvironment($virtuser);
# Why is the word "abbreviation" so long?
# And why do we drive on parkways, but park on driveways?
@abbreviations = qw( SELECT FROM WHERE INSERT DELETE UPDATE );
%abbreviations = map { ( $_, uc substr($_,0,1) ) } @abbreviations;
$slashdb = getCurrentDB();
$constants = getCurrentStatic();
my $vus_hr = {
map { ( $_, 1 ) }
@{$slashdb->sqlSelectColArrayref('virtual_user', 'dbs')}
};
my $vus = [ (
$slashdb->{virtual_user},
sort { $a cmp $b }
grep { $_ ne $slashdb->{virtual_user} }
keys %$vus_hr
) ];
my $log_db = $slashdb->getDBs()->{log}[0]{virtual_user} || '';
# Set up the @db array based on what virtual users we have.
@db = ( );
for my $vu (@$vus) {
my $db = getObject("Slash::DB", $vu);
my($version) = $db->sqlSelect("VERSION()") =~ /(\d)/;
push @db, {
vu => $vu,
db => $db,
version => $version,
};
}
# If no virtual users are defined, we have just the one.
if (!@db) {
my($version) = $slashdb->sqlSelect("VERSION()") =~ /(\d)/;
@db = ( {
vu => $virtuser,
db => $slashdb,
version => $version,
} );
}
# We already have a handle to the DB that writes accesslog, but let's get
# a separate copy in this special var for it.
$logdb = getObject('Slash::DB', { db_type => "log" });
$lines_since_header = 999999;
$num_cycles = 0;
$loadavg_sum = 0;
$pages_total = 0;
$comments_total = 0;
$queries_total = 0;
$maxlog_first = $logdb->sqlSelect("MAX(id)", "accesslog");
$maxlog_last = $maxlog_first;
$maxcid_first = $slashdb->getMaxCid();
$maxcid_last = $maxcid_first;
$time_first = Time::HiRes::time;
$time_last = $time_first;
for my $db (@db) {
$db->{status_query} = "SHOW STATUS";
$db->{innostatus_query} = "SHOW INNODB STATUS"
if $db->{version} == 4
&& $db->{vu} ne 'search'; # hack, hack
$db->{proclist_query} = "SHOW FULL PROCESSLIST";
($db->{queries_first}, $db->{slow_queries_first})
= ($db->{queries_last}, $db->{slow_queries_last})
= getQueries($db);
}
}
sub run {
# Drop the starting time onto the console just in case we later
# want to correlate times.
print scalar(gmtime) . "\n";
# And print the key for the hitrate column because it's not
# very intuitive.
print "InnoDB cache (h)itrate key: + >99% - 96-99% X <96% ? unknown\n";
while (1) {
print_header() if $lines_since_header > $height;
++$num_cycles;
sleep_until();
$time_new = Time::HiRes::time;
$maxlog_new = $logdb->sqlSelect("MAX(id)", "accesslog");
$maxcid_new = $slashdb->getMaxCid();
update_db_ary();
$loadavg = getLoadavg();
$loadavg_sum += $loadavg;
print_first_line($time_new);
print_second_line($time_new);
print_proclist();
print "\n"; ++$lines_since_header;
$time_last = $time_new;
$maxcid_last = $maxcid_new;
$maxlog_last = $maxlog_new;
for my $db (@db) {
$db->{queries_last} = $db->{queries_new};
$db->{slow_queries_last} = $db->{slow_queries_new};
}
}
}
############################################################
sub print_header {
# First line of header: spaces over the global stuff, headers for DBs
printf("%4s %4s %5s %4s %4s", "", "", "", "", "");
for my $db (@db) {
my $wid = 14;
$wid += 2 if $db->{innostatus_query};
my $name = $db->{vu};
my $len = length($name);
print join("",
" ",
"-" x (int(($wid-$len-1)/2)),
$name,
"-" x (int(($wid-$len-0)/2)));
}
print "\n";
# Second line of header: names for the globals, names for DB columns
printf("%4s %4s %5s %4s %4s", "time", "load", "cps", "pps", "qpp");
for my $db (@db) {
printf(" %4s %3s %4s", "qps", "sps", "qpp");
print " h" if $db->{innostatus_query};
}
print "\n";
$lines_since_header = 0;
}
sub sleep_until {
my $sleep_time = $sleep * $num_cycles - (Time::HiRes::time - $time_first);
return if $sleep_time <= 0;
Time::HiRes::sleep($sleep_time);
}
sub update_db_ary {
for my $db (@db) {
($db->{queries_new}, $db->{slow_queries_new}) = getQueries($db);
($db->{hitrate}) = getHitRate($db) if $db->{innostatus_query};
getProclist($db);
}
}
sub print_first_line {
# First line: stats for the last $time_elapsed seconds
if ($weed_out_images) {
$pages = $logdb->sqlCount("accesslog",
"id BETWEEN " . ($maxlog_last+1) . " AND $maxlog_new"
. " AND op != 'image'");
} else {
$pages = $maxlog_new - $maxlog_last;
}
$pages_total += $pages;
$comments = $maxcid_new - $maxcid_last;
$comments_total = $maxcid_new - $maxcid_first;
$queries = 0;
for my $db (@db) {
$queries += $db->{queries_new} - $db->{queries_last};
}
$queries_total += $queries;
my $time_elapsed = $time_new - $time_last;
$time_elapsed = 0.01 if $time_elapsed < 0.01;
printf("%4s %4.2f %5.2f %4.1f %4.1f",
"",
$loadavg,
$comments / $time_elapsed,
$pages / $time_elapsed,
$queries / ($pages ? $pages : 1),
);
$pages = 1 if $pages < 1; # Below it's used in a denominator
for my $db (@db) {
my $slow = sprintf("%4.2f", ($db->{slow_queries_new} - $db->{slow_queries_last}) / $time_elapsed);
$slow =~ s/^0\././; $slow = substr($slow, 0, 3);
printf(" %4d $slow %4.1f",
($db->{queries_new} - $db->{queries_last}) / $time_elapsed,
($db->{queries_new} - $db->{queries_last}) / $pages,
);
if ($db->{innostatus_query}) {
if (!defined($db->{hitrate})) {
print " ?";
} elsif ($db->{hitrate} >= 99) {
print " +";
} elsif ($db->{hitrate} >= 96) {
print " -";
} else {
print " X";
}
}
}
print "\n"; ++$lines_since_header;
}
sub print_second_line {
# Second line: stats since slashmon started running
my $time_elapsed = $time_new - $time_first;
$time_elapsed = 0.01 if $time_elapsed < 0.01;
my $pt = $pages_total || 1;
printf("%4d %4.2f %5.2f %4.1f %4.1f",
$time_new - $time_first,
$loadavg_sum / $num_cycles,
$comments_total / $time_elapsed,
$pages_total / $time_elapsed,
$queries_total / $pt,
);
for my $db (@db) {
my $slow = sprintf("%4.2f", ($db->{slow_queries_new} - $db->{slow_queries_first}) / $time_elapsed);
$slow =~ s/^0\././; $slow = substr($slow, 0, 3);
printf(" %4d $slow %4.1f",
($db->{queries_new} - $db->{queries_first}) / $time_elapsed,
($db->{queries_new} - $db->{queries_first}) / $pt,
);
print " " if $db->{version} == 4;
}
print "\n"; ++$lines_since_header;
}
sub print_proclist {
OUTER: for my $db (@db) {
my $db_lines = 0;
my $pl = $db->{proclist};
INNER: for my $line (@$pl) {
my $text = substr($line->{text}, 0, $width - 13);
printf "%-8s %4d %s\n", $line->{vu}, $line->{Time}, $text;
++$lines_since_header;
++$db_lines; last INNER if $db_lines >= 8;
}
}
}
############################################################
sub getLoadavg {
open(my $fh, "</proc/loadavg") || return -1;
my $line = <$fh>;
my($loadavg) = split / /, $line;
$loadavg;
}
sub getQueries {
my($db) = @_;
my $query = $db->{status_query};
my $sth = $db->{db}{_dbh}->prepare($query);
my($q, $sq);
$sth->execute();
while (my($key, $val) = $sth->fetchrow()) {
$q = $val, next if $key eq "Questions";
$sq = $val if $key eq "Slow_queries";
}
$sth->finish();
if (!$q) {
print STDERR "getQueries returning q='$q' sq='$sq' for '$query'\n";
}
return($q, $sq);
}
sub getHitRate {
my($db) = @_;
my $query = $db->{innostatus_query};
my $sth = $db->{db}{_dbh}->prepare($query);
$sth->execute();
my($text) = $sth->fetchrow();
my($num, $denom) = $text =~ /Buffer pool hit rate (\d+)[ \/]+(\d+)/;
$sth->finish();
return undef unless $denom;
return 100*$num/$denom;
}
sub getProclist {
my($db) = @_;
my $query = $db->{proclist_query};
my $sth = $db->{db}{_dbh}->prepare($query);
my $threads = 0;
my $active = 0;
my $proclist = [ ];
$sth->execute();
while (my $S = $sth->fetchrow_hashref()) {
++$threads;
my $info = $$S{Info};
next if !$info;
next if $info =~ /SHOW.*PROCESSLIST/i;
next if $$S{Command} eq "Sleep";
next if $$S{Time} < $min_pl_time;
++$active;
$info =~ s/\s+/ /g;
$info =~ s/[^_\'\-\+\.A-Z0-9\(\)<> ,=\|]//gi;
$info =~ s/ +/ /g;
$info =~ s/, /,/g;
$info =~ s/\) /\)/g;
$info =~ s/DATE_ADD\((.*)\)/DA($1)/g;
$info =~ s/DATE_FORMAT\((.*)\)/DF($1)/g;
for my $abbr (@abbreviations) {
$info =~ s/\b$abbr\b/$abbreviations{$abbr}/gi;
}
push @$proclist, {
Time => $$S{Time},
vu => $db->{vu},
text => $info,
};
}
$sth->finish();
@$proclist = sort {
$b->{Time} <=> $a->{Time}
||
$a->{text} <=> $b->{text}
} @$proclist;
$db->{threads} = $threads;
$db->{threads_active} = $active;
$db->{proclist} = $proclist;
}
############################################################
sub usage {
print "*** $_[0]\n" if $_[0];
# Remember to doublecheck these match getopts()!
print <<EOT;
Usage: $PROGNAME [OPTIONS] ... [UIDs]
Monitor the Slash site's database.
Main options:
-h Help (this message)
-v Version
-i Accesslog may contain images; do a SELECT from it to
weed them out when counting pages
-u Virtual user (default "slash")
-w Width max in columns (default 76)
-r Height max in (r)ows (default 20)
-s Sleep time (default 10)
-t Min time before showing slow DB queries (default 1)
Global stats are the left 3 columns:
time Elapsed seconds
load CPU load on this machine
pps Pages per second
cps Comments per second
qpp Queries per page (total)
For each DB, the data is:
qps Queries per second
sps Slow queries per second
qpp Queries per page
h InnoDB cache hit rate percent (MySQL 4.x only)
("+" means > 99%, "-" means 96-99%, "X" means < 96%)
EOT
exit 0;
}
sub version {
print <<EOT;
$PROGNAME $VERSION
This code is a part of Slash, and is released under the GPL.
Copyright 1997-2004 by Open Source Development Network. See README
and COPYING for more information, or see http://slashcode.com/.
EOT
exit 0;
}