Skip to content

Instantly share code, notes, and snippets.

@riywo
Created November 16, 2012 00:40
Show Gist options
  • Save riywo/4082832 to your computer and use it in GitHub Desktop.
Save riywo/4082832 to your computer and use it in GitHub Desktop.
show variablesと今再起動したときのvariablesの差分出せるかなと思ってみたけど挫折した
#!/usr/bin/env perl
use strict;
use warnings;
my $config = fetch_config();
my $variables = fetch_variables(@ARGV);
print_diff($config, $variables);
sub fetch_config {
my @ret = `mysqld --help --verbose 2>/dev/null`;
chomp @ret;
my $config_val;
my $i = 0;
$i++ while ($ret[$i] !~ /^Variables/);
$i += 3;
for ($i..$#ret) {
last if ($ret[$_] eq '');
my ($k, $v) = ($ret[$_] =~ /^([^\s]+)\s+(.+)$/);
$k =~ s/_/-/g;
$config_val->{$k} = $v;
}
return $config_val;
}
sub fetch_variables {
my @args = @_;
my @ret = `mysql -N -e 'show variables' @args`;
chomp @ret;
my $variables;
for my $line (@ret) {
my ($k, $v) = split /\t/, $line;
$k =~ s/_/-/g;
$variables->{$k} = $v;
}
return $variables;
}
sub print_diff {
my ($a, $b) = @_;
my %uniq_key;
$uniq_key{$_} = 1 for (keys %$a);
$uniq_key{$_} = 1 for (keys %$b);
my $diff;
for my $k (keys %uniq_key) {
if (exists $a->{$k} and exists $b->{$k}) {
next if $a->{$k} eq $b->{$k};
$diff->{$k} = [$a->{$k}, $b->{$k}];
} else {
$diff->{$k} = [$a->{$k}, 'undef'] if exists $a->{$k};
$diff->{$k} = ['undef', $b->{$k}] if exists $b->{$k};
}
}
for my $k (sort keys %$diff) {
printf("%-50s %-50s %-50s\n", $k, $diff->{$k}->[0], $diff->{$k}->[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment