File: ftp.monitor

package info (click to toggle)
mon 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,860 kB
  • ctags: 538
  • sloc: perl: 15,847; ansic: 774; sh: 330; makefile: 46
file content (244 lines) | stat: -rwxr-xr-x 4,752 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/perl
#
# Use try to connect to a FTP server, and
# wait for the right output.
#
# For use with "mon".
#
# Arguments are "-p port -t timeout host [host...]"
#
# Adapted from "http.monitor" by
# Jim Trocki
#
# http.monitor originally written by
#
# Jon Meek
# American Cyanamid Company
# Princeton, NJ
#
# $Id: ftp.monitor,v 1.1.1.1.4.1 2007/05/08 11:25:42 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Getopt::Std;
use English;

getopts ("p:t:");
$PORT = $opt_p || 21;
$TIMEOUT = $opt_t || 30;

my %good;
my %bad;

#
# collect the status on all the hosts
#
foreach my $host (@ARGV) {
    my $result = ftpGET ($host, $PORT);

    if (!$result->{"ok"}) {
    	$bad{$host} = $result;
    } else {
    	$good{$host} = $result;
    }
}

#
# summary line
#
if (keys %bad == 0)
{
    print "\n";
}

else
{
    print join (" ", sort keys %bad), "\n";
}

#
# detail
#
foreach my $host (keys %bad) {
    print "$host failed with error " . $bad{$host}->{"error"}, "\n";

    print "detail for $host\n";
    print "==============================================================================\n";

    if ($bad{$host}->{"detail"} ne "")
    {
	print $bad{$host}->{"detail"};
    }
    
    else
    {
    	print "no detail\n";
    }

    print "\n";
}

print "\n";

foreach my $host (keys %good)
{
    print "$host succeeded\n";

    print "detail for $host\n";
    print "==============================================================================\n";

    if ($good{$host}->{"detail"} ne "")
    {
	print $good{$host}->{"detail"};
    }

    else
    {
    	print "no detail\n";
    }

    print "\n";
}

if (keys %bad != 0)
{
    exit 1;
}

exit 0;


sub ftpGET {
    use Socket;
    use Sys::Hostname;

    my($Server, $Port) = @_;
    my($ok);

    my $result = {
    	"ok" => 0,
	"error" => undef,
	"detail" => undef,
    };

###############################################################
    eval {
	local $SIG{ALRM} = sub { die "Timeout Alarm" };
	alarm $TIMEOUT;

	my $err = &OpenSocket($Server, $Port); # Open a connection to the server

	if ($err ne "") { # Failure to open the socket
	    $result = {
	    	"ok" => 0,
		"error" => $err,
		"detail" => undef,
	    };

	    return $result;
	}

	while ($in = <S>) {
	    $result->{"detail"} .= "    < $in";
	    if ($in =~ /^220 /) {
    	    	$result->{"ok"} = 1;
    	    	last;
    	    }
    	}

    	if (!$result->{"ok"}) {
	    alarm 0;
	    $result->{"ok"} = 0;
	    $result->{"error"} = "Connection refused";
	    close(S);
	    return undef;
    	}

	print S "quit\r\n";
	$result->{"detail"} .= "    > quit\n";

	while ($in = <S>) {
		$result->{"detail"} .= "    < $in";
		next if ($in =~ /^[0-9]{3}\-/);
		if ($in !~ /^221 /) {
			alarm 0;
			$result->{"ok"} = 0;
			$result->{"error"} = "FTP server error after quit";
			close(S);
			return undef;
		}
	}

	close(S);
	alarm 0; # Cancel the alarm
    };

    #
    # catch timeout
    #
    if ($EVAL_ERROR and ($EVAL_ERROR =~ /^Timeout Alarm/)) {
	$result->{"ok"} = 0;
	$result->{"error"} = "timeout";
    }

    return $result;
}


#
# Make a Berkeley socket connection between this program and a TCP port
# on another (or this) host. Port can be a number or a named service
#
# returns "" on success, or an error string on failure
#
sub OpenSocket {
    my ($host, $port) = @_;

    my $proto = (getprotobyname('tcp'))[2];

    return ("could not get protocol") if (!defined $proto);

    my $conn_port;

    if ($port =~ /^\d+$/) {
    	$conn_port = $port;

    } else {
	$conn_port = (getservbyname($port, 'tcp'))[2];
	return ("could not getservbyname for $port")
		if (!defined $conn_port);
    }

    my $host_addr = (gethostbyname($host))[4];

    return ("gethostbyname failure")
    		if (!defined $host_addr);

    my $that = sockaddr_in ($conn_port, $host_addr);

    if (!socket (S, &PF_INET, &SOCK_STREAM, $proto)) {
    	return ("socket: $!");
    }

    if (!connect (S, $that)) {
    	return ("connect: $!");
    }

    select(S); $| = 1; select(STDOUT);

    "";
}