1

I need to implement a daemon to drive network service monitoring probes using Perl.

The daemon should pre-fork a configured number of workers and a master process to fetch scheduled probes from the database and pass messages to the workers who will run the probes and insert the results into the database. One-way communication from master to worker should be sufficient.

I've played with some code using Proc::Daemon and IO::Pipe for master-to-worker IPC, but every attempt has ended in frustration. For what it's worth, I have no examples to present and they would probably only distract from the real question anyway.

Is my design, such as it is, sound?

I've seen several POD pages and tutorials covering bits and pieces of my requirements, but none that filled in the blanks on master-to-worker IPC. Can anyone offer links to articles that might help me understand how to implement this?

converter42
  • 7,400
  • 2
  • 29
  • 24

1 Answers1

2

Sounds like a job for HTTP. Why implement a pre-forking server when there is something on CPAN already?


master.pl, schedule from cron

use LWP::UserAgent;
sub fetch_probes { ... };

my %probe_data = fetch_probes;
my $ua = LWP::UserAgent->new;
my $res = $ua->post('http://localhost:5000', \%probe_data);
die $res->status_line unless $res->is_success;

worker.psgi, start app with starman --workers 32 --listen :5000

use Plack::Request;

sub run_probe { ... };
sub insert_db { ... }; # DBIx::Class magic goes here!

my $app = sub {
    my ($env) = @_;
    my $req = Plack::Request->new($env);
    my %probe_data = %{ $req->parameters };
    my $results = run_probe(%probe_data);
    return [200, ['Content-Type' => 'text/plain'], ['ok']] if insert_db($results);
    return [500, ['Content-Type' => 'text/plain'], ['the error message']];
}
daxim
  • 39,270
  • 4
  • 65
  • 132
  • That's a good suggestion, thank you. The only problem is that I need a shorter interval than cron provides and I'm going to be running enough probes that scheduling them all to run on the same interval is going to cause resource issues. – converter42 Dec 11 '11 at 02:53
  • 1
    This is a completely orthogonal problem. In http://stackoverflow.com/a/7532763/46395 I show how to run a piece of code every half second. – daxim Dec 11 '11 at 14:53