Created
September 4, 2014 23:25
-
-
Save abhinavn/df99537bfd244ccace26 to your computer and use it in GitHub Desktop.
iotop2csv
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/perl -w | |
| use strict; | |
| # Enabling strict with strict refs breaks the summary output (when ctrl+c is pressed) | |
| # because of the error Can't use string ("") as a HASH ref while "strict refs" in use. | |
| no strict 'refs'; | |
| #Initialize an empty hashref | |
| my $hash_ref = {}; | |
| #Catch CTRL+C and then call printsummary subroutine | |
| $SIG{'INT'} = \&printsummary; | |
| #Pipe the output from iotop to a file handle | |
| open fh, "iotop -bt|" or die $!; | |
| # Build the initial Total Value key/values | |
| $hash_ref->{'tsample'} = 0; | |
| $hash_ref->{'tread'} = 0; | |
| $hash_ref->{'twrite'} = 0; | |
| # Divider of columns | |
| my $div = ","; | |
| #Print the header | |
| print "#" x 50 . "\n"; | |
| print "Output from iotop -bt\n"; | |
| print "#" x 50 . "\n"; | |
| print "timestamp" . $div . "tid" . $div . "user" . $div . "read" . $div . "total read each tid" . $div . "write" . $div . "total write each tid" . $div . "cmd" . $div . "sample number" . $div . "Percent of Total Read" . $div . "Percent of Total Write" . "\n"; | |
| #While data from iotop process it. Since we get lots of 0.00 read and write values | |
| #don't waste time storing/printing values that are useless. Only store lines that | |
| #have a value to read/write. | |
| while(<fh>) { | |
| my ($ts,$tid,$prio,$user,$read,undef,$write,undef,undef,undef,undef,undef,$cmd) = split(/\s+/, "$_"); | |
| chomp($cmd); | |
| #Make sure we fill each tid with dummy values if the tid is a real valid number | |
| if ( !$hash_ref->{$tid} && ( $tid =~ m/^-?\d+$/ || $tid =~ m/^-?\d+[\/|\.]\d+$/ ) ) | |
| { | |
| $hash_ref->{$tid}->{'user'} = 'null'; | |
| $hash_ref->{$tid}->{'read'} = 0; | |
| $hash_ref->{$tid}->{'write'} = 0; | |
| $hash_ref->{$tid}->{'cmd'} = 'null'; | |
| $hash_ref->{$tid}->{'samples'} = 0; | |
| } | |
| #Continue if we have already created a key for the tid and the tid is a real valid number | |
| if ( $hash_ref->{$tid} && ( $tid =~ m/^-?\d+$/ || $tid =~ m/^-?\d+[\/|\.]\d+$/ ) ) | |
| { | |
| $hash_ref->{$tid}->{'user'} = $user; | |
| $hash_ref->{$tid}->{'read'} += $read if $read =~ m/^-?\d+$/ || $read =~ m/^-?\d+[\/|\.]\d+$/; | |
| $hash_ref->{$tid}->{'write'} += $write if $write =~ m/^-?\d+$/ || $write =~ m/^-?\d+[\/|\.]\d+$/; | |
| $hash_ref->{$tid}->{'cmd'} = $cmd; | |
| $hash_ref->{$tid}->{'samples'}++; | |
| # create totals key/value pair | |
| $hash_ref->{'tsample'}++; | |
| $hash_ref->{'tread'} += $read if $read =~ m/^-?\d+$/ || $read =~ m/^-?\d+[\/|\.]\d+$/; | |
| $hash_ref->{'twrite'} += $write if $write =~ m/^-?\d+$/ || $write =~ m/^-?\d+[\/|\.]\d+$/; | |
| # Print values of the read/write as they happen | |
| if ( ( $read =~ m/^-?\d+$/ || $read =~ m/^-?\d+[\/|\.]\d+$/ || $write =~ m/^-?\d+$/ || $write =~ m/^-?\d+[\/|\.]\d+$/ ) && ( $read > 0 || $write > 0 ) ) | |
| { | |
| my $ptr = 0; | |
| my $ptw = 0; | |
| $ptr = ( $hash_ref->{$tid}->{'read'} / $hash_ref->{'tread'} ) * 100 if ( $read > 0 ) && ( $hash_ref->{'tread'} > 0 ); | |
| $ptw = ( $hash_ref->{$tid}->{'write'} / $hash_ref->{'twrite'} ) * 100 if ( $write > 0 ) && ( $hash_ref->{'twrite'} > 0 ); | |
| print $ts . "$div"; | |
| print $tid . "$div"; | |
| print $hash_ref->{$tid}->{'user'} . "$div"; | |
| print $read . "$div"; | |
| print "$hash_ref->{$tid}->{'read'}" . "$div"; | |
| print $write . "$div"; | |
| print "$hash_ref->{$tid}->{'write'}" . "$div"; | |
| print $hash_ref->{$tid}->{'cmd'} . "$div"; | |
| print $hash_ref->{$tid}->{'samples'} . "$div"; | |
| print sprintf("%.2f", $ptr) . "$div"; | |
| print sprintf("%.2f", $ptw) . "\n"; | |
| } | |
| } | |
| } | |
| # Close the filehandle when we are done. | |
| close(fh); | |
| #Sub routine to print out a summary of disk I/O. | |
| sub printsummary { | |
| print "#" x 50 . "\n"; | |
| print "Caught Ctrl+C. Printing Disk I/O summary.\n"; | |
| print "#" x 50 . "\n"; | |
| print "Total Bytes Read: $hash_ref->{'tread'}\n"; | |
| print "Total Bytes Write: $hash_ref->{'twrite'}\n"; | |
| print "#" x 50 . "\n"; | |
| for my $id ( keys %$hash_ref ) | |
| { | |
| if ( $hash_ref->{$id}->{'read'} > 0 || $hash_ref->{$id}->{'write'} > 0 ) | |
| { | |
| my $psptr = 0; | |
| my $psptw = 0; | |
| $psptr = ( $hash_ref->{$id}->{'read'} / $hash_ref->{'tread'} ) * 100 if ( $hash_ref->{'tread'} > 0 ); | |
| $psptw = ( $hash_ref->{$id}->{'write'} / $hash_ref->{'twrite'} ) * 100 if ( $hash_ref->{'twrite'} > 0 ); | |
| print "$id" . " "; | |
| print $hash_ref->{$id}->{'user'} . " "; | |
| print $hash_ref->{$id}->{'read'} . " "; | |
| print $hash_ref->{$id}->{'write'} . " "; | |
| print $hash_ref->{$id}->{'cmd'} . " "; | |
| print $hash_ref->{$id}->{'samples'} . " "; | |
| print sprintf("%.2f", $psptr) . " "; | |
| print sprintf("%.2f", $psptw); | |
| print "\n" | |
| } | |
| } | |
| print "#" x 50 . "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment