Ad Widget

Collapse

yaap (yet another apache plugin)

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

    #1

    yaap (yet another apache plugin)

    I didn't like the other apache plugins, so I took my ASSP plugin and rewrote it to cater Apache.
    I just got it working so it may need some tinkering. Because it was a rewrite of another plugin it may contain some code that could look awkward....

    apache2zabbix -x
    will create an XML-file if you have Apache listening on localhost.
    apache2zabbix
    will send data collected from Apache at localhost:80
    apache2zabbix -v
    verbose output
    apache2zabbix -f file
    can crawl several websites and send it (format: hostname host port )

    It also works for lighttpd as I just found out. I added a little line to translate "servers" into "workers"....
    Now I can also monitor my Western Digital Worldbook's webserver for which I don't have an agent......

    cd /etc/cron.min
    ln -s /usr/local/sbin/apache2zabbix
    cat /usr/local/sbin/apache2zabbix

    Code:
    #!/bin/sh
    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    
    sender="zabbix_sender"
    sender_version=`${sender} -V 2>/dev/null | head -n1 | awk '{print $3}' | grep -o '[0-9]*\.' | head -n2 | tr -cd '0-9'`
    zabbixagentd_conf=/etc/zabbix/zabbix_agentd.conf
    
    errlevel=0
    DEBUG=0
    XML=0
    
    HEADLESS=0
    tty >/dev/null || HEADLESS=1
    
    while getopts vrxf: name
    do
      case $name in
      v)   DEBUG=1;;
      x)   XML=1;;
      f)   HOSTSFILE="$OPTARG";;
      ?)   printf "Usage: %s: [-v verbose] [-x ] [-f hostfile]\n" $0
           exit 2;;
      esac
    done
    shift $(($OPTIND - 1))
    
    if [ -z "${HOSTSFILE}" ] ; then
      if [ -e /etc/apache2zabbix.conf ] ; then
        [ ${HEADLESS} -eq 0 ] && echo "Found config file /etc/apache2zabbix.conf, will use it"
        HOSTSFILE=/etc/apache2zabbix.conf
      fi
    fi
    
    curl_report () {
      if [ $curl_error -eq 127 ]  ; then
        echo "curl is not installed" >&2
      elif [ $curl_error -gt 50 ] ; then
        echo "Probably no permission, check the setting in Apache" >&2
      elif [ $curl_error -eq 7 ] || [ $curl_error -eq 28 ] ; then
        echo "Couldn't reach it, check firewall or address" >&2
      fi
      [ $curl_error -eq 0 ] || return 1
    }
    
    _gethostinfo () {
    
      URLname=$1
      URLport=$2
    
      [ -z "${URLname}" ] && URLname=localhost
      [ -z "${URLport}" ] && URLport=80
    
      # Try and find out if the server I want to reach is the same as the server it is running on
      # If so, then use the parameter --interface lo
      IFACE_PARAM=
      echo "${URLname}" | grep -qE '(127.0.0.1|localhost)' && IFACE_PARAM='--interface lo '
    
      if [ -z "${IFACE_PARAM}" ] ; then
        URLip=`echo "${URLname}" | egrep -o '^([0-9]{1,3}\.){3}[0-9]{1,3}$'`
        [ -z "${URLip}" ] && URLip=`host -tA ${URLname} | grep -o 'has address .*' | head -n1 | awk '{print $3}'`
        [ -z "${URLip}" ] || ifconfig | grep 'inet addr' | awk '{print $2}' | awk -F: '{print $2}' | grep -q ${URLip} && IFACE_PARAM='--interface lo '
      fi
    
      [ ${DEBUG} -ne 0 ] && echo "Fetching http://${URLname}:${URLport}/server-status?auto"
    
      curl http://${URLname}:${URLport}/server-status?auto ${IFACE_PARAM} --connect-timeout 2 -f 2>/dev/null > ${TMPFILE2}
      curl_error=$?
    
      egrep '^[A-Za-z ]+: [0-9.]' ${TMPFILE2} > ${TMPFILE1}
      if [ -s ${TMPFILE1} ] ; then
        echo -n '' >${SENDFILE}
        while read statline ; do
          KEY="`echo "${statline}" | awk -F: '{print $1}' | tr [[:upper:]] [[:lower:]] | sed 's/ //g;s/servers$/workers/'`"
          VALUE="`echo "${statline}" | awk -F': ' '{print $2}'`"
    
          echo "- apache[${KEY}] ${VALUE}" >>${SENDFILE}
        done < ${TMPFILE1}
        if [ $DEBUG -eq 0 ] ; then
          ${sender} -z ${Server} -s ${Hostname} -p ${ServerPort} -i ${SENDFILE} 2>&1 >/dev/null
        else
          [ ${HEADLESS} -eq 0 ] && echo "Sending \"${SENDFILE}\" using ${sender} to  ${Server}:${ServerPort}"
          ${sender} -vv -z ${Server} -s ${Hostname} -p ${ServerPort} -i ${SENDFILE}
        fi
      else
        curl_report
      fi
    }
    
    _write_header () {
    
      XDATE=`date '+%d.%m.%y'`
      XTIME=`date '+%H.%M'`
      echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" >>${TMPFILE3}
      echo -e "<zabbix_export date=\"${XDATE}\" time=\"${XTIME}\" version=\"1.0\">" >>${TMPFILE3}
      echo -e "\t<hosts>"            >>${TMPFILE3}
      echo -e "\t\t<host name=\"Template_App_Apache\">"    >>${TMPFILE3}
      echo -e "\t\t\t<proxy_hostid>0</proxy_hostid>"     >>${TMPFILE3}
      echo -e "\t\t\t<useip>1</useip>"                   >>${TMPFILE3}
      echo -e "\t\t\t<dns></dns>"                        >>${TMPFILE3}
      echo -e "\t\t\t<ip>127.0.0.1</ip>"                 >>${TMPFILE3}
      echo -e "\t\t\t<port>10050</port>"                 >>${TMPFILE3}
      echo -e "\t\t\t<status>3</status>"                 >>${TMPFILE3}
      echo -e "\t\t\t<useipmi>0</useipmi>"               >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_ip>127.0.0.1</ipmi_ip>"       >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_port>623</ipmi_port>"         >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_authtype>0</ipmi_authtype>"   >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_privilege>2</ipmi_privilege>" >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_username></ipmi_username>"    >>${TMPFILE3}
      echo -e "\t\t\t<ipmi_password></ipmi_password>"    >>${TMPFILE3}
      echo -e "\t\t\t<groups>"                           >>${TMPFILE3}
      echo -e "\t\t\t\t<group>Templates</group>"         >>${TMPFILE3}
      echo -e "\t\t\t</groups>"                          >>${TMPFILE3}
      echo -e "\t\t\t<triggers></triggers>"              >>${TMPFILE3}
      echo -e "\t\t\t<items>"                            >>${TMPFILE3}
    }
    
    _write_footer () {
    
      echo -e "\t\t\t</items>"                           >>${TMPFILE3}
      echo -e "\t\t\t<templates/>"                       >>${TMPFILE3}
      echo -e "\t\t\t<graphs/>"                          >>${TMPFILE3}
      echo -e "\t\t\t<macros/>"                          >>${TMPFILE3}
      echo -e "\t\t</host>"                              >>${TMPFILE3}
      echo -e "\t</hosts>"                               >>${TMPFILE3}
      echo -e "\t<dependencies/>"                        >>${TMPFILE3}
      echo -e "</zabbix_export>"                         >>${TMPFILE3}
    }
    
    
    _write_body () {
      echo -e "\t\t\t\t<item key=\"apache[${KEY}]\" type=\"2\" value_type=\"${VALUE_TYPE}\">" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<description>${apacheKEY}</description>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<ipmi_sensor></ipmi_sensor>"  >>${TMPFILE3}
      echo -e "\t\t\t\t\t<delay>60</delay>"            >>${TMPFILE3}
      echo -e "\t\t\t\t\t<history>30</history>"        >>${TMPFILE3}
      echo -e "\t\t\t\t\t<trends>365</trends>"         >>${TMPFILE3}
      echo -e "\t\t\t\t\t<status>0</status>"           >>${TMPFILE3}
      echo -e "\t\t\t\t\t<data_type>0</data_type>"     >>${TMPFILE3}
      echo -e "\t\t\t\t\t<units></units>"              >>${TMPFILE3}
      echo -e "\t\t\t\t\t<multiplier>0</multiplier>"   >>${TMPFILE3}
      echo -e "\t\t\t\t\t<delta>0</delta>"             >>${TMPFILE3}
      echo -e "\t\t\t\t\t<formula>1</formula>"         >>${TMPFILE3}
      echo -e "\t\t\t\t\t<lastlogsize>0</lastlogsize>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<logtimefmt></logtimefmt>"    >>${TMPFILE3}
      echo -e "\t\t\t\t\t<delay_flex></delay_flex>"    >>${TMPFILE3}
      echo -e "\t\t\t\t\t<authtype>0</authtype>"       >>${TMPFILE3}
      echo -e "\t\t\t\t\t<username></username>"        >>${TMPFILE3}
      echo -e "\t\t\t\t\t<password></password>"        >>${TMPFILE3}
      echo -e "\t\t\t\t\t<publickey></publickey>"         >>${TMPFILE3}
      echo -e "\t\t\t\t\t<privatekey></privatekey>"       >>${TMPFILE3}
      echo -e "\t\t\t\t\t<params></params>"               >>${TMPFILE3}
      echo -e "\t\t\t\t\t<trapper_hosts></trapper_hosts>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmp_community>public</snmp_community>"  >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmp_oid>interfaces.ifTable.ifEntry.ifInOctets.1</snmp_oid>"    >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmp_port>161</snmp_port>"  >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmpv3_securityname></snmpv3_securityname>"     >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmpv3_securitylevel>0</snmpv3_securitylevel>"  >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmpv3_authpassphrase></snmpv3_authpassphrase>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<snmpv3_privpassphrase></snmpv3_privpassphrase>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t<applications>"           >>${TMPFILE3}
      echo -e "\t\t\t\t\t\t<application>Apache</application>" >>${TMPFILE3}
      echo -e "\t\t\t\t\t</applications>"          >>${TMPFILE3}
    
      echo -e "\t\t\t\t</item>" >>${TMPFILE3}
    }
    
    TMPFILE1=`mktemp`
    TMPFILE2=`mktemp`
    
    Server=127.0.0.1
    Hostname=localhost
    ServerPort=10051
    egrep -i '^(hostname|server)' /etc/zabbix/zabbix_agentd.conf >${TMPFILE1}
    . ${TMPFILE1}
    
    if [ ${XML} -eq 0 ] ; then
      SENDFILE=`mktemp`
      if [ -z "${HOSTSFILE}" ]  ; then
        _gethostinfo localhost 80
      else
        while read hostline ; do
          if ! echo "${hostline}" | grep -q '^#' ; then
    
            [ $DEBUG -eq 0 ] || echo "${hostline}"
    
            Hostname=`echo "${hostline}" | awk '{print $1}'`
            URLname=`echo "${hostline}"  | awk '{print $2}'`
            URLport=`echo "${hostline}"  | awk '{print $3}' | tr -cd '0-9'`
    
            [ -z ${URLport} ] || _gethostinfo ${URLname} ${URLport}
          fi
        done <${HOSTSFILE}
      fi
      rm -f ${SENDFILE}
    else
      TMPFILE3=`mktemp`
      # Create XML file
      curl http://localhost/server-status?auto --interface lo --connect-timeout 2 -f 2>/dev/null > ${TMPFILE2}
      curl_error=$?
      egrep '[A-Za-z ]+: [0-9.]' ${TMPFILE2} > ${TMPFILE1}
    
      if [ -s "${TMPFILE1}" ] ; then
        _write_header
        while read statline ; do
          apacheKEY="`echo "${statline}" | awk -F: '{print $1}'`"
          KEY="`echo "${apacheKEY}" | tr [[:upper:]] [[:lower:]] | sed 's/ //g'`"
          VALUE="`echo "${statline}" | awk -F': ' '{print $2}'`"
    
          VALUE_TYPE=3
          echo "${VALUE}" | grep -q '\.' && VALUE_TYPE=0
    
          _write_body
        done < ${TMPFILE1}
        _write_footer
        mv ${TMPFILE3} ~/apache2zabbix.xml
        echo "Wrote an XML to ~/apache2zabbix.xml"
      else
        echo "I didn't get any data from: http://localhost/server-status?auto (curl error $curl_error)" >&2
        curl_report
        errlevel=$?
      fi
      rm -f ${TMPFILE3} 2>/dev/null
    fi
    
    rm -f ${TMPFILE1} 2>/dev/null
    rm -f ${TMPFILE2} 2>/dev/null
    
    exit ${errlevel}
    Last edited by frater; 05-02-2011, 00:25.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
  • linuxgurugamer
    Member
    • Oct 2010
    • 66

    #2
    Updated your script

    I've updated your script to make it a bit more flexible.

    I added a few lines to the -? option to describe the hostfile format and a crontab entry.

    I added the following option:

    -s to specify either lighttpd or apache as the server

    The xml generation now will be specific for either lighttpd or apache

    I fixed a bug where if you had multiple servers in the zabbix_agentd.conf, it would not work, now it will upload to all servers listed in the file.

    I wasn't able to include the code, so I've uploaded it as an attachment
    Attached Files

    Comment

    Working...