Created
March 18, 2012 06:31
-
-
Save riywo/2069383 to your computer and use it in GitHub Desktop.
dstatのCSVをその場でパースしてなんかする方法
This file contains hidden or 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/env perl | |
use strict; | |
use warnings; | |
use POSIX qw/mkfifo/; | |
use File::Temp qw/tempfile/; | |
use Scalar::Util qw/looks_like_number/; | |
use IO::Handle; | |
my $log_file = '/tmp/dstat.log'; | |
my (undef, $pipe) = tempfile("dstat_XXXX", DIR => '/tmp', SUFFIX => '.pipe'); | |
unlink $pipe; | |
mkfifo($pipe, 0666); | |
my $pid = fork; | |
if ($pid) { | |
open my $dstat, '<', $pipe or die; | |
open my $log, '>', $log_file or die; | |
$log->autoflush; | |
my @columns = qw/ | |
cpu-usr cpu-sys cpu-idl cpu-wai cpu-hiq cpu-siq | |
la-1m la-5m la-15m | |
mem-used mem-buff mem-cach mem-free | |
dsk-read dsk-writ | |
io-read io-writ | |
net-recv net-send | |
/; # for dstat -Tclmdrn | |
print $log join("\t", ('epoch', @columns)) . "\n"; | |
while (my $line = <$dstat>) { | |
chomp $line; | |
my @data = split /,/, $line; | |
next unless(looks_like_number $data[0]); | |
my $time = int(shift @data); | |
print $log join("\t", ($time, @data)) . "\n"; | |
} | |
close $dstat; close $log; | |
} else { | |
system("dstat -Tclmdrn --output $pipe"); | |
} | |
unlink $pipe; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment