git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@941 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2004/09/28 18:58:01... | ... |
@@ -17,6 +17,11 @@ |
17 | 17 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | 18 |
|
19 | 19 |
# clamavmon - monitor a network for virus intrusion |
20 |
+# Usage "clamavmon [machines...]" where machines is an optional list of machine |
|
21 |
+# running clamd. These will turn from green to red if clamd dies. Clicking on |
|
22 |
+# the machine name gives the version of clamd running there. |
|
23 |
+# FIXME: clicking should give the data base information and date of last |
|
24 |
+# freshclam |
|
20 | 25 |
|
21 | 26 |
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' if 0; |
22 | 27 |
|
... | ... |
@@ -24,50 +29,126 @@ use strict; |
24 | 24 |
use IO::Socket::INET; |
25 | 25 |
use Tk; |
26 | 26 |
use Tk::Dialog; |
27 |
-use threads; # Tk isn't thread safe :-( This will need some careful work |
|
27 |
+use threads; # Tk isn't thread safe, which really means it can't be used |
|
28 |
+ # in applications such as this which gather and display data |
|
29 |
+ # in real time. I would have prefered to use TCL, but that |
|
30 |
+ # doesn't come with UDP support, so I'd have to build that in |
|
31 |
+ # as extension. |
|
28 | 32 |
|
29 | 33 |
my $mw = MainWindow->new; |
30 |
-my $history = ""; |
|
31 | 34 |
|
32 |
-my $text = $mw->Scrolled('Text', -width => 50, -scrollbars => 'ow')->pack; |
|
35 |
+# $mw->bind('<Alt-F4>' => \&quit); |
|
36 |
+ |
|
37 |
+my $top = $mw->Frame(); |
|
38 |
+my $middle = $mw->Frame(); |
|
39 |
+my $bottom = $mw->Frame(); |
|
40 |
+ |
|
41 |
+my @machines; |
|
42 |
+ |
|
43 |
+if(@ARGV) { |
|
44 |
+ my $offset = 0; |
|
45 |
+ foreach(@ARGV) { |
|
46 |
+ $machines[$offset++] = $top->Button(-text => $_, -command => [ \&drill, $_ ])->pack(-side => 'left'); |
|
47 |
+ } |
|
48 |
+} |
|
49 |
+ |
|
50 |
+$top->pack(); |
|
51 |
+ |
|
52 |
+my $text = $middle->Scrolled('Text', -width => 50, -scrollbars => 'ow')->pack; |
|
53 |
+# $text->bind('<Alt-F4>' => \&quit); |
|
54 |
+ |
|
55 |
+$middle->pack(); |
|
33 | 56 |
|
34 |
-my $button1 = $mw->Button(-text => 'About', -command => \&about)->pack(-side => 'left'); |
|
35 |
-# my $button2 = $mw->Button(-text => 'Quit', -command => \&quit)->pack(-side => 'left'); |
|
36 |
-my $button2 = $mw->Button(-text => 'Quit', -command => \&exit)->pack(-side => 'left'); |
|
57 |
+my $b1 = $bottom->Button(-text => 'About', -command => \&about)->pack(-side => 'left'); |
|
58 |
+# my $b2 = $bottom->Button(-text => 'Quit', -command => \&quit)->pack(-side => 'left'); |
|
59 |
+my $b2 = $bottom->Button(-text => 'Quit', -command => \&exit)->pack(-side => 'left'); |
|
37 | 60 |
|
38 |
-my $t = threads->new(\&listener); |
|
61 |
+$bottom->pack(); |
|
62 |
+ |
|
63 |
+my $t1 = threads->new(\&listener); |
|
64 |
+ |
|
65 |
+if(@ARGV) { |
|
66 |
+ my $t2 = threads->new(\&pinger); |
|
67 |
+} |
|
68 |
+ |
|
69 |
+# $t->detach(); |
|
39 | 70 |
|
40 | 71 |
my $quitting = 0; |
41 | 72 |
|
73 |
+my $history = ""; |
|
74 |
+ |
|
42 | 75 |
MainLoop; |
43 | 76 |
|
44 | 77 |
sub listener { |
45 |
- my $MySocket = IO::Socket::INET->new( |
|
46 |
- LocalPort => 3310, |
|
47 |
- Proto => 'udp', |
|
48 |
- Type => SOCK_DGRAM) or die "$0: socket: $!\n"; |
|
78 |
+ until($quitting) { |
|
79 |
+ my $sock = IO::Socket::INET->new( |
|
80 |
+ LocalPort => 3310, |
|
81 |
+ Proto => 'udp', |
|
82 |
+ Type => SOCK_DGRAM) or die "$0: socket: $!\n"; |
|
49 | 83 |
|
50 |
- unless($quitting) { |
|
51 | 84 |
my $mess; |
52 |
- $MySocket->recv($mess, 128); |
|
85 |
+ $sock->recv($mess, 128); |
|
53 | 86 |
|
54 |
- my ($rport, $ipaddr) = sockaddr_in($MySocket->peername); |
|
87 |
+ my ($rport, $ipaddr) = sockaddr_in($sock->peername); |
|
88 |
+ |
|
89 |
+ close $sock; |
|
55 | 90 |
|
56 | 91 |
$text->insert('end', "From " . inet_ntoa($ipaddr) . " $mess\n"); |
57 | 92 |
|
58 |
- print "From " . inet_ntoa($ipaddr) . " $mess\n"; |
|
93 |
+ # print "From " . inet_ntoa($ipaddr) . " $mess\n"; |
|
59 | 94 |
|
60 | 95 |
# $text->Contents($history); |
61 |
- $text->pack; |
|
96 |
+ # $text->pack; |
|
97 |
+ } |
|
98 |
+ print "quit\n"; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+sub pinger { |
|
102 |
+ until($quitting) { |
|
103 |
+ foreach(@machines) { |
|
104 |
+ my $machine = $_->cget('-text'); |
|
105 |
+ my $sock = IO::Socket::INET->new( |
|
106 |
+ PeerPort => 3310, |
|
107 |
+ PeerAddr => $machine, |
|
108 |
+ Proto => 'tcp', |
|
109 |
+ Timeout => 10, |
|
110 |
+ Type => SOCK_STREAM); |
|
111 |
+ |
|
112 |
+ my $mess = ""; |
|
113 |
+ |
|
114 |
+ my $background = $_->cget('-background'); |
|
115 |
+ |
|
116 |
+ if($sock) { |
|
117 |
+ print $sock "PING\n"; |
|
118 |
+ |
|
119 |
+ $mess = <$sock>; |
|
120 |
+ |
|
121 |
+ close $sock; |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ if($mess ne "PONG\n") { |
|
125 |
+ # print "$machine is down\n"; |
|
126 |
+ if($background ne 'red') { |
|
127 |
+ $_->configure(-background => 'red'); |
|
128 |
+ } |
|
129 |
+ } else { |
|
130 |
+ # print "$machine is up\n"; |
|
131 |
+ if($background ne 'green') { |
|
132 |
+ $_->configure(-background => 'green'); |
|
133 |
+ } |
|
134 |
+ } |
|
135 |
+ } |
|
136 |
+ select(undef, undef, undef, 60); |
|
62 | 137 |
} |
63 | 138 |
} |
64 | 139 |
|
65 | 140 |
sub quit { |
66 | 141 |
$quitting = 1; |
67 |
- $t->join(); |
|
142 |
+ # $t->join(); |
|
68 | 143 |
exit; |
69 | 144 |
} |
70 | 145 |
|
146 |
+# TODO: this should be modeless |
|
71 | 147 |
sub about { |
72 | 148 |
my $about = $mw->DialogBox( |
73 | 149 |
-title=>"About clamAVmon", |
... | ... |
@@ -83,3 +164,34 @@ sub about { |
83 | 83 |
|
84 | 84 |
$about->Show(); |
85 | 85 |
} |
86 |
+ |
|
87 |
+sub drill { |
|
88 |
+ my $machine = shift; |
|
89 |
+ |
|
90 |
+ my $sock = IO::Socket::INET->new( |
|
91 |
+ PeerPort => 3310, |
|
92 |
+ PeerAddr => $machine, |
|
93 |
+ Proto => 'tcp', |
|
94 |
+ Timeout => 10, |
|
95 |
+ Type => SOCK_STREAM) or die "$0: socket: $!\n"; |
|
96 |
+ |
|
97 |
+ print $sock "VERSION\n"; |
|
98 |
+ |
|
99 |
+ my $mess = <$sock>; |
|
100 |
+ |
|
101 |
+ close $sock; |
|
102 |
+ |
|
103 |
+ print $mess; |
|
104 |
+ |
|
105 |
+ my $state = $mw->DialogBox( |
|
106 |
+ -title=>$machine, |
|
107 |
+ -buttons=>["OK"] |
|
108 |
+ ); |
|
109 |
+ |
|
110 |
+ $state->add('Label', |
|
111 |
+ -anchor => 'w', |
|
112 |
+ -justify => 'left', |
|
113 |
+ -text => $mess)->pack; |
|
114 |
+ |
|
115 |
+ $state->Show(); |
|
116 |
+} |