Zabbix版本:3.2.4

实战监控Apache

1.1 源码编译构建apache服务

#为了避免冲突,若系统中已经安装httpd服务,务必删除
[root@apache ~]# rpm -q httpd
package httpd is not installed
[root@apache ~]# /etc/init.d/iptables stop
[root@apache ~]# setenforce 0
setenforce: SELinux is disabled
#源码构建apache
[root@apache httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
[root@apache httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
[root@apache httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
[root@apache httpd-2.2.17]# cd /usr/local/httpd/
[root@apache httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@apache httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@apache httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@apache httpd]# chmod +x /etc/init.d/httpd
[root@apache ~]# cd /usr/local/httpd/htdocs/
[root@apache htdocs]# echo "`hostname -I` I am apache" > index.html
[root@apache htdocs]# cat index.html
192.168.0.222 I am apache
[root@apache conf]# awk '/#ServerName/{print NR,$0}' httpd.conf
97 #ServerName www.example.com:80
[root@apache conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf
[root@apache conf]# sed -n '97p' httpd.conf
ServerName www.yunjisuan.com:80
[root@apache conf]# /etc/init.d/httpd start
[root@apache conf]# netstat -antup | grep httpd
tcp 0 0 :::80 :::* LISTEN 53288/httpd
[root@apache conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
[root@apache conf]# tail -1 /etc/hosts
192.168.0.222 www.yunjisuan.com
[root@apache conf]# curl www.yunjisuan.com
192.168.0.222 I am apache

1.2 开启apache状态页

要打开apache的ServerStatus页面,需要在httpd.conf文件最下边加入代码段

ExtendedStatus On
<location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</location>

最后,重启动apache即可

1.3 下载apache zabbix模版并解压

#从如下地址下载apache zabbix模版:
[root@apache ~]# wget https://github.com/lorf/zapache/archive/master.zip
#解压zip格式压缩文件
[root@apache ~]# unzip master
[root@apache ~]# ls -d zapache-master
zapache-master
[root@apache ~]# cd zapache-master/
[root@apache ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── userparameter_zapache.conf.sample
├── zapache
├── zapache-template-active.xml
└── zapache-template.xml
0 directories, 6 files
#此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。
#首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:
[root@apache zapache-master]# chmod 755 /etc/zabbix/scripts/zapache
[root@apache scripts]# cd /etc/zabbix/scripts/
[root@apache scripts]# ./zapache
zapache version: 1.5
usage:
./zapache [<url>] TotalAccesses - Check total accesses.
./zapache [<url>] TotalKBytes - Check total KBytes.
./zapache [<url>] CPULoad - Check CPU load.
./zapache [<url>] Uptime - Check uptime.
./zapache [<url>] ReqPerSec - Check requests per second.
./zapache [<url>] BytesPerSec - Check Bytes per second.
./zapache [<url>] BytesPerReq - Check Bytes per request.
./zapache [<url>] BusyWorkers - Check busy workers.
./zapache [<url>] IdleWorkers - Check idle workers.
./zapache [<url>] version - Version of this script.
./zapache [<url>] ping - Check if Apache is up.
./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
./zapache [<url>] StartingUp - Check Starting Up processess.
./zapache [<url>] ReadingRequest - Check Reading Request processess.
./zapache [<url>] SendingReply - Check Sending Reply processess.
./zapache [<url>] KeepAlive - Check KeepAlive Processess.
./zapache [<url>] DNSLookup - Check DNSLookup Processess.
./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
./zapache [<url>] Logging - Check Logging Processess.
./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.
#接着将userparameter_zapache.conf.sample文件放到/etc/zabbix/zabbix_agentd.d目录下,并重命名为userparameter_zapache.conf文件,然后修改键值命令的路径。
#最后,重启zabbix-agent服务
[root@apache zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
[root@apache zabbix_agentd.d]# tail -1 userparameter_zapache.conf
UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1 #本行要修改
[root@apache zapache-master]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent: [ OK ]
Starting Zabbix agent: [ OK ]

1.4 测试键值

#在zabbix-agent端查看脚本用法
[root@apache zabbix_agentd.d]# cd /etc/zabbix/scripts/
[root@apache scripts]# ./zapache
zapache version: 1.5
usage:
./zapache [<url>] TotalAccesses - Check total accesses.
./zapache [<url>] TotalKBytes - Check total KBytes.
./zapache [<url>] CPULoad - Check CPU load.
./zapache [<url>] Uptime - Check uptime.
./zapache [<url>] ReqPerSec - Check requests per second.
./zapache [<url>] BytesPerSec - Check Bytes per second.
./zapache [<url>] BytesPerReq - Check Bytes per request.
./zapache [<url>] BusyWorkers - Check busy workers.
./zapache [<url>] IdleWorkers - Check idle workers.
./zapache [<url>] version - Version of this script.
./zapache [<url>] ping - Check if Apache is up.
./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
./zapache [<url>] StartingUp - Check Starting Up processess.
./zapache [<url>] ReadingRequest - Check Reading Request processess.
./zapache [<url>] SendingReply - Check Sending Reply processess.
./zapache [<url>] KeepAlive - Check KeepAlive Processess.
./zapache [<url>] DNSLookup - Check DNSLookup Processess.
./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
./zapache [<url>] Logging - Check Logging Processess.
./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.
#我们发现脚本需要传递两个参数,即url和选项
#然后我们在zabbix-server端进行键值测试
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"
9
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com version]"
1.5
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com ping]"
1

1.5 在zabbix-server的Web页面上监控apache

1.5.1 创建一个监控apache的主机

Zabbix监控Apache_zabbix

Zabbix监控Apache_zabbix_02

1.5.2 导入带有apache监控项的模版

zabbix默认并没有监控apache的监控项模版。我们需要将它导入到apache中。在之前解压出来的目录中,有待导入的web模版

[root@apache ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── zapache-template-active.xml #Web模版
└── zapache-template.xml #Web模版
0 directories, 4 files

我们将这两个.xml后缀的拷贝到windows上,然后从Web界面导入进去

Zabbix监控Apache_apache_03

Zabbix监控Apache_监控_04

导入以后Web端就有监控apache的监控项模版了

1.5.3 将模版链接到apache主机

Zabbix监控Apache_zabbix_05

Zabbix监控Apache_监控_06

Zabbix监控Apache_zabbix_07