#!/usr/bin/perl #use warnings; use Net::hostent; #use strict; use IO::Socket; use Tk; use Tk::Font; use JSON; use threads (); use threads::shared (); use Getopt::Long; #my $peerhost = "127.0.0.1"; #my $peerport = "59001"; #my $timeout = "10"; GetOptions( "h=s" => \$peerhost, "p=s" => \$peerport, "t=s" => \$timeout ); if ($peerhost== "") {&nopara}; if ($peerport== "") {&nopara}; if ($timeout== "") {&nopara}; sub nopara { print "server [options]\n\n", " -h Monitoring Host/IP Address 10.10.10.10\n", " -p Monitoring Host Port\n", " -t Alarm Timeout in sec\n"; exit; } my @sdata0 :shared; my $thr0; my %data :shared; my $st1 = "Monitoring Server OK"; my $bg = "green"; my $mw = MainWindow->new(); my $menubar = $mw->Menu; #$mw->minsize( qw(300 300)); $mw->title("Status Monitor v1.1"); $mw->configure(-background=>'grey'); $mw->fontCreate('font1', -family => 'Verdana',-size => 8); $mw->configure(-menu => $menubar); my $file = $menubar->Menubutton(-label => 'File',-underline => 0); my $opt = $menubar->Menubutton(-label => 'Options',-underline => 0); my $about = $menubar->Menubutton(-label => '?',-underline => 0); $file->command(-label => "Quit", -command => sub{$mw->destroy}); $opt->command(-label => "Reconnect", -command => sub{&startthr;}); $about->command(-label => "Info",-command => [\&about, "About"],); my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref(); sub about ($) { my ($arg) = @_; my $tw=$mw->Toplevel(-title => "Hilfe $arg"); my $mes=$tw->Message(-text => "Multicast Checker Status Monitor\nby Martijn Niedlich\n", -width => '10c', -justify => 'center')-> pack(-side => 'top'); my $but=$tw->Button(-text => "Schließen", -command => [$tw => 'destroy'])-> pack(-side => 'top'); } sub TCP_CLI { my $socket = new IO::Socket::INET ( PeerAddr => $peerhost, PeerPort => $peerport, Proto => 'tcp', ) or die "Keinen Server gefunden unter $peerhost:$peerport\n"; $socket->send("\n"); #printf "[Connect to %s]\n", $socket->peerport; while ( $socket ) { my $recv_data; $socket->send("\n"); #%data = ""; $socket->recv($recv_data,1024); #lock %data; # print $recv_data; %data = %{$JSONObject->decode($recv_data)}; sleep 1; } } sub startthr { $thr0 = threads->new(\&TCP_CLI); #$thr0 = threads->new(\&checkdata); &statusbars; } &startthr; #my $grid; sub checkdata { my @sdata0; my $counter = 3; $sdata1[7] = "MC Monitoring Server Online"; $sdata1[8] = "green"; if ($thr0->is_joinable()) {$sdata1[7] = "MC Monitoring Server Offline"; $sdata1[8] = "red"} #while( my ($k, $v) = each %data ) { foreach $key ( keys %data) { $counter++; my $ctime = time - $timeout; @sdata0 = split(/-/, $key); if ($key !=~ "/REF/" ) { if ($data{REF} > $data{$key}) {$sdata0[2] = "ERROR";$sdata0[3] = "red";}else{$sdata0[2] = "OK";$sdata0[3] = "green";} if ($data{$key} == "00") { $sdata0[4] = "NEVER"; $sdata0[3] = "blue" } else {$sdata0[4] = scalar localtime($data{$key});}; if ($sdata1[8] eq "red") { $sdata0[3] = "yellow"}; $servicename->configure(-text=> $data{NAME}); $labels1[$counter]->configure(-background=> $sdata0[3]); $labels2[$counter]->configure(-background=> $sdata0[3]); $labels3[$counter]->configure(-text=> $sdata0[2],-background=> $sdata0[3]); $labels4[$counter]->configure(-text=> $sdata0[4],-background=> $sdata0[3]); } } $statusbarserverstatus->configure(-text=>$sdata1[7],-background=> $sdata1[8]); $statusbarserver1->configure(-background=>$sdata1[8]); $statusbarserver2->configure(-background=>$sdata1[8]); } sub statusbars { $servicename=$mw->Label(-width => "52", -font => 'font1',-text => "SERVICE NAME",-relief => 'groove',-background=>"grey", -font => 'font1')->grid(-row => 0, -column => 1,-columnspan=>4,-sticky=>'ew'); $headbarserver=$mw->Label(-width => "10", -font => 'font1',-text => "Mon Srv",-relief => 'groove',-background=>"grey", -font => 'font1')->grid(-row => 1, -column => 1,-sticky=>'w'); $headbarserver=$mw->Label(-width => "7", -font => 'font1',-text => "Mon Port",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 1, -column => 2,-sticky=>'ew'); $headbarserver=$mw->Label(-width => "25", -font => 'font1',-text => "Status",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 1, -column => 3,-columnspan=>2,-sticky=>'ew'); $sdata0[5] = $peerhost; $sdata0[6] = $peerport; $statusbarserver1=$mw->Label(-width => "10", -font => 'font1',-text => $sdata0[5],-relief => 'groove',-background=>"grey", -font => 'font1')->grid(-row => 2, -column => 1,-sticky=>'w'); $statusbarserver2=$mw->Label(-width => "7", -font => 'font1',-text => $sdata0[6],-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 2, -column => 2,-sticky=>'w'); $statusbarserverstatus=$mw->Label(-width => "25", -font => 'font1',-text => "-",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 2, -column => 3,-columnspan=>2,-sticky=>'ew'); $headbardata=$mw->Label(-width => "10", -font => 'font1',-text => "MC Group",-relief => 'groove',-background=>"grey", -font => 'font1')->grid(-row => 3, -column => 1); $headbardata=$mw->Label(-width => "7", -font => 'font1',-text => "MC Port",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 3, -column => 2); $headbardata=$mw->Label(-width => "10", -font => 'font1',-text => "Status",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => 3, -column => 3); $headbardata=$mw->Label(-width => "25", -font => 'font1',-text => "Last Data seen",-relief => 'groove',-bd => 2,-background=>"grey", -font => 'font1')->grid(-row => 3, -column => 4); my $counter2 = 3; foreach $key (keys %data) { $counter2++; my $ctime = time - 10; my @sdata1 = split(/-/, $key); if ($key !=~ "/REF/" and $key != "/NAME/") { $labels1[$counter2]=$mw->Label(-width => "10",-text => $sdata1[0],-relief => 'groove',-background=>"grey", -font => 'font1')->grid(-row => $counter2, -column => 1); $labels2[$counter2]=$mw->Label(-width => "7",-text => $sdata1[1],-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => $counter2, -column => 2); $labels3[$counter2]=$mw->Label(-width => "10",-text => "NO DATA",-relief => 'groove',-bd =>2,-background=>"grey", -font => 'font1')->grid(-row => $counter2, -column => 3); $labels4[$counter2]=$mw->Label(-width => "25",-text => "NO DATA",-relief => 'groove',-bd => 2,-background=>"grey", -font => 'font1')->grid(-row => $counter2, -column => 4); } } } #sleep 1; my $counter22 = 0; sub event { if (%data) { } else { $counter22++; if ($counter22 <= 20000) { &event; } } } &event; &statusbars; $mw->repeat(500 => \&run); MainLoop; sub run { &checkdata; $mw->update; }