Code:
#!/usr/bin/perl -w
use strict;
my $dynamips = '/home/ios/dynamips-0.2.8-RC2-amd64.bin';
my @ports = ('7200', '7201', '7202', '7203');
my $proc_limit = '70';
foreach my $port(@ports) {
my $pid = `ps -ef | grep -v grep | grep dynamips | grep $port | tr -s ' ' | cut -d ' ' -f 2`;
if (!$pid) {
exec ("nice -n 19 $dynamips -H $port >> /dev/null 2>&1 &") or print STDERR "Error: $!";
} else {
chomp($pid);
my $pid_cpulimit = `ps -ef | grep -v grep | grep cpulimit | grep $pid | tr -s ' ' | cut -d ' ' -f 2`;
if (!$pid_cpulimit) {
exec ("/usr/bin/cpulimit --pid $pid --limit $proc_limit >> /dev/null 2>&1 &");
}
}
}
my @cpulimit = `ps -ef | grep -v grep | grep cpulimit | tr -s ' ' | cut -d ' ' -f 2`;
if ($#cpulimit > $#ports) {
chomp($cpulimit[0]);
`kill $cpulimit[0] >> /dev/null 2>&1 &`;
}
I'm using this script to have some external dynamips started all the time.
If U work as a network admin and have access to a lot of linux powerfull machines, this is perfect.
set cron as follows:
*/1 * * * * /home/ios/gns3_addon.pl
last part of the script will kill old cpulimit daemons, sometimes, when U stop all routers, dynamips daemons will die with a segmentation fault error. but the cpulimit daemon will remain.
I have the number of ports equal to the number of cores. dynamips is not a multicore program, so this is ok.
I hope this will be good for somebody.