Monitoring of SFTP
Posted: Wed Nov 23, 2016 10:04 am
Hi
How can I monitor a SFTP-server?
How can I monitor a SFTP-server?
Support for Nagios products and services
https://support.nagios.com/forum/
Code: Select all
[root@centos7x64 libexec]# ./check_tcp -H 192.168.3.190 -p 22
TCP OK - 0.001 second response time on 192.168.3.190 port 22|time=0.001168s;;;0.000000;10.000000
Code: Select all
#!/usr/bin/perl -w
#
use Net::SFTP;
Net::SSH;
use Net::SSH::Perl;
use Getopt::Long qw(:config no_ignore_case);
my $hostname = 'localhost';
my $username = 'anonymous';
my $password = '[email protected]';
my $port = '22';
my $directory = '/';
my $file = '';
my $verbose = '0';
my $passive = '0';
my $timeout = "30";
my $version = "0.0.2";
sub help ();
my $options_okay = GetOptions (
'hostname|H=s' => \$hostname,
'username|u=s' => \$username,
'password|p=s' => \$password,
'port|P=s' => \$port,
'directory=s' => \$directory,
'file=s' => \$file,
'verbose=i' => \$verbose,
'version' => \$version,
'help|h' => sub {help}
);
# creating connection
my $sftp = Net::SFTP->new("$hostname", Debug => $verbose, Port => $port, Timeout => $timeout, Passive => $passive ) or die "ERROR: Cannot conect to $hostname\n";
# print "ERROR: Cannot connect to $hostname, exiting...\n";
#exit 2;
if (!$sftp->login("$username","$password")) {
print "ERROR: Sever says: ", $sftp->message;
exit 2;
}
if ($file eq "") {
if (!$sftp->ls("$directory")) {
print "WARNING: server says: " , $sftp->message;
exit 1;
} else {
print $sftp->message;
}
} else {
if (!$sftp->ls("$file")) {
print "WARNING: server says: " , $sftp->message;
exit 1;
} else {
if (!$sftp->get("$file","/tmp/check_sftp")) {
print "WARNING: server says: " , $sftp->message;
} else {
my @message = $sftp->message;
chomp @message;
print "OK: ", "$message[0] $message[1]\n";
exit 0;
}
}
}
$sftp->quit;
sub help () {
print "Copyright (c) 2016 Clinton van Axel,
This plugin checks a ftp server, logins supported. The check downloads a file and returns
OK when the download succeeded.
-H, --hostname=HOST
Name or IP address of host to check
-u, --username=username
SFTP username
-p --password=password
SFTP Password
-P --port=INTEGER
TCP port of the SFTP server (default 21)
-d --directory=PATH
Directory on the remote SFTP server
-f --file=FILENAME
Filename of the file on the remote SFTP server
-v --verbose=INT
Print verbose stuff when performing the check, default 0, other for debugging.
-h --help
Print this message.
-v --version
Print version number.
";
exit 0;
}