Files
scripts-conlxsyslog03/root/netflow/rotate_program.pl
conetadm 5718e70f15 init
2024-11-14 21:11:06 +01:00

52 lines
1.6 KiB
Perl
Executable File

#!/usr/bin/perl
use File::Basename;
use DBI;
my ($db_user, $db_name, $db_pass) = ('root', 'netflow', 'lunakoshix');
my $dbh = DBI->connect("DBI:mysql:database=$db_name", $db_user, $db_pass);
my $dir = dirname("$0");
my $dat = `date "+%s"`;
chomp $dat;
my $logfile = "$dir/logs/$dat-rotate_program.log";
my $flowfile = "$dir/data/$ARGV[0]";
my @FLOWS;
open LOG, ">>$logfile";
printf LOG "-----\n";
printf LOG "%s\n", `date`;
printf LOG "Neues File %s\n\n", $flowfile;
printf LOG "Hole exporter von DB\n";
my $query = $dbh->prepare("select * from exporter");
$query->execute() or die $query->err_str;
while (my ($id, $ip) = $query->fetchrow_array() ) { # Alle Exporter durchlaufen
printf LOG "IP %s\n", $ip;
printf LOG "flow-cat %s | flow-filter -e %s | flow-print -f 5\n", $flowfile, $ip;
@FLOWS = `flow-cat $flowfile | flow-filter -e $ip | flow-print -f 5 | head -n 100`; # Flows eines Exporters lesen
my $flowcount = @FLOWS;
printf LOG "insert %d flows in DB\n\n", $flowcount;
foreach (@FLOWS) { # Alles Flows dieses einen Exporters in DB jubeln
chomp;
if (/^[0-9]/) {
s/\s+/ /g;
s/^ //;
my ($start, $ende, $sif, $sip, $sp, $dif, $dip, $dp, $p, $f, $pa, $o) = split / /, $_;
$insert = $dbh->prepare("insert into flows set flow_exporter='$id',flow_src_if='$sif',flow_src_ip='$sip',flow_src_port='$sp',flow_dst_if='$dif',flow_dst_ip='$dip',flow_dst_port='$dp',flow_protocol='$p',flow_flag='$f',flow_packets='$pa',flow_octets='$o'");
$insert->execute() or die $insert->err_str;
} else {
}
}
}
printf LOG "Ende\n";
unlink ($flowfile);
close LOG;