From 4b83635595e318692ff793de62f1b98e6e063bc7 Mon Sep 17 00:00:00 2001 From: Simon Whittaker Date: Thu, 30 Aug 2012 13:49:22 +0100 Subject: [PATCH] Added nginx check for upstream access times --- plugins/nginx/nginx_upstream | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 plugins/nginx/nginx_upstream diff --git a/plugins/nginx/nginx_upstream b/plugins/nginx/nginx_upstream new file mode 100644 index 00000000..ee3ab974 --- /dev/null +++ b/plugins/nginx/nginx_upstream @@ -0,0 +1,56 @@ +#!/usr/bin/python +#requires log format as below +#log_format cache '$remote_addr - $host [$time_local] "$request" $status ' +# '$body_bytes_sent "$http_referer" ' +# 'rt=$request_time ut="$upstream_response_time" ' +# 'cs=$upstream_cache_status'; + +#License BSD +#Created by Simon Whittaker simon+github@swbh.net +#based on nginx_cache_hit_rate + +from __future__ import with_statement +import re +import sys +fname = "/var/log/nginx/access.log" # File to check + +if len(sys.argv) > 1: + if sys.argv[1]=="config": + print "graph_args --base 1000 -l 0" + print "graph_title NGINX Upstream times" + print "graph_category nginx" + print "graph_vlabel milliseconds" + print "upstream.label upstream" + print "upstream.warning 5000" + print "upstream.critical 10000" + print "graph_info Shows the average time of connections to upstream servers for the primary site" + sys.exit(0) + +with open(fname, "r") as f: + f.seek (0, 2) # Seek @ EOF + fsize = f.tell() # Get Size + f.seek (max (fsize-20480, 0), 0) # Set pos @ last n chars + lines = f.readlines() # Read to end + +lines = lines[-1000:] #last 1000 lines you might want to change this + +re1='.*?' # Non-greedy match on filler +re2='(ut)' # Word 1 +re3='(=)' # Any Single Character 1 +re4='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])' # Float 1 + +rg = re.compile(re1+re2+re3+re4,re.IGNORECASE|re.DOTALL) +totaltimeforrequests=0 +numberofrequests=0 +for line in lines: + m = rg.search(line) + if m: + word1=m.group(1) + c1=m.group(2) + float1=m.group(3) + numberofrequests=numberofrequests+1 + totaltimeforrequests=totaltimeforrequests+float(float1) + +upstream=(totaltimeforrequests/numberofrequests)*1000 +upstream=int(upstream) +print "upstream.value "+str(upstream)