Zabbix监控apache与MySQL

Zabbix监控apacheMySQL

Apache

1.      上传check_apache/conf-apache.conf

#!/bin/bash
#
# Name: zapache
#
# Checks Apache activity.
#
# Author: Alejandro Michavila
# Modified for Scoreboard Values: Murat Koc, [email protected]
# Modified for using also as external script: Murat Koc, [email protected]
# Modified for outputting usage or ZBX_NOTSUPPORTED: Alejandro Michavila
#
# Version: 1.4
#
 
zapachever="1.4"
rval=0
 
function usage()
{
    echo "zapache version: $zapachever"
    echo "usage:"
    echo "    $0 TotalAccesses                   -- Check total accesses."
    echo "    $0 TotalKBytes                     -- Check total KBytes."
    echo "    $0 Uptime                          -- Check uptime."
    echo "    $0 ReqPerSec                       -- Check requests per second."
    echo "    $0 BytesPerSec                     -- Check Bytes per second."
    echo "    $0 BytesPerReq                     -- Check Bytes per request."
    echo "    $0 BusyWorkers                     -- Check busy workers."
    echo "    $0 IdleWorkers                     -- Check idle workers."
    echo "    $0 version                         -- Version of this script."
    echo "    $0 WaitingForConnection            -- Check Waiting for Connection processess."
    echo "    $0 StartingUp                      -- Check Starting Up processess."
    echo "    $0 ReadingRequest                  -- Check Reading Request processess."
    echo "    $0 SendingReply                    -- Check Sending Reply processess."
    echo "    $0 KeepAlive                       -- Check KeepAlive Processess."
    echo "    $0 DNSLookup                       -- Check DNSLookup Processess."
    echo "    $0 ClosingConnection               -- Check Closing Connection Processess."
    echo "    $0 Logging                         -- Check Logging Processess."
    echo "    $0 GracefullyFinishing             -- Check Gracefully Finishing Processess."
    echo "    $0 IdleCleanupOfWorker             -- Check Idle Cleanup of Worker Processess."
    echo "    $0 OpenSlotWithNoCurrentProcess    -- Check Open Slots with No Current Process."
} 
 
########
# Main #
######## 
 
if [[ $# ==  1 ]];then
    #Agent Mode
    VAR=$(wget --quiet -O - http://localhost/server-status?auto)
    CASE_VALUE=$1
elif [[ $# == 2 ]];then
    #External Script Mode
    VAR=$(wget --quiet -O - http://$1/server-status?auto)
    CASE_VALUE=$2
else
    #No Parameter
	usage
    exit 0
fi
 
if [[ -z $VAR ]]; then
	echo "ZBX_NOTSUPPORTED"
    exit 1
fi
 
case $CASE_VALUE in
'TotalAccesses')
    echo "$VAR"|grep "Total Accesses:"|awk '{print $3}'
    rval=$?;;
'TotalKBytes')
    VER=$(echo "$VAR"|grep "Total kBytes:"|awk '{print $3}')
    MULT=$(($VER * 1024))
    echo "$MULT"
    rval=$?;;
'Uptime')
    echo "$VAR"|grep "Uptime:"|awk '{print $2}'
    rval=$?;;
'ReqPerSec')
    echo "$VAR"|grep "ReqPerSec:"|awk '{print $2}'
    rval=$?;;
'BytesPerSec')
    echo "$VAR"|grep "BytesPerSec:"|awk '{print $2}'
    rval=$?;;
'BytesPerReq')
    echo "$VAR"|grep "BytesPerReq:"|awk '{print $2}'
    rval=$?;;
'BusyWorkers')
    echo "$VAR"|grep "BusyWorkers:"|awk '{print $2}'
    rval=$?;;
'IdleWorkers')
    echo "$VAR"|grep "IdleWorkers:"|awk '{print $2}'
	rval=$?;;
'WaitingForConnection')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "_" } ; { print NF-1 }'
    rval=$?;;
'StartingUp')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "S" } ; { print NF-1 }'
    rval=$?;;
'ReadingRequest')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "R" } ; { print NF-1 }'
    rval=$?;;
'SendingReply')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "W" } ; { print NF-1 }'
    rval=$?;;
'KeepAlive')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "K" } ; { print NF-1 }'
    rval=$?;;
'DNSLookup')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "D" } ; { print NF-1 }'
    rval=$?;;
'ClosingConnection')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "C" } ; { print NF-1 }'
    rval=$?;;
'Logging')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "L" } ; { print NF-1 }'
    rval=$?;;
'GracefullyFinishing')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "G" } ; { print NF-1 }'
    rval=$?;;
'IdleCleanupOfWorker')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "I" } ; { print NF-1 }'
    rval=$?;;
'OpenSlotWithNoCurrentProcess')
    echo "$VAR"|grep "Scoreboard:"| awk '{print $2}'| awk 'BEGIN { FS = "." } ; {data_type>0 print NF-1 }'
    rval=$?;;
'version')
    ApacheVersion=$(/usr/sbin/apachectl -v 2> /dev/null | head -n 1 | awk '{print $3}' | cut -d '/' -f 2 | tail -n 1)
    echo "$ApacheVersion"
    exit $rval;;
*)
	usage
    exit $rval;;
esac
 
if [ "$rval" -ne 0 ]; then
      echo "ZBX_NOTSUPPORTED"
fi
 
exit $rval
UserParameter=apache[*],/usr/local/bin/zapache.sh \$1


2.      #mvcheck_apache /usr/local/zabbix/bin

3.      #chmod+x /usr/local/zabbix/bin/check_apache #授权可执行权限

4.      修改脚本#vim–n check_apache

按键128

Apacheversion=…修改正确的apache安装路径

5.      修改配置文件#vim/etc/zabbix_agentd.cof

启用并修改正确的zabbix路径:include=/usr/local/zabbix/etc/zabbix_agent.conf.d/*.conf

移动配置文件conf-apache.confzabbix_agent配置目录下#mv conf-apache.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/

修改此文件#vim /usr/local/zabix/etc/zabbix_agent.conf.d/conf-apache.conf

UserParameter=apache[*],/usr/local/zabbix/bin/check_apache    \$1 //修改成正确路径

6.      配置httpd文件

#vim /etc/httpd/conf/httpd.conf

末尾添加

#

# Allow server statusreports generated by mod_status,

# with the URL ofhttp://servername/server-status

 

# Uncomment and changethe "192.0.2.0/24" to allow access from other hosts.

#

SetHandlerserver-status

Order deny,allow

Deny from all

Allow from127.0.0.1  ::1

Allow from192.168.0.0/24

# Keep track ofextended status information for each request

ExtendedStatus On

# Determine ifmod_status displays the first 63 characters of a request or

# the last 63, assumingthe request itself is greater than 63 chars.

# Default: Off

#SeeRequestTail On

# Show ProxyLoadBalancer status in mod_status

ProxyStatus On

重启服务#service httpd restart

7.      测试脚本是否正常#./check_apache –h

8.      Zabbix前端界面操作

导入模版;配置-模版-选择模版-导入



    2.0
    2015-03-19T15:46:48Z
    
        
            Templates
        
    
    
        
    
    
        
            {Template App Apache Service:proc.num[{$APACHE}].last(0)}=0
            Apache: Process not running
            
            0
            4
            
            0
            
        
    


主机关联模版:点击对应的主机-添加-更新

MySQL

1.      进入MySQL数据库创建用户zabbix,并且给权限

Mysql>grant all on *.* to “zabbix-monitor”@”%” identified by ‘123456’

Mysql>flush privileges

Mysql>quit

2.      我使用的是zabbix自带的MySQL监控脚本

复制#cp/usr/local/src/zabbix-3.0.5/conf/zabbix_agentd/userparameter_mysl.conf  /usr/local/zabbix/etc/zabbix_agentd.conf.d/

修改#vim /usr/local/zabbix/etc/zabbix_agentd.conf

启用并修改正确的路径

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf  

3.      建立数据库和zabbix的链接信息#vim /usr/local/zabbix/etc/.my.cnf

#Zabbix Agent

[mysql]

host=localhost

user=zabbix-monitor

password=123456

socket=/var/lib/mysql/mysql.sock

[mysqladmin]

host=localhost

 user=zabbix-monitor

password=123456

socket= /var/lib/mysql/mysql.sock

:wq

4.      修改#vim/usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf

home=/var/lib/zabbix全部改成home=/usr/local/zabbix/etc/

5.      重启zabbix_agent服务#service zabbix_agentd restart

6.      Zabbix前端配置

配置-模版-添加-更新

你可能感兴趣的:(运维)