Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Last active December 20, 2015 05:19
Show Gist options
  • Save toritori0318/6077379 to your computer and use it in GitHub Desktop.
Save toritori0318/6077379 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use RedisDB;
use Config::General;
use Array::Diff;
use Text::Diff;
my $redis_conf = shift or die 'usage:redis-conf-diff.pl <redis_config_path>';
my $gconf = Config::General->new($redis_conf);
my %default = (
'pidfile' => '/var/run/redis.pid',
'bind' => '127.0.0.1',
'port' => 6379,
'daemonize' => 'no',
'databases' => 16,
'rdbcompression' => 'no',
'activerehashing' => 'yes',
'repl-ping-slave-period' => '10',
'repl-timeout' => '60',
'slave-priority' => '100',
'maxmemory-policy' => 'volatile-lru',
'maxmemory-samples' => '3',
'hz' => '10',
'logfile' => 'stdout',
'unixsocketperm' => 0,
'watchdog-period' => 0,
);
sub clean_config {
my ($config) = @_;
for my $key (keys %$config) {
if(!defined $config->{$key} || $config->{$key} eq '') {
$config->{$key} = $default{$key};
next;
}
if(ref $config->{$key} eq 'ARRAY') {
$config->{$key} = join(' ', map { &revert_unit(beauty_item($_)) } @{$config->{$key}})
} else {
$config->{$key} = beauty_item($config->{$key});
}
}
return %$config;
}
sub make_hash2text {
my ($hash) = @_;
my @rows;
for my $key (sort keys %$hash) {
push @rows, sprintf("%s = %s",$key, $hash->{$key}||'');
}
return join("\n", @rows);
}
sub beauty_item {
my ($item) = @_;
return join(' ', map { $_ =~ s|/$||; $_; } map { revert_unit($_) } split / /, $item);
}
sub revert_unit {
my ($str) = @_;
my $num = sub { return $_[0] ? 1024 : 1000 } ;
if($str =~ m/(\d+)k(b)?$/i) {
$num = $num->($2);
return $1 * $num;
}
elsif($str =~ m/(\d+)m(b)?$/i) {
$num = $num->($2);
return $1 * $num * $num;
}
elsif($str =~ m/(\d+)g(b)?$/i) {
$num = $num->($2);
return $1 * $num * $num * $num;
}
return $str;
}
sub main {
# file config
my %file_config = clean_config({$gconf->getall});
# memory config
my $redis = RedisDB->new(host => 'localhost', port => 6379);
my %memory_config = @{$redis->config_get('*')};
%memory_config = clean_config(\%memory_config);
# keys diff
my $diff = Array::Diff->diff([sort keys %memory_config], [sort keys %file_config]);
# file only
if(scalar @{$diff->added} > 0) {
delete($file_config{$_}) for @{$diff->added};
}
# memory only
if(scalar @{$diff->deleted} > 0) {
$file_config{$_} = $default{$_} for @{$diff->deleted};
}
# text diff
my $memory_str = make_hash2text(\%memory_config);
my $file_str = make_hash2text(\%file_config);
my $tdiff = Text::Diff::diff(
\$memory_str,
\$file_str,
{
STYLE => "Unified",
FILENAME_A => '[memory]',
FILENAME_B => $redis_conf,
},
);
print $tdiff;
exit (($tdiff) ? 1 : 0);
}
main();
#################################################################
# 1. 差分がない状態
# 実行
% perl redis-conf-diff.pl /usr/local/etc/redis.conf
# ステータス
% echo $?
0
#################################################################
# 2. 差分がある状態
# メモリ上の値を変えてみる
% redis-cli config set lua-time-limit 6000
OK
# 実行。差分表示
% perl redis-conf-diff.pl /usr/local/etc/redis.conf
--- [memory]
+++ /usr/local/etc/redis.conf
@@ -16,7 +16,7 @@
list-max-ziplist-entries = 512
list-max-ziplist-value = 64
loglevel = notice
-lua-time-limit = 6000
+lua-time-limit = 5000
maxclients = 3000
maxmemory = 20000000
no-appendfsync-on-rewrite = no
# ステータス
% echo $?
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment