-
Notifications
You must be signed in to change notification settings - Fork 93
/
zbx_nginx_stats.py
executable file
·196 lines (155 loc) · 5.48 KB
/
zbx_nginx_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python
import urllib2, base64, re, struct, time, socket, sys, datetime, os.path
try:
import json
except:
import simplejson as json
zabbix_host = '127.0.0.1' # Zabbix server IP
zabbix_port = 10051 # Zabbix server port
hostname = 'Zabbix Agent' # Name of monitored host, like it shows in zabbix web ui
time_delta = 1 # grep interval in minutes
# URL to nginx stat (http_stub_status_module)
stat_url = 'https://nginx.server/nginx_stat'
# Nginx log file path
nginx_log_file_path = '/var/log/nginx/access.log'
# Optional Basic Auth
username = 'user'
password = 'pass'
# Temp file, with log file cursor position
seek_file = '/tmp/nginx_log_stat'
class Metric(object):
def __init__(self, host, key, value, clock=None):
self.host = host
self.key = key
self.value = value
self.clock = clock
def __repr__(self):
if self.clock is None:
return 'Metric(%r, %r, %r)' % (self.host, self.key, self.value)
return 'Metric(%r, %r, %r, %r)' % (self.host, self.key, self.value, self.clock)
def send_to_zabbix(metrics, zabbix_host='127.0.0.1', zabbix_port=10051):
j = json.dumps
metrics_data = []
for m in metrics:
clock = m.clock or ('%d' % time.time())
metrics_data.append(('{"host":%s,"key":%s,"value":%s,"clock":%s}') % (j(m.host), j(m.key), j(m.value), j(clock)))
json_data = ('{"request":"sender data","data":[%s]}') % (','.join(metrics_data))
data_len = struct.pack('<Q', len(json_data))
packet = 'ZBXD\x01'+ data_len + json_data
#print packet
#print ':'.join(x.encode('hex') for x in packet)
try:
zabbix = socket.socket()
zabbix.connect((zabbix_host, zabbix_port))
zabbix.sendall(packet)
resp_hdr = _recv_all(zabbix, 13)
if not resp_hdr.startswith('ZBXD\x01') or len(resp_hdr) != 13:
print 'Wrong zabbix response'
return False
resp_body_len = struct.unpack('<Q', resp_hdr[5:])[0]
resp_body = zabbix.recv(resp_body_len)
zabbix.close()
resp = json.loads(resp_body)
#print resp
if resp.get('response') != 'success':
print 'Got error from Zabbix: %s' % resp
return False
return True
except:
print 'Error while sending data to Zabbix'
return False
def _recv_all(sock, count):
buf = ''
while len(buf)<count:
chunk = sock.recv(count-len(buf))
if not chunk:
return buf
buf += chunk
return buf
def get(url, login, passwd):
req = urllib2.Request(url)
if login and passwd:
base64string = base64.encodestring('%s:%s' % (login, passwd)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
q = urllib2.urlopen(req)
res = q.read()
q.close()
return res
def parse_nginx_stat(data):
a = {}
# Active connections
a['active_connections'] = re.match(r'(.*):\s(\d*)', data[0], re.M | re.I).group(2)
# Accepts
a['accepted_connections'] = re.match(r'\s(\d*)\s(\d*)\s(\d*)', data[2], re.M | re.I).group(1)
# Handled
a['handled_connections'] = re.match(r'\s(\d*)\s(\d*)\s(\d*)', data[2], re.M | re.I).group(2)
# Requests
a['handled_requests'] = re.match(r'\s(\d*)\s(\d*)\s(\d*)', data[2], re.M | re.I).group(3)
# Reading
a['header_reading'] = re.match(r'(.*):\s(\d*)(.*):\s(\d*)(.*):\s(\d*)', data[3], re.M | re.I).group(2)
# Writing
a['body_reading'] = re.match(r'(.*):\s(\d*)(.*):\s(\d*)(.*):\s(\d*)', data[3], re.M | re.I).group(4)
# Waiting
a['keepalive_connections'] = re.match(r'(.*):\s(\d*)(.*):\s(\d*)(.*):\s(\d*)', data[3], re.M | re.I).group(6)
return a
def read_seek(file):
if os.path.isfile(file):
f = open(file, 'r')
try:
result = int(f.readline())
f.close()
return result
except:
return 0
else:
return 0
def write_seek(file, value):
f = open(file, 'w')
f.write(value)
f.close()
#print '[12/Mar/2014:03:21:13 +0400]'
d = datetime.datetime.now()-datetime.timedelta(minutes=time_delta)
minute = int(time.mktime(d.timetuple()) / 60)*60
d = d.strftime('%d/%b/%Y:%H:%M')
total_rps = 0
rps = [0]*60
tps = [0]*60
res_code = {}
nf = open(nginx_log_file_path, 'r')
new_seek = seek = read_seek(seek_file)
# if new log file, don't do seek
if os.path.getsize(nginx_log_file_path) > seek:
nf.seek(seek)
line = nf.readline()
while line:
if d in line:
new_seek = nf.tell()
total_rps += 1
sec = int(re.match('(.*):(\d+):(\d+):(\d+)\s.*]', line).group(4))
code = re.match(r'(.*)"\s(\d*)\s', line).group(2)
if code in res_code:
res_code[code] += 1
else:
res_code[code] = 1
rps[sec] += 1
line = nf.readline()
if total_rps != 0:
write_seek(seek_file, str(new_seek))
nf.close()
metric = (len(sys.argv) >= 2) and re.match(r'nginx\[(.*)\]', sys.argv[1], re.M | re.I).group(1) or False
data = get(stat_url, username, password).split('\n')
data = parse_nginx_stat(data)
data_to_send = []
# Adding the metrics to response
if not metric:
for i in data:
data_to_send.append(Metric(hostname, ('nginx[%s]' % i), data[i]))
else:
print data[metric]
# Adding the request per seconds to response
for t in range(0,60):
data_to_send.append(Metric(hostname, 'nginx[rps]', rps[t], minute+t))
# Adding the response codes stats to respons
for t in res_code:
data_to_send.append(Metric(hostname, ('nginx[%s]' % t), res_code[t]))
send_to_zabbix(data_to_send, zabbix_host, zabbix_port)