I'm on FreeBSD 6.3, installed grok FreeBSD port with thanks.
First of all many thanks for a great script, have just spent the last
few days working on getting grok to block comment trackback spammers,
ftp/ssh auth brute attacks, smtp auth attacks and so.
All works very well but have got stuck on log rotation...
I have grok watching a number of logfiles - the following entry in
grok.conf is typical:
-snip-
file "/path/to/spamblock.log" {
type "spamblock rejects" {
match = "REJECTED: [No API-created comments|Trackback URL
invalid|Filtered by Akismet\.com].*, IP %IP%.*";
reaction = {
my $type = "spamblocklog";
my $ip = meta2string("%IP%", $v);
my $cmd = "pfctl -t $type -T add $ip";
my $outfile = "/etc/pf/tables/$type.pf";
`$cmd && echo "$ip" >> $outfile`;
};
};
};
-snip-
So here this works well - grok watches for any matching patterns and
if it finds any it will first act to block the IP at the firewall
using pfctl and then go on to add the IP to a pf table file in /etc/pf/
tables.
However the problem starts when the /path/to/spamblock.log file is
rotated (which is daily) - grok continues to watch the old logfile
instead of the new logfile.
To get around this I hacked together a simple script to restart grok
just after midnight every day. In doing so though I found that - as
mentioned in the last post to this list - when grok is terminated
externally (for example using something like 'kill <pid>' where <pid>
is the pid of grok) the child processes that grok has spawned do not
die and continue to run. These processes are those 'tail -f0' and
'sh' starter scripts.
This problem is the same with the FreeBSD rc script installed into /
usr/local/etc/rc.d. Executing:
/usr/local/etc/rc.d/grok stop
will kill the main grok process, but fails to kill off any child
processes that grok has spawned (to be fair though it shouldn't really
be the job of the rc script to kill off any of grok's children).
So my feature request would be to have grok trap and handle external
signal requests. For example, when a kill signal is sent on the
commandline like:
kill 1234
(where 1234 is the pid of grok)
would it please be possible to have grok trap that signal and as part
of it's shutdown process go on to in turn kill any child processes
that it's spawned.
I tried doing this myself in the grok script using something like:
$SIG{KILL} = \®exhack::hackteardown();
but this failed woefully - my perl is just bearly bordering on
intermediate so ... :(
My hackaround at present is a perl script I run daily at 1201 which
grabs each pid with '/usr/local/bin/grok' in the commandline. It then
checks to see if each pid is a session leader or not and if it is, it
kills any child procs using:
/bin/kill -- -<pid>
(basically kills any process with the PGID of <pid>).
It also kills any procs that aren't session leaders (this is either
the process that forks or that is forked, didn't work out which!).
All of this is very hacked up and prone to dodginess! Have only just
done this tonight and it appears to work, but would be a lot easier to
just have grok trap the kill signal it receives and then act on it
internally to kill off any child procs.
Hope this is clear.
Here's the perl sub from my rotate script, should give the gist of
what it does:
-snip-
sub grok_stop {
# Stop grok:
$pids=`$pgrep -f "[p]erl.*$grok"`;
@pids=split("\n", $pids);
if(@pids == 0){
die("No running procs found, exiting\n");
}
foreach(@pids){
# find out if this is a session leader
$ps=`ps -o "state=" $_`;
if($ps=~/s/){
# this is a sess leader, kill all proces with
this pgid:
$cmd="$kill -- -$_";
`$cmd`;
} else {
$cmd="$kill $_";
`$cmd`;
}
}
}
-snip-
An even better feature for grok to have included would be one which
handles log rotation without the need to have grok restart at all -
I'm no IPC guru but would this be possible for example by trapping INT
interrupt signals (which newsyslog can send out when it rotates
logfiles) and then rereading the grok config file to start watching
the new logfile?
Once again though, many thanks for a great script!
Regards.
--
Jez Hancock
- System Administrator / PHP Developer
http://munk.me.uk/
http://freebsd.munk.me.uk/ - FreeBSD Admin Weblog
http://tf-b4rt.berlios.de/ - Torrentflux-b4rt
http://ipfwstats.sf.net/ - IPFW peruser traffic logging