webstat 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webstat/__init__.py +3 -0
- webstat/cli.py +407 -0
- webstat/core/checker.py +177 -0
- webstat/core/models.py +113 -0
- webstat/core/notifier.py +157 -0
- webstat/core/scheduler.py +110 -0
- webstat/core/store.py +210 -0
- webstat/utils/__init__.py +5 -0
- webstat/utils/formatting.py +74 -0
- webstat/utils/url.py +62 -0
- webstat-0.1.0.dist-info/METADATA +111 -0
- webstat-0.1.0.dist-info/RECORD +16 -0
- webstat-0.1.0.dist-info/WHEEL +5 -0
- webstat-0.1.0.dist-info/entry_points.txt +2 -0
- webstat-0.1.0.dist-info/licenses/LICENSE +21 -0
- webstat-0.1.0.dist-info/top_level.txt +1 -0
webstat/__init__.py
ADDED
webstat/cli.py
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
"""Command line interface for webstat."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from typing import List, Optional
|
|
9
|
+
|
|
10
|
+
import typer
|
|
11
|
+
from rich.console import Console
|
|
12
|
+
from rich.live import Live
|
|
13
|
+
from rich.panel import Panel
|
|
14
|
+
from rich.spinner import Spinner
|
|
15
|
+
from rich.table import Table
|
|
16
|
+
from rich.text import Text
|
|
17
|
+
|
|
18
|
+
from webstat.core.checker import HttpChecker
|
|
19
|
+
from webstat.core.models import CheckResult, MonitoredSite, SiteStatus
|
|
20
|
+
from webstat.core.notifier import NotificationDispatcher
|
|
21
|
+
from webstat.core.scheduler import WatchScheduler
|
|
22
|
+
from webstat.core.store import SiteStore
|
|
23
|
+
from webstat.utils.formatting import build_sites_table, get_latency_text, get_status_badge
|
|
24
|
+
from webstat.utils.url import get_display_name, normalize_url
|
|
25
|
+
|
|
26
|
+
app = typer.Typer(
|
|
27
|
+
name="webstat",
|
|
28
|
+
help="Fast website status checker, background watcher, and desktop reminder.",
|
|
29
|
+
no_args_is_help=False,
|
|
30
|
+
)
|
|
31
|
+
console = Console()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@app.command(name="check")
|
|
35
|
+
def check_command(
|
|
36
|
+
url: str = typer.Argument(..., help="Website URL or domain name (e.g. github.com)"),
|
|
37
|
+
watch: bool = typer.Option(
|
|
38
|
+
False,
|
|
39
|
+
"--watch",
|
|
40
|
+
"-w",
|
|
41
|
+
help="Automatically watch in foreground if site is down or not responding",
|
|
42
|
+
),
|
|
43
|
+
open_browser: bool = typer.Option(
|
|
44
|
+
False,
|
|
45
|
+
"--open",
|
|
46
|
+
"-o",
|
|
47
|
+
help="Automatically open website in default browser when reachable",
|
|
48
|
+
),
|
|
49
|
+
timeout: float = typer.Option(8.0, "--timeout", "-t", help="HTTP timeout in seconds"),
|
|
50
|
+
check_ssl: bool = typer.Option(False, "--check-ssl", "-s", help="Check TLS/SSL certificate expiration days"),
|
|
51
|
+
json_output: bool = typer.Option(False, "--json", "-j", help="Output raw JSON"),
|
|
52
|
+
) -> None:
|
|
53
|
+
"""Check the status of a website immediately."""
|
|
54
|
+
if timeout <= 0:
|
|
55
|
+
console.print("[bold red]Error:[/bold red] Timeout must be greater than 0.")
|
|
56
|
+
raise typer.Exit(code=1)
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
normalized = normalize_url(url)
|
|
60
|
+
except Exception as e:
|
|
61
|
+
console.print(f"[bold red]Error:[/bold red] {e}")
|
|
62
|
+
raise typer.Exit(code=1)
|
|
63
|
+
|
|
64
|
+
checker = HttpChecker(timeout=timeout, check_ssl=check_ssl)
|
|
65
|
+
|
|
66
|
+
with console.status(f"[cyan]Checking[/cyan] {normalized}...", spinner="dots"):
|
|
67
|
+
result = asyncio.run(checker.check(normalized))
|
|
68
|
+
|
|
69
|
+
# Automatically update store if site is in watchlist
|
|
70
|
+
store = SiteStore()
|
|
71
|
+
site = store.get_by_id_or_url(normalized)
|
|
72
|
+
if site:
|
|
73
|
+
site.status = result.status
|
|
74
|
+
site.status_code = result.status_code
|
|
75
|
+
site.latency_ms = result.latency_ms
|
|
76
|
+
site.last_checked = result.timestamp
|
|
77
|
+
site.last_error = result.error_message
|
|
78
|
+
if result.is_up:
|
|
79
|
+
site.retries = 0
|
|
80
|
+
else:
|
|
81
|
+
site.retries += 1
|
|
82
|
+
store.update_site(site)
|
|
83
|
+
|
|
84
|
+
if json_output:
|
|
85
|
+
print(json.dumps(result.to_dict(), indent=2))
|
|
86
|
+
return
|
|
87
|
+
|
|
88
|
+
badge = get_status_badge(result.status, result.status_code)
|
|
89
|
+
latency = get_latency_text(result.latency_ms)
|
|
90
|
+
|
|
91
|
+
details = []
|
|
92
|
+
if result.latency_ms:
|
|
93
|
+
details.append(f"Latency: {result.latency_ms} ms")
|
|
94
|
+
if result.redirect_chain:
|
|
95
|
+
details.append(f"Redirects: {' -> '.join(result.redirect_chain)}")
|
|
96
|
+
if result.ssl_expires_days is not None:
|
|
97
|
+
if result.ssl_expires_days < 7:
|
|
98
|
+
details.append(f"[bold yellow]SSL Warning: Expires in {result.ssl_expires_days} days![/bold yellow]")
|
|
99
|
+
else:
|
|
100
|
+
details.append(f"SSL: Valid ({result.ssl_expires_days} days left)")
|
|
101
|
+
if result.error_message:
|
|
102
|
+
details.append(f"[red]Error: {result.error_message}[/red]")
|
|
103
|
+
|
|
104
|
+
details_str = "\n".join(details)
|
|
105
|
+
panel_content = f"{badge} [bold white]{normalized}[/bold white]\n\n{details_str}"
|
|
106
|
+
|
|
107
|
+
console.print(
|
|
108
|
+
Panel(
|
|
109
|
+
panel_content,
|
|
110
|
+
title="[bold cyan]Webstat Status[/bold cyan]",
|
|
111
|
+
border_style="green" if result.is_up else "red",
|
|
112
|
+
expand=False,
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if result.is_up:
|
|
117
|
+
if open_browser:
|
|
118
|
+
import subprocess
|
|
119
|
+
subprocess.Popen(["xdg-open", normalized], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
# Site is down or non-positive
|
|
123
|
+
if watch:
|
|
124
|
+
console.print("\n[yellow]Site is not responding positively. Entering watch mode...[/yellow]")
|
|
125
|
+
run_watch_loop(
|
|
126
|
+
urls=[url],
|
|
127
|
+
auto_open=open_browser,
|
|
128
|
+
poll_interval=5.0,
|
|
129
|
+
keep_watching=False,
|
|
130
|
+
with_bell=True,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@app.command(name="watch")
|
|
135
|
+
def watch_command(
|
|
136
|
+
urls: List[str] = typer.Argument(..., help="One or more website URLs or domains to monitor"),
|
|
137
|
+
open_browser: bool = typer.Option(
|
|
138
|
+
False,
|
|
139
|
+
"--open",
|
|
140
|
+
"-o",
|
|
141
|
+
help="Automatically open website in default browser when reachable",
|
|
142
|
+
),
|
|
143
|
+
interval: float = typer.Option(
|
|
144
|
+
5.0,
|
|
145
|
+
"--interval",
|
|
146
|
+
"-i",
|
|
147
|
+
help="Polling interval in seconds",
|
|
148
|
+
),
|
|
149
|
+
keep: bool = typer.Option(
|
|
150
|
+
False,
|
|
151
|
+
"--keep",
|
|
152
|
+
"-k",
|
|
153
|
+
help="Keep watching even after site recovers",
|
|
154
|
+
),
|
|
155
|
+
no_bell: bool = typer.Option(
|
|
156
|
+
False,
|
|
157
|
+
"--no-bell",
|
|
158
|
+
help="Disable audible terminal bell on recovery",
|
|
159
|
+
),
|
|
160
|
+
) -> None:
|
|
161
|
+
"""Watch one or more websites and alert you with desktop notification when they are UP."""
|
|
162
|
+
if interval < 0.5:
|
|
163
|
+
console.print("[bold red]Error:[/bold red] Polling interval must be at least 0.5 seconds.")
|
|
164
|
+
raise typer.Exit(code=1)
|
|
165
|
+
|
|
166
|
+
run_watch_loop(
|
|
167
|
+
urls=urls,
|
|
168
|
+
auto_open=open_browser,
|
|
169
|
+
poll_interval=interval,
|
|
170
|
+
keep_watching=keep,
|
|
171
|
+
with_bell=not no_bell,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def run_watch_loop(
|
|
176
|
+
urls: List[str],
|
|
177
|
+
auto_open: bool,
|
|
178
|
+
poll_interval: float,
|
|
179
|
+
keep_watching: bool,
|
|
180
|
+
with_bell: bool,
|
|
181
|
+
) -> None:
|
|
182
|
+
store = SiteStore()
|
|
183
|
+
sites: list[MonitoredSite] = []
|
|
184
|
+
|
|
185
|
+
for u in urls:
|
|
186
|
+
try:
|
|
187
|
+
site = store.add(
|
|
188
|
+
raw_url=u,
|
|
189
|
+
auto_open=auto_open,
|
|
190
|
+
notify_on_up=True,
|
|
191
|
+
keep_watching=keep_watching,
|
|
192
|
+
)
|
|
193
|
+
sites.append(site)
|
|
194
|
+
except Exception as e:
|
|
195
|
+
console.print(f"[bold red]Skipping invalid URL '{u}':[/bold red] {e}")
|
|
196
|
+
|
|
197
|
+
if not sites:
|
|
198
|
+
raise typer.Exit(code=1)
|
|
199
|
+
|
|
200
|
+
dispatcher = NotificationDispatcher(with_bell=with_bell)
|
|
201
|
+
scheduler = WatchScheduler(
|
|
202
|
+
store=store,
|
|
203
|
+
dispatcher=dispatcher,
|
|
204
|
+
poll_interval=poll_interval,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
console.print(
|
|
208
|
+
f"[bold cyan]Monitoring {len(sites)} site(s)...[/bold cyan] (Press [bold red]Ctrl+C[/bold red] to stop)\n"
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
def render_progress_table() -> Table:
|
|
212
|
+
table = Table(box=None, expand=True, padding=(0, 1))
|
|
213
|
+
table.add_column("Site", style="bold white", width=30)
|
|
214
|
+
table.add_column("Status", width=22)
|
|
215
|
+
table.add_column("Latency", justify="right", width=12)
|
|
216
|
+
table.add_column("Checks", justify="right", width=10)
|
|
217
|
+
table.add_column("Details", style="dim")
|
|
218
|
+
|
|
219
|
+
for s in sites:
|
|
220
|
+
badge = get_status_badge(s.status, s.status_code)
|
|
221
|
+
latency = get_latency_text(s.latency_ms)
|
|
222
|
+
checks_count = str(s.retries) if s.retries > 0 else "1"
|
|
223
|
+
details = s.last_error or ("Ready" if s.is_up else "Checking...")
|
|
224
|
+
table.add_row(s.display_name, badge, latency, checks_count, details)
|
|
225
|
+
|
|
226
|
+
return table
|
|
227
|
+
|
|
228
|
+
recovered_any = False
|
|
229
|
+
|
|
230
|
+
try:
|
|
231
|
+
with Live(render_progress_table(), refresh_per_second=2, console=console) as live:
|
|
232
|
+
def on_tick(site: MonitoredSite, result: CheckResult, just_recovered: bool):
|
|
233
|
+
nonlocal recovered_any
|
|
234
|
+
if just_recovered:
|
|
235
|
+
recovered_any = True
|
|
236
|
+
live.update(render_progress_table())
|
|
237
|
+
|
|
238
|
+
asyncio.run(scheduler.watch_sites(sites, on_tick=on_tick))
|
|
239
|
+
|
|
240
|
+
except KeyboardInterrupt:
|
|
241
|
+
console.print("\n[yellow]Watch stopped by user.[/yellow]")
|
|
242
|
+
return
|
|
243
|
+
|
|
244
|
+
if recovered_any:
|
|
245
|
+
console.print("\n[bold green]✓ Site is back online! Notification sent.[/bold green]")
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@app.command(name="add")
|
|
249
|
+
def add_command(
|
|
250
|
+
url: str = typer.Argument(..., help="Website URL to add to background watchlist"),
|
|
251
|
+
open_browser: bool = typer.Option(
|
|
252
|
+
False,
|
|
253
|
+
"--open",
|
|
254
|
+
"-o",
|
|
255
|
+
help="Automatically launch browser when site recovers",
|
|
256
|
+
),
|
|
257
|
+
keep: bool = typer.Option(
|
|
258
|
+
False,
|
|
259
|
+
"--keep",
|
|
260
|
+
"-k",
|
|
261
|
+
help="Keep monitoring continuously even after it recovers",
|
|
262
|
+
),
|
|
263
|
+
) -> None:
|
|
264
|
+
"""Add a website to the persistent background monitoring watchlist."""
|
|
265
|
+
store = SiteStore()
|
|
266
|
+
try:
|
|
267
|
+
site = store.add(
|
|
268
|
+
raw_url=url,
|
|
269
|
+
auto_open=open_browser,
|
|
270
|
+
notify_on_up=True,
|
|
271
|
+
keep_watching=keep,
|
|
272
|
+
)
|
|
273
|
+
# Perform fast initial health check so numbers appear immediately
|
|
274
|
+
checker = HttpChecker(timeout=4.0)
|
|
275
|
+
result = asyncio.run(checker.check(site.url))
|
|
276
|
+
site.status = result.status
|
|
277
|
+
site.status_code = result.status_code
|
|
278
|
+
site.latency_ms = result.latency_ms
|
|
279
|
+
site.last_checked = result.timestamp
|
|
280
|
+
site.last_error = result.error_message
|
|
281
|
+
store.update_site(site)
|
|
282
|
+
|
|
283
|
+
console.print(f"[bold green]Added:[/bold green] {site.display_name} ({site.url}) [{site.status.value}]")
|
|
284
|
+
except Exception as e:
|
|
285
|
+
console.print(f"[bold red]Failed to add site:[/bold red] {e}")
|
|
286
|
+
raise typer.Exit(code=1)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
@app.command(name="list")
|
|
290
|
+
def list_command(
|
|
291
|
+
json_output: bool = typer.Option(False, "--json", "-j", help="Output raw JSON array"),
|
|
292
|
+
) -> None:
|
|
293
|
+
"""List all currently watched websites and their statuses."""
|
|
294
|
+
store = SiteStore()
|
|
295
|
+
sites = store.load_all()
|
|
296
|
+
|
|
297
|
+
if json_output:
|
|
298
|
+
data = [s.to_dict() for s in sites]
|
|
299
|
+
print(json.dumps(data, indent=2))
|
|
300
|
+
return
|
|
301
|
+
|
|
302
|
+
if not sites:
|
|
303
|
+
console.print("[dim]No websites in watchlist. Add one with: [bold cyan]webstat add <url>[/bold cyan][/dim]")
|
|
304
|
+
return
|
|
305
|
+
|
|
306
|
+
console.print(build_sites_table(sites))
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
@app.command(name="remove")
|
|
310
|
+
def remove_command(
|
|
311
|
+
target: str = typer.Argument(..., help="URL, domain name, or site ID to remove"),
|
|
312
|
+
) -> None:
|
|
313
|
+
"""Remove a website from the monitoring watchlist."""
|
|
314
|
+
store = SiteStore()
|
|
315
|
+
if store.remove(target):
|
|
316
|
+
console.print(f"[bold green]Removed:[/bold green] '{target}' from watchlist.")
|
|
317
|
+
else:
|
|
318
|
+
console.print(f"[bold red]Not found:[/bold red] No site matching '{target}' in watchlist.")
|
|
319
|
+
raise typer.Exit(code=1)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@app.command(name="clear")
|
|
323
|
+
def clear_command() -> None:
|
|
324
|
+
"""Clear all websites from the watchlist."""
|
|
325
|
+
store = SiteStore()
|
|
326
|
+
store.clear()
|
|
327
|
+
console.print("[bold green]Watchlist cleared.[/bold green]")
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
@app.command(name="refresh")
|
|
331
|
+
def refresh_command() -> None:
|
|
332
|
+
"""Check all monitored websites immediately and update their status."""
|
|
333
|
+
store = SiteStore()
|
|
334
|
+
sites = store.load_all()
|
|
335
|
+
if not sites:
|
|
336
|
+
console.print("[dim]No websites in watchlist.[/dim]")
|
|
337
|
+
return
|
|
338
|
+
dispatcher = NotificationDispatcher()
|
|
339
|
+
checker = HttpChecker()
|
|
340
|
+
scheduler = WatchScheduler(store=store, dispatcher=dispatcher, checker=checker)
|
|
341
|
+
|
|
342
|
+
async def run_all():
|
|
343
|
+
sem = asyncio.Semaphore(10)
|
|
344
|
+
|
|
345
|
+
async def bounded_check(s):
|
|
346
|
+
async with sem:
|
|
347
|
+
return await scheduler.check_and_update(s)
|
|
348
|
+
|
|
349
|
+
tasks = [bounded_check(s) for s in sites]
|
|
350
|
+
await asyncio.gather(*tasks)
|
|
351
|
+
|
|
352
|
+
asyncio.run(run_all())
|
|
353
|
+
console.print(f"[bold green]Refreshed {len(sites)} website(s).[/bold green]")
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
@app.command(name="daemon")
|
|
357
|
+
def daemon_command(
|
|
358
|
+
interval: float = typer.Option(10.0, "--interval", "-i", help="Polling interval in seconds"),
|
|
359
|
+
) -> None:
|
|
360
|
+
"""Run the continuous background monitoring daemon."""
|
|
361
|
+
if interval < 1.0:
|
|
362
|
+
console.print("[bold red]Error:[/bold red] Daemon polling interval must be at least 1.0 second.")
|
|
363
|
+
raise typer.Exit(code=1)
|
|
364
|
+
|
|
365
|
+
store = SiteStore()
|
|
366
|
+
dispatcher = NotificationDispatcher()
|
|
367
|
+
checker = HttpChecker()
|
|
368
|
+
scheduler = WatchScheduler(store=store, dispatcher=dispatcher, checker=checker, poll_interval=interval)
|
|
369
|
+
|
|
370
|
+
console.print("[bold cyan]webstat daemon started.[/bold cyan] Watching state queue...")
|
|
371
|
+
|
|
372
|
+
async def daemon_loop():
|
|
373
|
+
while True:
|
|
374
|
+
sites = store.load_all()
|
|
375
|
+
# Only poll sites that are either down or marked keep_watching
|
|
376
|
+
to_poll = [s for s in sites if not s.is_up or s.keep_watching or s.status == SiteStatus.UNKNOWN]
|
|
377
|
+
if to_poll:
|
|
378
|
+
for site in to_poll:
|
|
379
|
+
result, just_recovered = await scheduler.check_and_update(site)
|
|
380
|
+
if just_recovered:
|
|
381
|
+
console.print(f"[green]Recovered:[/green] {site.display_name} -> {result.status_code} OK")
|
|
382
|
+
await asyncio.sleep(interval)
|
|
383
|
+
|
|
384
|
+
try:
|
|
385
|
+
asyncio.run(daemon_loop())
|
|
386
|
+
except KeyboardInterrupt:
|
|
387
|
+
console.print("\n[yellow]webstat daemon stopped.[/yellow]")
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
KNOWN_COMMANDS = {"check", "watch", "add", "list", "remove", "clear", "refresh", "daemon"}
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
@app.callback()
|
|
394
|
+
def main_callback() -> None:
|
|
395
|
+
"""webstat: Website status checker and desktop recovery reminder."""
|
|
396
|
+
pass
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def main() -> None:
|
|
400
|
+
# If the first non-flag argument is not a known subcommand, default to "check"
|
|
401
|
+
if len(sys.argv) > 1 and not sys.argv[1].startswith("-") and sys.argv[1] not in KNOWN_COMMANDS:
|
|
402
|
+
sys.argv.insert(1, "check")
|
|
403
|
+
app()
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
if __name__ == "__main__":
|
|
407
|
+
main()
|
webstat/core/checker.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Health check probe implementations for webstat."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import abc
|
|
6
|
+
import datetime
|
|
7
|
+
import socket
|
|
8
|
+
import ssl
|
|
9
|
+
import time
|
|
10
|
+
from urllib.parse import urlparse
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
from webstat.core.models import CheckResult, SiteStatus
|
|
15
|
+
from webstat.utils.url import normalize_url
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BaseChecker(abc.ABC):
|
|
19
|
+
"""Abstract base class for all health checkers/probes."""
|
|
20
|
+
|
|
21
|
+
@abc.abstractmethod
|
|
22
|
+
async def check(self, url: str) -> CheckResult:
|
|
23
|
+
"""Execute the probe and return a CheckResult."""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HttpChecker(BaseChecker):
|
|
28
|
+
"""Standard HTTP/HTTPS checker with latency measurement and error categorization."""
|
|
29
|
+
|
|
30
|
+
def __init__(self, timeout: float = 5.0, user_agent: str = "webstat/0.1.0", check_ssl: bool = False):
|
|
31
|
+
self.timeout = timeout
|
|
32
|
+
self.user_agent = user_agent
|
|
33
|
+
self.check_ssl = check_ssl
|
|
34
|
+
|
|
35
|
+
async def check(self, url: str) -> CheckResult:
|
|
36
|
+
normalized = normalize_url(url)
|
|
37
|
+
redirect_chain: list[str] = []
|
|
38
|
+
start_time = time.perf_counter()
|
|
39
|
+
|
|
40
|
+
headers = {
|
|
41
|
+
"User-Agent": self.user_agent,
|
|
42
|
+
"Accept": "*/*",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
async with httpx.AsyncClient(
|
|
47
|
+
timeout=self.timeout,
|
|
48
|
+
follow_redirects=True,
|
|
49
|
+
max_redirects=10,
|
|
50
|
+
headers=headers,
|
|
51
|
+
verify=True,
|
|
52
|
+
) as client:
|
|
53
|
+
# Try fast HEAD probe first to avoid downloading large bodies
|
|
54
|
+
status_code: int
|
|
55
|
+
used_stream = False
|
|
56
|
+
try:
|
|
57
|
+
head_response = await client.head(normalized)
|
|
58
|
+
if head_response.status_code in (405, 501):
|
|
59
|
+
used_stream = True
|
|
60
|
+
else:
|
|
61
|
+
status_code = head_response.status_code
|
|
62
|
+
if head_response.history:
|
|
63
|
+
for resp in head_response.history:
|
|
64
|
+
redirect_chain.append(f"{resp.status_code} -> {resp.url}")
|
|
65
|
+
except (httpx.RequestError, httpx.HTTPStatusError):
|
|
66
|
+
used_stream = True
|
|
67
|
+
|
|
68
|
+
if used_stream:
|
|
69
|
+
# Stream GET to avoid buffering unbounded response bodies into RAM
|
|
70
|
+
async with client.stream("GET", normalized) as stream_resp:
|
|
71
|
+
status_code = stream_resp.status_code
|
|
72
|
+
if stream_resp.history:
|
|
73
|
+
for resp in stream_resp.history:
|
|
74
|
+
redirect_chain.append(f"{resp.status_code} -> {resp.url}")
|
|
75
|
+
|
|
76
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
77
|
+
|
|
78
|
+
if 200 <= status_code < 300:
|
|
79
|
+
status = SiteStatus.ONLINE
|
|
80
|
+
elif 300 <= status_code < 400:
|
|
81
|
+
status = SiteStatus.REDIRECT
|
|
82
|
+
elif 400 <= status_code < 500:
|
|
83
|
+
status = SiteStatus.CLIENT_ERROR
|
|
84
|
+
else:
|
|
85
|
+
status = SiteStatus.SERVER_ERROR
|
|
86
|
+
|
|
87
|
+
ssl_days = None
|
|
88
|
+
if self.check_ssl and normalized.startswith("https://"):
|
|
89
|
+
ssl_days = self._check_ssl_expiry(normalized)
|
|
90
|
+
|
|
91
|
+
return CheckResult(
|
|
92
|
+
url=normalized,
|
|
93
|
+
status=status,
|
|
94
|
+
status_code=status_code,
|
|
95
|
+
latency_ms=elapsed_ms,
|
|
96
|
+
redirect_chain=redirect_chain,
|
|
97
|
+
ssl_expires_days=ssl_days,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
except httpx.ConnectTimeout:
|
|
101
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
102
|
+
return CheckResult(
|
|
103
|
+
url=normalized,
|
|
104
|
+
status=SiteStatus.UNREACHABLE,
|
|
105
|
+
latency_ms=elapsed_ms,
|
|
106
|
+
error_message="Connection timed out",
|
|
107
|
+
)
|
|
108
|
+
except httpx.ReadTimeout:
|
|
109
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
110
|
+
return CheckResult(
|
|
111
|
+
url=normalized,
|
|
112
|
+
status=SiteStatus.UNREACHABLE,
|
|
113
|
+
latency_ms=elapsed_ms,
|
|
114
|
+
error_message="Response read timed out",
|
|
115
|
+
)
|
|
116
|
+
except httpx.ConnectError as e:
|
|
117
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
118
|
+
err_str = str(e)
|
|
119
|
+
if "Name or service not known" in err_str or "getaddrinfo failed" in err_str:
|
|
120
|
+
msg = "DNS lookup failed (host not found)"
|
|
121
|
+
elif "Connection refused" in err_str:
|
|
122
|
+
msg = "Connection refused (port closed or server down)"
|
|
123
|
+
else:
|
|
124
|
+
msg = f"Connection error: {err_str}"
|
|
125
|
+
return CheckResult(
|
|
126
|
+
url=normalized,
|
|
127
|
+
status=SiteStatus.UNREACHABLE,
|
|
128
|
+
latency_ms=elapsed_ms,
|
|
129
|
+
error_message=msg,
|
|
130
|
+
)
|
|
131
|
+
except httpx.HTTPError as e:
|
|
132
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
133
|
+
return CheckResult(
|
|
134
|
+
url=normalized,
|
|
135
|
+
status=SiteStatus.UNREACHABLE,
|
|
136
|
+
latency_ms=elapsed_ms,
|
|
137
|
+
error_message=f"HTTP protocol error: {e}",
|
|
138
|
+
)
|
|
139
|
+
except Exception as e:
|
|
140
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 1)
|
|
141
|
+
return CheckResult(
|
|
142
|
+
url=normalized,
|
|
143
|
+
status=SiteStatus.UNREACHABLE,
|
|
144
|
+
latency_ms=elapsed_ms,
|
|
145
|
+
error_message=str(e),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def _check_ssl_expiry(self, url: str) -> int | None:
|
|
149
|
+
"""Quick check for TLS certificate expiration days remaining."""
|
|
150
|
+
try:
|
|
151
|
+
parsed = urlparse(url)
|
|
152
|
+
host = parsed.hostname
|
|
153
|
+
port = parsed.port or 443
|
|
154
|
+
if not host:
|
|
155
|
+
return None
|
|
156
|
+
|
|
157
|
+
ctx = ssl.create_default_context()
|
|
158
|
+
with socket.create_connection((host, port), timeout=3.0) as sock:
|
|
159
|
+
with ctx.wrap_socket(sock, server_hostname=host) as ssock:
|
|
160
|
+
cert = ssock.getpeercert()
|
|
161
|
+
if not cert or "notAfter" not in cert:
|
|
162
|
+
return None
|
|
163
|
+
# notAfter example: 'May 15 12:00:00 2026 GMT'
|
|
164
|
+
expiry_dt = datetime.datetime.strptime(
|
|
165
|
+
cert["notAfter"], "%b %d %H:%M:%S %Y %Z"
|
|
166
|
+
).replace(tzinfo=datetime.timezone.utc)
|
|
167
|
+
now_dt = datetime.datetime.now(datetime.timezone.utc)
|
|
168
|
+
remaining_seconds = (expiry_dt - now_dt).total_seconds()
|
|
169
|
+
return max(0, int(remaining_seconds // 86400))
|
|
170
|
+
except Exception:
|
|
171
|
+
return None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
async def check_url(url: str, timeout: float = 8.0) -> CheckResult:
|
|
175
|
+
"""Convenience helper to check a single URL asynchronously."""
|
|
176
|
+
checker = HttpChecker(timeout=timeout)
|
|
177
|
+
return await checker.check(url)
|
webstat/core/models.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Data models and enums for webstat."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from dataclasses import asdict, dataclass, field
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class SiteStatus(str, Enum):
|
|
12
|
+
ONLINE = "ONLINE"
|
|
13
|
+
REDIRECT = "REDIRECT"
|
|
14
|
+
CLIENT_ERROR = "CLIENT_ERROR"
|
|
15
|
+
SERVER_ERROR = "SERVER_ERROR"
|
|
16
|
+
UNREACHABLE = "UNREACHABLE"
|
|
17
|
+
UNKNOWN = "UNKNOWN"
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def is_positive(self) -> bool:
|
|
21
|
+
"""True if the site responded positively and can be browsed."""
|
|
22
|
+
return self in (SiteStatus.ONLINE, SiteStatus.REDIRECT)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class CheckResult:
|
|
27
|
+
url: str
|
|
28
|
+
status: SiteStatus
|
|
29
|
+
status_code: int | None = None
|
|
30
|
+
latency_ms: float | None = None
|
|
31
|
+
redirect_chain: list[str] = field(default_factory=list)
|
|
32
|
+
error_message: str | None = None
|
|
33
|
+
ssl_expires_days: int | None = None
|
|
34
|
+
timestamp: float = field(default_factory=time.time)
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def is_up(self) -> bool:
|
|
38
|
+
return self.status.is_positive
|
|
39
|
+
|
|
40
|
+
def to_dict(self) -> dict[str, Any]:
|
|
41
|
+
data = asdict(self)
|
|
42
|
+
data["status"] = self.status.value
|
|
43
|
+
data["is_up"] = self.is_up
|
|
44
|
+
return data
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class MonitoredSite:
|
|
49
|
+
id: str
|
|
50
|
+
url: str
|
|
51
|
+
display_name: str
|
|
52
|
+
raw_input: str
|
|
53
|
+
auto_open: bool = False
|
|
54
|
+
notify_on_up: bool = True
|
|
55
|
+
keep_watching: bool = False
|
|
56
|
+
status: SiteStatus = SiteStatus.UNKNOWN
|
|
57
|
+
status_code: int | None = None
|
|
58
|
+
latency_ms: float | None = None
|
|
59
|
+
last_checked: float | None = None
|
|
60
|
+
retries: int = 0
|
|
61
|
+
last_error: str | None = None
|
|
62
|
+
created_at: float = field(default_factory=time.time)
|
|
63
|
+
last_notified_at: float | None = None
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def is_up(self) -> bool:
|
|
67
|
+
return self.status.is_positive
|
|
68
|
+
|
|
69
|
+
def to_dict(self) -> dict[str, Any]:
|
|
70
|
+
return {
|
|
71
|
+
"id": self.id,
|
|
72
|
+
"url": self.url,
|
|
73
|
+
"display_name": self.display_name,
|
|
74
|
+
"raw_input": self.raw_input,
|
|
75
|
+
"auto_open": self.auto_open,
|
|
76
|
+
"notify_on_up": self.notify_on_up,
|
|
77
|
+
"keep_watching": self.keep_watching,
|
|
78
|
+
"status": self.status.value,
|
|
79
|
+
"status_code": self.status_code,
|
|
80
|
+
"latency_ms": self.latency_ms,
|
|
81
|
+
"last_checked": self.last_checked,
|
|
82
|
+
"retries": self.retries,
|
|
83
|
+
"last_error": self.last_error,
|
|
84
|
+
"created_at": self.created_at,
|
|
85
|
+
"last_notified_at": self.last_notified_at,
|
|
86
|
+
"is_up": self.is_up,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def from_dict(cls, data: dict[str, Any]) -> MonitoredSite:
|
|
91
|
+
status_val = data.get("status", SiteStatus.UNKNOWN.value)
|
|
92
|
+
try:
|
|
93
|
+
status = SiteStatus(status_val)
|
|
94
|
+
except ValueError:
|
|
95
|
+
status = SiteStatus.UNKNOWN
|
|
96
|
+
|
|
97
|
+
return cls(
|
|
98
|
+
id=data["id"],
|
|
99
|
+
url=data["url"],
|
|
100
|
+
display_name=data.get("display_name", data["url"]),
|
|
101
|
+
raw_input=data.get("raw_input", data["url"]),
|
|
102
|
+
auto_open=data.get("auto_open", False),
|
|
103
|
+
notify_on_up=data.get("notify_on_up", True),
|
|
104
|
+
keep_watching=data.get("keep_watching", False),
|
|
105
|
+
status=status,
|
|
106
|
+
status_code=data.get("status_code"),
|
|
107
|
+
latency_ms=data.get("latency_ms"),
|
|
108
|
+
last_checked=data.get("last_checked"),
|
|
109
|
+
retries=data.get("retries", 0),
|
|
110
|
+
last_error=data.get("last_error"),
|
|
111
|
+
created_at=data.get("created_at", time.time()),
|
|
112
|
+
last_notified_at=data.get("last_notified_at"),
|
|
113
|
+
)
|