root / plugins / nginx / nginx_upstream @ a7139bca
Historique | Voir | Annoter | Télécharger (1,96 ko)
| 1 |
#!/usr/bin/env python |
|---|---|
| 2 |
#requires log format as below |
| 3 |
#log_format cache '$remote_addr - $host [$time_local] "$request" $status ' |
| 4 |
# '$body_bytes_sent "$http_referer" ' |
| 5 |
# 'rt=$request_time ut="$upstream_response_time" ' |
| 6 |
# 'cs=$upstream_cache_status'; |
| 7 |
|
| 8 |
#License BSD |
| 9 |
#Created by Simon Whittaker simon+github@swbh.net |
| 10 |
#based on nginx_cache_hit_rate |
| 11 |
|
| 12 |
from __future__ import with_statement |
| 13 |
import re |
| 14 |
import sys |
| 15 |
fname = "/var/log/nginx/access.log" # File to check |
| 16 |
|
| 17 |
if len(sys.argv) > 1: |
| 18 |
if sys.argv[1]=="config": |
| 19 |
print "graph_args --base 1000 -l 0" |
| 20 |
print "graph_title NGINX Upstream times" |
| 21 |
print "graph_category webserver" |
| 22 |
print "graph_vlabel milliseconds" |
| 23 |
print "upstream.label upstream" |
| 24 |
print "upstream.warning 5000" |
| 25 |
print "upstream.critical 10000" |
| 26 |
print "graph_info Shows the average time of connections to upstream servers for the primary site" |
| 27 |
sys.exit(0) |
| 28 |
|
| 29 |
with open(fname, "r") as f: |
| 30 |
f.seek (0, 2) # Seek @ EOF |
| 31 |
fsize = f.tell() # Get Size |
| 32 |
f.seek (max (fsize-20480, 0), 0) # Set pos @ last n chars |
| 33 |
lines = f.readlines() # Read to end |
| 34 |
|
| 35 |
lines = lines[-1000:] #last 1000 lines you might want to change this |
| 36 |
|
| 37 |
re1='.*?' # Non-greedy match on filler |
| 38 |
re2='(ut)' # Word 1 |
| 39 |
re3='(=)' # Any Single Character 1 |
| 40 |
re4='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])' # Float 1 |
| 41 |
|
| 42 |
rg = re.compile(re1+re2+re3+re4,re.IGNORECASE|re.DOTALL) |
| 43 |
totaltimeforrequests=0 |
| 44 |
numberofrequests=0 |
| 45 |
for line in lines: |
| 46 |
m = rg.search(line) |
| 47 |
if m: |
| 48 |
word1=m.group(1) |
| 49 |
c1=m.group(2) |
| 50 |
float1=m.group(3) |
| 51 |
numberofrequests=numberofrequests+1 |
| 52 |
totaltimeforrequests=totaltimeforrequests+float(float1) |
| 53 |
|
| 54 |
upstream=(totaltimeforrequests/numberofrequests)*1000 |
| 55 |
upstream=int(upstream) |
| 56 |
print "upstream.value "+str(upstream) |
