/[mon-modules]/tcpch.monitor
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /tcpch.monitor

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Mon Jun 23 21:55:26 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
modification of original tcpch.monitor to allow fast-expect: basically,
take 1K of output immidiatly after send (and before remote service
close socket)

1 dpavlin 1.1 #!/usr/bin/perl -w
2     #
3     # try to connect to a particular
4     # port on a bunch of hosts. For use with "mon".
5     #
6     # Options are
7     # -p <port-num>
8     # -t <connect-timeout-in-seconds> (default 15)
9     # -s <string to send upon connecting to provoke some output>
10     # -e <Perl regexp to expect in response>
11     # -f <Perl regexp to expect in response immidiatly after sent string>
12     # -q <string to send before closing after parsing response>
13     # -d <string to use as line delimiter for regexp matching>
14    
15     # without /-s/-e/-q/, just checks that the socket can be opened
16     # and closed.
17    
18     # cheap transformations done on send/quit/delim strings - \r and \n are
19     # converted to CR and LF. \\ is not supported - no escape possible.
20    
21     # sample usage:
22     #
23     # smtp: tcpch.monitor -p 25 -e '^220\b' -q 'QUIT\r\n'
24     # web: tcpch.monitor -p 80 -s 'GET / HTTP/1.0\r\n' -e '^HTTP.*200 OK'
25     # saplpr: tcpch.monitor -p 515 -s '4 x 1\n' -f 'This is SAPLPD'
26    
27    
28     #
29     # Jim Trocki, trockij@transmeta.com
30     # updated August 2000 by Ed Ravin <eravin@panix.com> for send/expect/quit
31     #
32     # $Id: tcpch.monitor,v 1.4 2001/09/22 04:45:25 root Exp root $
33     #
34     # Copyright (C) 1998, Jim Trocki
35     #
36     # This program is free software; you can redistribute it and/or modify
37     # it under the terms of the GNU General Public License as published by
38     # the Free Software Foundation; either version 2 of the License, or
39     # (at your option) any later version.
40     #
41     # This program is distributed in the hope that it will be useful,
42     # but WITHOUT ANY WARRANTY; without even the implied warranty of
43     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44     # GNU General Public License for more details.
45     #
46     # You should have received a copy of the GNU General Public License
47     # along with this program; if not, write to the Free Software
48     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49     #
50     use Getopt::Std;
51     use Socket;
52    
53     my %opt;
54     getopts ("d:p:t:s:e:f:q:", \%opt);
55     $USAGE= "Usage: tcpch.monitor -p port [-t timeout] [-s sendstr] [-e regexp] [-q quitstr] [-d line-delim]\n";
56    
57     my $PORT = $opt{"p"} || undef;
58     my $TIMEOUT = $opt{"t"} || 15;
59    
60     my $SEND= $opt{"s"} || undef;
61     my $EXPECT= $opt{"e"} || undef;
62     my $FEXPECT= $opt{"f"} || undef;
63     my $QUITSTR=$opt{"q"} || undef;
64     my $DELIM= $opt{"d"} || "\n";
65     if ($DELIM)
66     {
67     $DELIM=~ s/\\n/\n/g;
68     $DELIM=~ s/\\r/\r/g;
69     }
70    
71     my @failures = ();
72     my @detail = ();
73    
74     my $ALARM = 0;
75    
76     sub checkbuf # buffer, regexp
77     {
78     my ($buffer, $regexp)= @_;
79    
80     return $buffer =~ /$regexp/ if ($DELIM eq '');
81    
82     my @lines= split($DELIM, $buffer);
83    
84     foreach my $line (@lines)
85     {
86     if ($line =~ /$regexp/)
87     {
88     return 1;
89     }
90     }
91     return 0;
92     }
93    
94     die $USAGE unless (@ARGV > 0);
95     die "$0: missing port number\n" unless defined $PORT;
96    
97     foreach my $host (@ARGV) {
98     my $pro = getprotobyname ('tcp');
99    
100     if (!defined $pro) {
101     die "(local err) could not getprotobyname\n";
102     }
103    
104     if (!defined socket (S, PF_INET, SOCK_STREAM, $pro)) {
105     die "(local err) could not create socket: $!\n";
106     }
107    
108     my $a = inet_aton ($host);
109     if (!defined $a) {
110     push @failures, $host;
111     push @detail, "(local err) $host could not inet_aton";
112     close (S);
113     next;
114     }
115    
116     my $sin = sockaddr_in ($PORT, $a);
117     if (!defined $sin) {
118     push @failures, $host;
119     push @detail, "(local err) $host could not sockaddr_in";
120     close (S);
121     next;
122     }
123    
124     my $r;
125    
126     eval {
127     local $SIG{"ALRM"} = sub { die "alarm\n" };
128    
129     alarm $TIMEOUT;
130    
131     $r = connect (S, $sin);
132    
133     alarm 0;
134     };
135    
136     if ($@) {
137     push @failures, $host;
138    
139     if ($@ eq "alarm\n") {
140     push @detail, "$host timeout on connect";
141     } else {
142     push @detail, "$host interrupted syscall on connect: $!";
143     }
144    
145     close (S);
146     next;
147     }
148    
149     if (!defined $r) {
150     push @failures, $host;
151     push @detail, "$host: could not connect: $!";
152     close (S);
153     next;
154     }
155    
156     select S; $|= 1; select STDOUT;
157    
158     if (defined($SEND))
159     {
160     my $rc= undef;
161    
162     $SEND=~ s/\\n/\n/g;
163     $SEND=~ s/\\r/\r/g;
164     eval {
165     local $SIG{"ALRM"} = sub { die "alarm\n" };
166    
167     alarm $TIMEOUT;
168     $rc= send S, $SEND, 0;
169     if ($FEXPECT) {
170     recv S, $rxdata, 1024, 0;
171     }
172     alarm 0;
173     };
174     if ($@) {
175     push @failures, $host;
176    
177     if ($@ eq "alarm\n") {
178     push @detail, "$host timeout on write";
179     } else {
180     push @detail, "$host interrupted syscall on write: $!";
181     }
182     }
183    
184     if (! $rc)
185     {
186     push @failures, $host;
187     push @detail, "$host: write failed: $!";
188     close (S);
189     next;
190     }
191     }
192    
193     if (defined($FEXPECT) && ! checkbuf($rxdata, $FEXPECT))
194     {
195     push @failures, $host;
196     push @detail, "$host: did not recv expected response";
197     close (S);
198     next;
199     }
200    
201     if (defined($EXPECT))
202     {
203     # read and match
204    
205     my $rc= undef;
206     my $alldata= "";
207    
208     eval {
209     local $SIG{"ALRM"} = sub { die "alarm\n" };
210    
211     alarm $TIMEOUT;
212    
213     $rc= recv S, $rxdata, 1024, 0;
214     $alldata= $alldata . $rxdata;
215    
216     while ( !checkbuf($alldata, $EXPECT))
217     {
218     $rc= recv S, $rxdata, 1024, 0;
219     $alldata= $alldata . $rxdata;
220     }
221     alarm 0;
222     };
223     if ($@) {
224     push @failures, $host;
225    
226     if ($@ eq "alarm\n") {
227     push @detail, "$host timeout on read";
228     } else {
229     push @detail, "$host interrupted syscall on read: $!";
230     }
231     }
232     if ($rc)
233     {
234     push @failures, $host;
235     push @detail, "$host: recv failed : $!";
236     close (S);
237     next;
238     }
239    
240     if (! checkbuf($alldata, $EXPECT))
241     {
242     push @failures, $host;
243     push @detail, "$host: did not recv expected response";
244     close (S);
245     next;
246     }
247     }
248    
249     if (defined($QUITSTR))
250     {
251     my $rc= undef;
252    
253     $QUITSTR=~ s/\\n/\n/g;
254     $QUITSTR=~ s/\\r/\r/g;
255    
256     eval {
257     local $SIG{"ALRM"} = sub { die "alarm\n" };
258    
259     alarm $TIMEOUT;
260     $rc= send S, $QUITSTR, 0;
261     alarm 0;
262     };
263     if ($@) {
264     push @failures, $host;
265    
266     if ($@ eq "alarm\n") {
267     push @detail, "$host timeout writing quitstr";
268     } else {
269     push @detail, "$host interrupted syscall writing quitstr: $!";
270     }
271     }
272    
273     if (! $rc)
274     {
275     push @failures, $host;
276     push @detail, "$host: quit write failed: $!";
277     close (S);
278     next;
279     }
280     }
281    
282     if (!defined close (S)) {
283     push @failures, $host;
284     push @detail, "$host: could not close socket: $!";
285     next;
286     }
287     }
288    
289     if (@failures == 0) {
290     exit 0;
291     }
292    
293     print "@failures\n";
294     print "\n", join ("\n", @detail), "\n";
295    
296     exit 1;

  ViewVC Help
Powered by ViewVC 1.1.26