init III
This commit is contained in:
242
Perl syslog server/syslog.pl
Normal file
242
Perl syslog server/syslog.pl
Normal file
@@ -0,0 +1,242 @@
|
||||
#!/bin/perl
|
||||
use Net::Syslogd;
|
||||
use Thread;
|
||||
use Tk;
|
||||
use strict;
|
||||
|
||||
my $LOGFILE :shared;
|
||||
$LOGFILE = "syslog.txt";
|
||||
|
||||
#my $sysloghandle :shared;
|
||||
|
||||
# Main Window
|
||||
my $mw = MainWindow->new();
|
||||
my $mbar = $mw -> Menu();
|
||||
my $i;
|
||||
|
||||
my $stlab = $mw -> Label(-height=>1, -text=>"received");
|
||||
my $orlab = $mw -> Label(-height=>1, -text=>"origin");
|
||||
my $iplab = $mw -> Label(-height=>1, -text=>"sender");
|
||||
my $falab = $mw -> Label(-height=>1, -text=>"facility");
|
||||
my $selab = $mw -> Label(-height=>1, -text=>"severity");
|
||||
my $tilab = $mw -> Label(-height=>1, -text=>"time");
|
||||
my $holab = $mw -> Label(-height=>1, -text=>"hostname");
|
||||
my $melab = $mw -> Label(-height=>1, -text=>"message");
|
||||
|
||||
my $sttxt;# = $mw -> Text(-width=>21, -wrap=>"none");
|
||||
my $ortxt;
|
||||
my $iptxt;# = $mw -> Text(-width=>16, -wrap=>"none");
|
||||
my $fatxt;# = $mw -> Text(-width=>8, -wrap=>"none");
|
||||
my $setxt;# = $mw -> Text(-width=>15, -wrap=>"none");
|
||||
my $titxt;# = $mw -> Text(-width=>15, -wrap=>"none");
|
||||
my $hotxt;# = $mw -> Text(-width=>15, -wrap=>"none");
|
||||
my $metxt;# = $meframe -> Text(-width=>15, -wrap=>"none");#-width=>10, -height=>10, -wrap=>"none");
|
||||
|
||||
# Frame für Messagetext und Scrollbalken
|
||||
#my $meframe = $mw->Frame();
|
||||
|
||||
# Vertikaler Scrollbalken für Textfelder
|
||||
my $srl_y = $mw -> Scrollbar();
|
||||
|
||||
# Horizontaler Scrollbalken für Textfelder
|
||||
my $srl_x = $mw -> Scrollbar();
|
||||
|
||||
# Definition der Textfelder
|
||||
my $textboxes = [ $sttxt = $mw -> Text(-width=>21, -wrap=>"none"),
|
||||
$ortxt = $mw -> Text(-width=>16, -wrap=>"none"),
|
||||
$iptxt = $mw -> Text(-width=>16, -wrap=>"none"),
|
||||
$fatxt = $mw -> Text(-width=>8, -wrap=>"none"),
|
||||
$setxt = $mw -> Text(-width=>15, -wrap=>"none"),
|
||||
$titxt = $mw -> Text(-width=>15, -wrap=>"none"),
|
||||
$hotxt = $mw -> Text(-width=>15, -wrap=>"none"),
|
||||
$metxt = $mw -> Text(-width=>15, -wrap=>"none"),
|
||||
];
|
||||
|
||||
# y-Achse
|
||||
# This method is called when one Listbox is scrolled with the keyboard
|
||||
# It makes the Scrollbar reflect the change, and scrolls the other lists
|
||||
sub scroll_textboxes_y {
|
||||
my ($sb, $scrolled, $lbs, @args) = @_;
|
||||
$sb->set(@args); # tell the Scrollbar what to display
|
||||
my ($top, $bottom) = $scrolled->yview( );
|
||||
foreach my $list (@$lbs) {
|
||||
$list->yviewMoveto($top); # adjust each lb
|
||||
}
|
||||
}
|
||||
# x-Achse
|
||||
sub scroll_textboxes_x {
|
||||
my ($sb, $scrolled, $lbs, @args) = @_;
|
||||
$sb->set(@args); # tell the Scrollbar what to display
|
||||
my ($top, $bottom) = $scrolled->xview( );
|
||||
foreach my $list (@$lbs) {
|
||||
$list->xviewMoveto($top); # adjust each lb
|
||||
}
|
||||
}
|
||||
|
||||
# Configure each Listbox to call &scroll_textboxes, y-Achse
|
||||
foreach my $list (@$textboxes) {
|
||||
$list->configure(-yscrollcommand => [ \&scroll_textboxes_y, $srl_y, $list, $textboxes ]);
|
||||
}
|
||||
|
||||
# Configure each Listbox to call &scroll_textboxes, x-Achse
|
||||
foreach my $list (@$textboxes) {
|
||||
$list->configure(-xscrollcommand => [ \&scroll_textboxes_x, $srl_x, $list, $textboxes ]);
|
||||
}
|
||||
|
||||
# y-Achse
|
||||
# Configure the Scrollbar to scroll each Listbox
|
||||
$srl_y->configure(-orient=>'v', -command => sub { foreach my $list (@$textboxes) {
|
||||
$list->yview(@_);
|
||||
}});
|
||||
# Vertikaler Scrollbalken für Textfelder Ende
|
||||
# x-Achse
|
||||
$srl_x->configure(-orient=>'h', -command => sub { foreach my $list (@$textboxes) {
|
||||
$list->xview(@_);
|
||||
}});
|
||||
|
||||
|
||||
# Horizontaler Scrollbalken für Messages Textbox
|
||||
#my $srl_x = $mw -> Scrollbar(-orient=>'h',-command=>[xview => $metxt]);
|
||||
#$metxt -> configure(-yscrollcommand=>['set', $srl_y], -xscrollcommand=>['set',$srl_x]);
|
||||
# Horizontaler Scrollbalken für Messages Textbox Ende
|
||||
|
||||
# Menüleiste
|
||||
$mw -> configure(-menu => $mbar);
|
||||
my $help = $mbar -> cascade(-label =>"Help", -underline=>0, -tearoff => 0);
|
||||
$help -> command(-label =>"About", -command => sub {return; });
|
||||
# Menüleiste Ende
|
||||
|
||||
$stlab -> grid (-row=>5, -column=>5);
|
||||
$orlab -> grid (-row=>5, -column=>8);
|
||||
$iplab -> grid (-row=>5, -column=>10);
|
||||
$falab -> grid (-row=>5, -column=>15);
|
||||
$selab -> grid (-row=>5, -column=>20);
|
||||
$tilab -> grid (-row=>5, -column=>25);
|
||||
$holab -> grid (-row=>5, -column=>30);
|
||||
$melab -> grid (-row=>5, -column=>35);
|
||||
|
||||
$sttxt -> grid(-row=>10,-column=>5, -sticky=>"NSEW");
|
||||
$ortxt -> grid(-row=>10,-column=>8, -sticky=>"NSEW");
|
||||
$iptxt -> grid(-row=>10,-column=>10, -sticky=>"NSEW");
|
||||
$fatxt -> grid(-row=>10,-column=>15, -sticky=>"NSEW");
|
||||
$setxt -> grid(-row=>10,-column=>20, -sticky=>"NSEW");
|
||||
$titxt -> grid(-row=>10,-column=>25, -sticky=>"NSEW");
|
||||
$hotxt -> grid(-row=>10,-column=>30, -sticky=>"NSEW");
|
||||
$metxt -> grid(-row=>10,-column=>35, -sticky=>"NSEW");
|
||||
#$meframe -> grid(-row=>10,-column=>7, -sticky=>"NSEW");
|
||||
|
||||
|
||||
#$mw->gridColumnconfigure(0, -weight => 1);
|
||||
$mw->gridColumnconfigure(5, -weight => 0);
|
||||
$mw->gridColumnconfigure(8, -weight => 0);
|
||||
$mw->gridColumnconfigure(10, -weight => 0);
|
||||
$mw->gridColumnconfigure(15, -weight => 0);
|
||||
$mw->gridColumnconfigure(20, -weight => 0);
|
||||
$mw->gridColumnconfigure(25, -weight => 0);
|
||||
$mw->gridColumnconfigure(30, -weight => 0);
|
||||
$mw->gridColumnconfigure(35, -weight => 1);
|
||||
#$meframe->gridColumnconfigure(1, -weight => 1);
|
||||
|
||||
$mw->gridRowconfigure(10, -weight => 1);
|
||||
#$meframe->gridRowconfigure(1, -weight => 1);
|
||||
|
||||
$srl_y -> grid(-row=>10,-column=>1,-sticky=>"ns");
|
||||
$srl_x -> grid(-row=>15,-column=>5,-columnspan=>35,-sticky=>"ew");
|
||||
|
||||
|
||||
my $thr = new Thread \&syslogdeamon;
|
||||
|
||||
|
||||
MainLoop;
|
||||
|
||||
#$sysloghandle->close();
|
||||
print "Tschüss'n\n";
|
||||
|
||||
|
||||
sub syslogdeamon {
|
||||
my $sysloghandle = Net::Syslogd->new() or die "Error creating Syslogd listener: ", Net::Syslogd->error;
|
||||
|
||||
# Logfile einlesen
|
||||
open SYSLOG, "<$LOGFILE";
|
||||
foreach (<SYSLOG>) {
|
||||
chomp;
|
||||
my $la; my $b; my $c; my $d; my $e; my $f; my $g; my $h;
|
||||
($a,$b,$c,$d,$e,$f,$g,$h) = split/\|/,$_;
|
||||
|
||||
$sttxt->insert("1.0", "$a\n");
|
||||
$ortxt->insert("1.0", "$b\n");
|
||||
$iptxt->insert("1.0", "$c\n");
|
||||
$fatxt->insert("1.0", "$d\n");
|
||||
$setxt->insert("1.0", "$e\n");
|
||||
$titxt->insert("1.0", "$f\n");
|
||||
$hotxt->insert("1.0", "$g\n");
|
||||
$metxt->insert("1.0", "$h\n");
|
||||
}
|
||||
close SYSLOG;
|
||||
|
||||
while (1) {
|
||||
my $message = $sysloghandle->get_message();
|
||||
|
||||
if (!defined($message)) {
|
||||
printf "$0: %s\n", Net::Syslogd->error;
|
||||
exit 1;
|
||||
} elsif ($message == 0) {
|
||||
next;
|
||||
}
|
||||
|
||||
if (!defined($message->process_message())) {
|
||||
printf "$0: %s\n", Net::Syslogd->error;
|
||||
} else {
|
||||
my $peeraddr = $message->peeraddr;
|
||||
my $port = $message->peerport;
|
||||
my $facility = $message->facility;
|
||||
my $severity = $message->severity;
|
||||
my $time = $message->time;
|
||||
my $hostname = $message->hostname;
|
||||
my $message = $message->message;
|
||||
my $localt = localtime;
|
||||
|
||||
($localt) = $localt =~ /.{3} (.*)/;
|
||||
|
||||
chomp($peeraddr); chomp($port); chomp($facility); chomp($severity); chomp($time); chomp($hostname); chomp($message);
|
||||
|
||||
#printf "A:%s F:%s S:%s T:%s H:%s M:%s\n", $peeraddr, $facility, $severity, $time, $hostname, $message;
|
||||
|
||||
my $sender = $peeraddr;
|
||||
|
||||
$time = "" if ($time == "0");
|
||||
$hostname = "" if ($hostname == "0");
|
||||
|
||||
if ($message =~ /^Address=/) {
|
||||
$hostname = "";
|
||||
if ($message =~ /^Address=([^\s]*) (.*:[0-9]{1,2}) ([^\s]*) (.*)/) {
|
||||
($peeraddr, $time, undef, $message) = $message =~ /^Address=([^\s]*) (.*:[0-9]{1,2}) ([^\s]*) (.*)/;
|
||||
$message =~ s/$peeraddr//;
|
||||
$message .= " # fwd. by $sender";
|
||||
}
|
||||
else {
|
||||
($peeraddr, $message) = $message =~ /^Address=([^\s]*) (.*)/;
|
||||
$message =~ s/^Address=$peeraddr//;
|
||||
$message .= " # fwd. by $sender";
|
||||
}
|
||||
}
|
||||
$message =~ s/\s{2,}/ /;
|
||||
|
||||
# Logfile schreiben
|
||||
my $log = sprintf "%s|%s|%s|%s|%s|%s|%s|%s\n", $localt, $peeraddr, $sender, $facility, $severity, $time, $hostname, $message;
|
||||
open SYSLOG, ">>$LOGFILE";
|
||||
printf SYSLOG "$log";
|
||||
close SYSLOG;
|
||||
|
||||
$sttxt->insert("1.0", "$localt\n");
|
||||
$ortxt->insert("1.0", "$peeraddr\n");
|
||||
$iptxt->insert("1.0", "$sender\n");
|
||||
$fatxt->insert("1.0", "$facility\n");
|
||||
$setxt->insert("1.0", "$severity\n");
|
||||
$titxt->insert("1.0", "$time\n");
|
||||
$hotxt->insert("1.0", "$hostname\n");
|
||||
$metxt->insert("1.0", "$message\n");
|
||||
#$txt -> configure(-state=>"disabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user