一、Zabbix定义
Zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活机制让系统管理人员快速定位存在的各种问题。
zabbix由两部分组成,agent端和server端
agent端:主机通过安装agent方式采集数据。
server端:通过agent发送的数据,写入数据库,并通过php+apache在web前端展示。
二、工作原理
zabbix agent需要安装到被监控的主机上,它负责定期收集各项数据,并发送到zabbox server端,zabbix server将数据存储到数据库中,zabbix web根据数据在前端进行展现和绘图。
这里的agent收集数据分为主动和被动两种模式:
主动:agent请求server获取主动的监控项列表,并主动将监控向内需要检测的数据提交给server/proxy
被动:server向agent请求获取监控项的数据,agent返回数据。
三、zabbix的安装部署
配置:
1、关闭selinux和iptables
2、安装zabbix rpm包仓库和zabbix-server-mysql和zabbix-web-mysql
3、安装apache和mariadb-server
4、启动mysql服务,创建zabbix数据库,创建zabbix帐号
5、导入默认的zabbix数据库信息
6、修改zabbix_server.conf配置文件
7、启动apache和zabbix-server服务,并网址访问测试
8、逐步安装
安装完成!!
在被监控的主机上下载安装zabbix-agent并添加到zabbix web中监控
1、在server2:172.25.77.2上安装zabbix-agent
2、配置zabbix_agent.conf
指定zabbix_server端
ServerActive主动提交监控数据时使用
zabbix-agent标识,这里我与agent主机名一致
完成后启动服务
在服务器端(zabbix web )添加监控
创建主机
点击配置—>主机—>创建主机
添加主机和模版如图
主机名称:必须要和agent中的HostName中一样,否则会出错
agent代理程序的接口填写zabbix-agent的ip
模版–选择–“Template OS Linux” --选择–添加; 添加 完成 agent添加到zabbix监控中,默认是被动模式。
完成后点击配置—>主机查看,如图添加成功
监控http(不太理解,需要修改)
在被监控的主机上下载apache
systemctl start httpd.service
[root@oneapm-test scripts]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Apr 20 2018 18:10:38
[root@oneapm-test scripts]# cat /etc/httpd/conf/httpd.conf //httpd.conf文件增加如下内容
<Location /server-status>
setHandler server-status
Require all granted
</Location>
ExtendedStatus On
[root@oneapm-test scripts]# systemctl restart httpd //重启httpd服务
访问apache状态页面
[root@oneapm-test scripts]# curl http://localhost:82/server-status?auto
Total Accesses: 28
Total kBytes: 27
Uptime: 7300
ReqPerSec: .00383562
BytesPerSec: 3.7874
BytesPerReq: 987.429
BusyWorkers: 1
IdleWorkers: 5
Scoreboard:
二、脚本编写
#! /bin/bash
#
# Name: zapache
#
# Checks Apache activity.
#
# Version: 1.5
#
zapachever="1.5"
rval=0
value=""
cache_seconds="60"
[ "$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:82/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
umask 077
# $UID is bash-specific
cache_prefix="zapache-$UID-${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() { $curl --insecure --silent --location -H "Cache-Control: no-cache" "$@"; }
else
wget="`which wget`"
if [ "$wget" ]; then
fetch_url() { $wget --no-check-certificate --quiet --header "Cache-Control: no-cache" -O - "$@"; }
else
echo "ZBX_NOTSUPPORTED"
exit 1
fi
fi
fetch_url "$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 split($2,notused,"_")-1}' < \"$cache\"`"
rval=$?;;
'StartingUp')
value="`awk '/^Scoreboard:/ {print split($2,notused,"S")-1}' < \"$cache\"`"
rval=$?;;
'ReadingRequest')
value="`awk '/^Scoreboard:/ {print split($2,notused,"R")-1}' < \"$cache\"`"
rval=$?;;
'SendingReply')
value="`awk '/^Scoreboard:/ {print split($2,notused,"W")-1}' < \"$cache\"`"
rval=$?;;
'KeepAlive')
value="`awk '/^Scoreboard:/ {print split($2,notused,"K")-1}' < \"$cache\"`"
rval=$?;;
'DNSLookup')
value="`awk '/^Scoreboard:/ {print split($2,notused,"D")-1}' < \"$cache\"`"
rval=$?;;
'ClosingConnection')
value="`awk '/^Scoreboard:/ {print split($2,notused,"C")-1}' < \"$cache\"`"
rval=$?;;
'Logging')
value="`awk '/^Scoreboard:/ {print split($2,notused,"L")-1}' < \"$cache\"`"
rval=$?;;
'GracefullyFinishing')
value="`awk '/^Scoreboard:/ {print split($2,notused,"G")-1}' < \"$cache\"`"
rval=$?;;
'IdleCleanupOfWorker')
value="`awk '/^Scoreboard:/ {print split($2,notused,"I")-1}' < \"$cache\"`"
rval=$?;;
'OpenSlotWithNoCurrentProcess')
value="`awk '/^Scoreboard:/ {print split($2,notused,".")-1}' < \"$cache\"`"
rval=$?;;
*)
usage
exit 1;;
esac
if [ "$rval" -eq 0 -a -z "$value" ]; then
case "$CASE_VALUE" in
# Theese metrics are output only if non-zero
'CPULoad' | 'ReqPerSec' | 'BytesPerSec' | 'BytesPerReq')
value=0
;;
*)
rval=1
;;
esac
fi
if [ "$rval" -ne 0 ]; then
echo "ZBX_NOTSUPPORTED"
fi
echo "$value"
exit $rval
#
# end zapache
授权脚本运行权限
[root@oneapm-test scripts]# ll zapache.sh
-rwxr-xr-x. 1 root root 6114 7月 16 13:19 zapache.sh
[root@oneapm-test scripts]# tail -1 ../etc/zabbix_agentd.conf
UserParameter=zapache[*],/usr/local/zabbix/scripts/zapache.sh \$1
在Zabbix服务器监控项测试
[root@Zabbix ~]# /zabbix/bin/zabbix_get -s 172.19.47.225 -p 20050 -k 'zapache[ping]'
1
[root@Zabbix ~]# /zabbix/bin/zabbix_get -s 172.19.47.225 -p 20050 -k 'zapache[version]'
1.5