Agent

 view release on metacpan or  search on metacpan

examples/FreeSpace.pa  view on Meta::CPAN

#!/usr/bin/perl

##
# The FreeSpace agent - observes free space on multiple hosts.
# Steve Purkis <spurkis@engsoc.carleton.ca>
# April 30, 1998
##

# NOTE: AN AGENT'S GLOBAL VARIABLES ARE **NOT** PERSISTANT!!
# Only those stored in $self are!
# Programming an agent is like programming a recursive function.

package Agent::FreeSpace;
@ISA = qw( Agent );

sub new {
	my ($class, %args) = @_;
	my $self = {};
	foreach (keys(%args)) { $self->{"$_"} = $args{"$_"}; }
	bless $self, $class;
}

sub agent_main {
	my ($self, @args) = @_;
	my $to = \$self->{To};

	print "At: $$to\n" if $self->{verbose};
	if ($$to) {
		${$self->{Space}}{$$to} = $self->space();
	}

	unless ($self->{To} = shift @{$self->{'Hosts'}}) {
		return $self->summary();
	}

	# [attempt] jump to next host:
	print "Jumping to $$to... ";
	my $msg = new Agent::Message(
		Body      => [ "Run me\n", $self->store() ],
		Transport => TCP,
		Address   => $$to
	);
	if ($msg->send) { print "done.\n"; }
	else            { print "couldn't send agent!\n"; }
}


sub space {
	my ($self, @args) = @_;

	my $space;
	if ($^O =~ /linux/i) {
		my @df = `df -h`;
		shift @df;	# lose the header
		foreach (@df) {
			if ($_ =~ /\S+\s+\S+\s+\S+\s+([\d\.]+)M/) {
				$space += $1;
			}
		}
	} elsif ($^O =~ /win32/i) {
		# not yet.
	}

	$space .= "Mb free";
	print "Space: $space\n" if $self->{verbose};
	return $space;
}

sub summary {
	my ($self, @args) = @_;

	print "+------\n|FreeSpace Agent summary:\n";
	foreach (keys(%{$self->{Space}})) {
		print "|\t$_ =>\t", ${$self->{Space}}{$_}, "\n";
	}
	print "+------\n";
}

1;



( run in 0.990 second using v1.01-cache-2.11-cpan-39bf76dae61 )