龙空技术网

Zabbix监控Apache、Nginx操作实战

老王的日志 313

前言:

今天朋友们对“awstats分析nginx日志”大约比较讲究,看官们都想要剖析一些“awstats分析nginx日志”的相关文章。那么小编也在网上网罗了一些有关“awstats分析nginx日志””的相关知识,希望兄弟们能喜欢,朋友们快快来学习一下吧!

为了避免冲突,若系统中已经安装httpd服务,务必删除

rpm -q httpd

/etc/init.d/iptables stop

setenforce 0

源码构建apache

tar xf httpd-2.2.17.tar.gz -C /usr/src/

cd /usr/src/httpd-2.2.17/

./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install

cd /usr/local/httpd/

ls

ln -s /usr/local/httpd/bin/* /usr/local/bin/

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

chmod +x /etc/init.d/httpd

cd /usr/local/httpd/htdocs/

echo "`hostname -I` I am apache" > index.html

cat index.html

cd /usr/local/httpd/conf

awk '/#ServerName/{print NR,$0}' httpd.conf

sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf

sed -n '97p' httpd.conf

/etc/init.d/httpd start

netstat -antup | grep httpd

echo "`hostname -I` ; >> /etc/hosts

tail -1 /etc/hosts

curl

开启apache状态页

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

1. ExtendedStatus On

2. <location /server-status>

3. SetHandler server-status

4. Order Deny,Allow

5. Deny from all

6. Allow from 127.0.0.1

7. </location>

最后,重启动apache即可

下载apache zabbix模版并解压

解压zip格式压缩文件

unzip master

ls -d zapache-master

cd zapache-master/

ls

此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。

首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:

cp zapache /etc/zabbix/scripts/

cd /etc/zabbix/scripts/

chmod 755 /etc/zabbix/scripts/zapache

./zapache

1. zapache version: 1.5

2. usage:

3. ./zapache [<url>] TotalAccesses - Check total accesses.

4. ./zapache [<url>] TotalKBytes - Check total KBytes.

5. ./zapache [<url>] CPULoad - Check CPU load.

6. ./zapache [<url>] Uptime - Check uptime.

7. ./zapache [<url>] ReqPerSec - Check requests per second.

8. ./zapache [<url>] BytesPerSec - Check Bytes per second.

9. ./zapache [<url>] BytesPerReq - Check Bytes per request.

10. ./zapache [<url>] BusyWorkers - Check busy workers.

11. ./zapache [<url>] IdleWorkers - Check idle workers.

12. ./zapache [<url>] version - Version of this script.

13. ./zapache [<url>] ping - Check if Apache is up.

14. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.

15. ./zapache [<url>] StartingUp - Check Starting Up processess.

16. ./zapache [<url>] ReadingRequest - Check Reading Request processess.

17. ./zapache [<url>] SendingReply - Check Sending Reply processess.

18. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.

19. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.

20. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.

21. ./zapache [<url>] Logging - Check Logging Processess.

22. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.

23. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.

24. ./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.

接着将userparameter_zapache.conf.sample文件放到/etc/zabbix/zabbix_agentd.d目录下,并重命名为userparameter_zapache.conf文件,然后修改键值命令的路径。

最后,重启zabbix-agent服务

cd /root/zapache-master/

cp userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf

cd /etc/zabbix/zabbix_agentd.d/

vim userparameter_zapache.conf

tail -1 userparameter_zapache.conf

1. UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1 #本行要修改

/etc/init.d/zabbix-agent restart

测试键值

在zabbix-agent端查看脚本用法

cd /etc/zabbix/scripts/

./zapache

1. zapache version: 1.5

2. usage:

3. ./zapache [<url>] TotalAccesses - Check total accesses.

4. ./zapache [<url>] TotalKBytes - Check total KBytes.

5. ./zapache [<url>] CPULoad - Check CPU load.

6. ./zapache [<url>] Uptime - Check uptime.

7. ./zapache [<url>] ReqPerSec - Check requests per second.

8. ./zapache [<url>] BytesPerSec - Check Bytes per second.

9. ./zapache [<url>] BytesPerReq - Check Bytes per request.

10. ./zapache [<url>] BusyWorkers - Check busy workers.

11. ./zapache [<url>] IdleWorkers - Check idle workers.

12. ./zapache [<url>] version - Version of this script.

13. ./zapache [<url>] ping - Check if Apache is up.

14. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.

15. ./zapache [<url>] StartingUp - Check Starting Up processess.

16. ./zapache [<url>] ReadingRequest - Check Reading Request processess.

17. ./zapache [<url>] SendingReply - Check Sending Reply processess.

18. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.

19. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.

20. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.

21. ./zapache [<url>] Logging - Check Logging Processess.

22. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.

23. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.

24. ./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.

我们发现脚本需要传递两个参数,即url和选项,然后我们在zabbix-server端进行键值测试

zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"

zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[ version]"

zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[ ping]"

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

(1)创建一个监控apache的主机

(2)导入apache监控项的模版(上边有模板下载链接)

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

1. [root@apache ~]# tree zapache-master/

2. zapache-master/

3. ├── httpd-server-status.conf.sample

4. ├── README.md

5. ├── zapache-template-active.xml #Web模版

6. └── zapache-template.xml #Web模版

7. 0 directories, 4 files

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

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

(3)将模版链接到apache主机

实战监控nginx

开启nginx状态页

vim /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx

1. #在nginx的配置文件中,添加status配置

2. location = /nginx-status {

3. stub_status on;

4. access_log off;

5. allow 127.0.0.1;

6. allow 192.168.200.78; #zabbix服务器地址一般是内网地址

7. deny all;

8. }

访问设置好的nginx-status链接

curl 192.168.200.78/nginx-status

1. nginx status详细说明如下:

2.

3. Active connections:对后端发起的活动连接数;

4. accepts:nginx总共处理了多少个连接;

5. handled:nginx成功创建了几次握手;

6. requests:nginx总共处理了多少请求。

7. Reading:nginx读取客户端的header数;

8. Writing:nginx返回给客户端的header数;

9. Waiting:nginx请求处理完成,正在等待下一请求指令的连接

编写nginx状态监控脚本

vim nginx_status.sh

cat nginx_status.sh

1. #!/bin/bash

2. # Set Variables

3. HOST="127.0.0.1"

4. PORT="80"

5. #Functions to return nginx stats

6.

7. function active(){

8. /usr/bin/curl "; 2>/dev/null | grep "Active" | awk '{print $NF}'

9. }

10.

11. function reading(){

12. /usr/bin/curl "; 2>/dev/null | grep "Reading" | awk '{print $2}'

13. }

14.

15. function writing(){

16. /usr/bin/curl "; 2>/dev/null | grep "Writing" | awk '{print $4}'

17. }

18.

19. function accepts(){

20. /usr/bin/curl "; 2>/dev/null | awk 'NR==3' | awk '{print $1}'

21. }

22.

23. function handled(){

24. /usr/bin/curl "; 2>/dev/null | awk 'NR==3' | awk '{print $2}'

25. }

26.

27. function requests(){

28. /usr/bin/curl "; 2>/dev/null | awk 'NR==3' | awk '{print $3}'

29. }

30.

31. function ping(){

32. /sbin/pidof nginx | wc -l

33. }

34.

35. # Run the requested function

36.

37. $1

给脚本可执行权限并且授权

chmod +x nginx_status.sh

chown zabbix.zabbix nginx_status.sh

在zabbix-agent端配置nginx键值配置文件并重启agent客户端

cd /etc/zabbix/zabbix_agentd.d

vim nginx.conf

cat nginx.conf

1. UserParameter=nginx.active,/usr/bin/curl -s "; | grep "Active" | awk '{print $NF}'

2. UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1

/etc/init.d/zabbix-agent restart

在zabbix-server端测试键值

zabbix_get -s 192.168.200.78 -k "nginx.status[reading]" -p 10050

zabbix_get -s 192.168.200.78 -k "nginx.status[active]" -p 10050

zabbix_get -s 192.168.200.78 -k "nginx.status[reading]" -p 10050

zabbix_get -s 192.168.200.78 -k "nginx.status[writing]" -p 10050

zabbix_get -s 192.168.200.78 -k "nginx.status[ping]" -p 10050

接下来我们需要在Web端导入nginx监控模版(上文有模板下载链接)

异常调试

如果Zabbix图形界面一直没有获取到数据,可以通过在zabbix server上执行zabbix_get手动测试: /usr/local/zabbix/bin/zabbix_get -s 192.168.200.78 -p 10050 -k "nginx.status[active]" 如果获取数据有问题,会给出相应提示,如果能够获取到数据,那就没问题了。

标签: #awstats分析nginx日志