HEX
Server: LiteSpeed
System: Linux s3604.bom1.stableserver.net 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: dmstechonline (1480)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //lib64/nagios/plugins/check_apachewebserver
#!/usr/bin/python3

import requests
import re
import sys
import argparse
import subprocess
from collections import defaultdict

def service_is_active(service):
    try:
        result = subprocess.run(
            ["systemctl", "-q", "is-active", service],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )
        return result.returncode == 0
    except Exception:
        return False

if service_is_active("lshttpd"):
    print("[OK] LiteSpeed is active, Apache check skipped")
    sys.exit(0)

if not service_is_active("httpd"):
    print("[OK] Apache is not active")
    sys.exit(0)

parser = argparse.ArgumentParser()
parser.add_argument('-c', type=int, default=200, help='Threshold value for CRITICAL high traffic domains')
parser.add_argument('-w', type=int, default=50, help='Threshold value for WARNING moderate traffic domains')
args = parser.parse_args()

try:
    response = requests.get(
        'http://127.0.0.1/whm-server-status',
        headers={'User-Agent': 'A2 User Agent OPS TEAM/4147 Gecko/20190813'},
        timeout=10
    )
    response.raise_for_status()
    content = response.text
except Exception as e:
    print("[UNKNOWN] Failed to fetch Apache status page: {}".format(e))
    sys.exit(3)

domain_counts = defaultdict(int)

for match in re.finditer(r'<td nowrap>([^<:]+)(?::\d+)?</td><td nowrap>', content):
    vhost = match.group(1).strip()
    if vhost:
        domain_counts[vhost] += 1

high_traffic_domains = [(domain, count) for domain, count in domain_counts.items() if count > args.c]
moderate_traffic_domains = [(domain, count) for domain, count in domain_counts.items() if count > args.w and count <= args.c]

if high_traffic_domains:
    output = "[CRITICAL] " + ", ".join(
        ["{} ({} req/s)".format(domain, count) for domain, count in sorted(high_traffic_domains, key=lambda x: x[1], reverse=True)]
    )
    print(output)
    sys.exit(2)
elif moderate_traffic_domains:
    output = "[WARNING] " + ", ".join(
        ["{} ({} req/s)".format(domain, count) for domain, count in sorted(moderate_traffic_domains, key=lambda x: x[1], reverse=True)]
    )
    print(output)
    sys.exit(1)
else:
    print("[OK] No high traffic Apache domains found")
    sys.exit(0)