Page 1 of 1

Monitoring of SFTP

Posted: Wed Nov 23, 2016 10:04 am
by sth_bytelab
Hi
How can I monitor a SFTP-server?

Re: Monitoring of SFTP

Posted: Wed Nov 23, 2016 11:50 am
by rkennedy
What are you looking to monitor exactly on it? I would take a look at our Exchange to see if any of the plugins will work for you - https://exchange.nagios.org

If you just want to make sure it's working, you could use something simple like check_tcp.

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

Re: Monitoring of SFTP

Posted: Thu Nov 24, 2016 9:55 am
by sth_bytelab
Thank you for the reply.
I would like to login in to the SFTP server to check that it is available.
I would also like to check if it is available from outside of our local subnet. Is there a way there I can rebound the tcp-command through another server?

Re: Monitoring of SFTP

Posted: Fri Nov 25, 2016 2:17 pm
by ruffsense
something like this?

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;
}
save it as check_sftp.pl

Re: Monitoring of SFTP

Posted: Mon Nov 28, 2016 2:29 pm
by rkennedy
Thanks @ruffsense!

You can run checks from other machines by using check_nrpe as a 'proxy' between different machines.