-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserverwatcher.py
More file actions
254 lines (216 loc) · 8.54 KB
/
Copy pathwebserverwatcher.py
File metadata and controls
254 lines (216 loc) · 8.54 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
"""WebServerWatcher monitors web server logs for successful 200 codes."""
# webserverwatcher.py
# WebServerWatcher v2026.07.02
# Copyright (C) 2026 Michael McMahon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# "Who will watch the watchmen?"
# WebServerWatcher monitors active web server logs for successful 200 codes. If
# a successfuly 200 code is not found within a reasonable time, the web server
# service will be restarted.
# The goal of this project is to be more responsive than systemd and more
# accurate than a random pause.
# In order to run, this script requires permission to restart services and view
# log files. Run with this command:
# python3 webservicewatcher.py
# This only uses standard python libraries so in hopes of living off the land.
# Import libraries
from datetime import datetime
import os
import subprocess
import sys
import syslog
import time
import configparser
# Configuration. Path may be given as argv[1] or $WEBSERVERWATCHER_CONFIG;
# otherwise default to the config beside this script (not the CWD).
default_config = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "config", "webserverwatcher.ini"
)
config_path = (
sys.argv[1] if len(sys.argv) > 1
else os.environ.get("WEBSERVERWATCHER_CONFIG", default_config)
)
config = configparser.ConfigParser()
if not config.read(config_path):
if debug > 0:
syslog.syslog(syslog.LOG_ERR, f"Config file not found: {config_path}")
print(f"Error: config file not found at {config_path}")
print("Copy config/webserverwatcher.ini.default to "
"config/webserverwatcher.ini and edit it.")
sys.exit(1)
# Access values
# Enable/disable verbose debugging
debug = config.getint("general", "debug")
# The log file will be monitored for activity
logfile = config.get("log", "logfile")
# The time field to compare to current time.
timefield = config.getint("log", "timefield")
# Seconds since a 200 code to restart the web service.
WINDOW_SECONDS = config.getfloat("time", "WINDOW_SECONDS")
# Seconds to wait after service restart.
WAIT_SECONDS = config.getfloat("time", "WAIT_SECONDS")
# Service that needs to be restarted
webservice = config.get("systemd", "webservice")
# Path for systemctl
systemctl_path = config.get("systemd", "systemctl_path")
if debug == 2:
print("Variables:")
print(f"logfile: {logfile}")
print(f"Time: {WINDOW_SECONDS}:{WAIT_SECONDS}")
print(f"Service: {webservice}")
print(f"systemctl: {systemctl_path}")
def get_status(line):
"""Return the HTTP status code from a combined/common log line.
The status is the first token after the closing quote of the request,
e.g. ... "GET / HTTP/1.1" 200 9575 ... Returns None if absent.
"""
parts = line.split('"')
if len(parts) < 3:
return None
after = parts[2].split()
return after[0] if after else None
def read_last_matching_line(filepath):
try:
# Read file line by line to handle line breaks correctly
with open(filepath, "r", encoding="utf-8") as f:
lines = f.readlines()
# Read lines from the bottom up.
for i in range(len(lines) - 1, -1, -1):
line = lines[i].strip()
# Match on the parsed status field, not a bare " 200 " anywhere
# in the line (which also hits byte counts, URLs and UAs).
#if get_status(line) == "200":
# This format allows you to add serveral valid HTTP status codes.
if get_status(line) in ("200", "503"):
return line.strip()
# If no match is found, return None.
return None
except FileNotFoundError:
if debug > 0:
syslog.syslog(syslog.LOG_ERR, f"Log file not found: {filepath}")
print(f"Error: File not found at {filepath}")
def process_log_time(line):
"""
Parse a single line from the log file, parse the timestamp, and return the
time in seconds since epoch.
"""
# Example NGINX/Apache2 log line:
# 127.0.0.1 - - [03/Apr/2026:16:44:19 -0400] "GET / HTTP/1.1" 200 9575 "-"
# "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36
# (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
# Remove the first parts (IP address and dashes) to isolate the timestamp.
parts = line.split(" ")
# print(parts)
if len(parts) > 1:
timestamp_str = parts[timefield].split()[0].strip("[]")
if debug > 1:
print(timestamp_str)
# Parse the timestamp string in seconds since epoch.
# Format: DD/Mon/YY:HH:MM:SS
# 03/Apr/2026:16:44:19
try:
dt = datetime.strptime(timestamp_str, "%d/%b/%Y:%H:%M:%S")
if debug > 2:
print(f"Last 200 time: {dt}")
ts_sec = dt.timestamp()
if debug > 2:
print(f"Last 200 timestamp: {ts_sec}")
except ValueError:
if debug > 0:
syslog.syslog(
syslog.LOG_ERR,
"Parsing timestamp failed. Fix datetime parsing.",
)
print("Error: Parsing timestamp failed. Fix datetime parsing.")
print(f"Time field: {timestamp_str}")
return None
if debug > 2:
print(ts_sec, line)
return ts_sec
# Find current time in seconds since epoch.
def get_current_time():
return time.time()
def restart_service():
try:
# Run the systemctl command using subprocess.
# systemctl restart apache2
if debug > 0:
syslog.syslog(
syslog.LOG_ERR,
f"Restarting {webservice} due to 200 inactivity.",
)
print(f"Restarting {webservice} due to 200 inactivity.")
# TODO Add dry-run mode.
# print(f"/bin/echo {systemctl_path} restart {webservice}")
# result = subprocess.run(
# ['/bin/echo', systemctl_path, 'restart', webservice],
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
result = subprocess.run(
[systemctl_path, "restart", webservice],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if debug > 2:
print(result.stdout, result.stderr, result.returncode)
if result.returncode == 0:
print("Service restarted successfully.")
else:
err = result.stderr.decode(errors="replace").strip()
if debug > 0:
syslog.syslog(
syslog.LOG_ERR,
f"Restart of {webservice} failed "
f"(exit {result.returncode}): {err}",
)
print(f"Error restarting service: {err}")
except FileNotFoundError:
print("systemctl not found. Make sure systemd is installed.")
def check_for_200():
result = read_last_matching_line(logfile)
if result:
if debug > 1:
print(f"Found last matching line: {result}")
last_200_time = process_log_time(result)
if last_200_time is None:
return
if debug > 2:
print(last_200_time)
current_time = get_current_time()
if debug > 2:
print(f"Current time: {current_time}")
if debug > 1:
print(f"Is {current_time - last_200_time:.2f} > {WINDOW_SECONDS} ?")
if (current_time - last_200_time) > WINDOW_SECONDS:
if debug > 0:
syslog.syslog(syslog.LOG_ERR, f"Engaging {webservice} restart!")
print(f"Engaging {webservice} restart!")
restart_service()
else:
if debug > 1:
print("No match found.")
print("The logs might have just rotated.")
def main():
if debug > 0:
syslog.syslog(syslog.LOG_INFO, "Process started.")
while True:
check_for_200()
if debug > 1:
print(f"Waiting for {WAIT_SECONDS} seconds...")
# Wait for the approximate time for new 200 codes to come in.
time.sleep(WAIT_SECONDS)
if __name__ == "__main__":
main()