Files
parserfilter/lib/ssh.pm
2024-03-09 15:36:42 +01:00

60 lines
1.9 KiB
Perl

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 $retmsg = DBI::errstr;
$seqsth->execute or $retmsg = DBI::errstr unless($retmsg);
$seq = $seqsth->fetchrow_arrayref->[0] or $retmsg = DBI::errstr unless($retmsg);
}
return { retval => 0, retmsg => $retmsg, error => 1 } if($retmsg);
my $sth = $self->{'dbh'}->prepare("SELECT msg,seq FROM logs WHERE program = 'sshd' AND seq > $seq") or $retmsg = DBI::errstr unless($retmsg);
$sth->execute or $retmsg = DBI::errstr unless($retmsg);
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;