Ad Widget

Collapse

Sendmail

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

    #1

    Sendmail

    A script to monitor sendmail.... (I'm still using it)

    Make sure your /etc/zabbix/zabbix_agentd.conf contains at least the proper hostname

    If you have questions, just ask in this thread.

    create the items:
    Code:
    stat_data_format_error
    maxfixed
    stat_localconfigurationerror
    reject4
    reject5
    domain
    stat_sent
    stat_service_unavailable
    stat_user_unknown
    /usr/local/sbin/zabbix-sendmail
    Code:
    #!/bin/sh
    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    
    NAME=`echo "$(basename $(readlink -f $0))" | tr ' +=-' '_' | tr '[A-Z]' '[a-z]' | tr -cd [a-z_]`
    OFFSETFILE=/tmp/${NAME}.offset
    MAILLOG=/var/log/maillog
    
    if [ -z "`which logtail`" ] ; then
      echo "logtail is not installed, install with: apt-get install logtail" >&2
      exit 1
    fi
    
    sender=`which zabbix_sender` 2>/dev/null
    [ -z "${sender}" ] && sender=/usr/bin/zabbix_sender
    if [ ! -x "${sender}" ] ; then
      echo "zabbix_sender is not installed, install with: apt-get install zabbix-sender" >&2
      exit 1
    fi
    
    ZABBIX_CONF=/etc/zabbix/zabbix_agentd.conf
    DEBUG=0
    
    function zsend {
    
      key="$2"
      [ -z "$key" ] && key="`echo "$1" | sed 's/^ *//' | tr ' +=-' '_' | tr '[A-Z]' '[a-z]' | tr -cd [a-z_]`"
      value=`grep -c "$1" ${LOGSNIPPET}`
    
      if [ ${DEBUG} -eq 0 ] ; then
        ${sender} -c $ZABBIX_CONF -k "${key}" -o "${value}" 2>&1 >/dev/null
      else
        echo "Send key \"${key}\" with value \"${value}\"" >&2
        ${sender} -c $ZABBIX_CONF -vv -k "${key}" -o "${value}"
      fi
    }
    
    LOGSNIPPET=`mktemp`
    LOG=/var/log/maillog
    
    logtail -f ${LOG} -o ${OFFSETFILE} >${LOGSNIPPET}
    
    zsend ' stat=Data format error'
    zsend ' stat=Deferred'
    zsend ' stat=Host unknown'
    zsend ' stat=Local.configuration.error'
    zsend ' stat=maillog Message exceeds maximum fixed size' "maxfixed"
    zsend ' stat=Sent'
    zsend ' stat=Service unavailable'
    zsend ' stat=User unknown'
    zsend ' Domain of sender address .* does not exist' 'domain'
    zsend ' reject=4' 'reject4'
    zsend ' reject=5' 'reject5'
    
    rm -f ${LOGSNIPPET}
    create a cronjob, preferably with a symbolic link
    ln -s /usr/local/sbin/zabbix-sendmail /etc/cron.min/zabbix-sendmail
    Last edited by frater; 30-10-2010, 17:57.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
  • frater
    Senior Member
    • Oct 2010
    • 340

    #2
    Although it seems nobody is using it, I will post an updated version here.

    I had the mailq working from the agent with a supporting script becuase I couldn't do it directly from the agent.

    Anybody knows why I can't put this in my /etc/zabbix/zabbix_agentd.conf:
    Code:
    UserParameter=sendmailq,/usr/bin/mailq | tail -n1 | awk '{print $3}'
    I stopped investigating it and decided to put the mailq in this script as well...
    It needed a modify for this.
    I still would like to know the answer to my previous question.

    This is working flawless:

    # cat /usr/local/sbin/zabbix-sendmail
    Code:
    #!/bin/sh
    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    
    NAME=`echo "$(basename $(readlink -f $0))" | tr ' +=-' '_' | tr '[A-Z]' '[a-z]' | tr -cd [a-z_]`
    OFFSETFILE=/tmp/${NAME}.offset
    MAILLOG=/var/log/maillog
    
    if [ -z "`which logtail`" ] ; then
      echo "logtail is not installed, install with: apt-get install logtail" >&2
      exit 1
    fi
    
    sender=`which zabbix_sender` 2>/dev/null
    [ -z "${sender}" ] && sender=/usr/bin/zabbix_sender
    if [ ! -x "${sender}" ] ; then
      echo "zabbix_sender is not installed, install with: apt-get install zabbix-sender" >&2
      exit 1
    fi
    
    ZABBIX_CONF=/etc/zabbix/zabbix_agentd.conf
    DEBUG=1
    
    function zsend {
    
      key="$2"
      [ -z "$key" ] && key="`echo "$1" | sed 's/^ *//' | tr ' +=-' '_' | tr '[A-Z]' '[a-z]' | tr -cd [a-z_]`"
      value="$3"
      [ -z "${value}" ] && value=`grep -c "$1" ${LOGSNIPPET}`
    
      if [ ${DEBUG} -eq 0 ] ; then
        ${sender} -c $ZABBIX_CONF -k "${key}" -o "${value}" 2>&1 >/dev/null
      else
        echo "Send key \"${key}\" with value \"${value}\"" >&2
        ${sender} -c $ZABBIX_CONF -vv -k "${key}" -o "${value}"
      fi
    }
    
    LOGSNIPPET=`mktemp`
    LOG=/var/log/maillog
    
    # Take the last piece of the log that's not been read.
    logtail -f ${LOG} -o ${OFFSETFILE} >${LOGSNIPPET}
    
    # get the number of mails in the queue
    sendmailq=`mailq 2>/dev/null | tail -n1 | awk '{print $3}'`
    [ -z "${sendmailq}" ] && sendmailq=0
    
    # Send the queue
    zsend 'mailq' 'mailq' ${sendmailq}
    
    # Analyse the log and send the data
    zsend ' stat=Data format error'
    zsend ' stat=Deferred'
    zsend ' stat=Host unknown'
    zsend ' stat=Local.configuration.error'
    zsend ' stat=maillog Message exceeds maximum fixed size' "maxfixed"
    zsend ' stat=Sent'
    zsend ' stat=Service unavailable'
    zsend ' stat=User unknown'
    zsend ' Domain of sender address .* does not exist' 'domain'
    zsend ' reject=4' 'reject4'
    zsend ' reject=5' 'reject5'
    
    rm -f ${LOGSNIPPET}
    I also created a template....
    Attached Files
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP

    Comment

    • Dunmarie
      Junior Member
      • Jan 2009
      • 6

      #3
      Thx. It works like beautifully.

      Comment

      • boe
        Junior Member
        • Dec 2012
        • 24

        #4
        Originally posted by frater
        Anybody knows why I can't put this in my /etc/zabbix/zabbix_agentd.conf:
        Code:
        UserParameter=sendmailq,/usr/bin/mailq | tail -n1 | awk '{print $3}'
        I stopped investigating it and decided to put the mailq in this script as well...
        It needed a modify for this.
        I still would like to know the answer to my previous question.
        mailq must run from the root, zabbix-agent run mailq from user zabbix.

        Try, UserParameter=sendmailq,sudo /usr/bin/mailq | tail -n1 | awk '{print $3}'

        Comment

        Working...