Diff -- /var/www/vhosts/3dshawn.com/site1/alert_test_catcher.cgi

O Operator
Diff

/var/www/vhosts/3dshawn.com/site1/alert_test_catcher.cgi

modified on local at 2026-07-11 18:57:53

Added
+12
lines
Removed
-2
lines
Context
83
unchanged
Blobs
from 0c1ec2c322e7
to 215f8f105f61
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
11#!/usr/bin/perl
22#======================================================================
33# DriftSense -- /alert_test_catcher.cgi
44#
55# Tiny endpoint that accepts POSTs from the alert dispatcher and appends
66# the raw body + headers to /var/log/drift_sense/test_catcher.log. Used
77# for end-to-end alert testing without needing an external webhook.
88#
99# GET /alert_test_catcher.cgi -> HTML view of the last N events
1010# POST anything -> log it, return 200 JSON
1111# GET /alert_test_catcher.cgi?raw=1 -> tail the log as text/plain
1212#======================================================================
1313use strict;
1414use warnings;
1515use CGI ();
1616use POSIX ();
1717
1818my $LOG = '/var/log/drift_sense/test_catcher.log';
1919
2020$|=1;
2121my $cgi = CGI->new;
2222my $method = $ENV{REQUEST_METHOD} || 'GET';
2323
2424if ($method eq 'POST') {
2525 my $body = '';
26 if (defined $ENV{CONTENT_LENGTH} && $ENV{CONTENT_LENGTH} > 0) {
27 read(STDIN, $body, $ENV{CONTENT_LENGTH});
26 # Prefer CGI.pm's POSTDATA -- handles Plesk/nginx buffering + suEXEC
27 # STDIN quirks reliably; fall back to sysread if that returns empty.
28 $body = $cgi->param('POSTDATA') // '';
29 if ($body eq '' && defined $ENV{CONTENT_LENGTH} && $ENV{CONTENT_LENGTH} > 0) {
30 binmode STDIN;
31 my $want = int($ENV{CONTENT_LENGTH});
32 while (length($body) < $want) {
33 my $chunk = '';
34 my $n = sysread(STDIN, $chunk, $want - length($body));
35 last unless $n;
36 $body .= $chunk;
37 }
2838 }
2939 my $stamp = POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime);
3040 my @hdrs;
3141 foreach my $k (sort keys %ENV) {
3242 next unless $k =~ /^(HTTP_|CONTENT_|REQUEST_|REMOTE_)/;
3343 push @hdrs, " $k = $ENV{$k}";
3444 }
3545 if (open(my $fh, '>>', $LOG)) {
3646 print $fh "==================================================\n";
3747 print $fh "[$stamp] delivery received\n";
3848 print $fh join("\n", @hdrs), "\n";
3949 print $fh "-- body ($ENV{CONTENT_LENGTH} bytes) --\n";
4050 print $fh $body, "\n";
4151 close($fh);
4252 }
4353 print "Content-Type: application/json\n\n";
4454 print '{"ok":true,"received_bytes":' . length($body) . '}';
4555 exit 0;
4656}
4757
4858# GET modes
4959if ($cgi->param('raw')) {
5060 print "Content-Type: text/plain; charset=utf-8\nCache-Control: no-cache\n\n";
5161 if (open(my $fh, '<', $LOG)) {
5262 my @lines = <$fh>; close($fh);
5363 # tail last 500 lines
5464 my $start = @lines > 500 ? scalar(@lines) - 500 : 0;
5565 print join('', @lines[$start .. $#lines]);
5666 } else {
5767 print "(no deliveries yet)\n";
5868 }
5969 exit 0;
6070}
6171
6272# HTML view
6373print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
6474my $body = '';
6575if (open(my $fh, '<', $LOG)) {
6676 local $/; $body = <$fh>; close($fh);
6777}
6878my $entries = 0;
6979$entries++ while $body =~ /===+/g;
7080
7181my $esc = $body // '';
7282$esc =~ s/&/&amp;/g; $esc =~ s/</&lt;/g; $esc =~ s/>/&gt;/g;
7383
7484print <<"HTML";
7585<!doctype html><html><head><meta charset="utf-8"><title>Alert catcher -- DriftSense</title>
7686<link rel="stylesheet" href="/assets/styles/drift_sense.css">
7787<meta http-equiv="refresh" content="10">
7888</head><body>
7989<div style="padding:24px;max-width:1200px;margin:0 auto">
8090 <h1 style="color:var(--accent);font-family:var(--sans)">Alert test catcher</h1>
8191 <p style="color:var(--text-dim)">POST anything here and it gets appended to <code>$LOG</code>. Auto-refreshes every 10s. <a href="?raw=1" style="color:var(--accent-hi)">raw view</a></p>
8292 <div style="color:var(--text);font-size:12.5px;font-family:var(--mono);background:var(--bg-2);border:1px solid var(--border);border-radius:12px;padding:14px 16px;white-space:pre-wrap;max-height:75vh;overflow:auto">$esc</div>
8393</div>
8494</body></html>
8595HTML