-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheck_apache2_status.py
executable file
·118 lines (96 loc) · 3.82 KB
/
check_apache2_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright 2014 - Guillaume Subiron (Sysnove)
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
#
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu
import argparse
import requests
import time
import json
import re
TMP_FILE = "/var/tmp/nagios_check_apache2_status_last_run"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hostname", default="localhost")
parser.add_argument("-P", "--port", default=80)
parser.add_argument("-S", "--ssl", nargs="?", default=False, const=True)
parser.add_argument("-s", "--status-page", default="server-status")
parser.add_argument("-w", "--total-warning", default=80, type=int)
args = parser.parse_args()
scheme = "https" if args.ssl else "http"
url = "%s://%s:%s/%s?auto" % (scheme, args.hostname, args.port, args.status_page)
try:
r = requests.get(url, verify=False)
except:
print("UNKNOWN - Error requesting %s" % url)
raise SystemExit(3)
if r.status_code != 200:
print("CRITICAL - %s returns %s" % (url, r.status_code))
raise SystemExit(2)
values = dict()
for line in r.text.splitlines():
values[line.split(":")[0]] = " ".join(line.split(":")[1:]).strip()
for k, v in values.items():
try:
values[k] = int(v)
except:
pass
try:
version = values["ServerVersion"].split(" ")[0]
except KeyError:
version = "Apache2"
values["MaxWorkers"] = len(values["Scoreboard"])
values["WarnWorkers"] = int(values["MaxWorkers"] * (args.total_warning / 100.0))
values["TotalWorkers"] = values["IdleWorkers"] + values["BusyWorkers"]
now = int(time.time())
# In Apache2 status, ReqPerSec & BytesPerSec are computed since the last restart of Apache2.
# Here we recompute them since the last call of this plugin.
try:
last_check = json.load(open(TMP_FILE))
except:
print("UNKNOWN - %s does not exist, please run the check again." % TMP_FILE)
raise SystemExit(3)
finally:
json.dump((now, values["Total Accesses"], values["Total kBytes"]), open(TMP_FILE, "w"))
values["ReqPerSec"] = "%.2f" % max(
0, round((values["Total Accesses"] - last_check[1]) / (now - last_check[0]), 2)
)
values["BytesPerSec"] = "%.2f" % max(
0, round(((values["Total kBytes"] - last_check[2]) * 1000) / (now - last_check[0]), 2)
)
perfdata = (
"Uptime: %(Uptime)s, ReqPerSec: %(ReqPerSec)s, BytesPerSec: %(BytesPerSec)s, Workers: %(TotalWorkers)s (Busy: %(BusyWorkers)s, Idle: %(IdleWorkers)s)|Uptime=%(Uptime)ss; ReqPerSec=%(ReqPerSec)s; BytesPerSec=%(BytesPerSec)s; BusyWorkers=%(BusyWorkers)s;%(WarnWorkers)s;%(MaxWorkers)s;0;%(MaxWorkers)s; IdleWorkers=%(IdleWorkers)s;"
% values
)
if values["IdleWorkers"] < 2:
print("%s WARNING (IdleWorkers < 2) - %s" % (version, perfdata))
raise SystemExit(1)
if values["BusyWorkers"] >= values["WarnWorkers"]:
print(
"%s WARNING (BusyWorkers > WarnWorkers=%s) - %s"
% (version, values["WarnWorkers"], perfdata)
)
raise SystemExit(1)
if values["TotalWorkers"] > values["MaxWorkers"]:
print(
"%s WARNING (TotalWorkers > MaxWorkers=%s) - %s"
% (version, values["MaxWorkers"], perfdata)
)
raise SystemExit(1)
else:
print("%s OK - %s" % (version, perfdata))
if __name__ == "__main__":
main()