#!/usr/bin/perl # our needed packages use strict "vars"; use Getopt::Std; # Global Variables # command line options my $opt_string = "hd:c:i:t:o:"; getopts( "$opt_string", \my %opt ) or usage() and exit(1); # system("clear"); if ( $opt{'h'} ) { usage(); exit 0; } my $DEVICE = $opt{'d'}; my $COMMAND = $opt{'c'}; my $INTERVAL = $opt{'i'}; my $TOTALTIME = $opt{'t'}; my $OUTFILE = $opt{'o'}; ( usage() and die "Please supply a device name\n" ) unless $DEVICE; if ($COMMAND and $TOTALTIME) { usage() and die "-c and -t options can not be used at the same time! \n"; } if (not $TOTALTIME and not $COMMAND) { ( usage() and die "Please supply at least on of this parameters, command to run or duration time!\n" ); } ( usage() and die "Please supply an interval time\n" ) unless $INTERVAL; ( usage() and die "Please supply a file name\n" ) unless $OUTFILE; if ($TOTALTIME and $INTERVAL > $TOTALTIME) { (usage() and die "INTERVAL cannot be greater than TOTAL TIME!! \n"); } #################### Main part ######################## chomp($DEVICE); print "selected device to investigate is: " . $DEVICE . "\n"; my $FILE = "/proc/diskstats"; my $T=1; my $start = time; my $devicecount=0; #### checking if the entered device is exist or not! open (STATS, "cat $FILE |") or die "Error creating file $!\n"; while (my $line = ) { if ($line =~ /($DEVICE)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)/ ) { $devicecount++; } } if ($devicecount == 0 ){ (die "Specified device,(" .$DEVICE. ") could not be found on this machine! \n"); } ### Doing the script by considering TOTALTIME mode if ($TOTALTIME){ while ($T=1) { if (time >= $start+$TOTALTIME){ $T = 0; exit } open (STATS, "cat $FILE |") or die "Error creating file $!\n"; while (my $line = ) { if ($line =~ /($DEVICE)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)/ ) { print $line; } } sleep($INTERVAL); } } ### Doing the script by considering COMMAND mode if ($COMMAND){ my $USER = qx("whoami"); chomp $USER; # system("$COMMAND&"); sleep 1; my $goon = 1; while( $goon ){ open(PSAUX,"ps aux |") or die "Error creating PSAUX file $!\n"; my $found = 0; while ( my $targetline = ) { if ($targetline =~ /^$USER\s+(\d+)\s.*\d:\d\d\s$COMMAND.*$/) { open (STATS, "cat $FILE |") or die "Error creating file $!\n"; while (my $line = ) { if ($line =~ /($DEVICE)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)/ ) { print "$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12\n"; # print $line; } } sleep($INTERVAL); $found = 1; } } close (PSAUX); if ( not $found ){ $goon = 0; } } } # ********************************************************************* sub usage { print "-h help\n"; print "-d Device name for investigate (e.g.: vda1) \n"; print "-c <'command'> a command to run and track (This cannot be used by the -t option at the same time!) \n"; print "-i interval time in seconds \n"; print "-t total time of running the script in seconds (duration) \n"; print "-o Output file name \n\n"; }