-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyslog-alert.tcl
More file actions
executable file
·673 lines (564 loc) · 16.8 KB
/
Copy pathsyslog-alert.tcl
File metadata and controls
executable file
·673 lines (564 loc) · 16.8 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/bin/sh
# the next line restarts using tclsh \
exec /usr/bin/env tclsh "$0" "$@"
## Copyright (C) 2020 nic@boet.cc
# https://github.com/nabbi/syslog-alert
# Harden PATH to trusted directories only
set ::env(PATH) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Maximum allowed length for a single syslog input line (bytes)
set ::MAX_LINE_LENGTH 8192
oo::class create SQLite {
constructor {args} {
package require sqlite3
sqlite3 Db :memory:
next {*}$args
}
destructor {
Db close
}
}
oo::class create Contacts {
constructor {args} {
variable debug
variable trace
set debug 0
set trace 0
Db eval {CREATE TABLE contacts(name text, "group" text, email text, page text)}
next {*}$args
}
method PopulateContacts {contactsDict} {
# Insert contact rows into SQLite from a normalized contacts dict
# Each contact with multiple groups gets one row per group
dict for {name info} $contactsDict {
set groups [dict get $info groups]
set email ""
set page ""
if {[dict exists $info email]} { set email [dict get $info email] }
if {[dict exists $info page]} { set page [dict get $info page] }
foreach g $groups {
Db eval {INSERT INTO contacts VALUES(:name,:g,:email,:page)}
}
}
}
method ImportContacts {} {
# Legacy
set path "/etc/syslog-ng/alert-contacts.conf"
set conf [open $path {r}]
set lines [split [read $conf] "\n"]
close $conf
foreach l $lines {
if { [string index $l 0] == "#" || [string length $l] == 0 } {
continue
}
lassign $l name group email page
Db eval {INSERT INTO contacts VALUES(:name,:group,:email,:page)}
}
}
method Group {groups a} {
set results [list]
foreach g $groups {
switch -glob -- $a {
"email" { lappend results {*}[Db eval {SELECT "email" FROM contacts WHERE "group"=:g}] }
"page" { lappend results {*}[Db eval {SELECT "page" FROM contacts WHERE "group"=:g}] }
default { return }
}
}
return [join [lsearch -all -inline -not -exact $results {}] ", "]
}
method page {g s b} {
set to [my Group $g "page"]
if { [string length $to] > 0 } {
my Sendmail "$to" $s $b
}
}
method email {g s b} {
set to [my Group $g "email"]
if { [string length $to] > 0 } {
my Sendmail $to "Subject: $s" $b
}
}
method Sendmail {to subject body} {
variable debug
variable fromAddr
set to [string map {\n {} \r {}} $to]
set subject [string map {\n {} \r {}} $subject]
if {[info exists fromAddr] && $fromAddr ne ""} {
set msg "From: $fromAddr"
} else {
set msg "From: syslog@[info hostname]"
}
append msg \n "To: $to" \n
append msg $subject \n\n
append msg $body \n
if {$debug} { puts "## msg: $msg" }
if {[info exists ::env(SYSLOG_ALERT_TESTMODE)] && $::env(SYSLOG_ALERT_TESTMODE)} {
# Test mode: append to capture file instead of sending email
set actionsFile [expr {[info exists ::env(SYSLOG_ALERT_ACTIONS_FILE)]
? $::env(SYSLOG_ALERT_ACTIONS_FILE) : "/dev/stderr"}]
set fd [open $actionsFile a]
puts $fd "ACTION: to=<$to> subject=<$subject>"
puts $fd "BODY: $body"
puts $fd "---"
close $fd
return
}
exec -- sendmail -oi -t << $msg &
}
}
oo::class create Alert {
mixin SQLite Contacts
constructor {args} {
variable debug
variable trace
variable fromAddr
variable configFile
variable normalizedRules
# Parse constructor args (passed from CLI as key-value pairs)
set configFile ""
set fromAddr ""
if {[dict exists $args configFile]} {
set configFile [dict get $args configFile]
}
# create table for tracking which alerts
Db eval {CREATE TABLE alert(time int, hash text primary key)}
# Load config: try new format first, then legacy
my LoadAndApplyConfig
if {$debug} { puts "## Config imported" }
}
method LoadAndApplyConfig {} {
variable configFile
variable debug
variable trace
variable fromAddr
variable normalizedRules
if {$configFile ne ""} {
# Explicit --config path: must be new format
set normalizedRules [my LoadConfig $configFile]
} elseif {[file exists "/etc/syslog-ng/syslog-alert.conf"]} {
set normalizedRules [my LoadConfig "/etc/syslog-ng/syslog-alert.conf"]
} else {
# Legacy fallback
puts "notice: using legacy config files (alert.conf + alert-contacts.conf). Consider migrating to syslog-alert.conf"
set normalizedRules [my LoadLegacyConfig]
}
my CreatePatterns $normalizedRules
}
method LoadConfig {path} {
# Load new unified syslog-alert.conf format
variable debug
variable trace
variable fromAddr
set fd [open $path r]
set raw [read $fd]
close $fd
# Strip comment lines (# at start of line) before parsing as dict
set cleaned ""
foreach line [split $raw "\n"] {
set trimmed [string trimleft $line]
if {[string index $trimmed 0] eq "#"} { continue }
append cleaned $line \n
}
set config $cleaned
if {[catch {dict size $config} err]} {
puts "fatal: failed to parse config file $path: $err"
exit 1
}
# Validate top-level sections
if {![dict exists $config contacts]} {
puts "fatal: config missing 'contacts' section"
exit 1
}
if {![dict exists $config rules]} {
puts "fatal: config missing 'rules' section"
exit 1
}
# Process global section
if {[dict exists $config global]} {
set g [dict get $config global]
if {[dict exists $g debug]} { set debug [dict get $g debug] }
if {[dict exists $g trace]} { set trace [dict get $g trace] }
if {[dict exists $g from]} { set fromAddr [dict get $g from] }
}
# Process contacts
set contactsDict [dict get $config contacts]
dict for {name info} $contactsDict {
if {![dict exists $info groups]} {
puts "fatal: contact '$name' missing 'groups'"
exit 1
}
if {![dict exists $info email] && ![dict exists $info page]} {
puts "fatal: contact '$name' must have at least one of 'email' or 'page'"
exit 1
}
}
my PopulateContacts $contactsDict
if {$debug} { puts "## Database ## contacts imported" }
# Process rules into normalized list
set rulesDict [dict get $config rules]
set normalized [list]
dict for {ruleName ruleBody} $rulesDict {
set rule [dict create name $ruleName]
if {![dict exists $ruleBody pattern]} {
puts "fatal: rule '$ruleName' missing 'pattern'"
exit 1
}
dict set rule pattern [dict get $ruleBody pattern]
# Optional fields with defaults
dict set rule exclude [expr {[dict exists $ruleBody exclude] ? [dict get $ruleBody exclude] : ""}]
dict set rule subject [expr {[dict exists $ruleBody subject] ? [dict get $ruleBody subject] : ""}]
set delay [expr {[dict exists $ruleBody delay] ? [dict get $ruleBody delay] : 0}]
if {![string is integer -strict $delay] || $delay < 0} {
puts "fatal: rule '$ruleName' has invalid delay '$delay'"
exit 1
}
dict set rule delay $delay
dict set rule email [expr {[dict exists $ruleBody email] ? [dict get $ruleBody email] : ""}]
dict set rule page [expr {[dict exists $ruleBody page] ? [dict get $ruleBody page] : ""}]
set ignore [expr {[dict exists $ruleBody ignore] ? [dict get $ruleBody ignore] : 0}]
if {$ignore ne "" && $ignore != 0 && $ignore != 1} {
puts "fatal: rule '$ruleName' has invalid ignore flag '$ignore'"
exit 1
}
dict set rule ignore $ignore
dict set rule custom [expr {[dict exists $ruleBody custom] ? [dict get $ruleBody custom] : ""}]
lappend normalized $rule
}
return $normalized
}
method LoadLegacyConfig {} {
# Load legacy two-file format, return normalized rules list
variable debug
variable trace
# Load contacts via legacy method
my ImportContacts
if {$debug} { puts "## Database ## contacts imported" }
# Load alert rules
set conf [open /etc/syslog-ng/alert.conf {r}]
set lines [split [read $conf] "\n"]
close $conf
set normalized [list]
foreach l $lines {
if { [string index $l 0] == "#" || [string length $l] == 0 } {
continue
}
if { [llength $l] != 8 } {
puts "#ignore bad config line: $l"
continue
}
lassign $l pattern exclude hash delay email page ignore custom
if { ![string is integer -strict $delay] || $delay < 0 } {
puts "#ignore bad delay value in config line: $l"
continue
}
if { $ignore ne {} && $ignore != 0 && $ignore != 1 } {
puts "#ignore bad ignore flag in config line: $l"
continue
}
# Legacy hash/subject has embedded quotes, strip them
set hash [string trim $hash "\""]
set rule [dict create \
name "" \
pattern $pattern \
exclude $exclude \
subject $hash \
delay $delay \
email $email \
page $page \
ignore $ignore \
custom $custom \
]
lappend normalized $rule
}
if {[llength $normalized] == 0} {
puts "fatal: no rules loaded from legacy config."
exit 1
}
return $normalized
}
method Recent {delta hash} {
set now [expr {[info exists ::env(SYSLOG_ALERT_CLOCK)] ? $::env(SYSLOG_ALERT_CLOCK) : [clock seconds]}]
set time [Db eval {SELECT time FROM alert WHERE hash=:hash}]
if { [string length $time] <= 0} {
Db eval {INSERT INTO alert VALUES(:now,:hash)}
return 100
} elseif { [expr {$now-$time}] > $delta } {
Db eval {UPDATE alert SET time=:now WHERE hash=:hash}
return 200
} else {
return 0
}
}
method purge {} {
set now [expr {[info exists ::env(SYSLOG_ALERT_CLOCK)] ? $::env(SYSLOG_ALERT_CLOCK) : [clock seconds]}]
set historic [expr {$now - 259200}]
Db eval {DELETE FROM alert WHERE time<=:historic}
}
method CreatePatterns {rules} {
# Generate the patterns method from normalized rules list
variable trace
append method "oo::define Alert method patterns \{line\} \{\n\n"
append method "lassign \$line log(isodate) log(host) log(facility) log(level) log(msghdr) log(msg)\n"
append method "set log(all) \"\$log(isodate) \$log(host) \$log(facility).\$log(level) \$log(msghdr)\$log(msg)\"\n"
set split "\\\["
append method "set log(program) \[lindex \[split \$log(msghdr) \"$split\"\] 0\]\n"
append method "\nswitch -glob -nocase -- \$log(all) \{\n"
foreach rule $rules {
set pattern [dict get $rule pattern]
set exclude [dict get $rule exclude]
set subject [dict get $rule subject]
set delay [dict get $rule delay]
set email [dict get $rule email]
set page [dict get $rule page]
set ignore [dict get $rule ignore]
set custom [dict get $rule custom]
# Use subject as hash for throttling
set hash $subject
set i 1
foreach p $pattern {
# Quote pattern for switch body (handles empty strings)
if {$p eq "default"} {
set qp "default"
} else {
set qp "\"$p\""
}
if { [llength $pattern] > $i } {
incr i
append sw "$qp -\n"
continue
}
if { $ignore == 1 } {
append sw "$qp \{ return \}\n"
continue
} else {
append sw "$qp \{ \n"
foreach e $exclude {
set eqIdx [string first "=" $e]
set ek [string range $e 0 $eqIdx-1]
set ev [string range $e $eqIdx+1 end]
set ev [string trim $ev]
if {[string length $ev] >= 2 &&
[string index $ev 0] eq "\"" &&
[string index $ev end] eq "\""} {
set ev [string range $ev 1 end-1]
}
append sw "\tif \{ \[string match -nocase $ev \$log($ek)\] \} \{ return \}\n"
}
append sw "\tif \{ \[my Recent $delay \"$hash\"\] \} \{\n"
append sw "\t\tset subject \"$hash\"\n"
if { [string length $custom] > 0 } {
append sw "\t\t$custom\n"
}
if { [string length $email] > 0 } {
append sw "\t\tmy email \"$email\" \"\$subject\" \$log(all)\n"
}
if { [string length $page] > 0 } {
append sw "\t\tmy page \"$page\" \"\$subject\" \$log(msg)\n"
}
append sw "\t\}\n\}\n"
continue
}
}
}
if { ! [info exists sw] } {
puts "fatal: no switch conditions compiled."
exit 1
}
append method "$sw\}\n"
append method "\}\n"
eval $method
if {$trace} { puts "## method patterns\n[info class definition Alert patterns]" }
}
method dumpconfig {} {
variable normalizedRules
puts "# Normalized configuration"
puts ""
set i 0
foreach rule $normalizedRules {
incr i
set name [dict get $rule name]
if {$name eq ""} { set name "rule-$i" }
puts "rule: $name"
puts " pattern: [dict get $rule pattern]"
set exclude [dict get $rule exclude]
if {$exclude ne ""} { puts " exclude: $exclude" }
set subject [dict get $rule subject]
if {$subject ne ""} { puts " subject: $subject" }
puts " delay: [dict get $rule delay]"
set email [dict get $rule email]
if {$email ne ""} { puts " email: $email" }
set page [dict get $rule page]
if {$page ne ""} { puts " page: $page" }
set ignore [dict get $rule ignore]
if {$ignore == 1} { puts " ignore: 1" }
set custom [dict get $rule custom]
if {$custom ne ""} { puts " custom: $custom" }
puts ""
}
}
method explain {inputLine} {
# Dry-run a syslog line through patterns, showing what would match
variable normalizedRules
if {[llength $inputLine] != 6} {
puts "error: input must be a 6-element Tcl list"
puts "format: {ISODATE} {HOST} {FACILITY} {LEVEL} {MSGHDR} {MSG}"
return
}
lassign $inputLine isodate host facility level msghdr msg
set all "$isodate $host $facility.$level $msghdr$msg"
set split "\["
set program [lindex [split $msghdr $split] 0]
puts "Input:"
puts " isodate: $isodate"
puts " host: $host"
puts " facility: $facility"
puts " level: $level"
puts " msghdr: $msghdr"
puts " msg: $msg"
puts " all: $all"
puts " program: $program"
puts ""
set matched 0
set ruleNum 0
foreach rule $normalizedRules {
incr ruleNum
set name [dict get $rule name]
if {$name eq ""} { set name "rule-$ruleNum" }
set pattern [dict get $rule pattern]
set exclude [dict get $rule exclude]
set ignore [dict get $rule ignore]
foreach p $pattern {
if {$p eq "default" || [string match -nocase $p $all]} {
puts "MATCHED rule: $name"
puts " pattern: $p"
# Check excludes
set excluded 0
foreach e $exclude {
set eqIdx [string first "=" $e]
set ek [string range $e 0 $eqIdx-1]
set ev [string range $e $eqIdx+1 end]
set ev [string trim $ev]
if {[string length $ev] >= 2 &&
[string index $ev 0] eq "\"" &&
[string index $ev end] eq "\""} {
set ev [string range $ev 1 end-1]
}
set testVal ""
switch -- $ek {
all { set testVal $all }
host { set testVal $host }
facility { set testVal $facility }
level { set testVal $level }
msghdr { set testVal $msghdr }
msg { set testVal $msg }
program { set testVal $program }
isodate { set testVal $isodate }
}
if {[string match -nocase $ev $testVal]} {
puts " EXCLUDED by: $e"
set excluded 1
}
}
if {!$excluded} {
if {$ignore == 1} {
puts " action: IGNORE (no alert)"
} else {
puts " subject: [dict get $rule subject]"
puts " delay: [dict get $rule delay]s"
set email [dict get $rule email]
if {$email ne ""} { puts " email: $email" }
set page [dict get $rule page]
if {$page ne ""} { puts " page: $page" }
set custom [dict get $rule custom]
if {$custom ne ""} { puts " custom: $custom" }
}
}
set matched 1
break
}
}
if {$matched} { break }
}
if {!$matched} {
puts "NO MATCH: no rule matched this input"
}
}
}
# --- CLI argument parsing ---
set configFile ""
set checkConfig 0
set dumpConfig 0
set explainLine ""
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
switch -- $arg {
"--config" {
incr i
set configFile [lindex $argv $i]
}
"--check-config" {
set checkConfig 1
}
"--dump-config" {
set dumpConfig 1
}
"--explain" {
incr i
set explainLine [lindex $argv $i]
}
}
}
# Helper to create Alert with config args
proc CreateAlert {configFile} {
if {$configFile ne ""} {
return [Alert new configFile $configFile]
} else {
return [Alert new]
}
}
# --check-config mode
if {$checkConfig} {
if {[catch {set syslog [CreateAlert $configFile]} err]} {
puts "CONFIG ERROR: $err"
exit 1
}
puts "Configuration OK"
$syslog destroy
exit 0
}
# --dump-config mode
if {$dumpConfig} {
if {[catch {set syslog [CreateAlert $configFile]} err]} {
puts "CONFIG ERROR: $err"
exit 1
}
$syslog dumpconfig
$syslog destroy
exit 0
}
# --explain mode
if {$explainLine ne ""} {
if {[catch {set syslog [CreateAlert $configFile]} err]} {
puts "CONFIG ERROR: $err"
exit 1
}
$syslog explain $explainLine
$syslog destroy
exit 0
}
# Normal operation: read from stdin
set syslog [CreateAlert $configFile]
set counter 0
while { [gets stdin line] >= 0 } {
if { [string length $line] > $::MAX_LINE_LENGTH } { continue }
if { [llength $line] != 6 } { continue }
if { [catch {$syslog patterns $line} err] } {
puts "#error processing line: $err"
}
incr counter
if {$counter > 100} {
set counter 0
$syslog purge
}
}
$syslog destroy
exit 0