开发者

Calling a function in Perl with different properties

开发者 https://www.devze.com 2023-02-05 17:22 出处:网络
I have written a Perl script that would start a SNMP session and extracting the data/counters and it\'s value to a csv file. There are 7 perl scripts; different properties/definition/variables on the

I have written a Perl script that would start a SNMP session and extracting the data/counters and it's value to a csv file. There are 7 perl scripts; different properties/definition/variables on the top.. but the engine is the same.

At this point, those 7 perl scripts are redundant except for the defined variables. Is there a way to keep the execution perl script as a properties/execution file and keep the engine in a another file? This properties/execution perl script will call the engine (using the properties defined in it's own script).

So in short, I want to use the variables in their own script (as an execution as well), but calls a specific function from a unified "engine".

i.e.

retrieve_mibs1.pl retrieve_mibs2.pl retrieve_mibs3.pl retrieve_mibs4.pl retrieve_mibs5.pl retrieve_mibs6.pl retrieve_mibs7.pl

retrieve_mibs1.pl

#!/usr/local/bin/perl

use Net::SNMP;

##DEFINITION START

my @Servers = (
  'server1',
  'server2',
);

my $PORT = 161;

my $COMMUNITY = 'secret';

my $BASEOID = '1.2.3.4.5.6.7.8';

my $COUNTERS = [
  [11,'TotalIncomingFromPPH'],
  [12,'TotalFailedIncomingFromPPH'],
];

##ENGINE START
sub main {
  my $stamp = gmtime();
  my @oids = ();
  foreach my $counter (@$COUNTERS) {
    push @oids,("$BASEOID.$$counter[0].0");
  }
  foreach my $server (@Servers) {
    print "$stamp$SEPARATOR$server";
    my ($session,$error) = Net::SNMP->session(-version => 1,-hostname => $server,-port => $PORT,-community => $COMMUNITY);
    if ($session) {
      my $result = $session->get_request(-varbindlist => \@oids);
      if (defined $result) {
        foreach my $oid (@oids) {
          print $SEPARATOR,$result->{$oid};
        }
      } else {
        print开发者_开发百科 STDERR "$stamp Request error: ",$session->error,"\n";
        print "$SEPARATOR-1" x scalar(@oids);
      }
    } else {
      print STDERR "$stamp Session error: $error\n";
      print "$SEPARATOR-1" x scalar(@oids);
    }
    print "\n";
  }
}
main();


You could do it using eval: set up the variables in one file, then open the engine and eval it's content.

variables.pl (set up your variables and call the engine):

use warnings;
use strict;
use Carp;
use English '-no_match_vars';

require "engine.pl"; # so that we can call it's subs

# DEFINITION START
our $VAR1    = "Hello";
our $VAR2    = "World";

# CALL THE ENGINE
print "START ENGINE:\n";
engine(); # call engine
print "DONE\n";

engine.pl (the actual working stuff):

sub engine{
    print "INSIDE ENGINE\n";
    print "Var1: $VAR1\n";
    print "Var2: $VAR2\n";
}
1;  # return a true value

Other alternatives would be:

  • pass the definitions as command line parameters directly to engine.pl and evaluate the contents of @ARGV
  • write a perl module containing the engine and use this module
  • store the parameters in a config file and read it in from your engine (e.g. using Config::IniFiles)


Two thoughts come to mind immediately:

Build a Perl module for your common code, and then require or use the module as your needs dictate. (The difference is mostly whether you want to run LynxLee::run_servers() or run_servers() -- do you want the module to influence your current scope or not.)

Use symbolic links: create these symlinks: retrieve_mibs1.pl -> retrieve_mibs.pl retrieve_mibs2.pl -> retrieve_mibs.pl, and so on, then set the variables based on the program name:

#!/usr/bin/perl -w

use File::Basename;

my $name = basename($0);

my @Servers, $PORT, $COMMUNITY, $BASEOID, $COUNTERS;

if($name ~= /retrieve_mibs1\.pl/) {
    @Servers = (
        'server1',
        'server2',
    );

    # ...
} elsif ($name ~= /retrieve_mibs2\.pl/) {
    @Servers = (
        'server3',
        'server4',
    );

    # ...
}

Indexing into a hash with the name of the program to retrieve the parameters would be much cleaner, but I'm not so good at Perl references. :)


I'm not sure what the problem is so I'm guessing a little. You have code in various places that is the same each time save for some variables. This is the very definition of a subroutine.


Maybe the problem is that you don't know how to include the common code in those various scripts. This is fairly easy: You write that code in a perl module. This is basically a file ending in pm instead of pl. Of course you have to take care of a bunch of things such as exporting your functions. Perldoc should be of great help.

0

精彩评论

暂无评论...
验证码 换一张
取 消