Skip to content

Commit

Permalink
Merge pull request #42 from hirose31/feature/enhance-redis
Browse files Browse the repository at this point in the history
Add several graphs of redis
  • Loading branch information
kazeburo committed Dec 20, 2013
2 parents cb478b4 + cd01f38 commit 739f703
Showing 1 changed file with 119 additions and 3 deletions.
122 changes: 119 additions & 3 deletions lib/CloudForecast/Data/Redis.pm
Expand Up @@ -18,9 +18,19 @@ CloudForecast::Data::Redis - redis resource monitor

rrds map { [$_,'COUNTER'] } qw/totalcmd totalconn/;
rrds map { [$_,'GAUGE'] } qw/conncli connslv usedmem unsaved fragmentation/;
extend_rrd 'evicted', 'COUNTER';
extend_rrd 'pubsub_ch', 'GAUGE';
extend_rrd 'keys', 'GAUGE';
extend_rrd 'slowlog', 'GAUGE';

graphs 'cmd' => 'Command Processed';
graphs 'conn' => 'Connections';
graphs 'mem' => 'Memory Usage';
graphs 'keys' => 'Keys';
graphs 'evicted' => 'Evicted Keys/sec';
graphs 'fragmentation' => 'Fragmentation Ratio';
graphs 'pubsub_ch' => 'Pub/Sub Channels';
graphs 'slowlog' => 'Slowlog(total)';
graphs 'unsaved' => 'Unsaved Changes';

title {
Expand Down Expand Up @@ -50,10 +60,38 @@ fetcher {
die "could not retrieve status from $host:$port" unless $raw_stats;

my %stats;
my $keys;
foreach my $line ( split /\r?\n/, $raw_stats ) {
chomp($line);chomp($line);
if ( $line =~ /^([^:]+?):(.+)$/ ) {
$stats{$1} = $2;
my($k,$v) = ($1,$2);
$stats{$k} = $v;
if ($k =~ /^db[0-9]+/) {
$keys += $v =~ /keys=(\d+),/ ? $1 : 0;
}
}
}

my $raw_res;
### slowlog
$client->write("slowlog len\r\n",1);
$raw_res = $client->read(1);
my $slowlog = $raw_res =~ /:([0-9]+)/ ? $1 : 0;

### config get
my %config;
$client->write("config get *\r\n",1);
$raw_res = $client->read(1);
my $ck;
foreach my $line ( split /\r?\n/, $raw_res ) {
chomp($line);chomp($line);
next if $line =~ /^[\*\$]/;

if (! $ck) {
$ck = $line;
} else {
$config{$ck} = $line;
$ck = "";
}
}

Expand All @@ -71,6 +109,11 @@ fetcher {
push @sysinfo, $stats_key => $stats{$stats_key}
if exists $stats{$stats_key};
}
push @sysinfo, 'maxmemory' => _unit($config{maxmemory} || 0);
foreach my $config_key (qw/maxclients rdbcompression appendonly maxmemory-policy appendfsync save slowlog-max-len/) {
push @sysinfo, $config_key => $config{$config_key}
if exists $config{$config_key};
}

$c->ledge_set( 'sysinfo', \@sysinfo );

Expand All @@ -83,10 +126,38 @@ fetcher {
$stats{connected_slaves},
$stats{used_memory},
$stats{changes_since_last_save},
int($stats{mem_fragmentation_ratio} * 100)
$stats{mem_fragmentation_ratio},
$stats{evicted_keys},
$stats{pubsub_channels},
$keys,
$slowlog,
];
}
};

sub _unit {
my $n = shift;
my($base, $unit);

return $n unless $n =~ /^\d+$/;
if ($n >= 1073741824) {
$base = 1073741824;
$unit = 'GB';
} elsif ($n >= 1048576) {
$base = 1048576;
$unit = 'MB';
} elsif ($n >= 1024) {
$base = 1024;
$unit = 'KB';
} else {
$base = 1;
$unit = 'B';
}

$n = sprintf '%.2f', $n/$base;
while($n =~ s/(.*\d)(\d\d\d)/$1,$2/){};

return $n.$unit;
}

__DATA__
@@ cmd
Expand Down Expand Up @@ -119,11 +190,14 @@ GPRINT:my3:MIN:Min\:%5.1lf\l
@@ mem
DEF:my1=<%RRD%>:usedmem:AVERAGE
CDEF:sm=my1,900,TREND
CDEF:cf=86400,-8,1800,sm,PREDICT
AREA:my1#4682B4:Used
GPRINT:my1:LAST:Cur\:%5.1lf%sB
GPRINT:my1:AVERAGE:Ave\:%5.1lf%sB
GPRINT:my1:MAX:Max\:%5.1lf%sB
GPRINT:my1:MIN:Min\:%5.1lf%sB\l
LINE1:cf#780a85:Prediction:dashes=4,6
@@ unsaved
DEF:my1=<%RRD%>:unsaved:AVERAGE
Expand All @@ -133,3 +207,45 @@ GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l
@@ fragmentation
DEF:my1=<%RRD%>:fragmentation:AVERAGE
LINE1:my1#491815:Fragmentation
GPRINT:my1:LAST:Cur\:%5.1lf
GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l
@@ evicted
DEF:my1=<%RRD_EXTEND evicted %>:evicted:AVERAGE
LINE1:my1#800040:Evicted Keys/sec
GPRINT:my1:LAST:Cur\:%5.1lf
GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l
@@ pubsub_ch
DEF:my1=<%RRD_EXTEND pubsub_ch %>:pubsub_ch:AVERAGE
LINE2:my1#2E8B57:Pub/Sub Channels
GPRINT:my1:LAST:Cur\:%5.1lf
GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l
@@ keys
DEF:my1=<%RRD_EXTEND keys %>:keys:AVERAGE
CDEF:sm=my1,900,TREND
CDEF:cf=86400,-8,1800,sm,PREDICT
AREA:my1#00A000:Keys
GPRINT:my1:LAST:Cur\:%5.1lf
GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l
LINE1:cf#780a85:Prediction:dashes=4,6
@@ slowlog
DEF:my1=<%RRD_EXTEND slowlog %>:slowlog:AVERAGE
AREA:my1#00c000:Slowlog
GPRINT:my1:LAST:Cur\:%5.1lf
GPRINT:my1:AVERAGE:Ave\:%5.1lf
GPRINT:my1:MAX:Max\:%5.1lf
GPRINT:my1:MIN:Min\:%5.1lf\l

0 comments on commit 739f703

Please sign in to comment.