Skip Menu |

This queue is for tickets about the Nagios-Object CPAN distribution.

Report information
The Basics
Id: 47912
Status: resolved
Priority: 0/
Queue: Nagios-Object

People
Owner: duncan_j_ferguson [...] yahoo.co.uk
Requestors: PJNEWMAN [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.21.5
Fixed in: 0.21.7



Subject: Nagios::StatusLog very slow parsing large files
When parsing the status log for our relatively large Nagios install (290 hosts, 1347 services), it was taking quite a while (6s+, possibly up to ten (including a little XML processing too)), which wasn't acceptable for our usage. We've therefore applied the following fix, which as far as I can tell won't actually affect the output, it certainly doesn't seem to on our system, I can think of a few cases where it might, but I believe they would break the existing parser anyway. Either for future inclusion perhaps, or just as an option for people who want a speed increase. Our total script processing, including some XML processing is now about 1.2 seconds on average. The subroutine becomes: sub update_v2 ($) { my $self = shift; my %handlers = ( host => sub { my $item = shift; my $host = $item->{host_name}; if ( !exists $self->{HOST}{$host} ) { $self->{HOST}{$host} = {}; } _copy( $item, $self->{HOST}{$host} ); }, service => sub { my $item = shift; my $host = $item->{host_name}; my $svc = $item->{service_description}; if ( !exists $self->{SERVICE}{$host}{$svc} ) { $self->{SERVICE}{$host}{$svc} = {}; } _copy( $item, $self->{SERVICE}{$host}{$svc} ); }, info => sub { _copy( shift, $self->{INFO} ); }, program => sub { _copy( shift, $self->{PROGRAM} ); } ); my $log_fh = gensym; open( $log_fh, "<$self->{LOGFILE}" ) || croak "could not open file $self->{LOGFILE} for reading: $!"; # change the first line of the RE to this: # (info|program|host|service) \s* {( # to make it a bit more careful, but it has a measurable cost on runtime my $entry_re = qr/ # capture the type into $1 (\w+) \s* # capture all of the text between the brackets into $2 {( .*? )} /xs; my @lines = <$log_fh>; my $file = "@lines"; #Drop comments because we don't need them. $file =~ s/#.*\n//mg; $file =~ s/[\r\n]+\s*/\n/g; # clean up whitespace and newlines while($file =~ /$entry_re/g){ ( my $type, my $text) = ( $1, $2 ); my %item = map { split /\s*=\s*/, $_, 2 } split /\n/, $text; $handlers{$type}->( \%item ); } close( $log_fh ); 1; }
Thanks for the patch - I want to include some extra v3 config files to run tests againsst before I release. I'll let you know when its applied in git and/or released.
Could you please generate this as a unified diff and attach to this ticket, otherwise it will be a lot harder for me to apply. Thanks Duncs
On Fri Jul 17 09:38:31 2009, duncs wrote: Show quoted text
> Thanks for the patch - I want to include some extra v3 config files to > run tests againsst before I release. I'll let you know when its
applied Show quoted text
> in git and/or released.
I assume the v3 config files are just an aside, rather than anything to do with this patch, given it is just for parsing v2 files. Although I guess a similar speed-up may be possible for v3 as well, I've not looked into it yet, as we are still running v2, although planning to upgrade soon, so watch this space... On Fri Jul 17 10:59:07 2009, duncs wrote: Show quoted text
> Could you please generate this as a unified diff and attach to this > ticket, otherwise it will be a lot harder for me to apply. > > Thanks > > Duncs
Okay I think I've finally got a unified diff sorted for you, our version came via Ubuntu's packaging system and seems to have differences itself to the one offered direct from CPAN (more crediting to yourself for a start and moving the _copy function into the update_v2 function), anyway let me know how you get on/if you have any problems, I could always just give you a unified diff of just that function instead, although I guess that might be less helpful. PN
File difference report generated by CSDiff by ComponentSoftware on 22/07/2009 19:32 Base file: StatusLog.pm Compared file: StatusLog.mod.pm 280,283d279 < # match the last bracket only if followed by another definition < (?=(?: \s* (?:info|program|host|service) \s* { | \Z) ) < # capture remaining text (1-2 lines) into $3 for re-processing < (.*)$ 286,292c282,290 < my $entry = ''; < while ( my $line = <$log_fh> ) { < next if ( $line =~ /^\s*#/ ); < $entry .= $line; < if ( $entry =~ m/$entry_re/ ) { < ( my $type, my $text, $entry ) = ( $1, $2, $3 ); < $text =~ s/[\r\n]+\s*/\n/g; # clean up whitespace and newlines --- > my @lines = <$log_fh>; > my $file = "@lines"; > #Drop comments if we don't need them as it should speed things up a little bit. > #Comment out the line below if you do want to keep comments > $file =~ s/#.*\n//mg; > $file =~ s/[\r\n]+\s*/\n/g; # clean up whitespace and newlines > > while($file =~ /$entry_re/g){ > ( my $type, my $text ) = ( $1, $2 ); 295d292 < } <--------------------- End of report --------------------->
Patch now applied, thanks
Subject: Nagios::StatusLog very slow parsing large files - v3 fix probably possible too
On Sat Aug 08 04:23:30 2009, duncs wrote: Show quoted text
> Patch now applied, thanks
Hi, Thanks for doing that, I really appreciate your work on this module. Just to say I've just had a look at the update_v3 code, and it appears to be identical to the v2 stuff, apart from the handlers defined, so I personally see no reason to expect that my changes won't work fine if applied in the same way to that function too. However I don't currently use a v3 system, but may have access to one, so I'll try and test it out. Alternatively if yourself or anyone else wants to have a go, then I think it should be fine. Regards, PN
On Sun Aug 09 06:31:53 2009, PJNEWMAN wrote: Show quoted text
> Just to say I've just had a look at the update_v3 code, and it appears > to be identical to the v2 stuff, apart from the handlers defined, so I > personally see no reason to expect that my changes won't work fine if > applied in the same way to that function too.
The tests I have run show it is ok so have applied to the code repo for release in 0.21.8 - not yet released