diff --git a/check_nginx_status.py b/check_nginx_status.py index 135d287cf6947de53abaf78f832d43cd1b0c07aa..96bde89add4398412e55a92fa0c6d9a55ce423b8 100755 --- a/check_nginx_status.py +++ b/check_nginx_status.py @@ -19,7 +19,8 @@ # gustavo chaves for findinx and fixing some bugs # # reimplemented by dogtown with more features -# adjusted by Perun team to Python3 and support of authentication and unverified SSL usage +# adjusted by Perun team to Python3 +# and support of authentication and unverified SSL usage # # TODO: # - make -t test1,test2,test available w/ -w 1,2,3 -c 1,2,3 @@ -46,9 +47,9 @@ perfdata = "" def usage(): print( """ - + check_nginx_status.py is a Nagios-Plugin -to monitor nginx status and alerts on various values, +to monitor nginx status and alerts on various values, based on teh output from HttpStubStatus - Module it also creates, based on the returned values, a csv to store data @@ -56,10 +57,11 @@ it also creates, based on the returned values, a csv to store data Usage: - check_nginx_status [-H|--HOST] [-p|--port] [-u|--url] [-U|--user] [-P|--password] [-s|--ssl] - [-t|--test] [-w|--warning] [-c|--critical] + check_nginx_status [-H|--HOST] [-p|--port] [-u|--url] [-U|--user] [-P|--password] + [-s|--ssl] [-t|--test] [-w|--warning] [-c|--critical] [-o|--output] [-r|--resultfile][-n|--noresult] - [-h|--help] [-v|--version] [-d|--debug] [-S|--servername] [-D|--disablesslverifyhostname] + [-h|--help] [-v|--version] [-d|--debug] [-S|--servername] + [-D|--disablesslverifyhostname] Options: @@ -72,32 +74,33 @@ Options: Default: localhost --SERVERNAME|-S) - (host header of HTTP request) use it if you specified an IP in -H to match the good Virtualhost in your target - + (host header of HTTP request) use it if you specified an IP in -H + to match the good Virtualhost in your target + --port|-p) - Sets connection-port + Sets connection-port Default: 80/http, 443/https - + --ssl|-s) Turns on SSL Default: off - + --url|-u) - Sets nginx status url path. + Sets nginx status url path. Default: /nginx_status - + --user|-U) - Sets nginx status BasicAuth username. + Sets nginx status BasicAuth username. Default: off - + --password|-P) - Sets nginx status BasicAuth password. + Sets nginx status BasicAuth password. Default: off --disablesslverifyhostname|-D) Do not verify ssl session Default: off (do verify) - + --test|-t) Sets the test(check)_value for w/c if used, -w/-c is mandatory @@ -105,44 +108,44 @@ Options: possible Values: active_conns -> active connections - accepts_err -> difference between accepted and + accepts_err -> difference between accepted and handled requests (should be 0) requests -> check for requests/connection reading -> actual value for reading headers writing -> value for active requests waiting -> actual keep-alive-connections checktime -> checks if this check need more than - given -w/-c milliseconds - + given -w/-c milliseconds + --calculated checks --------------- rps -> requests per seconds cps -> connections per second dreq -> delta requests to the previous one dcon -> delta connections to the previous one - - these checks are calculated at runtime with a timeframe - between the latest and the current check; time is + + these checks are calculated at runtime with a timeframe + between the latest and the current check; time is extracted from the timestamp of the result_file - - to disable calculation (no files are written) use -n; + + to disable calculation (no files are written) use -n; you cannot use -t [rps,cps,dreq,dcon] with -n; this will raise an error and the plugin returns UNKNOWN see -r - option for an alternate filepath for temporary results - + --warning|-w) Sets a warning level for selected test(check) Default: off - + --critical|-c) Sets a critical level for selected test(check) Default: off - + --debug|-d) - turn on debugging - messages (use this for manual testing, + turn on debugging - messages (use this for manual testing, never via nagios-checks; beware of the messy output - Default: off - + Default: off + --version|-v) display version and exit @@ -152,32 +155,32 @@ Options: --resultfile|-r) /path/to/check_nginx.results{.csv} - please note, beside the values from the actual check + please note, beside the values from the actual check (eg.g check_nginx.results) a second file is created, if not existent, and written on each plugin-run - (check_nginx.results.csv), containign a historic view on all + (check_nginx.results.csv), containign a historic view on all extracted values default: /tmp/check_nginx.results{.csv} --noresult|-n) - never write a results-file; CANNOT be used with calculated checks - -t [rps|cps|dreq|dcon] - default: off - + never write a results-file; CANNOT be used with calculated checks + -t [rps|cps|dreq|dcon] + default: off + *** ) -> please dont use this option, not implemented or not functional Examples: just get all perfdata, url is default (/nginx_status) - ./check_nginx_status.py --HOST www.example.com + ./check_nginx_status.py --HOST www.example.com just get active connections perfdata - ./check_nginx_status.py -H www.example.com -o - + ./check_nginx_status.py -H www.example.com -o + check for plugin_checktime, error > 10ms (warning) or 50ms (error) and output only perfdata for that values ./check_nginx_status.py -H www.example.com -u /status -w 10 -c 50 -o - + check for requests per second, alert on > 300/1000 active connections ./check_nginx_status.py -H www.example.com -u /status -t rps -w 300 -c 1000 @@ -185,8 +188,9 @@ Examples: ./check_nginx_status.py -H www.example.com -t accepts_err -w 1 -c 50 Performancedata: - - NginxStatus.Check OK | ac=1;acc=64; han=64; req=64; err=0; rpc=1; rps=0; cps=0; dreq=1; + + NginxStatus.Check OK | ac=1;acc=64; han=64; req=64; err=0; + rpc=1; rps=0; cps=0; dreq=1; dcon=1; read=0; writ=1; wait=0; ct=6ms; ac -> active connections @@ -194,13 +198,14 @@ Performancedata: han -> totally handled connections req -> total requests err -> diff between acc - han, thus errors - rpc -> requests per connection (req/han) + rpc -> requests per connection (req/han) rps -> requests per second (calculated) from last checkrun vs actual values cps -> connections per (calculated) from last checkrun vs actual values dreq -> request-delta from last checkrun vs actual values - dcon -> accepted-connection-delta from last checkrun vs actual values + dcon -> accepted-connection-delta from last checkrun vs actual values read -> reading requests from clients - writ -> reading request body, processes request, or writes response to a client + writ -> reading request body, processes request, + or writes response to a client wait -> keep-alive connections, actually it is ac - (read + writ) ct -> checktime (connection time) for this check @@ -208,8 +213,8 @@ Performancedata: Nginx-Config be sure to have your nginx compiled with Status-Module - (--with-http_stub_status_module), you might want to test - your installation with nginx -V + (--with-http_stub_status_module), you might want to test + your installation with nginx -V http://wiki.nginx.org/HttpStubStatusModule location /nginx_status { @@ -219,7 +224,7 @@ Nginx-Config deny all; } - + Requirements: nginx compiled with HttpStubStatusModule (see Nginx-Config) @@ -227,7 +232,7 @@ Requirements: Docs & Download: https://bitbucket.org/maresystem/dogtown-nagios-plugins - + """ ) @@ -237,9 +242,9 @@ def ver(): """ check_nginx_status version : %s - + usage : check_nginx_status -h - + """ % version ) @@ -320,20 +325,8 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): f = open(result_file, "r") ro = f.readline().split(";") f.close() - o_ac = int(ro[0]) o_acc = int(ro[1]) - o_han = int(ro[2]) o_req = int(ro[3]) - o_err = int(ro[4]) - o_rpc = int(ro[5]) - o_rps = int(ro[6]) - o_cps = int(ro[7]) - o_dreq = int(ro[8]) - o_dcon = int(ro[9]) - o_read = int(ro[10]) - o_writ = int(ro[11]) - o_wait = int(ro[12]) - o_ct = int(ro[13]) last = int(os.path.getmtime(result_file)) dtime = now - last if req >= o_req: @@ -347,7 +340,7 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): dcon = acc cps = int(dcon / dtime) - except: + except OSError: print_debug( "cannot read/process result_file :: %s \n use -r" % result_file ) @@ -356,8 +349,8 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): else: if test in ("rps", "cps", "dreq", "dcon"): print( - "NginxStatus.%s UNKNONW - noresult selected (-n), cannot calculate test_results" - % (test) + "NginxStatus.%s UNKNONW" % (test) + + " - noresult selected (-n), cannot calculate test_results" ) sys.exit(3) @@ -372,7 +365,7 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): % (ac, acc, han, req, err, rpc, rps, cps, dreq, dcon, read, writ, wait, ct) ) f.close() - except: + except OSError: print_debug("cannot create result_file :: %s \n use -r" % result_file) return (rps, cps, dreq, dcon) csv = "%s.csv" % result_file @@ -381,9 +374,12 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): print_debug("creating result_file.csv -> %s" % result_file) f = open(csv, "w") f.write( - """"timestamp"; "active conns"; "accepted"; "handled"; "requests"; "req_errors"; "reqs per conn"; "reqs per sec"; "conns per sec"; "delta reqs"; "delta conns"; "reading"; "writing"; "waiting"; "checktime"; \n""" + '"timestamp"; "active conns"; "accepted"; "handled"; "requests";' + + ' "req_errors"; "reqs per conn"; "reqs per sec"; "conns per sec";' + + ' "delta reqs"; "delta conns"; "reading"; "writing"; "waiting";' + + ' "checktime"; \n' ) - except: + except OSError: print("ERR.cannot create result_file.csv :: %s \n use -r" % csv) print_debug("writing result_file.csv -> %s.csv" % result_file) @@ -410,7 +406,7 @@ def calculate(req, acc, ac, han, err, rpc, read, writ, wait, ct): ) ) f.close() - except: + except OSError: print("ERR.cannot write result_file.csv :: %s \n use -r" % csv) return (rps, cps, dreq, dcon) @@ -559,9 +555,9 @@ def main(): print_debug( """-- status-report (perfdata)--- - + active_conns : %s - accepted conns : %s + accepted conns : %s handled : %s requests : %s accept_errors : %s @@ -573,9 +569,9 @@ def main(): reading : %s writing : %s waiting : %s - + checktime : %s ms - + """ % (ac, acc, han, req, err, rpc, rps, cps, dreq, dcon, read, writ, wait, ct) ) @@ -661,8 +657,10 @@ def main(): perfdata = "%s=%s;" % (test, ta) else: perfdata = ( - "ac=%s;acc=%s; han=%s; req=%s; err=%s; rpc=%s; rps=%s; cps=%s; dreq=%s; dcon=%s; read=%s; writ=%s; wait=%s; ct=%sms;" - % (ac, acc, han, req, err, rpc, rps, cps, dreq, dcon, read, writ, wait, ct) + "ac=%s;acc=%s; han=%s; req=%s; err=%s; rpc=%s; rps=%s; cps=%s; dreq=%s;" + % (ac, acc, han, req, err, rpc, rps, cps, dreq) + + " dcon=%s; read=%s; writ=%s; wait=%s; ct=%sms;" + % (dcon, read, writ, wait, ct) ) print_debug("perfdata: %s" % perfdata)