diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-02-10 11:38:21 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-02-10 11:38:21 +0200 |
commit | 304127cdec228e72ea3b12d3ee1bcd3fc5f0e893 (patch) | |
tree | 0f750373d698f73c314580d7b7b0ed0781f6f29f | |
download | syslogck-304127cdec228e72ea3b12d3ee1bcd3fc5f0e893.tar.gz syslogck-304127cdec228e72ea3b12d3ee1bcd3fc5f0e893.tar.bz2 |
Initial commit
-rw-r--r-- | syslogck | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/syslogck b/syslogck new file mode 100644 index 0000000..c59cb6d --- /dev/null +++ b/syslogck | |||
@@ -0,0 +1,109 @@ | |||
1 | #!/usr/bin/perl | ||
2 | |||
3 | use strict; | ||
4 | use Sys::Syslog qw(:standard :macros); | ||
5 | use Data::Dumper; | ||
6 | |||
7 | my $facility = 'user'; | ||
8 | |||
9 | my %prio_order = ('debug' => 0, | ||
10 | 'info' => 1, | ||
11 | 'notice' => 2, | ||
12 | 'warn' => 3, | ||
13 | 'warning' => 3, | ||
14 | 'err' => 4, | ||
15 | 'error' => 4, | ||
16 | 'crit' => 5, | ||
17 | 'alert' => 6, | ||
18 | 'emerg' => 7, | ||
19 | 'panic' => 7); | ||
20 | |||
21 | my $priority = $prio_order{info}; | ||
22 | |||
23 | sub match_selector { | ||
24 | my ($sel) = @_; | ||
25 | my $match; | ||
26 | $sel =~ s/\s+//g; | ||
27 | print "matching $sel\n"; | ||
28 | foreach my $ent (split /;/, $sel) { | ||
29 | print " ent=$ent\n"; | ||
30 | if ($ent =~ /^(?<fac>.+)\.(?<pri>.*)$/) { | ||
31 | print " f=$+{fac},p=$+{pri}\n"; | ||
32 | if (match_facility($+{fac})) { | ||
33 | if ($+{pri} eq 'none') { | ||
34 | $match = 0; | ||
35 | } elsif (match_priority($+{pri})) { | ||
36 | $match = 1; | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | print "M $match\n" | ||
41 | } | ||
42 | print ($match ? "+MATCH\n" : "-NOPE\n"); | ||
43 | return $match; | ||
44 | } | ||
45 | |||
46 | sub match_facility { | ||
47 | my ($arg) = @_; | ||
48 | foreach my $f (split /,/, $arg) { | ||
49 | $f =~ s/\..*//; | ||
50 | print " f=$f\n"; | ||
51 | return 1 if $f eq '*' || $f eq $facility; | ||
52 | } | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | sub match_priority { | ||
57 | my ($pri) = @_; | ||
58 | my $match = 0; | ||
59 | |||
60 | print " p=$pri :: "; | ||
61 | my $neg = $pri =~ s/^!(.+)/$1/; | ||
62 | print " not " if ($neg); | ||
63 | my $eq = $pri =~ s/^=(.+)/$1/; | ||
64 | if ($pri eq '*') { | ||
65 | print "*"; | ||
66 | $match = 1; | ||
67 | } else { | ||
68 | next unless exists($prio_order{$pri}); | ||
69 | if ($eq) { | ||
70 | print $prio_order{$pri}." == $priority"; | ||
71 | $match = $prio_order{$pri} == $priority; | ||
72 | } else { | ||
73 | print $prio_order{$pri}." <= $priority"; | ||
74 | $match = $prio_order{$pri} <= $priority; | ||
75 | } | ||
76 | } | ||
77 | $match = !$match if $neg; | ||
78 | print ":: $match\n"; | ||
79 | return $match; | ||
80 | } | ||
81 | |||
82 | sub find_actions { | ||
83 | my $file = shift; | ||
84 | my @actions; | ||
85 | if (open(my $fd, '<', $file)) { | ||
86 | while (<$fd>) { | ||
87 | chomp; | ||
88 | s/^\s+//; | ||
89 | next if /^#/; | ||
90 | if (/\\$/) { | ||
91 | chop; | ||
92 | $_ .= <$fd>; | ||
93 | redo; | ||
94 | } | ||
95 | if (/^(?<sel>.+?)\s+(?<buf>-?)(?<stream>[^\s]+)$/) { | ||
96 | push @actions, $+{stream} if match_selector($+{sel}); | ||
97 | } | ||
98 | } | ||
99 | } else { | ||
100 | warn "can't open $file: $!"; | ||
101 | return undef; | ||
102 | } | ||
103 | return @actions; | ||
104 | } | ||
105 | |||
106 | my @act = find_actions("/etc/syslog.conf"); | ||
107 | print Dumper([ @act ]); | ||
108 | |||
109 | |||