#!/usr/bin/perl $|=1; use Thread; use IO::Socket::Multicast; #use Win32::Console::ANSI qw /Cursor Title/; #use Term::ANSIScreen qw /:screen :cursor/; use strict; my %mcasts :shared; my %mcdata :shared; my %mcopen :shared; my %stream :shared; my $thr; my $TimeOut = 30; my $starttime; my $lastdown; my $lastdowns; # Konfigdatei einlesen open CONF, "<$0.conf"; my @config=; close CONF; foreach (@config) { chomp; next if ($_ =~ /^#/); next if (length($_)==0); $mcasts{$_}=''; } # Threads für die einzelnen Multicastgruppen anstarten foreach (keys %mcasts) { $thr = new Thread (\&WaitForMessage,$_); } # Startzeit, lesbar my $t=localtime; # startzeit, als fortlaufende zahl $starttime = time; # Anzeige aufbauen while (1) { print "\e[2J\n"; #aktuelle zeit, als fortlaufende zahl my $atime=time; printf "program runs since %s (for %d seconds)\n\n", $t, $atime-$starttime; printf "%7s | %15s | %5s | %15s | %s | %s\n", "SESSION", "MCAST Grp", "Port", "Interface", "Open", "Last Data"; open LOG, ">>$0.log"; foreach (sort keys %mcasts) { my ($session,$IP,$PORT,$IF)=split /:/,$_; my $open="no"; $open="yes" if ($mcopen{$_} == 1); if ($mcdata{$_} eq "") { printf "%7d | %15s | %5s | %15s | %4s | %s\n", $session, $IP, $PORT, $IF, $open, 'NEVER'; } else { if ($atime-$mcasts{$_} > $TimeOut) { printf "%7d | %15s | %5s | %15s | %4s | more than %d sec ago\n", $session, $IP, $PORT, $IF, $open, $TimeOut; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime; $lastdown=localtime; $lastdowns=$session; if ($hour>=7 and $hour<=19) { if ($stream{$_} == 1) { #printf LOG "%s\n", scalar localtime; printf LOG "%7d | %15s | %5s | %15s | %4s | at %s down for %d seconds\n", $session, $IP, $PORT, $IF, $open, scalar localtime, $TimeOut-1; } $stream{$_}=0; } } else { printf "%7d | %15s | %5s | %15s | %4s | %d seconds ago\n", $session, $IP, $PORT, $IF, $open, $atime-$mcasts{$_}; if ($stream{$_} == 0) { #printf LOG "%s\n", scalar localtime; printf LOG "%7d | %15s | %5s | %15s | %4s | live since %s\n", $session, $IP, $PORT, $IF, $open, scalar localtime; } $stream{$_}=1; } } } close LOG; printf "\nLetzter Fehler: Session $lastdowns am $lastdown\n" if ($lastdowns ne ""); printf "\nKein Fehler seit Programmstart aufgetreten\n" if ($lastdowns eq ""); printf "\nv1.6 by Andre Geißler\n"; sleep 1; } close LOG; # Baut Sockets für Multicast Empfang auf und sichert die zuletzt empfangenen Daten und dazu einen Zeitstempel sub WaitForMessage { my ($IPPORT)=@_; my ($SESSION, $IP, $PORT, $IF) = split/:/,$IPPORT; my $sock; $mcopen{$IPPORT}=0; while (!$sock) { $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1); sleep 1; } $sock->mcast_add($IP, $IF); $mcopen{$IPPORT}=1; my $data; while (1) { $sock->recv($data,512); $mcdata{$IPPORT}="DATA received in the last $TimeOut seconds"; #$data; $mcasts{$IPPORT}=time; } }