Need to do sftp server transaction check such as upload/download a test file?
Here's the script for that. You can also download it here
#!/usr/bin/perl
use Net::SFTP;
use Try::Tiny;
$SCRIPT_NAME="check_sftp";
$SCRIPT_PATH="/tmp/check_sftp";
$local_dir=$SCRIPT_PATH;
$SSH_KEY_FILE=$local_dir."/id_rsa";
#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
if($ARGV[0] eq "" || $ARGV[1] eq "" || $ARGV[2] eq "")
{
print "usage: ./$SCRIPT_NAME.pl server_name user_name sftp_directory\n";
exit;
}
$SERVER_NAME=$ARGV[0];
$USER_NAME=$ARGV[1];
$sftp_dir=$ARGV[2];
#crreate the test file
$test_str=`date +"%m-%d-%Y_%H:%M"`;
chomp($test_str);
$test_file=$SERVER_NAME."_test.txt";
$local_test_file=$local_dir."/".$test_file;
$sftp_test_file=$sftp_dir."/".$test_file;
$command="echo $test_str > $local_test_file";
system($command);
#set sftp connecton detail
%sftp_args=( user => "$USER_NAME", ssh_args => {identity_files => ["$SSH_KEY_FILE"]} );
$sftp_args{warn}=false;
#$sftp_args{debug} = 1;
try
{
$sftp=Net::SFTP->new($SERVER_NAME, %sftp_args) or die "Cannot connect to $SERVER_NAME";
#upload file
undef $staus;
$status=$sftp->put($local_test_file,$sftp_test_file);
if(!defined($status))
{
print "Cannot upload file to $SERVER_NAME";
exit;
}
#remove local test file
$command="rm $local_test_file";
system($command);
#download the test file
undef $staus;
$status=$sftp->get($sftp_test_file,$local_test_file);
if(!defined($status))
{
print "Cannot download file from $SERVER_NAME";
exit;
}
#check to make sure the test file got downloaded from the sftp
$result=`ls $local_test_file`;
chomp($result);
if($result ne $local_test_file)
{
print "Cannot upload/download file from $SERVER_NAME";
exit;
}
#make sure the file got downloaded correctly and contain the correct string
open(READ_FILE,$local_test_file);
$str=<READ_FILE>;
chomp($str);
if($str eq $test_str)
{
print "Success";
}
else
{
print "Cannot upload/download file from $SERVER_NAME";
}
close(READ_FILE);
#remove local test file
$command="rm $local_test_file";
system($command);
}
catch
{
print "Cannot connect to $SERVER_NAME";
exit;
};
Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts
Friday, March 29, 2013
Dump variable value in perl
You can use Data::Dumper to print out the values stored in a perl variable similar to var_dump in php.
Data:Dumper should come as part of your standard perl distribution, so you don't need to install it.
If not, you can install it easily using cpan, such as
cpan install Data::Dumper
Usage Example:
use Data::Dumper;
my $var = (
a => 'value1',
b => 'value2',
);
print Dumper($var);
Data:Dumper should come as part of your standard perl distribution, so you don't need to install it.
If not, you can install it easily using cpan, such as
cpan install Data::Dumper
Usage Example:
use Data::Dumper;
my $var = (
a => 'value1',
b => 'value2',
);
print Dumper($var);
Labels:
Perl
Thursday, March 28, 2013
Handle exception in perl
The easiest way to catch perl exception is to use Try::Tiny
Here's an example:
use Try::Tiny;
try
{
#put your normal code here or the code that you think might generate exception
$sftp=Net::SFTP->new($SERVER_NAME, %sftp_args) or die "Cannot connect to sftp server";
}
catch
{
#catch the exception and execute your exception handling code here such as printing a custom error
print "Cannot connect to sftp server";
exit;
};
Here's an example:
use Try::Tiny;
try
{
#put your normal code here or the code that you think might generate exception
$sftp=Net::SFTP->new($SERVER_NAME, %sftp_args) or die "Cannot connect to sftp server";
}
catch
{
#catch the exception and execute your exception handling code here such as printing a custom error
print "Cannot connect to sftp server";
exit;
};
Labels:
Perl
Monday, March 25, 2013
Nagios Apache Audit
Nagios Web Interface shows a lot of info about the hosts and services, but it 's not easy audit the change that were made to it such as who made a change?
The following script grab the apache log for all the changes that were made in nagios for that particular day.
(You can download the script here: nagios_apache_audit.pl)
#!/usr/bin/perl
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
#$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
#print $theTime;
my $today="$dayOfMonth/$months[$month]/$year";
#print $today . "\n";
open FILE, "/var/log/httpd/access_log" or die "cannot open apache log $!";
my @lines=<FILE>;
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following changes were made to nagios:\n<BR>";
print "---------------------------------------------------\n\n<BR><BR>";
foreach $line (@lines)
{
if($line=~m/$today/ && $line=~m/POST/ && $line=~m/cmd.cgi\?cmd_typ=/)
{
}
The following script grab the apache log for all the changes that were made in nagios for that particular day.
(You can download the script here: nagios_apache_audit.pl)
#!/usr/bin/perl
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
#$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
#print $theTime;
my $today="$dayOfMonth/$months[$month]/$year";
#print $today . "\n";
open FILE, "/var/log/httpd/access_log" or die "cannot open apache log $!";
my @lines=<FILE>;
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following changes were made to nagios:\n<BR>";
print "---------------------------------------------------\n\n<BR><BR>";
foreach $line (@lines)
{
if($line=~m/$today/ && $line=~m/POST/ && $line=~m/cmd.cgi\?cmd_typ=/)
{
print $line . "<BR>";
}}
Nagios Audit
Nagios Web Interface shows a lot of info about the hosts and services, but it 's not easy to find all the hosts and services problems that were already acknowledged or were scheduled for downtime. Also, when you have a lot of hosts and services to monitor, it is sometime hard to tell if the scheduled downtime should be over or if the check should be re-enabled.
The following script will show all the hosts and services that were disabled, acknowledged, or scheduled for downtime. It print out a html page, so it could potential be integrated with the nagios web interface.
(You can download the script here: nagios_audit.pl)
#!/usr/bin/perl
use LWP;
my $browser = LWP::UserAgent->new;
$browser->agent('Nagios Audit');
my $servername='localhost';
my $port = 80;
my $realm = 'Nagios Access';
my $username = 'nagiosaudit';
my $password = 'password';
$browser->credentials("$servername:$port", $realm, $username, $password);
my $url='http://'.$servername.'/nagios/cgi-bin/status.cgi?host=all';
my $response = $browser->get(
$url
);
my $nagios_status=$response->as_string;
Find_Disabled_Check_Host();
Find_Disabled_Notification_Host();
Find_Scheduled_Downtime_Host();
Find_Acknowledged_Host();
Find_Disabled_Check_Service();
Find_Disabled_Notification_Service();
Find_Scheduled_Downtime_Service();
Find_Acknowledged_Service();
#####################################################################
sub Find_Host
{
my $search_string=$_[0];
#go through all the hosts, (the one on the left column)
my @lines=split(/<TD align=left valign=center CLASS=/,$nagios_status);
foreach my $line (@lines)
{
#If it's type one and the host check is disabled, then find the host name
if ( $line=~m/A HREF='extinfo.cgi\?type=1&host=/ && $line=~m/$search_string/ )
{
my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=1&host=/,$line);
my $str=@temp[0];
my $start_pos=index($str,"host=");
my $offset=5;
$start_pos+=$offset;
my $end_pos=index($str,"' title");
my $length=$end_pos-$start_pos;
my $host_name=substr($str,$start_pos,$length);
print $host_name . "\n<BR><BR>";
}
}
}
#####################################################################
sub Find_Disabled_Check_Host
{
print "\n<BR><BR>---------------------------------------------------\n<BR><BR>";
print "Active Check for the following hosts were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('Checks of this host have been disabled');
}
#####################################################################
sub Find_Disabled_Notification_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Notification for the following hosts were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('Notifications for this host have been disabled');
}
#####################################################################
sub Find_Scheduled_Downtime_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following hosts are under maintenance:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('This host is currently in a period of scheduled downtime');
}
#####################################################################
sub Find_Acknowledged_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following hosts' problems have been acknowledged\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('This host problem has been acknowledged');
}
#####################################################################
sub Find_Service
{
#print "\n<BR>---------------------------------------------------\n<BR>";
#print "Active Check for the following services were disabled:\n<BR>";
#print "---------------------------------------------------\n<BR>\n<BR>";
#go through all the services
my @lines=split(/<TD ALIGN=LEFT valign=center CLASS=/,$nagios_status);
my $search_string=$_[0];
foreach my $line (@lines)
{
#If it's type one and the service check is disabled, then find the host name and service name
if ( $line=~m/A HREF='extinfo.cgi\?type=2&host=/ && $line=~m/$search_string/ )
{
my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=2&host=/,$line);
my $str=@temp[1];
my $start_pos=0;
my $offset=0;
$start_pos+=$offset;
my $end_pos=index($str,'&');
my $length=$end_pos-$start_pos;
my $host_name=substr($str,$start_pos,$length);
my @temp2=split(/#comments/,$str);
$str=@temp2[0];
$start_pos=index($str,"service=");
$offset=8;
$start_pos+=$offset;
$end_pos=index($str,"'>");
my $service_name="";
if($end_pos!=-1)
{
$length=$end_pos-$start_pos;
$service_name=substr($str,$start_pos,$length);
}
else
{
$service_name=substr($str,$start_pos);
}
print $host_name . " : " . $service_name . "\n<BR>";
}
}
}
#####################################################################
sub Find_Disabled_Check_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Active Check for the following services were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('Active checks of the service have been disabled - only passive checks are being accepted');
}
#####################################################################
sub Find_Disabled_Notification_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Notification for the following services were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('Notifications for this service have been disabled');
}
#####################################################################
sub Find_Scheduled_Downtime_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following services are under maintenance:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('This service is currently in a period of scheduled downtime');
}
#####################################################################
sub Find_Acknowledged_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following services' problems have been acknowledged\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('This service problem has been acknowledged');
}
The following script will show all the hosts and services that were disabled, acknowledged, or scheduled for downtime. It print out a html page, so it could potential be integrated with the nagios web interface.
(You can download the script here: nagios_audit.pl)
#!/usr/bin/perl
use LWP;
my $browser = LWP::UserAgent->new;
$browser->agent('Nagios Audit');
my $servername='localhost';
my $port = 80;
my $realm = 'Nagios Access';
my $username = 'nagiosaudit';
my $password = 'password';
$browser->credentials("$servername:$port", $realm, $username, $password);
my $url='http://'.$servername.'/nagios/cgi-bin/status.cgi?host=all';
my $response = $browser->get(
$url
);
my $nagios_status=$response->as_string;
Find_Disabled_Check_Host();
Find_Disabled_Notification_Host();
Find_Scheduled_Downtime_Host();
Find_Acknowledged_Host();
Find_Disabled_Check_Service();
Find_Disabled_Notification_Service();
Find_Scheduled_Downtime_Service();
Find_Acknowledged_Service();
#####################################################################
sub Find_Host
{
my $search_string=$_[0];
#go through all the hosts, (the one on the left column)
my @lines=split(/<TD align=left valign=center CLASS=/,$nagios_status);
foreach my $line (@lines)
{
#If it's type one and the host check is disabled, then find the host name
if ( $line=~m/A HREF='extinfo.cgi\?type=1&host=/ && $line=~m/$search_string/ )
{
my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=1&host=/,$line);
my $str=@temp[0];
my $start_pos=index($str,"host=");
my $offset=5;
$start_pos+=$offset;
my $end_pos=index($str,"' title");
my $length=$end_pos-$start_pos;
my $host_name=substr($str,$start_pos,$length);
print $host_name . "\n<BR><BR>";
}
}
}
#####################################################################
sub Find_Disabled_Check_Host
{
print "\n<BR><BR>---------------------------------------------------\n<BR><BR>";
print "Active Check for the following hosts were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('Checks of this host have been disabled');
}
#####################################################################
sub Find_Disabled_Notification_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Notification for the following hosts were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('Notifications for this host have been disabled');
}
#####################################################################
sub Find_Scheduled_Downtime_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following hosts are under maintenance:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('This host is currently in a period of scheduled downtime');
}
#####################################################################
sub Find_Acknowledged_Host
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following hosts' problems have been acknowledged\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Host('This host problem has been acknowledged');
}
#####################################################################
sub Find_Service
{
#print "\n<BR>---------------------------------------------------\n<BR>";
#print "Active Check for the following services were disabled:\n<BR>";
#print "---------------------------------------------------\n<BR>\n<BR>";
#go through all the services
my @lines=split(/<TD ALIGN=LEFT valign=center CLASS=/,$nagios_status);
my $search_string=$_[0];
foreach my $line (@lines)
{
#If it's type one and the service check is disabled, then find the host name and service name
if ( $line=~m/A HREF='extinfo.cgi\?type=2&host=/ && $line=~m/$search_string/ )
{
my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=2&host=/,$line);
my $str=@temp[1];
my $start_pos=0;
my $offset=0;
$start_pos+=$offset;
my $end_pos=index($str,'&');
my $length=$end_pos-$start_pos;
my $host_name=substr($str,$start_pos,$length);
my @temp2=split(/#comments/,$str);
$str=@temp2[0];
$start_pos=index($str,"service=");
$offset=8;
$start_pos+=$offset;
$end_pos=index($str,"'>");
my $service_name="";
if($end_pos!=-1)
{
$length=$end_pos-$start_pos;
$service_name=substr($str,$start_pos,$length);
}
else
{
$service_name=substr($str,$start_pos);
}
print $host_name . " : " . $service_name . "\n<BR>";
}
}
}
#####################################################################
sub Find_Disabled_Check_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Active Check for the following services were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('Active checks of the service have been disabled - only passive checks are being accepted');
}
#####################################################################
sub Find_Disabled_Notification_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "Notification for the following services were disabled:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('Notifications for this service have been disabled');
}
#####################################################################
sub Find_Scheduled_Downtime_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following services are under maintenance:\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('This service is currently in a period of scheduled downtime');
}
#####################################################################
sub Find_Acknowledged_Service
{
print "\n<BR>---------------------------------------------------\n<BR>";
print "The following services' problems have been acknowledged\n<BR>";
print "---------------------------------------------------\n<BR>\n<BR>";
Find_Service('This service problem has been acknowledged');
}
Subscribe to:
Posts (Atom)