Optimization Perl script
#!/usr/bin/perl -w
use strict;
use CGI;
package SwitchGUI;
sub new {
my ($classe, $nom, $nbports, $gio) = @_;
my $this = {
"nom" => $nom,
"nbports" => $nbports,
"gio" => $gio
};
bless($this, $classe);
$this->afficher();
return $this;
}
sub afficher {
my ($this) = @_;
my @tab = ( 1 .. $this->{nbports} );
my @odd = grep { $_ % 2 } @tab;
my @even = grep { not $_ % 2 } @tab;
my $cgi = new CGI;
my $i;
my $j;
print "<div id=\"$this->{nom}\" class=\"switch\">\n";
print $cgi->h2("$this->{nom}");
print "<div class=\"ports\">";
for my $port (@odd) {
my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
if ($res =~ /^Erreur /) {
print $cgi->img({
src => 'ressources/interface_haut_down.png',
alt => "port n°$port",
}), "\n",
}
else {
print $cgi->a({class=&g开发者_Python百科t;"tooltip", title=>$res},$cgi->img({
src => 'ressources/interface_haut_up.png',
alt => "port n°$port",
}), "\n",)
}
}
print "<br/>";
for my $port (@even) {
my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
if ($res =~ /^Erreur/) {
print $cgi->img({
src => 'ressources/interface_bas_down.png',
alt => "port n°$port",
}), "\n",
}
else {
if ($this->getDuplex($res)!="Full") {
print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
src => 'ressources/interface_bas_duplex.png',
alt => "port n°$port",
}), "\n",)
}
elsif ($this->getVitesse($res)!="100"){
print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
src => 'ressources/interface_bas_speed.png',
alt => "port n°$port",
}), "\n",)
}
else {
print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
src => 'ressources/interface_bas_up.png',
alt => "port n°$port",
}), "\n",)
}
}
}
print "</div>";
print "<div class=\"gio\">";
for ($j=0;$j<$this->{gio};$j++) {
my $req = system("perl ifname-index.pl -h $this->{nom} -i GigabitEthernet0/$j -c reseau &");
print $cgi->img({
src => 'ressources/interface_bas_down.png',
alt => "port",
});
}
print "</div>\n";
print "</div>\n";
}
1;
It executes a perl script (which uses SNMP to query network equipment), and depending of the return of this script, it displays an appropriate image and description. This script is used for ajax call, from another cgi script.
My question is: can I execute multiple script by adding &
or something similar
at the end of the following line?
my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
While i don't want comment much things like using CGI and "print" (in 2011 is really archaic), I will comment two lines:
my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
...
my $req = system("perl ifname-index.pl -h $this->{nom} -i GigabitEthernet0/$j -c reseau &");
Starting another perl-processes really slowing speed down.
You're making package for displaying HTML, but not for polling?
Re-factor ifname-index.pl
to subroutine. So,
my $res = get_request_interface(name => $this->{nom}, interface => "FastEthernet0/$port");
or to an package (the right way) - something like...
my $interface = My::Interface::Handler->new();
my $res = $interface->get_request;
...
my $another_result = $interface->get_request;
#etc
And ofc, it is possible start more (multiple) processes and communicate with them, but the solution will be probably more complicated than refactoring ifname-index.pl to subroutine. (read this: http://faq.perl.org/perlfaq8.html#How_do_I_start_a_pro)
Summarization for a "cool" app - based on comments:
- build a web page where you list the interfaces, for example N-status lines for N ports
- the page will send N ajax (parallel) requests to the server for the status with javascript
- the server will execute N parallel SNMP requests, and send N ajax responses
- the page will get responses from the server and update the correct divs
With above way:
- the user get immediately an web page
- the page has a feedback for user - "wait, i'm working on getting status"
- the server executing N parallel requests to snmp
- ajax responses updating the page as they come from the server
For the web part is the best to use PGSI-type server. Check CPAN, several one exists.
Tatsuhiko Miyagawa
is "The Perl Hero" for these days :)
Ps:
- http://www.perlcritic.org
- http://onyxneon.com/books/modern_perl/
精彩评论