Repository
Munin (contrib)
Last change
2020-10-28
Graph Categories
Keywords
Language
Python (2.x)

nginx_upstream

Sadly there is no documentation for this plugin.

#!/usr/bin/env 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 webserver"
                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)