Diff -- /var/www/vhosts/3dshawn.com/site1/alerts.cgi
Diff
/var/www/vhosts/3dshawn.com/site1/alerts.cgi
modified on local at 2026-07-12 00:02:06
Added
+12
lines
Removed
-3
lines
Context
219
unchanged
Blobs
from ec54b82a47de
to 3738a3d43d87
to 3738a3d43d87
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /alerts.cgi |
| 4 | 4 | # |
| 5 | 5 | # List, add, and edit alert rules. Also shows recent delivery history |
| 6 | 6 | # per rule (last 30 events). POST handling: |
| 7 | 7 | # op=create -> insert |
| 8 | 8 | # op=update -> update by alert_rule_id |
| 9 | 9 | # op=delete -> soft delete (is_active=0) |
| 10 | 10 | # op=toggle -> flip is_active |
| 11 | 11 | #====================================================================== |
| 12 | 12 | use strict; |
| 13 | 13 | use warnings; |
| 14 | 14 | use CGI (); |
| 15 | 15 | use MODS::Config; |
| 16 | 16 | use MODS::DBConnect; |
| 17 | 17 | use MODS::Template; |
| 18 | 18 | use MODS::PageWrapper; |
| 19 | 19 | |
| 20 | 20 | my $cgi = CGI->new; |
| 21 | 21 | my $db = MODS::DBConnect->new; |
| 22 | 22 | my $tpl = MODS::Template->new; |
| 23 | 23 | |
| 24 | 24 | $|=1; |
| 25 | 25 | |
| 26 | 26 | my $dbh = $db->db_connect or die "DriftSense: cannot connect to storage DB\n"; |
| 27 | 27 | |
| 28 | 28 | # ---- POST actions --------------------------------------------------- |
| 29 | 29 | my $method = $ENV{REQUEST_METHOD} || 'GET'; |
| 30 | 30 | if ($method eq 'POST') { |
| 31 | 31 | my $op = $cgi->param('op') || ''; |
| 32 | 32 | |
| 33 | 33 | if ($op eq 'create' || $op eq 'update') { |
| 34 | 34 | my $rid = $op eq 'update' ? int($cgi->param('alert_rule_id') || 0) : 0; |
| 35 | 35 | my $name = _clean($cgi->param('rule_name'), 200); |
| 36 | 36 | my $kind = _one_of($cgi->param('match_kind'), 'either', 'file', 'schema', 'either'); |
| 37 | 37 | my $glob = _clean($cgi->param('match_path_glob'), 500); |
| 38 | 38 | my $stat = _clean($cgi->param('match_status_list'), 200); |
| 39 | 39 | my $xts = $cgi->param('exclude_ts_only') ? 1 : 0; |
| 40 | 40 | my $dkind = _one_of($cgi->param('delivery_kind'), |
| 41 | 41 | 'generic_webhook', qw(slack discord generic_webhook email)); |
| 42 | 42 | my $durl = _clean($cgi->param('delivery_url'), 2000); |
| 43 | 43 | my $dmail = _clean($cgi->param('delivery_email'), 500); |
| 44 | 44 | my $rlim = int($cgi->param('rate_limit_per_min') || 0); |
| 45 | 45 | my $csec = int($cgi->param('coalesce_window_s') || 0); |
| 46 | 46 | my $act = $cgi->param('is_active') ? 1 : 0; |
| 47 | my $fthr = int($cgi->param('frequency_threshold') || 0); | |
| 48 | my $fwin = int($cgi->param('frequency_window_min') || 60); | |
| 49 | $fwin = 60 if $fwin < 1; | |
| 47 | 50 | |
| 48 | 51 | my $q = sub { $dbh->quote($_[0] // '') }; |
| 49 | 52 | |
| 50 | 53 | if ($op eq 'create') { |
| 51 | 54 | $db->db_readwrite($dbh, qq~ |
| 52 | 55 | INSERT INTO ALERT_RULES |
| 53 | 56 | (rule_name, is_active, match_kind, match_path_glob, |
| 54 | 57 | match_status_list, exclude_ts_only, |
| 55 | 58 | delivery_kind, delivery_url, delivery_email, |
| 56 | rate_limit_per_min, coalesce_window_s) | |
| 59 | rate_limit_per_min, coalesce_window_s, | |
| 60 | frequency_threshold, frequency_window_min) | |
| 57 | 61 | VALUES |
| 58 | 62 | (@{[ $q->($name) ]}, $act, @{[ $q->($kind) ]}, |
| 59 | 63 | @{[ $q->($glob) ]}, @{[ $q->($stat) ]}, $xts, |
| 60 | 64 | @{[ $q->($dkind) ]}, @{[ $q->($durl) ]}, @{[ $q->($dmail) ]}, |
| 61 | $rlim, $csec) | |
| 65 | $rlim, $csec, $fthr, $fwin) | |
| 62 | 66 | ~, __FILE__, __LINE__); |
| 63 | 67 | } else { |
| 64 | 68 | $db->db_readwrite($dbh, qq~ |
| 65 | 69 | UPDATE ALERT_RULES |
| 66 | 70 | SET rule_name = @{[ $q->($name) ]}, |
| 67 | 71 | is_active = $act, |
| 68 | 72 | match_kind = @{[ $q->($kind) ]}, |
| 69 | 73 | match_path_glob = @{[ $q->($glob) ]}, |
| 70 | 74 | match_status_list = @{[ $q->($stat) ]}, |
| 71 | 75 | exclude_ts_only = $xts, |
| 72 | 76 | delivery_kind = @{[ $q->($dkind) ]}, |
| 73 | 77 | delivery_url = @{[ $q->($durl) ]}, |
| 74 | 78 | delivery_email = @{[ $q->($dmail) ]}, |
| 75 | 79 | rate_limit_per_min = $rlim, |
| 76 | coalesce_window_s = $csec | |
| 80 | coalesce_window_s = $csec, | |
| 81 | frequency_threshold = $fthr, | |
| 82 | frequency_window_min = $fwin | |
| 77 | 83 | WHERE alert_rule_id = $rid |
| 78 | 84 | ~, __FILE__, __LINE__); |
| 79 | 85 | } |
| 80 | 86 | } |
| 81 | 87 | elsif ($op eq 'delete') { |
| 82 | 88 | my $rid = int($cgi->param('alert_rule_id') || 0); |
| 83 | 89 | $db->db_readwrite($dbh, |
| 84 | 90 | "UPDATE ALERT_RULES SET is_active = 0 WHERE alert_rule_id = $rid", |
| 85 | 91 | __FILE__, __LINE__); |
| 86 | 92 | } |
| 87 | 93 | elsif ($op eq 'toggle') { |
| 88 | 94 | my $rid = int($cgi->param('alert_rule_id') || 0); |
| 89 | 95 | $db->db_readwrite($dbh, |
| 90 | 96 | "UPDATE ALERT_RULES SET is_active = 1 - is_active WHERE alert_rule_id = $rid", |
| 91 | 97 | __FILE__, __LINE__); |
| 92 | 98 | } |
| 93 | 99 | |
| 94 | 100 | $db->db_disconnect($dbh); |
| 95 | 101 | print "Status: 302 Found\nLocation: /alerts.cgi?nc=" . int(rand() * 10_000_000) . "\n\n"; |
| 96 | 102 | exit; |
| 97 | 103 | } |
| 98 | 104 | |
| 99 | 105 | # ---- GET: list rules + delivery history ----------------------------- |
| 100 | 106 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 101 | 107 | |
| 102 | 108 | my @rules = $db->db_readwrite_multiple($dbh, q~ |
| 103 | 109 | SELECT alert_rule_id, rule_name, is_active, |
| 104 | 110 | match_kind, match_path_glob, match_status_list, exclude_ts_only, |
| 105 | 111 | delivery_kind, delivery_url, delivery_email, |
| 106 | 112 | rate_limit_per_min, coalesce_window_s, |
| 113 | frequency_threshold, frequency_window_min, | |
| 107 | 114 | fire_count, last_fired_at, |
| 108 | 115 | UNIX_TIMESTAMP(last_fired_at) AS last_fired_epoch, |
| 109 | 116 | UNIX_TIMESTAMP(created_at) AS created_epoch |
| 110 | 117 | FROM ALERT_RULES |
| 111 | 118 | ORDER BY is_active DESC, alert_rule_id ASC |
| 112 | 119 | ~, __FILE__, __LINE__); |
| 113 | 120 | |
| 114 | 121 | foreach my $r (@rules) { |
| 115 | 122 | $r->{status_pill} = $r->{is_active} ? 'pill-ok' : 'pill-mute'; |
| 116 | 123 | $r->{status_label} = $r->{is_active} ? 'active' : 'paused'; |
| 117 | 124 | $r->{kind_pill} = $r->{delivery_kind} eq 'slack' ? 'pill-info' |
| 118 | 125 | : $r->{delivery_kind} eq 'discord' ? 'pill-warn' |
| 119 | 126 | : 'pill-mute'; |
| 120 | 127 | $r->{match_glob_h} = $r->{match_path_glob} || '(all paths)'; |
| 121 | 128 | $r->{url_h} = _shorten($r->{delivery_url} || $r->{delivery_email} || '(no target)'); |
| 122 | 129 | $r->{last_fired_epoch} //= 0; |
| 123 | 130 | $r->{last_fired_h} = $r->{last_fired_at} || 'never'; |
| 124 | 131 | $r->{fire_count} //= 0; |
| 125 | 132 | $r->{edit_url} = "/alerts.cgi?edit=$r->{alert_rule_id}"; |
| 126 | 133 | } |
| 127 | 134 | |
| 128 | 135 | my @deliveries = $db->db_readwrite_multiple($dbh, q~ |
| 129 | 136 | SELECT d.delivery_id, d.alert_rule_id, r.rule_name, |
| 130 | 137 | d.source_kind, d.source_id, d.match_summary, |
| 131 | 138 | d.delivery_status, d.http_status, |
| 132 | 139 | UNIX_TIMESTAMP(d.fired_at) AS fired_epoch, |
| 133 | 140 | d.fired_at, |
| 134 | 141 | LEFT(d.error_message, 200) AS err_short |
| 135 | 142 | FROM ALERT_DELIVERIES d |
| 136 | 143 | LEFT JOIN ALERT_RULES r ON r.alert_rule_id = d.alert_rule_id |
| 137 | 144 | ORDER BY d.delivery_id DESC |
| 138 | 145 | LIMIT 30 |
| 139 | 146 | ~, __FILE__, __LINE__); |
| 140 | 147 | foreach my $d (@deliveries) { |
| 141 | 148 | $d->{delivery_pill} = $d->{delivery_status} eq 'sent' ? 'pill-ok' |
| 142 | 149 | : $d->{delivery_status} eq 'failed' ? 'pill-bad' |
| 143 | 150 | : $d->{delivery_status} eq 'skipped' ? 'pill-mute' |
| 144 | 151 | : 'pill-warn'; |
| 145 | 152 | $d->{source_url} = "/diff.cgi?kind=$d->{source_kind}&id=$d->{source_id}"; |
| 146 | 153 | $d->{summary_h} = _shorten($d->{match_summary} || '', 90); |
| 147 | 154 | $d->{err_h} = $d->{err_short} ? "err: $d->{err_short}" : ($d->{http_status} ? "HTTP $d->{http_status}" : '-'); |
| 148 | 155 | $d->{fired_epoch} //= 0; |
| 149 | 156 | } |
| 150 | 157 | |
| 151 | 158 | # ---- Edit mode: prefill form --------------------------------------- |
| 152 | 159 | my $edit_id = int($cgi->param('edit') || 0); |
| 153 | 160 | my $edit = {}; |
| 154 | 161 | if ($edit_id) { |
| 155 | 162 | ($edit) = grep { $_->{alert_rule_id} == $edit_id } @rules; |
| 156 | 163 | $edit ||= {}; |
| 157 | 164 | } |
| 158 | 165 | my $is_edit = $edit_id && $edit && $edit->{alert_rule_id} ? 1 : 0; |
| 159 | 166 | |
| 160 | 167 | # defaults for a fresh form |
| 161 | 168 | my %form = ( |
| 162 | 169 | rule_name => $edit->{rule_name} // '', |
| 163 | 170 | is_active_checked => (!$is_edit || $edit->{is_active}) ? 'checked' : '', |
| 164 | 171 | match_kind => $edit->{match_kind} // 'either', |
| 165 | 172 | match_path_glob => $edit->{match_path_glob} // '', |
| 166 | 173 | match_status_list => $edit->{match_status_list} // '', |
| 167 | 174 | exclude_ts_only_checked => (!$is_edit || $edit->{exclude_ts_only}) ? 'checked' : '', |
| 168 | 175 | delivery_kind => $edit->{delivery_kind} // 'generic_webhook', |
| 169 | 176 | delivery_url => $edit->{delivery_url} // '', |
| 170 | 177 | delivery_email => $edit->{delivery_email} // '', |
| 171 | 178 | rate_limit_per_min => $edit->{rate_limit_per_min} // 0, |
| 172 | 179 | coalesce_window_s => $edit->{coalesce_window_s} // 0, |
| 180 | frequency_threshold => $edit->{frequency_threshold} // 0, | |
| 181 | frequency_window_min => $edit->{frequency_window_min} // 60, | |
| 173 | 182 | ); |
| 174 | 183 | # select-option "selected" markers |
| 175 | 184 | foreach my $k (qw(either file schema)) { |
| 176 | 185 | $form{"mk_${k}_sel"} = $form{match_kind} eq $k ? 'selected' : ''; |
| 177 | 186 | } |
| 178 | 187 | foreach my $k (qw(generic_webhook slack discord email)) { |
| 179 | 188 | $form{"dk_${k}_sel"} = $form{delivery_kind} eq $k ? 'selected' : ''; |
| 180 | 189 | } |
| 181 | 190 | |
| 182 | 191 | $db->db_disconnect($dbh); |
| 183 | 192 | |
| 184 | 193 | # ---- Render body ---------------------------------------------------- |
| 185 | 194 | my $tvars = { |
| 186 | 195 | rules => \@rules, |
| 187 | 196 | has_rules => scalar(@rules) ? 1 : 0, |
| 188 | 197 | deliveries => \@deliveries, |
| 189 | 198 | has_deliveries => scalar(@deliveries) ? 1 : 0, |
| 190 | 199 | is_edit => $is_edit, |
| 191 | 200 | edit_id => $is_edit ? $edit->{alert_rule_id} : 0, |
| 192 | 201 | form_op => $is_edit ? 'update' : 'create', |
| 193 | 202 | %form, |
| 194 | 203 | }; |
| 195 | 204 | |
| 196 | 205 | my @body = $tpl->template('alerts.html', $tvars); |
| 197 | 206 | MODS::PageWrapper->new->wrapper( |
| 198 | 207 | page_title => 'Alerts', page_key => 'alerts', |
| 199 | 208 | body_html => join('', @body), |
| 200 | 209 | userinfo => { display_name => 'Operator' }, |
| 201 | 210 | ); |
| 202 | 211 | |
| 203 | 212 | #--------------------------------------------------------------------- |
| 204 | 213 | sub _clean { |
| 205 | 214 | my ($s, $max) = @_; |
| 206 | 215 | $s //= ''; |
| 207 | 216 | $s =~ s/[\r\n]/ /g; |
| 208 | 217 | $s =~ s/^\s+|\s+$//g; |
| 209 | 218 | $s = substr($s, 0, $max) if defined $max && length($s) > $max; |
| 210 | 219 | return $s; |
| 211 | 220 | } |
| 212 | 221 | sub _one_of { |
| 213 | 222 | my ($v, $default, @allowed) = @_; |
| 214 | 223 | $v //= ''; |
| 215 | 224 | return (grep { $_ eq $v } @allowed) ? $v : $default; |
| 216 | 225 | } |
| 217 | 226 | sub _shorten { |
| 218 | 227 | my ($s, $max) = @_; |
| 219 | 228 | $max //= 60; |
| 220 | 229 | $s //= ''; |
| 221 | 230 | return length($s) > $max ? substr($s, 0, $max - 3) . '...' : $s; |
| 222 | 231 | } |