Skip to content

Instantly share code, notes, and snippets.

@riywo
Created March 18, 2012 06:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riywo/2069383 to your computer and use it in GitHub Desktop.
Save riywo/2069383 to your computer and use it in GitHub Desktop.
dstatのCSVをその場でパースしてなんかする方法
#!/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