1
# This program is copyright 2012-2013 Percona Ireland Ltd.
2
# Feedback and improvements are welcome.
4
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
5
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
6
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
8
# This program is free software; you can redistribute it and/or modify it under
9
# the terms of the GNU General Public License as published by the Free Software
10
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
11
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16
# Place, Suite 330, Boston, MA 02111-1307 USA.
17
# ###########################################################################
18
# VersionCheck package
19
# ###########################################################################
23
# NOTE: VersionCheck 2.2 is not compatible with 2.1.
24
# In 2.1, the vc file did not have a special system
25
# instance with ID 0, and it used the file's mtime.
26
# In 2.2, the system and MySQL instances are all saved
27
# in the vc file, and the file's mtime doesn't matter.
30
use warnings FATAL => 'all';
31
use English qw(-no_match_vars);
33
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
36
local $Data::Dumper::Indent = 1;
37
local $Data::Dumper::Sortkeys = 1;
38
local $Data::Dumper::Quotekeys = 0;
40
use Digest::MD5 qw(md5_hex);
41
use Sys::Hostname qw(hostname);
42
use File::Basename qw();
47
require Percona::Toolkit;
51
# Return the version check file used to keep track of
52
# MySQL instance that have been checked and when. Some
53
# systems use random tmp dirs; we don't want that else
54
# every user will have their own vc file. One vc file
55
# per system is the goal, so prefer global sys dirs first.
57
my $file = 'percona-version-check';
58
my $home = $ENV{HOME} || $ENV{HOMEPATH} || $ENV{USERPROFILE} || '.';
61
'/etc/percona-toolkit',
66
sub version_check_file {
67
foreach my $dir ( @vc_dirs ) {
68
if ( -d $dir && -w $dir ) {
69
PTDEBUG && _d('Version check file', $file, 'in', $dir);
70
return $dir . '/' . $file;
73
PTDEBUG && _d('Version check file', $file, 'in', $ENV{PWD});
74
return $file; # in the CWD
78
# Return time limit between checks.
79
sub version_check_time_limit {
80
return 60 * 60 * 24; # one day
83
# #############################################################################
84
# Version check handlers
85
# #############################################################################
87
# Do a version check. This is only sub a caller/tool needs to call.
88
# Pass in an arrayref of hashrefs for each MySQL instance to check.
89
# Each hashref should have a dbh and a dsn.
91
# This sub fails silently, so you must use PTDEBUG to diagnose. Use
92
# PTDEBUG_VERSION_CHECK=1 and this sub will exit 255 when it's done
93
# (helpful in combination with PTDEBUG=1 so you don't get the tool's
96
# Use PERCONA_VERSION_CHECK_URL to set the version check API url,
97
# e.g. https://stage.v.percona.com for testing.
101
my $instances = $args{instances} || [];
102
my $instances_to_check;
104
# This sub should only be called if $o->get('version-check') is true,
105
# and it is by default because the option is on by default in PT 2.2.
106
# However, we do not want dev and testing to v-c, so even though this
107
# sub is called, force should be false because $o->got('version-check')
108
# is false, then check for a .bzr dir which indicates dev or testing.
109
# ../.bzr is when a tool is ran from /bin/; ../../.bzr is when a tool
110
# is ran as a module from /t/<tool>/.
111
PTDEBUG && _d('FindBin::Bin:', $FindBin::Bin);
112
if ( !$args{force} ) {
114
&& (-d "$FindBin::Bin/../.bzr" || -d "$FindBin::Bin/../../.bzr") ) {
115
PTDEBUG && _d("$FindBin::Bin/../.bzr disables --version-check");
121
# Name and ID the instances. The name is for debugging,
122
# and the ID is what the code uses to prevent double-checking.
123
foreach my $instance ( @$instances ) {
124
my ($name, $id) = get_instance_id($instance);
125
$instance->{name} = $name;
126
$instance->{id} = $id;
129
# Push a special instance for the system itself.
130
push @$instances, { name => 'system', id => 0 };
132
# Get the instances which haven't been checked in the 24 hours.
133
$instances_to_check = get_instances_to_check(
134
instances => $instances,
135
vc_file => $args{vc_file}, # testing
136
now => $args{now}, # testing
138
PTDEBUG && _d(scalar @$instances_to_check, 'instances to check');
139
return unless @$instances_to_check;
141
# Get the list of program to check from Percona. Try using
142
# https first; fallback to http if that fails (probably because
143
# IO::Socket::SSL isn't installed).
144
my $protocol = 'https'; # optimistic, but...
145
eval { require IO::Socket::SSL; };
147
PTDEBUG && _d($EVAL_ERROR);
150
PTDEBUG && _d('Using', $protocol);
152
my $advice = pingback(
153
instances => $instances_to_check,
154
protocol => $protocol,
155
url => $args{url} # testing
156
|| $ENV{PERCONA_VERSION_CHECK_URL} # testing
157
|| "$protocol://v.percona.com",
160
PTDEBUG && _d('Advice:', Dumper($advice));
161
if ( scalar @$advice > 1) {
162
print "\n# " . scalar @$advice . " software updates are "
166
print "\n# A software update is available:\n";
168
print join("\n", map { "# * $_" } @$advice), "\n\n";
172
PTDEBUG && _d('Version check failed:', $EVAL_ERROR);
175
# Always update the vc file, even if the version check fails.
176
if ( @$instances_to_check ) {
178
# Update the check time for things we checked. I.e. if we
179
# didn't check it, do _not_ update its time.
181
instances => $instances_to_check,
182
vc_file => $args{vc_file}, # testing
183
now => $args{now}, # testing
187
PTDEBUG && _d('Error updating version check file:', $EVAL_ERROR);
191
if ( $ENV{PTDEBUG_VERSION_CHECK} ) {
192
warn "Exiting because the PTDEBUG_VERSION_CHECK "
193
. "environment variable is defined.\n";
200
sub get_instances_to_check {
203
my $instances = $args{instances};
204
my $now = $args{now} || int(time);
205
my $vc_file = $args{vc_file} || version_check_file();
207
if ( !-f $vc_file ) {
208
PTDEBUG && _d('Version check file', $vc_file, 'does not exist;',
209
'version checking all instances');
213
# The version check file contains "ID,time" lines for each MySQL instance
214
# and a special "0,time" instance for the system. Another tool may have
215
# seen fewer or more instances than the current tool, but we'll read them
216
# all and check only the instances for the current tool.
217
open my $fh, '<', $vc_file or die "Cannot open $vc_file: $OS_ERROR";
218
chomp(my $file_contents = do { local $/ = undef; <$fh> });
219
PTDEBUG && _d('Version check file', $vc_file, 'contents:', $file_contents);
221
my %last_check_time_for = $file_contents =~ /^([^,]+),(.+)$/mg;
223
# Check the instances that have either 1) never been checked
224
# (or seen) before, or 2) were checked > check time limit ago.
225
my $check_time_limit = version_check_time_limit();
226
my @instances_to_check;
227
foreach my $instance ( @$instances ) {
228
my $last_check_time = $last_check_time_for{ $instance->{id} };
229
PTDEBUG && _d('Intsance', $instance->{id}, 'last checked',
230
$last_check_time, 'now', $now, 'diff', $now - ($last_check_time || 0),
231
'hours until next check',
233
($check_time_limit - ($now - ($last_check_time || 0))) / 3600);
234
if ( !defined $last_check_time
235
|| ($now - $last_check_time) >= $check_time_limit ) {
236
PTDEBUG && _d('Time to check', Dumper($instance));
237
push @instances_to_check, $instance;
241
return \@instances_to_check;
244
sub update_check_times {
247
my $instances = $args{instances};
248
my $now = $args{now} || int(time);
249
my $vc_file = $args{vc_file} || version_check_file();
250
PTDEBUG && _d('Updating last check time:', $now);
252
# We need to write back all instances to the file. The given
253
# instances are the ones updated, so use the current ts (now).
254
my %all_instances = map {
255
$_->{id} => { name => $_->{name}, ts => $now }
258
# If the file exists, read the instances in it, and if they're
259
# not one of the updated ones, save them with their original ts.
261
open my $fh, '<', $vc_file or die "Cannot read $vc_file: $OS_ERROR";
262
my $contents = do { local $/ = undef; <$fh> };
265
foreach my $line ( split("\n", ($contents || '')) ) {
266
my ($id, $ts) = split(',', $line);
267
if ( !exists $all_instances{$id} ) {
268
$all_instances{$id} = { ts => $ts }; # original ts, not updated
273
# Write back all instances, some with updated ts, others with their
275
open my $fh, '>', $vc_file or die "Cannot write to $vc_file: $OS_ERROR";
276
foreach my $id ( sort keys %all_instances ) {
277
PTDEBUG && _d('Updated:', $id, Dumper($all_instances{$id}));
278
print { $fh } $id . ',' . $all_instances{$id}->{ts} . "\n";
285
sub get_instance_id {
288
my $dbh = $instance->{dbh};
289
my $dsn = $instance->{dsn};
291
# MySQL 5.1+ has @@hostname and @@port
292
# MySQL 5.0 has @@hostname but port only in SHOW VARS
293
# MySQL 4.x has nothing, so we use the dsn
294
my $sql = q{SELECT CONCAT(@@hostname, @@port)};
296
my ($name) = eval { $dbh->selectrow_array($sql) };
299
PTDEBUG && _d($EVAL_ERROR);
300
$sql = q{SELECT @@hostname};
302
($name) = eval { $dbh->selectrow_array($sql) };
305
PTDEBUG && _d($EVAL_ERROR);
306
$name = ($dsn->{h} || 'localhost') . ($dsn->{P} || 3306);
310
$sql = q{SHOW VARIABLES LIKE 'port'};
312
my (undef, $port) = eval { $dbh->selectrow_array($sql) };
313
PTDEBUG && _d('port:', $port);
314
$name .= $port || '';
317
my $id = md5_hex($name);
319
PTDEBUG && _d('MySQL instance:', $id, $name, Dumper($dsn));
324
# #############################################################################
326
# #############################################################################
330
my @required_args = qw(url instances);
331
foreach my $arg ( @required_args ) {
332
die "I need a $arg arugment" unless $args{$arg};
334
my $url = $args{url};
335
my $instances = $args{instances};
338
my $ua = $args{ua} || HTTPMicro->new( timeout => 3 );
340
# GET https://upgrade.percona.com, the server will return
341
# a plaintext list of items/programs it wants the tool
342
# to get, one item per line with the format ITEM;TYPE[;VARS]
343
# ITEM is the pretty name of the item/program; TYPE is
344
# the type of ITEM that helps the tool determine how to
345
# get the item's version; and VARS is optional for certain
346
# items/types that need extra hints.
347
my $response = $ua->request('GET', $url);
348
PTDEBUG && _d('Server response:', Dumper($response));
349
die "No response from GET $url"
351
die("GET on $url returned HTTP status $response->{status}; expected 200\n",
352
($response->{content} || '')) if $response->{status} != 200;
353
die("GET on $url did not return any programs to check")
354
if !$response->{content};
356
# Parse the plaintext server response into a hashref keyed on
360
# type => "mysql_variables",
361
# vars => ["version", "version_comment"],
363
my $items = parse_server_response(
364
response => $response->{content}
366
die "Failed to parse server requested programs: $response->{content}"
367
if !scalar keys %$items;
369
# Get the versions for those items in another hashref also keyed on
371
# "MySQL" => "MySQL Community Server 5.1.49-log",
372
my $versions = get_versions(
374
instances => $instances,
376
die "Failed to get any program versions; should have at least gotten Perl"
377
if !scalar keys %$versions;
379
# Join the items and whatever versions are available and re-encode
380
# them in same simple plaintext item-per-line protocol, and send
381
# it back to Percona.
382
my $client_content = encode_client_response(
384
versions => $versions,
385
general_id => md5_hex( hostname() ),
388
my $client_response = {
389
headers => { "X-Percona-Toolkit-Tool" => File::Basename::basename($0) },
390
content => $client_content,
392
PTDEBUG && _d('Client response:', Dumper($client_response));
394
$response = $ua->request('POST', $url, $client_response);
395
PTDEBUG && _d('Server suggestions:', Dumper($response));
396
die "No response from POST $url $client_response"
398
die "POST $url returned HTTP status $response->{status}; expected 200"
399
if $response->{status} != 200;
401
# Response contents is empty if the server doesn't have any suggestions.
402
return unless $response->{content};
404
# If the server has suggestions for items, it sends them back in
405
# the same format: ITEM:TYPE:SUGGESTION\n. ITEM:TYPE is mostly for
406
# debugging; the tool just repports the suggestions.
407
$items = parse_server_response(
408
response => $response->{content},
411
die "Failed to parse server suggestions: $response->{content}"
412
if !scalar keys %$items;
413
my @suggestions = map { $_->{vars} }
414
sort { $a->{item} cmp $b->{item} }
417
return \@suggestions;
420
sub encode_client_response {
422
my @required_args = qw(items versions general_id);
423
foreach my $arg ( @required_args ) {
424
die "I need a $arg arugment" unless $args{$arg};
426
my ($items, $versions, $general_id) = @args{@required_args};
428
# There may not be a version for each item. For example, the server
429
# may have requested the "MySQL" (version) item, but if the tool
430
# didn't connect to MySQL, there won't be a $versions->{MySQL}.
431
# That's ok; just use what we've got.
432
# NOTE: the sort is only need to make testing deterministic.
434
foreach my $item ( sort keys %$items ) {
435
next unless exists $versions->{$item};
436
if ( ref($versions->{$item}) eq 'HASH' ) {
437
my $mysql_versions = $versions->{$item};
438
for my $id ( sort keys %$mysql_versions ) {
439
push @lines, join(';', $id, $item, $mysql_versions->{$id});
443
push @lines, join(';', $general_id, $item, $versions->{$item});
447
my $client_response = join("\n", @lines) . "\n";
448
return $client_response;
451
sub parse_server_response {
453
my @required_args = qw(response);
454
foreach my $arg ( @required_args ) {
455
die "I need a $arg arugment" unless $args{$arg};
457
my ($response) = @args{@required_args};
460
my ($item, $type, $vars) = split(";", $_);
461
if ( !defined $args{split_vars} || $args{split_vars} ) {
462
$vars = [ split(",", ($vars || '')) ];
469
} split("\n", $response);
471
PTDEBUG && _d('Items:', Dumper(\%items));
476
# Safety check: only these types of items are valid/official.
478
os_version => \&get_os_version,
479
perl_version => \&get_perl_version,
480
perl_module_version => \&get_perl_module_version,
481
mysql_variable => \&get_mysql_variable,
482
bin_version => \&get_bin_version,
488
if ( !exists $sub_for_type{ $item->{type} } ) {
489
PTDEBUG && _d('Invalid type:', $item->{type});
497
my @required_args = qw(items);
498
foreach my $arg ( @required_args ) {
499
die "I need a $arg arugment" unless $args{$arg};
501
my ($items) = @args{@required_args};
504
foreach my $item ( values %$items ) {
505
next unless valid_item($item);
507
my $version = $sub_for_type{ $item->{type} }->(
509
instances => $args{instances},
512
chomp $version unless ref($version);
513
$versions{$item->{item}} = $version;
517
PTDEBUG && _d('Error getting version for', Dumper($item), $EVAL_ERROR);
524
# #############################################################################
526
# #############################################################################
529
if ( $OSNAME eq 'MSWin32' ) {
531
return Win32::GetOSDisplayName();
534
chomp(my $platform = `uname -s`);
535
PTDEBUG && _d('platform:', $platform);
536
return $OSNAME unless $platform;
538
chomp(my $lsb_release
539
= `which lsb_release 2>/dev/null | awk '{print \$1}'` || '');
540
PTDEBUG && _d('lsb_release:', $lsb_release);
544
if ( $platform eq 'Linux' ) {
545
if ( -f "/etc/fedora-release" ) {
546
$release = `cat /etc/fedora-release`;
548
elsif ( -f "/etc/redhat-release" ) {
549
$release = `cat /etc/redhat-release`;
551
elsif ( -f "/etc/system-release" ) {
552
$release = `cat /etc/system-release`;
554
elsif ( $lsb_release ) {
555
$release = `$lsb_release -ds`;
557
elsif ( -f "/etc/lsb-release" ) {
558
$release = `grep DISTRIB_DESCRIPTION /etc/lsb-release`;
559
$release =~ s/^\w+="([^"]+)".+/$1/;
561
elsif ( -f "/etc/debian_version" ) {
562
chomp(my $rel = `cat /etc/debian_version`);
563
$release = "Debian $rel";
564
if ( -f "/etc/apt/sources.list" ) {
565
chomp(my $code_name = `awk '/^deb/ {print \$3}' /etc/apt/sources.list | awk -F/ '{print \$1}'| awk 'BEGIN {FS="|"} {print \$1}' | sort | uniq -c | sort -rn | head -n1 | awk '{print \$2}'`);
566
$release .= " ($code_name)" if $code_name;
569
elsif ( -f "/etc/os-release" ) { # openSUSE
570
chomp($release = `grep PRETTY_NAME /etc/os-release`);
571
$release =~ s/^PRETTY_NAME="(.+)"$/$1/;
573
elsif ( `ls /etc/*release 2>/dev/null` ) {
574
if ( `grep DISTRIB_DESCRIPTION /etc/*release 2>/dev/null` ) {
575
$release = `grep DISTRIB_DESCRIPTION /etc/*release | head -n1`;
578
$release = `cat /etc/*release | head -n1`;
582
elsif ( $platform =~ m/(?:BSD|^Darwin)$/ ) {
583
my $rel = `uname -r`;
584
$release = "$platform $rel";
586
elsif ( $platform eq "SunOS" ) {
587
my $rel = `head -n1 /etc/release` || `uname -r`;
588
$release = "$platform $rel";
592
PTDEBUG && _d('Failed to get the release, using platform');
593
$release = $platform;
597
# For Gentoo, which returns a value in quotes
598
$release =~ s/^"|"$//g;
600
PTDEBUG && _d('OS version =', $release);
604
sub get_perl_version {
606
my $item = $args{item};
609
my $version = sprintf '%vd', $PERL_VERSION;
610
PTDEBUG && _d('Perl version', $version);
614
sub get_perl_module_version {
616
my $item = $args{item};
619
# If there's a var, then its an explicit Perl variable name to get,
620
# else the item name is an implicity Perl module name to which we
621
# append ::VERSION to get the module's version.
622
my $var = '$' . $item->{item} . '::VERSION';
623
my $version = eval "use $item->{item}; $var;";
624
PTDEBUG && _d('Perl version for', $var, '=', $version);
628
sub get_mysql_variable {
629
return get_from_mysql(
637
my $show = $args{show};
638
my $item = $args{item};
639
my $instances = $args{instances};
640
return unless $show && $item;
642
if ( !$instances || !@$instances ) {
643
PTDEBUG && _d('Cannot check', $item,
644
'because there are no MySQL instances');
650
foreach my $instance ( @$instances ) {
651
next unless $instance->{id}; # special system instance has id=0
652
my $dbh = $instance->{dbh};
653
local $dbh->{FetchHashKeyName} = 'NAME_lc';
654
my $sql = qq/SHOW $show/;
656
my $rows = $dbh->selectall_hashref($sql, 'variable_name');
659
foreach my $var ( @{$item->{vars}} ) {
661
my $version = $rows->{$var}->{value};
662
PTDEBUG && _d('MySQL version for', $item->{item}, '=', $version,
663
'on', $instance->{name});
664
push @versions, $version;
666
$version_for{ $instance->{id} } = join(' ', @versions);
669
return \%version_for;
672
sub get_bin_version {
674
my $item = $args{item};
675
my $cmd = $item->{item};
678
my $sanitized_command = File::Basename::basename($cmd);
679
PTDEBUG && _d('cmd:', $cmd, 'sanitized:', $sanitized_command);
680
return if $sanitized_command !~ /\A[a-zA-Z0-9_-]+\z/;
682
my $output = `$sanitized_command --version 2>&1`;
683
PTDEBUG && _d('output:', $output);
685
my ($version) = $output =~ /v?([0-9]+\.[0-9]+(?:\.[\w-]+)?)/;
687
PTDEBUG && _d('Version for', $sanitized_command, '=', $version);
692
my ($package, undef, $line) = caller 0;
693
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
694
map { defined $_ ? $_ : 'undef' }
696
print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
701
# ###########################################################################
702
# End VersionCheck package
703
# ###########################################################################