Zabbix监控apache与MySQL
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.conf到zabbix_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
Template App Apache Service
Templates
Apache
-
Apache: BytesPerReq.
0
0
apache[BytesPerReq]
60
7
365
0
0
B
0
0
0
0
1
0
0
0
Apache
-
Apache: BytesPerSec.
0
0
apache[BytesPerSec]
60
7
365
0
0
B
0
0
0
0
1
0
0
0
Apache
-
Apache: ClosingConnection.
0
0
apache[ClosingConnection]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: DNSLookup.
0
0
apache[DNSLookup]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: GracefullyFinishing.
0
0
apache[GracefullyFinishing]
60
7
365
0
3
0
0
0
0
1
0
0
0
Apache
-
Apache: IdleCleanupOfWorker.
0
0
apache[IdleCleanupOfWorker]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: IdleWorkers.
0
0
apache[IdleWorkers]
60
7
365
0
3
0
0
0
0
1
0
0
0
Apache
-
Apache: KeepAlive.
0
0
apache[KeepAlive]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: Logging.
0
0
apache[Logging]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: Number of running processes $1
0
0
proc.num[{$APACHE}]
30
90
365
0
3
0
0
0
0
1
0
0
0
Apache
-
Apache: OpenSlotWithNoCurrentProcess.
0
0
apache[OpenSlotWithNoCurrentProcess]
60
7
365
1
0
0
0
0
0
1
0
0
0
Apache
-
Apache: ReadingRequest.
0
0
apache[ReadingRequest]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: ReqPerSec.
0
0
apache[ReqPerSec]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: SendingReply.
0
0
apache[SendingReply]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: StartingUp.
0
0
apache[StartingUp]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
-
Apache: TotalAccesses.
0
0
apache[TotalAccesses]
60
7
365
0
3
0
0
0
0
1
0
0
0
Apache
-
Apache: TotalKBytes.
0
0
apache[TotalKBytes]
60
7
365
0
0
B
0
0
0
0
1
0
0
0
Apache
-
Apache: Uptime.
0
0
apache[Uptime]
60
7
365
0
0
s
0
0
0
0
1
0
0
0
Apache
-
Apache: Used memory of $1
0
0
proc.mem[{$APACHE}]
30
90
365
0
3
B
0
0
0
0
1
0
0
0
Apache
-
Apache: Version.
0
0
apache[version]
60
7
365
0
4
0
0
0
0
1
0
0
0
Apache
-
Apache: WaitingForConnection.
0
0
apache[WaitingForConnection]
60
7
365
0
0
0
0
0
0
1
0
0
0
Apache
{$APACHE}
apache2
{$APACHE_PORT}
80
{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前端配置
配置-模版-添加-更新