Category Archives: Perl

Perl Script to collect Perfstats from NetApp Storage Systems

I have written a Perl script that is used to run perfstats on NetApp storage systems. This script runs on a Linux host.

The script checks for any running instance of perfstats on the storage system. If another perfstats instance is running on the filer, this script stops and logs an error message. The script can be scheduled to run as cronjob.

#!/usr/bin/perl
#/bin/bash

print_usage();

$datetime = &date;
$logdir = "/tmp/";
$projects_directory = "/prj/perfstats"; # directory that contains perfstats.sh script

my $args = $#ARGV + 1;
if ($args < 2) {
print "insufficient arguments exitting the script\n";
logit("insufficient arguments exitting the script\n");
exit 1;
}
logit("Starting perfcmd.pl\n");

$perfcmd_controller = shift;
$perfcmd_directory = shift; # perfcmd directory contains the perl script perfcmd.pl and perfstat.sh script.
#########################################################
# To use RSH use below command
$perfcmd_command = "/perfstat7_20130425.sh -f $perfcmd_controller -l root: -F -I -i 10 -t 5";
#########################################################
# To use SSH use below command
# $perfcmd_command    = "/perfstat7_20130425.sh -f $perfcmd_controller -S -l nitish -F -I -i 30 -t 4";
#########################################################
$perfcmd_final_cmd = "$perfcmd_directory$perfcmd_command";
$perfcmd_output_suffix = "/$perfcmd_controller.perfcmd.$datetime.out";
$perfcmd_output = "$projects_directory$perfcmd_output_suffix";

print "\n";

my @perf_processes = `ps aux | grep perfstat`;
my $process;
my $count = 0;

foreach $process (@perf_processes){
if ($process =~ /$perfcmd_controller/) {
logit("first instance of perfstats running for $perfcmd_controlle\n");
$count++;
}
}
if ($count >1) {
logit("exiting because another instance of perfstat is running for $perfcmd_controller\n");
exit 1;
}

logit("No previous iteration of perfstat is running for $perfcmd_controller");
# # Run the Perfstat and send output to the output file
$output = `$perfcmd_final_cmd > $perfcmd_output || die "Error runnng perfstat"`;

logit("Completed perfcmd.pl\n");

exit 0;

###############################
######### Functions ###########
###############################
sub date {
@date = localtime();
$year = $date[5] + 1900;
$month = '0'.($date[4]+1);
$day = $date[3];
$hour = $date[2];
$min = $date[1];
$sec = $date[0];
$fill = '_';

$out = $year.$fill.$month.$fill.$day.$fill.$hour.$fill.$min.$fill.$sec;
return $out;
}
sub logit {
my $s = shift;
my ($logsec,$logmin,$loghour,$logmday,$logmon,$logyear,$logwday,$logyday,$logisdst)=localtime(time);
my $prefix = "[";
my $suffix = "]";
my $l_name = "perfcmd";
my $logtimestamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d",$logyear+1900,$logmon+1,$logmday,$loghour,$logmin,$logsec);
my $logtimestamp_final = "$prefix$logtimestamp$suffix";
$logmon++;
my $logfile="$logdir$l_name-$logmon-$logmday-logfile.log";
my $fh;
open($fh, '>>', "$logfile") or die "$logfile: $!";
print $fh "$logtimestamp_final $s\n";
close($fh);
}
sub print_usage() {
print "################################################################################\n";
print "#\tusage:\n";
print "#\t./perfcmd <filer name or IP Address> <location of perfstat.sh>\n";
print "#\te.g.\n";
print "#\t./perfcmd.pl ntap_filer /usr/nitish/perfcmd\n";
print "#\t./perfcmd.pl  /usr/nitish/perfcmd\n";
print "################################################################################\n";
}

__END__
Please follow the below procedure to schedule cronjobs on linux host

bash
export EDITOR=vi
crontab -e
/usr/nitish/perfstats/perfcmd.pl
10,15,20,25 6-14 * * * /usr/nitish/perfstats/perfcmd.pl "ntap_filer" "/usr2/nitish/perfstats"
crontab -l