add check_nginx

This commit is contained in:
Cyberes 2023-05-29 12:00:44 -06:00
parent b98b0d5d96
commit e33aeb0eb0
2 changed files with 83 additions and 0 deletions

0
check_nginx Normal file → Executable file
View File

83
check_nginx.py Executable file
View File

@ -0,0 +1,83 @@
#!/usr/bin/env python
import argparse
import sys
import requests
from checker import nagios
def check_nginx_status(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
print("CRITICAL - Unable to connect to Nginx stub_status: {}".format(e))
sys.exit(2)
def parse_nginx_status(status):
try:
lines = status.strip().split("\n")
data = {}
for line in lines:
parts = line.split()
if "Active" in line:
data["active_connections"] = int(parts[2])
elif "server accepts handled requests" in line:
continue
elif len(parts) == 3 and parts[1].isdigit():
data["accepted"] = int(parts[0])
data["handled"] = int(parts[1])
data["requests"] = int(parts[2])
elif "Reading" in line:
data["reading"] = int(parts[1])
data["writing"] = int(parts[3])
data["waiting"] = int(parts[5])
return data
except Exception as e:
print(f'UNKNOWN: failed to parse status page: {e}')
sys.exit(nagios.UNKNOWN)
def main():
parser = argparse.ArgumentParser(description="Check Nginx status using stub_status.")
parser.add_argument("--url", default="http://localhost/nginx_status", help="URL to Nginx stub_status.")
parser.add_argument("--critical-active", type=int, default=0, help="Critical threshold for active connections. Default: disabled")
parser.add_argument("--warning-active", type=int, default=0, help="Warning threshold for active connections. Default: disabled")
parser.add_argument("--critical-waiting", type=int, default=0, help="Critical threshold for waiting connections. Default: disabled")
parser.add_argument("--warning-waiting", type=int, default=0, help="Warning threshold for waiting connections. Default: disabled")
args = parser.parse_args()
status = check_nginx_status(args.url)
data = parse_nginx_status(status)
perfdata = "| active_connections={active_connections};{warning};{critical} waiting={waiting};{warning_waiting};{critical_waiting} accepted={accepted} handled={handled} requests={requests} reading={reading} writing={writing}".format(
warning=args.warning_active,
critical=args.critical_active,
warning_waiting=args.warning_waiting,
critical_waiting=args.critical_waiting,
**data
)
status_str = "Active connections: {active_connections}, Waiting: {waiting}, Accepted: {accepted}, Handled: {handled}, Requests: {requests}, Reading: {reading}, Writing: {writing}".format(**data)
if args.warning_active > 0 and args.critical_active > 0 and args.warning_waiting > 0 and args.critical_waiting > 0:
if data["active_connections"] >= args.critical_active or data["waiting"] >= args.critical_waiting:
print("CRITICAL:", status_str, perfdata)
sys.exit(nagios.CRITICAL)
elif data["active_connections"] >= args.warning_active or data["waiting"] >= args.warning_waiting:
print("WARNING:", status_str, perfdata)
sys.exit(nagios.WARNING)
else:
print("OK:", status_str)
sys.exit(nagios.OK)
else:
print("OK:", status_str)
print('Critical and warning levels disabled.', perfdata)
sys.exit(nagios.OK)
if __name__ == "__main__":
main()