CPU SCRIPT #!/usr/bin/perl use strict; use warnings; my @cpu_cores = qw(4); while (--$cpu_cores[0] and fork) {}; while () {}; MEMORY SCRIPT #!/usr/bin/perl use strict; use warnings; print "$$"; fork for 1 .. 4; for (1 .. 10000) { my $a = "xxxxx" x int rand 10_000_000; my $b = ~$a; my $c = reverse $b; MAIN SCRIPT use strict; use warnings; use Net::OpenSSH::Parallel; use Term::ANSIColor; use DBI; #-------------------------------------------------------- #FINDING HOSTS FROM CLOUDERA MANAGER DATABASE #-------------------------------------------------------- print color 'bold blue'; print " Finding hostname of the hosts configured\n\n"; my $ip_address = `ping r01mgt -w 1 | awk -F "(" '/PING/{print \$2}' | awk -F")" '{print \$1}'`; chomp($ip_address); # connect my $dbh = DBI->connect("DBI:Pg:dbname=xxxx;host=$ip_address", "xxxx", "xxxx", {'RaiseError' => 1}); # execute SELECT query my $sth = $dbh->prepare("SELECT name FROM hosts"); $sth->execute(); # iterate through resultset and create an array my @hosts; while(my $ref = $sth->fetchrow_hashref()) { my($nodename, $hadoopname) = $ref->{'name'} =~ m/(\w+).(\w+)/; push(@hosts, "$nodename"); } @hosts = sort (@hosts); print color 'reset'; #----------------------------------------------------------- #COPYING THE PERL SCRIPTS AND EXECUTING THEM #----------------------------------------------------------- my $pssh = Net::OpenSSH::Parallel->new(); $pssh->add_host($_) for @hosts; $pssh->push('*', scp_put => '/root/cpu.pl', '/root/'); $pssh->push('*', scp_put => '/root/memory.pl', '/root/'); $pssh->push('*', command => '/usr/bin/perl', '/root/cpu.pl', '/tmp/cpu'); $pssh->push('*', command => '/usr/bin/perl', '/root/memory.pl', '/tmp/memory'); $pssh->run; USING THREADS TO RUN SCRIPTS IN PARALLEL #!/usr/bin/perl use strict; use warnings; use threads; sub cpu { my @cpu_cores = qw(4); print "CPU"; while (--$cpu_cores[0] and fork) {}; while () {}; } sub memory { print "MEMORY"; fork for 1 .. 4; for (1 .. 10000) { my $a = "xxxxx" x int rand 10_000_000; my $b = ~$a; my $c = reverse $b; } } my $firstThread = threads->create(\&cpu); my $secondThread = threads->create(\&memory); $_->join() foreach ( $firstThread, $secondThread );