package My::parser::ssh; use strict; use DBI; use warnings; use My::parser::ssh_parser; sub new { my $class = shift; my $self = {}; bless ($self, $class); $self->{'config'} = shift; $self->{'parser'} = My::parser::ssh_parser->new(); return $self; } sub fetch { my $self = shift; $self->{'dbh'} = $self->{'config'}->get_dbh unless($self->{'dbh'}); my $seq = $self->{'seq'} || 0; my $retmsg; my @toreturn; unless($seq) { my $seqsth = $self->{'dbh'}->prepare("SELECT seq FROM logs WHERE program = 'sshd' ORDER BY seq DESC LIMIT 1") or return { retval => 0, retmsg => DBI::errstr, error => 1 }; $seqsth->execute or return { retval => 0, retmsg => DBI::errstr, error => 1}; $seq = $seqsth->fetchrow_arrayref->[0] or return { retval => 0, retmsg => DBI::errstr, error => 1}; } my $sth = $self->{'dbh'}->prepare("SELECT msg,seq FROM logs WHERE program = 'sshd' AND seq > $seq") or return { retval => 1, retmsg => DBI::errstr, error => 1 }; $sth->execute or return { retval => 0, retmsg => DBI::errstr, error => 1}; while(my $ref = $sth->fetchrow_hashref) { my $string = $$ref{'msg'}; $seq = $$ref{'seq'}; push(@toreturn,$string); } $self->{'seq'} = $seq; return { retval => 0, retmsg => $retmsg, error => 1 } if($retmsg); return { retval => 0, retmsg => 'Nothing to return' } unless(scalar(@toreturn)); return { retval => 1, retmsg => 'Here comes the results', lines => \@toreturn }; } sub parse { my $self = shift; my @result; my $string = $self->fetch; if($string->{'retval'}) { foreach my $str(@{$string->{'lines'}}) { my $r = $self->{'parser'}->parser($str); if($r->{'retval'}) { delete $r->{'retval'}; push(@result,$r); } } return { retval => 1, retmsg => 'Here comes the results', lines => \@result }; } return { retval => 0, retmsg => "ssh-fetcher returned an error: $string->{'retmsg'}" } if($string->{'error'}); return { retval => 0 }; # nothing to return, nothing to say:) } 1;