Ad Widget

Collapse

Replacement for SMTP-check (supports SSL)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frater
    Senior Member
    • Oct 2010
    • 340

    #1

    Replacement for SMTP-check (supports SSL)

    I don't know what the "Simple" smtp-check does.
    All I know is that it triggers more than it should and it doesn't support SSL.

    I therefore wrote this small external script. Most of the work is done by "swaks". You can easily install it with yum or apt-get (apt-get install swaks).

    Code:
    Item
       Email (SMTP) server
       external
       smtpup[ {HOST.CONN1 ]
       interval 60s
       Show value "Service State"
    Code:
    mkdir /etc/zabbix/external
    ln -s /usr/local/sbin/smtpup /etc/zabbix/external/smtpup
    cat /usr/local/sbin/smtpup
    Code:
    #!/bin/bash
    export PATH=${PATH}:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin
    
    # Script mainly used for Zabbix (http://www.zabbix.com) to check if an SMTP-server is up
    # Zabbix itself has an smtp-check function, but due to its nature it gives many false positives.
    # This script, mainly because it uses swaks, has a dialogue with the SMTP-server that's comparable
    # with a real-life SMTP-session. It says Hello (HELO) and then the SMTP-server tells the things he
    # understands. This script then gives a QUIT, because it just wants to test if the server is working#
    # Put the script in /usr/local/sbin and make a symbolic link in /etc/zabbix/externalscripts
    #
    # ln -s /usr/local/sbin/smtpup /etc/zabbix/externalscripts/smptup
    #
    # The latter is important because it needs this to differentiate between executing it from
    # command-line and executing it from zabbix (zabbix gives an extra parameter)
    
    #
    # An item in zabbix is like this:
    #
    #       Description:            Email (SMTPAUTH) server is responding
    #       Type:                   external check
    #       Key:                    smtpup[ {HOST.CONN1} 587 ]
    #       Type of information:    Numeric (unsigned)
    #       Data type:              Decimal
    #       Update interval:        60
    #       Show value:             Service state
    
    
    # This Zabbix' workaround will lose the first paramater when executed from
    # /etc/zabbix/externalscripts/
    echo "${BASH_SOURCE}" | grep -q "zabbix" && shift 1
    
    # Server
    OPTIONS=
    SERVER="$1"
    PORT="$2"
    
    # CONSTANTS
    RETURN=0
    QUITON=HELO
    REPLY='^<. *250'
    TIMEOUT=5
    
    # Put your Internet FQDN-address here.
    # If your IP also has its reverse DNS to that same FQDN it is even better.
    
    # HELO=smtp.yourdomain.com
    HELO=
    [ -z "${HELO}" ] || OPTIONS="${OPTIONS} --helo ${HELO}"
    
    if [ ! -z "${SERVER}" ] ; then
     [ -z ${PORT} ]         && PORT=25
     [ "${PORT}" = "465" ]  && OPTIONS="${OPTIONS} -tlsc"
    
     swaks ${OPTIONS} -q ${QUITON} -p ${PORT} --timeout ${TIMEOUT} -s ${SERVER} 2>/dev/null | grep -q "${REPLY}" && RETURN=1
    fi
    echo "$RETURN"
    Last edited by frater; 20-01-2012, 12:12.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
  • frater
    Senior Member
    • Oct 2010
    • 340

    #2
    Both having used this script for a while and monitoring an extreme slow server I decided to enhance the script so it makes full use of the 'TimeOut=' value in /etc/zabbix/zabbix_server.conf

    Although I was using the "--timeout" value in swaks, it took significantly longer than the limit I imposed. This is not good with Zabbix, as it will turn off monitoring for this item when it is supposed to give an alarm bell.

    I decided to write a workaround for it by running swaks in the background and have my own wait cycle.

    More info can be found in the script itself.

    I have added the template Standalone where I use this script and 2 other I wrote (httpspeed & certexpire)

    Code:
    #!/bin/bash
    export PATH=${PATH}:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin
    
    # Script mainly used for Zabbix (http://www.zabbix.com) to check if an SMTP-server is up
    # Zabbix itself has an smtp-check function, but due to its nature it gives many false positives.
    # This script, mainly because it uses swaks, has a dialogue with the SMTP-server that's comparable
    # with a real-life SMTP-session. It says Hello (HELO) and then the SMTP-server tells the things he
    # understands. This script then gives a QUIT, because it just wants to test if the server is working#
    # Put the script in /usr/local/sbin and make a symbolic link in /etc/zabbix/externalscripts
    #
    # ln -s /usr/local/sbin/smtpup /etc/zabbix/externalscripts/smptup
    #
    # The latter is important because it needs this to differentiate between executing it from
    # command-line and executing it from zabbix (zabbix gives an extra parameter)
    
    #
    # An item in zabbix is like this:
    #
    #       Description:            Email (SMTPAUTH) server is responding
    #       Type:                   external check
    #       Key:                    smtpup[ {HOST.CONN1} 587 ]
    #       Type of information:    Numeric (unsigned)
    #       Data type:              Decimal
    #       Update interval:        60
    #       Show value:             Service state
    
    
    # This Zabbix' workaround will lose the first paramater when executed from
    # /etc/zabbix/externalscripts/
    
    TIMEOUT=20
    RETVAL=0
    
    # If called by zabbix, handle some things different
    if echo "${BASH_SOURCE}" | grep -q "zabbix" ; then
      # get rid of 1st parameter
      shift 1
    
      # Change TimeOut value to the one in /etc/zabbix/zabbix_server.conf
      ZABBIX_TIMEOUT=`grep -i '^Timeout' /etc/zabbix/zabbix_server.conf 2>/dev/null | awk -F= '{print $2}' | tr -cd '0-9'`
      if [ -z "${ZABBIX_TIMEOUT}" ] ; then
        TIMEOUT=3
      else
        # Let's take 1 second less than the one in /etc/zabbix/zabbix_server.conf and just hope to be in time
        TIMEOUT=$(( ${ZABBIX_TIMEOUT} - 1 ))
      fi
    fi
    
    # Server
    OPTIONS=
    SERVER="$1"
    PORT="$2"
    
    # CONSTANTS
    QUITON=HELO
    REPLY='^<. *250'
    
    # Put your Internet FQDN-address here.
    # If your IP also has its reverse DNS to that same FQDN it is even better.
    
    # HELO=smtp.yourdomain.com
    HELO=
    
    [ -z "${HELO}" ] || OPTIONS="${OPTIONS} --helo ${HELO}"
    
    if [ ! -z "${SERVER}" ] ; then
     [ -z ${PORT} ]         && PORT=25
     [ "${PORT}" = "465" ]  && OPTIONS="${OPTIONS} -tlsc"
    
     SCRATCH=`mktemp`
    
     # Somehow swaks doesn't properly obey TimeOuts..  I'm running it in background and use my own timeloop
    
     # suppress terminal output
     exec 2>/dev/null
     # Start swaks in background
     swaks --timeout ${TIMEOUT} ${OPTIONS} -q ${QUITON} -p ${PORT} -s ${SERVER} 2>/dev/null >${SCRATCH} &
    
     # little offset.... If it responds fast, it doesn't need to enter the loop
     sleep .2
    
     # Wait for swaks
     n=1
     while ! grep -q "${REPLY}" ${SCRATCH} ; do
      sleep .49
      grep -q "${REPLY}" ${SCRATCH} && break
      sleep .5
      [ $n -ge ${TIMEOUT} ] && break
      let n++
     done
    
     if grep -q "${REPLY}" ${SCRATCH} ; then
       RETVAL=1
     fi
    
     # If it didn't sign off, we should better kill it
     if ! grep -q "QUIT" ${SCRATCH} ; then
      # Too late you lazy bastard, I might as well kill you...
      kill %1 2>/dev/null
     fi
    
     rm -f ${SCRATCH} 2>/dev/null
    fi
    echo "$RETVAL"
    Attached Files
    Last edited by frater; 11-08-2011, 15:46.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP

    Comment

    Working...