Ad Widget

Collapse

Could we please have a macro {HOST.IP} ?

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

    #1

    Could we please have a macro {HOST.IP} ?

    I wrote a little external script that can check the validity of {HOST.DNS}
    The item function is:

    Code:
    dnsverify[ {HOST.DNS1} ]
    It returns 1 if everything is OK and 0 if there's an error of some kind (INVALID).
    The script doesn't just get the DNS name from the default DNS-server, but checks it directly at the Authorative server.
    All this works fine....

    I would like to have some additional functionality, but found out I'm missing the macro {HOST.IP1}
    I would like to compare the DNS to the IP it is supposed to have according to the Zabbix interface.

    Code:
    dnsverify[ {HOST.DNS1} {HOST.IP1} ]
    If {HOST.DNS1} can be resolved, but it doesn't correspond {HOST.IP1} it returns a 2.
    I have also changed the value mapping of "service state" and added an "Invalid" for the value of "2".

    The problem is that I can't fully implement what I want to do as the macro {HOST.IP1} does not exist.
    Can this be added or is there some workaround?
    Last edited by frater; 30-08-2011, 10:04.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
  • frater
    Senior Member
    • Oct 2010
    • 340

    #2
    Here's the script:

    Code:
    #!/bin/bash
    export PATH=${PATH}:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin
    
    # If called by zabbix, handle some things different
    echo "${BASH_SOURCE}" | grep -q "zabbix" && shift 1
    
    ARECORD="$1"
    IP_SHOULDBE="$2"
    RETVAL=0
    
    # If DNS is not supplied then return empty string to turn ITEM into "unsupported ITEM"
    [ -z "${ARECORD}" ] && exit
    
    # Sanitize DNS-record
    ARECORD=`echo "${ARECORD}" | tr '[A-Z]' '[a-z]'`
    SAN_ARECORD=`echo "${ARECORD}" | tr -cd '[.a-z0-9-]'`
    
    # ARECORD has invalid characters, abort
    [ "${ARECORD}" = "${SAN_ARECORD}" ] || exit
    
    # add a trailing dot if it's not there
    ! echo "${ARECORD}" | grep -q ".*\.$" && ARECORD="${ARECORD}."
    # extract TOPLEVEL for further processing
    TOPLEVEL=`echo "${ARECORD}" | egrep -o "[a-z0-9-]+\.[a-z]+\.$"`
    
    # Can't extract TOPLEVEL, make ITEM invalid by returning null string
    [ -z "${TOPLEVEL}" ] && exit
    
    # Fetch the SOA record (Start of Authority) to obtain the real DNS-server
    SOA=`host -t soa ${TOPLEVEL} 2>/dev/null | grep -o "has SOA record .*" | awk '{print $4}'`
    
    if [ ! -z "${SOA}" ] ; then
      IP_ACTUAL=`host ${ARECORD} ${SOA} 2>/dev/null | grep -o 'has address .*' | head -n1 | awk '{print $3}'`
    
      # Error resolving on Authorative server... Maybe it's a CNAME on a foreign domain??
      # An authorative server is often configured to resolve only local domains.
      # Further testing of the answer is NOT done using an authorative server, this is beyond the scope of this test.
      if [ -z "${IP_ACTUAL}" ] ; then
       CNAME=`host -t cname ${ARECORD} ${SOA} 2>/dev/null | grep -o 'alias for .*' | head -n1 | awk '{print $3}'`
       IP_ACTUAL=`host ${CNAME} 2>/dev/null | grep -o 'has address .*' | head -n1 | awk '{print $3}'`
      fi
    
      # We finally have a WINNER...
      if [ ! -z "${IP_ACTUAL}" ] ; then
        if [ -z "${IP_SHOULDBE}" ] || [ "${IP_SHOULDBE}" = '0.0.0.0' ] ; then
          RETVAL=1
        else
          RETVAL=2
          [ "${IP_SHOULDBE}" = "${IP_ACTUAL}" ] && RETVAL=1
        fi
      fi
    fi
    
    echo "${RETVAL}"
    Last edited by frater; 30-08-2011, 11:34.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP

    Comment

    • frater
      Senior Member
      • Oct 2010
      • 340

      #3
      It seems the macro {IPADDRESS1} is already the one I needed.
      The layout of this wiki: http://www.zabbix.com/documentation/.../config/macros

      made me think it was something else than the IP-address that was entered in the webif. I thought it was the IP it was talking to, but I tried it, did some tests and it worked fine.

      The script can be used within a template using this syntax:

      Code:
      zabbix external
      dnsverify[ {HOST.DNS1} {IPADDRESS} ]
      interval: 900
      Numeric unsigned
      Service State
      Trigger 1
      Code:
      DNS does not resolve for {HOSTNAME}
      {Template_Standalone:dnsverify[ {HOST.DNS1} {IPADDRESS1} ].last(0)}=0
      High
      Trigger 2
      Code:
      DNS does NOT correspond the IP given for {HOSTNAME}
      {Template_Standalone:dnsverify[ {HOST.DNS1} {IPADDRESS1} ].last(0)}=2
      High
      So, we don't need an extra macro....

      Maybe the moderator can move this thread to cookbook?
      Last edited by frater; 30-08-2011, 11:46.
      Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP

      Comment

      Working...