#!/usr/bin/perl -w # # Prototype Code # Copyright L A Walsh 2011 # use strict; use feature ':5.10'; use Fatal; use Time::HiRes qw(ualarm); my ($Devel, $Debuff_Output) = (1, 1); if ($Debuff_Output) { select STDERR; $| = 1; select STDOUT; $| = 1; } if ($Devel) { $ENV{PERL_HASH_SEED} = 0; use Carp qw(cluck confess); $SIG{__WARN__} = $SIG{__DIE__} = sub { $Carp::CarpLevel = 1 if defined $^S && !$^S; $SIG{__WARN__} = $SIG{__DIE__} = undef; confess $_[0] if defined $^S; die "Fatal Exit: Perl backtrace unavailable\n"; } } my %colors = ( 'black' => 0, 'red' => 1, 'green' => 2, 'yellow' => 3, 'blue' => 4, 'magenta' => 5, 'turquoise' => 6, 'white' => 7); my $interval = 2; my $Iostatargs = "-dk"; my $clear = `tput clear`; my $at00 = `tput cup 0 0`; my $term_reset_color = `tput sgr0`; my $norm_color = $term_reset_color; my $term_bold = `tput bold`; my $term_blue_color = `tput setaf $colors{blue}`; my $term_red_color = `tput setaf $colors{red}`; my $read_color = $term_blue_color; my $write_color = $term_red_color; my ($rfds, $wfds, $efds) = ('', '', ''); my $ioh; foreach (@ARGV) { /^(\d+)$/ and do { $interval = $1; next }; print STDERR "Unknown arg: \"$_\"\n"; sleep 1; } open($ioh, "iostat $interval $Iostatargs|") || die "Can't start iostat $?\n"; my $maxcols; sub maxcols { my $stty = `/bin/stty size $max_rate; $max_namelen = $namelen if $namelen > $max_namelen; push @data, [ $name, $rrate, $wrate, $total ]; } } my @sorted_data = sort { $a->[3] <=> $b->[3] } @data; print "mx_rate=$max_rate, mxnmln=$max_namelen, trmwid=$termwidth\n"; $max_namelen = 20 if ($max_namelen > 20); my $device_suffix = " : "; my $dev_sfxlen = length $device_suffix; my $max_scale_space = $termwidth - $max_namelen - $dev_sfxlen; my $units_str = "KB/s"; my $divisor = 1; if ($max_rate > 9999) { $max_rate /= ($divisor = 1024); $units_str = "MB/s"; } my @rate_formats = ("%.2f","%.1f","%.0f"); my $rate_format; my $desired_max_rate_format_len = 6; my $rate_str; my $rate_strlen; foreach (@rate_formats) { $rate_format = $_; $rate_str = sprintf $rate_format, $max_rate; $rate_strlen = length $rate_str; last if $rate_strlen <= $desired_max_rate_format_len; } my $scale_str = ($rate_format . " " . $units_str); my $ss_len = length $scale_str; my $mid_str0 = " |<-- Read+Write -- "; #for len calcs my $dev_format = "%-${max_namelen}.${max_namelen}s"; my $mid_string = " |<-- " . $term_bold . $read_color . "Read" . "$norm_color" . "+" . $write_color . "Write" . $norm_color . " -- "; my $end_string = "-->|"; my $label_fill_len = $termwidth - $max_namelen - $ss_len - 1 - length($mid_str0 . $end_string); my $label_str = $dev_format . $mid_string . $scale_str . " " . ('-' x $label_fill_len) . $end_string; print "lab_str=$label_str\n"; printf "$label_str\n", "Device", $rate_str; my $rate_per_space = ($max_rate) / $max_scale_space; print "units=$units_str, maxrt=$max_rate, rt_p_space=$rate_per_space\n"; foreach (@sorted_data) { my ($dev, $rrate, $wrate, $tot) = @$_; $rrate /= $divisor; $wrate /= $divisor; $tot /= $divisor; my $read_fill = int($rrate / $rate_per_space); my $write_fill = int($wrate / $rate_per_space); my $rdfill_ch = '='; my $wrfill_ch = '='; if ($read_fill < 1 && ($rrate / $rate_per_space) > 0) { $read_fill = 1; $rdfill_ch = "|"; } if ($write_fill < 1 && ($wrate / $rate_per_space) > 0) { $write_fill = 1; $wrfill_ch = "|"; } if ($read_fill + $write_fill > $max_scale_space) { if ($write_fill > $read_fill) { --$write_fill; } else { --$read_fill; } } printf $dev_format, $dev; print " : "; #print "(r=$read_fill, w=$write_fill)"; print $term_bold; if ($read_fill > 0) { print $read_color . ($rdfill_ch x $read_fill); } if ($write_fill > 0) { print $write_color . ($wrfill_ch x $write_fill); } print $norm_color. "\n"; } @data = undef; } # vim: ts=2 sw=2