Last active
December 10, 2015 02:39
-
-
Save mikeda/4369294 to your computer and use it in GitHub Desktop.
muninのRRDから簡単なレポートを作るサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use RRDs; | |
use Data::Dumper; | |
#ホストのリストをどこから取ってくるか・・・ | |
my $hostname = 'test01'; | |
my @rrds = ( | |
'cpu-user-d.rrd', | |
'cpu-iowait-d.rrd', | |
'cpu-system-d.rrd', | |
'cpu-idle-d.rrd', | |
# 'if_eth0-down-c.rrd', | |
# 'if_eth0-up-c.rrd' | |
); | |
my $end = time; | |
my $start = $end - 7*24*60*60; | |
# 1週間で取ると『9日間、6回(30分)ごとのサマリで保存データ』が選択されるはず | |
# MAX値を厳密に取るならMAXでやらないといけないけど、まぁいいだろう | |
for my $rrd (@rrds){ | |
my $file = "$hostname/$hostname-$rrd"; | |
my ($start, $step, $names, $data) = RRDs::fetch( | |
$file, | |
"AVERAGE", | |
"--start", $start, | |
"--end", $end, | |
); | |
my $cnt = 0; | |
my $sum = 0; | |
my $max; | |
for my $val (@$data){ | |
next unless $val->[0]; | |
$cnt ++; | |
$sum += $val->[0]; | |
$max = $val->[0] if !defined($max) || $val->[0] > $max; | |
} | |
print "$hostname $rrd\n"; | |
print " Start:\t". scalar(localtime($start)) ."\n"; | |
print " Step:\t$step sec\n"; | |
print " Avg:\t" . ($sum / $cnt) ."\n"; | |
print " Max:\t$max\n"; | |
} | |
# 出力例 | |
# | |
#test01 cpu-user-d.rrd | |
# Start: Mon Dec 17 23:00:00 2012 | |
# Step: 1800 sec | |
# Avg: 530.931495521748 | |
# Max: 1109.96094886307 | |
#test01 cpu-iowait-d.rrd | |
# Start: Mon Dec 17 23:00:00 2012 | |
# Step: 1800 sec | |
# Avg: 1.30228937790891 | |
# Max: 5.17463812243643 | |
#test01 cpu-system-d.rrd | |
# Start: Mon Dec 17 23:00:00 2012 | |
# Step: 1800 sec | |
# Avg: 22.7017262469694 | |
# Max: 40.8330043990301 | |
#test01 cpu-idle-d.rrd | |
# Start: Mon Dec 17 23:00:00 2012 | |
# Step: 1800 sec | |
# Avg: 633.125196775605 | |
# Max: 1140.9580395611 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!