[Shell Script] Check SFTP Availability With shell script

in #dev7 years ago

This article demonstrates how to use the community-distributed plugin check_sftp_avail in Nagios.

If you have this issue :
[nagios@supervision plugins]# ./check_sftp -p 22 -H 192.168.0.1
WARNING: Host key verification failed - unable to authenticate server

Solve it by add the RSA Fingerprint :
[nagios@supervision plugins]# sftp [email protected]
Connecting to 192.168.90.126...
The authenticity of host '192.168.90.126 (192.168.90.126)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.1' (RSA) to the list of known hosts.

#!/bin/sh
#
# Usage: ./check_sftp_avail [ -p <port> ] -H <host>
#
# Paths to commands used in this script.  These
# may have to be modified to match your system setup.

SFTP="/usr/bin/sftp"
RM="/bin/rm"

PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0.0"

. $PROGPATH/utils.sh

print_usage() {
    echo "Usage: $PROGNAME [ -p port ] -H host"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "sftp service availability plugin for Nagios"
    echo ""
    support
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

port=115  # Default for sftp
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --hostname)
            box=$2
        shift
            ;;
        -H)
            box=$2
        shift
            ;;
        --port)
            port=$2
        shift
            ;;
        -p)
            port=$2
        shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

# Make sure a hostname and port have been specified
if [ -z "$box" -o -z "$port" ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

tempfile=/tmp/check_sftp-$box.tmp
exitstatus=$STATE_UNKNOWN

$SFTP -b - $box &>$tempfile <<EOF




EOF

if [ ! -e "$tempfile" ]; then
        stdio="No sftp output found" && $RM -f $tempfile && echo $stdio && exit $STATE_UNKNOWN;
fi

status=`tail -2 $tempfile`


if [ "`grep -c 'Permission denied' $tempfile`" == "1" ]; then
    exit=$STATE_OK && stdio="OK: service available"
elif [ "`grep -c 'Host key verification failed' $tempfile`" == "1" ]; then
    exit=$STATE_WARNING && stdio="WARNING: Host key verification failed - unable to authenticate server"
else
    exit=$STATE_CRITICAL && stdio="CRITICAL: service unavailable"
fi

$RM -f $tempfile
echo $stdio
exit $exit
Sort:  

저렇게 하면 되는군요 좋은자료감사합니다

넵 좋게 봐주셔서 감사합니다.

우얼~~~ 조금 다듬어서 저희 sftp 모니터링에 적용해 봐야겠습니다.

Cheer Up!

  • from Clean STEEM activity supporter

great post...thanks for effort...up

thanks~

welcome:)

have a nice day~

You too:)