Inhaltsverzeichnis

Nagios Script: check_smb

Das folgende Skript wurde als Nagios Plugin erzeugt und ist ein einfaches Shell-Skript, welches prüft, ob der Samba Dienst des Remote-Systems verfügbar ist.

Requirements

Syntax

Return-Codes

Skript

bash

#!/bin/sh
#----------------------------------------------------------
# check_smb -- Script to check if Samba Service is available
#----------------------------------------------------------
# Author:  Thomas Baumann, tiri GmbH 
# Website: <a href="http://www.tiri.li/">http://www.tiri.li</a>
# Created: 2009-10-30
#----------------------------------------------------------

# DEFAULT return Code to 3==UNKNOWN
rc=3
SMBCLIENT=/usr/bin/smbclient

if [ $# -lt 1 ]; then
        echo "Option -$OPTARG requires an argument (Hostname/IP)" >&2
        exit 3
fi

if [ ! -x $SMBCLIENT ]; then
        echo "UNKNOWN: $SMBCLIENT not found"
        exit 1
fi
while getopts ":H:-help" opt ; do
  case $opt in
    H)
        RESULT=$($SMBCLIENT -N -L $OPTARG 2>&1)
        rc=$?
        [ $rc -eq 0 ] && echo -n "OK: "
        ;;
    \-)
        cat << EOHELP
# $(basename $0) - Simple Samba Check Script
#----------------------------------------------------------
# Author:  Thomas Baumann, tiri GmbH 
# Website: <a href="http://www.tiri.li/">http://www.tiri.li</a>
# Created: 2009-10-30
#----------------------------------------------------------

 This script takes only one simple Parameter, the Target
 Host, and checks with an anonymous / passwordless login
 if the Samba Service on the remote system is available.

Example:

 check_smb -H 127.0.0.1

Requirements:

 smbclient

EOHELP
        exit 0
        ;;
    \?)
        echo "Invalid option -$OPTARG" >&2
        exit 3
        ;;
    :)
        echo "Option -$OPTARG requires an argument (Hostname/IP)" >&2
        exit 3
        ;;
    *)
        echo "Invalid option -$OPTARG" >&2
        exit 3
        ;;
  esac
done
echo "$RESULT" | grep ' Server=' | head -1
exit $rc

Entstehung

Dieses Skript habe ich geschrieben, da das check_smb Skript von NagiosExchange nicht verfügbar war. Ferner ist es eine Demonstration dessen, wie einfach Nagios Plugins geschrieben werden können und wie die bash builtin Funktion getopts zu verwenden ist.