Thanks for reply and help Browser UK.
Need you help and opinion for disigning this collector.
Requirement: I will have many dataservers and host that will be divided in multiple groups( wil contain dataservers). We need to connect to each of dataserver/host and execute some commands/utility and process the data, bring back the data and write into a file.
ex: DSA1,DSA2...DSAN
1. conenct to DSA1, process and get the data and write into a file
2. once we get the data back from DSA1, may be after 1/2 hour will need to conenct again and get back data...this will continue depending on interval time.
above step 1 and 2 should run for many servers ( may be 1000s). Do you think thread queue is best way for this requirement ?
is below code is the best way to move forward ? how will I run the job again for particular group or DSA on demand basis?
Thanks a lot for your help in advance. any suggestions/directions will help.
#!/usr/bin/perl
use strict;
use threads;
use Data::Dumper;
use Thread::Queue;
warn "Using threads $threads::VERSION";
warn "Using Thread::Queue $Thread::Queue::VERSION";
my $THREADS = 3;
my %dataEntity;
while(<>){
chomp;
next if !length($_);
my ($dsName,$passwd) = split /\|/, $_;
$dataEntity{$dsName} = $passwd;
}
my $request = Thread::Queue->new;
my $response = Thread::Queue->new;
# Submit all requests
for my $dbname (keys %dataEntity) {
$request->enqueue([$dbname,$dataEntity{$dbname}]);
};
# Tell each thread that we're done
for (1..$THREADS) {
$request->enqueue(undef);
};
# Launch our threads
for (1..$THREADS) {
async(\&getData);
};
sub getData {
##
my $idx = 1;
while (my $job = $request->dequeue()) {
my ($dbname, $credentials) = @$job;
#connect to DB, retrieve information
#my $dbh = getConn($dbname,$credentials);
my %results;
#my $resArrRef = $dbh->selectall_arrayref("select srvname,dbna
+me from syslogins",{ Slice => {} });
# package some dummy results
my $resArrRef = [
{ srvname => "server:$dbname:".$idx++, dbname => $dbname,
+},
{ srvname => "server:$dbname:".$idx++, dbname => $dbname,
+},
{ srvname => "server:$dbname:".$idx++, dbname => $dbname,
+},
{ srvname => "server:$dbname:".$idx++, dbname => $dbname,
+},
];
foreach my $row ( @$resArrRef ) {
$results{$row->{srvname}} = $row->{dbname};
}
$response->enqueue(\%results);
}
# tell our main thread we're done
$response->enqueue( undef );
##
}
while ($THREADS) {
while (my $payload = $response->dequeue()) {
print Dumper $payload;
};
$THREADS--
}
sub getConn {
my ($DB,$pwd) = @_;
return $dbh;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.