zabbix监控apache_status

2023年 7月 15日 88.8k 0

1.在client的httpd.conf中添加如下

sudo vim /alidata/server/httpd/conf/httpd.conf
<Location /server-status>
    SetHandler server-status
    Allow from 127.0.0.1
    Order deny,allow
    Deny from all
</Location>
ExtendedStatus On

如果你有rewrite规则,则需要在.htaccess中加上一条,如下:

RewriteCond %{REQUEST_URI} !server-status需要写在index.php之前
sudo vim /alidata/www/jds.jince.com/.htaccess
# ThinkPHP Rewrite规则
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_URI} !server-status
   RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

禁止显示目录列表

Options -Indexes2,重加载apache配置Check uptime并重启apache

sudo /etc/init.d/httpd configtest 无错误后
sudo /etc/init.d/httpd graceful

可以使用curl 127.0.0.1/server-status测试3,在zabbix-agent上创建目录和脚本

sudo mkdir /etc/zabbix/scripts
sudo vim /etc/zabbix/scripts/check_apache.sh

check_apache.sh放入client的/etc/zabbix/scripts

#! /bin/bash
#
# Name: zapache
#
# Checks Apache activity.
#
# Author: Alejandro Michavila
# Modified for Scoreboard Values: Murat Koc, murat@profelis.com.tr
# Modified for using also as external script: Murat Koc, murat@profelis.com.tr
# Modified for outputting usage or ZBX_NOTSUPPORTED: Alejandro Michavila
# Modified to do cacheing for performance, dmitry.frolov@gmail.com
#
# Version: 1.5
#
 
zapachever="1.5"
rval=0
value=""
cache_seconds="30"
[ "$TMPDIR" ] || TMPDIR=/tmp
function usage()
{
    echo "zapache version: $zapachever"
    echo "usage:"
    echo "  $0 [<url>] TotalAccesses                 - Check total accesses."
    echo "  $0 [<url>] TotalKBytes                   - Check total KBytes."
    echo "  $0 [<url>] CPULoad                       - Check CPU load."
    echo "  $0 [<url>] Uptime                        - Check uptime."
    echo "  $0 [<url>] ReqPerSec                     - Check requests per second."
    echo "  $0 [<url>] BytesPerSec                   - Check Bytes per second."
    echo "  $0 [<url>] BytesPerReq                   - Check Bytes per request."
    echo "  $0 [<url>] BusyWorkers                   - Check busy workers."
    echo "  $0 [<url>] IdleWorkers                   - Check idle workers."
    echo "  $0 [<url>] version                       - Version of this script."
    echo "  $0 [<url>] ping                          - Check if Apache is up."
    echo "  $0 [<url>] WaitingForConnection          - Check Waiting for Connection processess."
    echo "  $0 [<url>] StartingUp                    - Check Starting Up processess."
    echo "  $0 [<url>] ReadingRequest                - Check Reading Request processess."
    echo "  $0 [<url>] SendingReply                  - Check Sending Reply processess."
    echo "  $0 [<url>] KeepAlive                     - Check KeepAlive Processess."
    echo "  $0 [<url>] DNSLookup                     - Check DNSLookup Processess."
    echo "  $0 [<url>] ClosingConnection             - Check Closing Connection Processess."
    echo "  $0 [<url>] Logging                       - Check Logging Processess."
    echo "  $0 [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess."
    echo "  $0 [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess."
    echo "  $0 [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process."
}

########
# Main #
########

if [[ $# ==  1 ]];then
    #Agent Mode
    STATUS_URL="http://localhost/server-status?auto"
    CASE_VALUE="$1"
elif [[ $# == 2 ]];then
    #External Script Mode
    STATUS_URL="$1"
    case "$STATUS_URL" in
        http://*|https://*) ;;
        *) STATUS_URL="http://$STATUS_URL/server-status?auto";;
    esac
    CASE_VALUE="$2"
else
    #No Parameter
    usage
    exit 0
fi

case "$CASE_VALUE" in
'version')
    echo "$zapachever"
    exit 0;;
esac

cache_prefix="zapache-${STATUS_URL//[^a-zA-Z0-9_-]/_}"
cache="$TMPDIR/$cache_prefix.cache"
cache_timestamp_check="$TMPDIR/$cache_prefix.ts"
# This assumes touch from coreutils
touch -d "@$((`date +%s` - ($cache_seconds - 1)))" "$cache_timestamp_check"

if [ "$cache" -ot "$cache_timestamp_check" ]; then
    curl="`which curl`"
    if [ "$curl" ]; then
        fetch_url_cmd="$curl --insecure --silent --location"
    else
        wget="`which wget`"
        if [ "$wget" ]; then
            fetch_url_cmd="$wget --no-check-certificate --quiet -O -"
        else
            echo "ZBX_NOTSUPPORTED"
            exit 1
        fi
    fi

    $fetch_url_cmd "$STATUS_URL" > "$cache"
    rval=$?
    if [ $rval != 0 ]; then
        echo "ZBX_NOTSUPPORTED"
        exit 1
    fi
fi

case "$CASE_VALUE" in
'ping')
    if [ ! -s "$cache" -o "$cache" -ot "$cache_timestamp_check" ]; then
        echo "0"
    else
        echo "1"
    fi
    exit 0;;
esac

if ! [ -s "$cache" ]; then
    echo "ZBX_NOTSUPPORTED"
    exit 1
fi
 
case "$CASE_VALUE" in
'TotalAccesses')
    value="`awk '/^Total Accesses:/ {print $3}' < "$cache"`"
    rval=$?;;
'TotalKBytes')
    value="`awk '/^Total kBytes:/ {print $3}' < "$cache"`"
    rval=$?;;
'CPULoad')
    value="`awk '/^CPULoad:/ {print $2}' < "$cache"`"
    rval=$?;;
'Uptime')
    value="`awk '/^Uptime:/ {print $2}' < "$cache"`"
    rval=$?;;
'ReqPerSec')
    value="`awk '/^ReqPerSec:/ {print $2}' < "$cache"`"
    rval=$?;;
'BytesPerSec')
    value="`awk '/^BytesPerSec:/ {print $2}' < "$cache"`"
    rval=$?;;
'BytesPerReq')
    value="`awk '/^BytesPerReq:/ {print $2}' < "$cache"`"
    rval=$?;;
'BusyWorkers')
    value="`awk '/^BusyWorkers:/ {print $2}' < "$cache"`"
    rval=$?;;
'IdleWorkers')
    value="`awk '/^IdleWorkers:/ {print $2}' < "$cache"`"
    rval=$?;;
'WaitingForConnection')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "_" }; { print NF-1 }'`"
    rval=$?;;
'StartingUp')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "S" }; { print NF-1 }'`"
    rval=$?;;
'ReadingRequest')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "R" }; { print NF-1 }'`"
    rval=$?;;
'SendingReply')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "W" }; { print NF-1 }'`"
    rval=$?;;
'KeepAlive')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "K" }; { print NF-1 }'`"
    rval=$?;;
'DNSLookup')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "D" }; { print NF-1 }'`"
    rval=$?;;
'ClosingConnection')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "C" }; { print NF-1 }'`"
    rval=$?;;
'Logging')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "L" }; { print NF-1 }'`"
    rval=$?;;
'GracefullyFinishing')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "G" }; { print NF-1 }'`"
    rval=$?;;
'IdleCleanupOfWorker')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "I" }; { print NF-1 }'`"
    rval=$?;;
'OpenSlotWithNoCurrentProcess')
    value="`awk '/^Scoreboard:/ {print $2}' < "$cache" | awk 'BEGIN { FS = "." }; { print NF-1 }'`"
    rval=$?;;
*)
    usage
    exit 1;;
esac

if [ "$rval" -eq 0 -a -z "$value" ]; then
    rval=1
fi
 
if [ "$rval" -ne 0 ]; then
    echo "ZBX_NOTSUPPORTED"
fi
 
echo "$value"
exit $rval
 
#
# end zapache

给脚本执行权限

sudo chmod +x /etc/zabbix/scripts/check_apache.sh

4.将userparameter_apache.conf放入client的/etc/zabbix/zabbix_agentd.d,内容如下:

sudo vim /etc/zabbix/zabbix_agentd.d/userparameter_apache.conf
UserParameter=zapache[*],/bin/bash /etc/zabbix/scripts/check_apache.sh $1

5.重启agentd

/etc/init.d/zabbix-agent restart
$ sudo /etc/init.d/zabbix-agent restart

测试

sudo sh /etc/zabbix/scripts/check_apache.sh CPULoad
2.06619

修改权限

sudo  chown zabbix.zabbix /tmp/zapache-http___localhost_server-status_auto.*

文件如下:

-rw-r--r-- 1 zabbix zabbix       440 Mar 17 14:29 zapache-http___localhost_server-status_auto.cache
-rw-r--r-- 1 zabbix zabbix         0 Mar 17 14:29 zapache-http___localhost_server-status_auto.ts

6.在zabbix server 中创建Template App Apache service模板,添加items, 关联需要监控的apache主机即可QQ截图20160317145059.pngQQ截图20160317145123.pngapache服务监控可参考如下:apache服务状态.pngapache触发器.png

相关文章

对接alertmanager创建钉钉卡片(1)
手把手教你搭建OpenFalcon监控系统
无需任何魔法即可使用 Ansible 的神奇变量“hostvars”
openobseve HA本地单集群模式
基于k8s上loggie/vector/openobserve日志收集
openobseve单节点和查询语法

发布评论