본문 바로가기

Monitoring Tools/Zabbix

9. [zabbix agent] nginx http response code and request time

반응형

1. zabbix_agentd.conf

UserParameter=nginx_code_status[*],/usr/local/etc/scripts/nginx_code.sh "$1"

 

2. nginx_code.sh

#!/bin/sh
file="/tmp/zbx_nginx_result"

case "$1" in
	200)
		cat $file | awk -F':' '{print $1}'
	;;
	300)
                cat $file | awk -F':' '{print $2}'
        ;;
	400)
                cat $file | awk -F':' '{print $3}'
        ;;
	500)
                cat $file | awk -F':' '{print $4}'
        ;;
	reqtime)
                cat $file | awk -F':' '{print $5}'
        ;;
esac

 

3. cron

* * * * * root /usr/local/etc/scripts/nginx_code.py

 

4. nginx_code.py

#!/usr/bin/python
import subprocess
import os
import sys

logpath = "/var/log/nginx"
tmppath = "/tmp"
#file_list = os.listdir(path)
#file_list_py = [file for file in file_list if file.endswith(".access.log")]
loglist=['domain.access.log']

code_a = 0
code_b = 0
code_c = 0
code_d = 0
reqtime = 0
chai = 0

def line_write(log,logpath,tmppath):
	tmpfile = open(tmppath+'/'+log, 'w')
	logfile = open(logpath+'/'+log, 'r')
	data = len(logfile.readlines())
	tmpfile.write(str(data))
	logfile.close()
	tmpfile.close()

def result_print(code_a,code_b,code_c,code_d,reqtime_avg):
	tmpfile = open(tmppath+'/zbx_nginx_result', 'w')
	data = str(code_a)+':'+str(code_b)+':'+str(code_c)+':'+str(code_d)+':'+str(reqtime_avg)
	tmpfile.write(data)
	tmpfile.close()

def compare_code_value(code):
	code2 = int(code)
	global code_a
	global code_b
	global code_c
	global code_d
	if code2 >= 200 and code2 < 300:
		code_a += 1
	elif code2 >= 300 and code2 < 400:
                code_b += 1
	elif code2 >= 400 and code2 < 500:
                code_c += 1
	elif code2 >= 500 and code2 < 600:
                code_d += 1

def calc_reqtime(reqtime,chai):
	if chai == 0:
		return(0)
	else:
		reqtime_avg = reqtime / chai
		return(round(reqtime_avg,3))

for i in range(0,len(loglist)):
	try:
		tmpfile = open(tmppath+'/'+loglist[i], 'r')
		line = tmpfile.readline()
		if not line:
			line_write(loglist[i],logpath,tmppath)
			continue
		tmpfile.close()
	except:
		line_write(loglist[i],logpath,tmppath)

	line_write(loglist[i],logpath,tmppath)
	tmpfile = open(tmppath+'/'+loglist[i], 'r')
	line2 = tmpfile.readline()
	if int(line) < int(line2):
		chai += int(line2) - int(line)
		with open(logpath+'/'+loglist[i]) as f:
			data = f.readlines()[int(line):int(line2)]
			for j in range(0,len(data)):
				code = data[j].split(' ')[0].split('[')[1].split('::')[0]
				compare_code_value(code)
				reqtime += float(data[j].split(' ')[0].split('[')[1].split('::')[1])
	elif int(line) > int(line2):
		chai += int(line)
		with open(logpath+'/'+loglist[i]) as f:
                        data = f.readlines()[1:int(line)]
                        for j in range(0,len(data)):
                                code = data[j].split(' ')[0].split('[')[1].split('::')[0]
                                compare_code_value(code)
                                reqtime += float(data[j].split(' ')[0].split('[')[1].split('::')[1])
	elif int(line) == int(line2):
		continue

reqtime_avg = calc_reqtime(reqtime,chai)
result_print(code_a,code_b,code_c,code_d,reqtime_avg)

 

5. zabbix server template

zbx_export_templates_nginx.xml
0.03MB
zbx_export_templates_nginx_code.xml
0.01MB

 

반응형

'Monitoring Tools > Zabbix' 카테고리의 다른 글

11. [zabbix agent] kafka  (0) 2021.03.17
10. [zabbix agent] bind  (0) 2021.03.17
8. [zabbix agent] jstat  (0) 2021.03.17
7. [zabbix agent] varnish  (0) 2021.03.17
6. [zabbix agent] redis  (0) 2021.03.17