191 lines
4.0 KiB
Perl
191 lines
4.0 KiB
Perl
#!/usr/bin/perl
|
|
use IO::Socket;
|
|
use Socket;
|
|
use Net::hostent;
|
|
#use warnings;
|
|
use Tk;
|
|
#use strict;
|
|
use IO::Socket::Multicast;
|
|
use Getopt::Long;
|
|
use threads ();
|
|
use threads::shared ();
|
|
use JSON::XS;
|
|
use POE;
|
|
use POE::Session;
|
|
use POE::Loop::Select;
|
|
use POE::Resource::Aliases;
|
|
use POE::Resource::Events;
|
|
use POE::Resource::Extrefs;
|
|
use POE::Resource::FileHandles;
|
|
use POE::Resource::SIDs;
|
|
use POE::Resource::Sessions;
|
|
use POE::Resource::Signals;
|
|
use POE::Resource::Statistics;
|
|
use POE::Component::Server::TCP;
|
|
|
|
my $mw = MainWindow->new();
|
|
my $menubar = $mw->Menu;
|
|
$mw->minsize( qw(300 300));
|
|
$mw->title("SERVER Status Monitor v1.0");
|
|
$mw->configure(-background=>'grey');
|
|
$mw->fontCreate('font1', -family => 'Verdana',-size => 12);
|
|
$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);
|
|
my $box = $mw->Listbox(-height => 15, -width => 100);
|
|
my $scroll = $mw->Scrollbar(-command => ['yview', $box]);
|
|
|
|
$file->command(-label => "Quit", -command => sub{exit;});
|
|
$opt->command(-label => "Reconnect", -command => sub{&startthr;});
|
|
$about->command(-label => "Info",-command => [\&about, "About"],);
|
|
|
|
$box->configure(-yscrollcommand => ['set', $scroll]);
|
|
#$box->pack(-side => 'left', -fill => 'both', -expand => 1);
|
|
#$scroll->pack(-side => 'right', -fill => 'y');
|
|
$box->grid(-row => 1, -column => 1, -sticky => "ew");
|
|
#$scroll->grid(-row => 1, -column => 1);
|
|
|
|
#my $table = new Term::Menu;
|
|
my $msg0 : shared = "NO MSG";
|
|
my $msg;
|
|
my %results : shared;
|
|
my $thr0;
|
|
my $ctime:shared;
|
|
my $name:shared;
|
|
my $servicename :shared =0;
|
|
my $Port :shared =0;
|
|
my $senddata;
|
|
|
|
|
|
|
|
GetOptions(
|
|
"s=s" => \$servicename,
|
|
"p=s" => \$Port,
|
|
);
|
|
|
|
|
|
|
|
sub nopara {
|
|
$msg1 = "server [options]\n\n";
|
|
$msg1.= " -s Service Name for Client\n";
|
|
$msg1.= " -p Monitoring Server Port\n";
|
|
#print $msg1;
|
|
return $msg1;
|
|
#exit;
|
|
|
|
}
|
|
|
|
|
|
if ($Port eq "0") {print &nopara; exit;};
|
|
if ($servicename eq "0") {print &nopara; exit;};
|
|
|
|
sub TCP_SRV {
|
|
my @rip:shared;
|
|
my @rport:shared;
|
|
|
|
my $coun = 0;
|
|
POE::Component::Server::TCP->new(
|
|
Alias => "server",
|
|
Port => $Port,
|
|
ClientInput => sub {
|
|
$coun++;
|
|
my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();
|
|
my ($session, $heap, $input) = @_[SESSION, HEAP, ARG0];
|
|
#print "Session ", $session->ID(), " got input send data:\n\n";
|
|
|
|
$rip0 = $heap->{remote_ip};
|
|
$rport = $heap->{remote_port};
|
|
logevent('',$session);
|
|
$ctime = time - 10;
|
|
$results{REF} = ($ctime);
|
|
$senddata=$JSONObject->encode(\%results);
|
|
$heap->{client}->put($senddata);
|
|
|
|
}
|
|
);
|
|
|
|
|
|
|
|
|
|
sub logevent {
|
|
|
|
my ($state, $session, $arg) = @_;
|
|
my $id = $session->ID();
|
|
my $log = scalar localtime(time).": session $id, client: $rip0, clientport: $rport";
|
|
print $log;
|
|
print "\n";
|
|
#print &nrthr;
|
|
$box->insert('end', $log);
|
|
$box->see('end');
|
|
|
|
}
|
|
|
|
$poe_kernel->run();
|
|
}
|
|
|
|
|
|
|
|
my $data0 :shared = 0;
|
|
my $thr0_tid;
|
|
my $group;
|
|
my $port;
|
|
my $tid;
|
|
sub MC_RECV0 {
|
|
my $sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>$port);
|
|
$sock->mcast_add($group) || die "Couldn't set group: $!\n";
|
|
|
|
while (1) {
|
|
#lock $data0;
|
|
my $data00 = "NODATA";
|
|
$sock->recv($data00,1024);
|
|
#$data0 = $data00;
|
|
$data0 = time;
|
|
$results{$group."-".$port} = ($data0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#threads->new(\&TCP_SRV2,$msg0);
|
|
#
|
|
|
|
my $counter;
|
|
my $line;
|
|
open CONFFILE, "<config.ini" or die("Could not open config.ini.");
|
|
|
|
foreach $line (<CONFFILE>) {
|
|
chomp($line);
|
|
$counter++;
|
|
my @data = split(/,/, $line);
|
|
$results{$data[0]."-".$data[1]} = "00";
|
|
$thr0 = threads->new(\&MC_RECV0,$group=$data[0], $port=$data[1], $tid=$counter);
|
|
|
|
}
|
|
|
|
sub nrthr {
|
|
my $count;
|
|
my @running;
|
|
@running = threads->list();
|
|
$count = @running;
|
|
$count = $count;
|
|
print "Threads Running: ".$count."\n";
|
|
}
|
|
|
|
threads->new(\&TCP_SRV);
|
|
|
|
foreach my $val (%results){
|
|
$msg0 .= $val;
|
|
print $val."\n";
|
|
}
|
|
|
|
|
|
|
|
$mw->repeat(1000 => \&run);
|
|
MainLoop;
|
|
sub run {
|
|
$mw->update;
|
|
|
|
}
|
|
|