Daemon.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 266 CC 19 SCAR 33.3
CC
19
Cov
0%
checklog
110.0
gd_postconfig
30.0
gd_preconfig
12.0
CRAP 28.0
1 # Copyright (C) 2006, David Muir Sharnoff <muir@idiom.com>
2  
3 package SyslogScan::Daemon;
4  
5 use strict;
6 use warnings;
7 use Daemon::Generic::Event;
8 use Plugins::Style1;
9 use Plugins::SimpleConfig;
10 use Plugins::API;
11 use FileHandle;
12 use Carp;
13 require Exporter;
14 our @ISA = qw(Daemon::Generic::Event);
15 our @EXPORT = qw(newdaemon);
16  
17 our $VERSION = 0.41;
18  
19 our $sighup = 0;
20 our $sigint = 0;
21  
22 my %loghandles;
23 our $sleep_time = 1;
24 our $debug = 0;
25  
26 my %config_items = (
27 logpriority => '',
28 sleeptime => \$sleep_time,
29 debug => \$debug,
30 );
31  
32 sub config_prefix { return '' };
33  
34 sub parse_config_line { simple_config_line(\%config_items, @_); }
35  
36 sub new { simple_new(\%config_items, @_); }
37  
38 sub gd_preconfig
39 {
40 my $self = shift;
41 print "START ssd preconfig\n" if $debug;
42  
43 $self->{api} = new Plugins::API { autoregister => $self },
44 log_rolled => { optional => 1 },
45 ;
46  
47 delete $self->{plugins};
48 $self->{plugins} = Plugins::Style1->new(api => $self->{api});
49 $self->{plugins}->readconfig($self->{configfile}, self => $self);
50 $self->{plugins}->initialize();
51 $self->{plugins}->invoke('preconfig', $self->{configfile});
52  
53 print "END ssd preconfig\n" if $debug;
54 return ();
55 }
56  
57 my %logs;
58 my %merged;
59 sub gd_postconfig
60 {
61 my ($self) = @_;
62 print "START ssd postconfig\n" if $debug;
63 closelogfiles();
64 %logs = ();
65 $self->{plugins}->invoke('postconfig');
66  
67 for my $plugin ($self->{plugins}->plugins) {
68 my (@newlogs) = $plugin->invoke('get_logs');
69 while (@newlogs) {
70 my ($log, $rxlist) = splice(@newlogs, 0, 2);
71 for my $rx (@$rxlist) {
72 print "Search for $rx in $log for $plugin\n" if $debug;
73 push(@{$logs{$log}{$rx}}, $plugin);
74 }
75 }
76 }
77 for my $log (keys %logs) {
78 my @rx = keys %{$logs{$log}};
79 $merged{$log} = shift(@rx);
80 for my $rx (@rx) {
81 $merged{$log} = qr/(?:$merged{$log}|$rx)/;
82 }
83 }
84 openlogfiles();
85 print "END ssd postconfig\n" if $debug;
86 if ($self->{logpriority}) {
87 $self->{gd_logpriority} = $self->{logpriority};
88 $self->gd_redirect_output;
89 }
90 }
91  
92 sub gd_setup_signals
93 {
94 my $self = shift;
95 $SIG{USR2} = sub {
96 $debug++;
97 print STDERR "Debugging level: $debug\n";
98 };
99 $SIG{USR1} = sub {
100 print STDERR "Debugging off\n";
101 $debug = 0;
102 };
103 $self->SUPER::gd_setup_signals();
104 }
105  
106 sub gd_run_body
107 {
108 my $self = shift;
109 for my $file (keys %logs) {
110 $self->checklog($file);
111 }
112 $self->{plugins}->invoke('periodic');
113 print "sleeping $sleep_time...\n"
114 if $debug >= 3;
115 }
116  
117 sub gd_interval
118 {
119 my $self = shift;
120 return $sleep_time;
121 }
122  
123 sub checklog
124 {
125 my ($self, $file) = @_;
126 print "checking for new stuff in $file\n"
127 if $debug;
128 my $fh = $loghandles{$file};
129 while (<$fh>) {
130 unless (/$merged{$file}/) {
131 print STDERR "not found: $_" if $debug >= 5;
132 }
133 print STDERR "gross found: $_" if $debug >= 4;
134 for my $rx (keys %{$logs{$file}}) {
135 print STDERR "trying $rx...\n" if $debug >= 3;
136 next unless /$rx/;
137 print STDERR "FOUND $rx\n" if $debug;
138 for my $plugin (@{$logs{$file}{$rx}}) {
139 $plugin->invoke('matched_line', $file, $rx);
140 }
141 }
142 }
143 if ((stat($fh))[1] != (stat($file))[1]) {
144 print "log file $file must have rolled, re-opening!\n" if $debug;
145 $loghandles{$file} = new FileHandle "<$file" || confess "cannot open $file: $!";
146 $self->{api}->log_rolled($file);
147 $self->checklog($file);
148 }
149 }
150  
151 sub openlogfiles
152 {
153 for my $file (keys %logs) {
154 my $fh;
155 $loghandles{$file} = $fh = new FileHandle "<$file"
156 || confess "cannot open $file: $!";
157 open($fh, "<$file")
158 || confess "cannot open $file: $!";
159 seek($fh, 0, 2)
160 || confess "cannot seek end of $file: $!";
161 }
162 }
163  
164 sub closelogfiles
165 {
166 for my $file (keys %loghandles) {
167 my $fh = $loghandles{$file};
168 $fh->close();
169 delete $loghandles{$file};
170 }
171 }
172  
173 package SyslogScan::Daemon::Plugin;
174  
175 use Plugins::Style1::Plugin;
176 use strict;
177 use warnings;
178 use Plugins::API;
179  
180 our @ISA = qw(Plugins::Style1::Plugin);
181  
182 sub set_api
183 {
184 my ($self, $ssd_configfile, @api) = @_;
185  
186 my $config = $self->{configfile} || $ssd_configfile;
187  
188 $self->{myapi} = Plugins::API->new;
189 $self->{myapi}->api(@api);
190 $self->{myapi}->autoregister($self);
191 $self->{myapi}->register(undef, parentapi => sub { return $self->{api} });
192  
193 $self->{plugins} = new Plugins context => $self->{context};
194 $self->{plugins}->readconfig($config, self => $self);
195  
196 $self->{plugins}->api($self->{myapi});
197 $self->{myapi}->plugins($self->{plugins});
198  
199 $self->{plugins}->initialize();
200 $self->{plugins}->invoke('preconfig', $config);
201 }
202  
203 sub postconfig {}
204 sub matched_line {}
205 sub preconfig {}
206 sub log_rolled {}
207  
208 sub get_logs
209 {
210 my ($self) = @_;
211 return () unless $self->{plugins};
212 my %logs;
213 my @r;
214 for my $plugin ($self->{plugins}->plugins) {
215 my (@newlogs) = $plugin->invoke('get_logs');
216 push(@r, @newlogs);
217 while (@newlogs) {
218 my ($log, $rxlist) = splice(@newlogs, 0, 2);
219 for my $rx (@$rxlist) {
220 push(@{$logs{$log}{$rx}}, $plugin);
221 }
222 }
223 }
224 $self->{logs} = \%logs;
225 return @r;
226 }
227  
228 sub periodic
229 {
230 my ($self) = @_;
231 return () unless $self->{plugins};
232 $self->{plugins}->invoke('periodic');
233 }
234  
235 1;
236