#!/usr/bin/perl
#
# Checks on Harvest gatherers and brokers.
#
#	* Makes TCP connections to gatherer and broker ports.
#	  Gives 'perror' if connect fails.
#
#	* Counts files in objects directories.  Stops counting after
#	  $MAXFILECOUNT, and reports directories containing less than
#	  $MAXFILECOUNT files.
#
#	* -v option reports successful connections and total count of
#	  files in objects directories.
#
# $Id: harvest-check.pl,v 2.1 1997/03/21 17:18:58 sxw Exp $

#############################################################################
#
#  Harvest Indexer http://www.arco.de/~kj/
#  ---------------------------------------
#
#  The Harvest Indexer is a continued development of code developed by
#  the Harvest Project. Development is carried out by numerous individuals
#  in the Internet community, and is not officially connected with the
#  original Harvest Project or its funding sources.
#
#  Please mail lee@arco.de if you are interested in participating
#  in the development effort.
#
#  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., 675 Mass Ave, Cambridge, MA 02139, USA.
#

$MAXFILECOUNT=100;	# the filecount procedure stops when it counts this
			# many files

$ENV{'HARVEST_HOME'} = "/usr/local/harvest"
	unless defined($ENV{'HARVEST_HOME'});

use IO::Socket::INET;

$verbose = 1
	if ($ARGV[0] && $ARGV[0] eq "-v");

$|=1;

while (<DATA>) {
	chop;
	next if (/^#/);
	($what, $name, $host, $port, $dir) = split;

	if ($host && $port) {
		$cmd = "QUIT\n" if ($what eq 'gatherer');
		print "$what $name $host $port: $!\n"
			unless ($c = &connect_tcp ($host, $port, $cmd));
		if ($verbose && $c) {
			printf "%-10s %-10s %-35s : UP\n",
				$what, $name, $host.":".$port;
		}
	}

	if ($dir) {
		$dir = "$ENV{'HARVEST_HOME'}/" . $dir
			unless ($dir =~ /^\//);
		$n = &filecount ($dir);
		printf "%7d objects in: $dir\n", $n
			if ($verbose || $n < $MAXFILECOUNT);
	}
}

exit 0;

sub connect_tcp {
	my ($host, $port, $cmd) = @_;

	my $socket = IO::Socket::INET->new ("$host:$port");

	return 0 unless ($socket);
	return 0 unless ($cmd eq '' || print $socket $cmd);
	return 0 unless close $socket;
	return 1;
}

sub filecount {
	my $dir = shift;
	my $n = 0;
	push (@dirs, $dir);

	while ($dir = shift @dirs) {
		unless (opendir (D, $dir)) {
			print STDERR "$dir: $!\n";
			return 0;
		}
		foreach $f (readdir D) {
			next if ($f eq '.');
			next if ($f eq '..');
			print STDERR "$dir/$f: $!\n"
				unless (stat ("$dir/$f"));
			push (@dirs, "$dir/$f")	if (-d _);
			$n++			if (-f _);
		}
		closedir D;
		return ($n) if ($verbose == 0 && $n > $MAXFILECOUNT);
	}
	$n;
}

__END__

#what    name		host			port  [objdir]
broker   default	localhost		8501  brokers/default/objects
gatherer default	localhost		8500
