"Fossies" - the Fresh Open Source Software Archive

Member "pandora_server/util/plugin/wizard_snmp_process.pl" (15 Sep 2021, 7187 Bytes) of package /linux/misc/pandorafms_server-7.0NG.757.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wizard_snmp_process.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl
    2 #
    3 ################################################################################
    4 #
    5 # SNMP wizard process
    6 # 
    7 # Requirements:
    8 #   Net::SNMP
    9 #   Crypt::DES
   10 #   Digest::SHA1
   11 # 
   12 # (c) Enrique Martin Garcia <enrique.martin@artica.es>
   13 #
   14 # 2020/03/10
   15 #
   16 ################################################################################
   17 
   18 use strict;
   19 use warnings;
   20 
   21 use POSIX qw(strftime);
   22 
   23 use Encode;
   24 use Encode::Locale;
   25 use Getopt::Long;
   26 use Net::SNMP;
   27 use Net::SNMP::Security::USM;
   28 
   29 my $HELP=<<EO_HELP;
   30 
   31 Check if a process is running (1) or not (0) in OID .1.3.6.1.2.1.25.4.2.1.2 SNMP tree.
   32 
   33 Usage: $0 -host "<ip_address>" -version "<snmp_version>" [SNMP] [SNMPv3] -process "<process_name>"
   34 
   35 -host                Target host
   36 -version             SNMP version (1, 2c, 3)
   37 
   38 -process             Process name to check if is running (case sensitive)
   39 
   40 [SNMP]
   41     -community       Community (only version 1 and 2c)
   42     -port            Target UDP port (Default 161)
   43 
   44 [SNMPv3]
   45     -user            Username
   46     -authMethod      Authentication method (MD5, SHA)
   47     -authPass        Authentication password
   48     -privMethod      Privacy method (DES, AES)
   49     -privPass        Privacy password
   50     -secLevel        Security level (noAuthNoPriv, authNoPriv, authPriv)
   51 
   52 Example: $0 -host 192.168.80.43 -community public -version 1 -process "httpd"
   53 
   54 EO_HELP
   55 
   56 #
   57 # FUNCTIONS
   58 ##############
   59 
   60 sub new_snmp_target {
   61     my ($config) = @_;
   62     my $target;
   63     my $error;
   64 
   65     if ($config->{'version'} ne '3'){
   66         ($target, $error) = Net::SNMP->session(
   67             -hostname      => $config->{'host'},
   68             -port          => $config->{'port'},
   69             -version       => $config->{'version'},
   70             -timeout       => $config->{'timeout'},
   71             -translate     => 0,
   72             -community     => $config->{'community'}
   73         );      
   74     }else{
   75         if ($config->{'sec_level'} =~ /^noAuthNoPriv$/i){
   76             ($target, $error) = Net::SNMP->session(
   77                 -hostname      => $config->{'host'},
   78                 -port          => $config->{'port'},
   79                 -version       => $config->{'version'},
   80                 -timeout       => $config->{'timeout'},
   81                 -translate     => 0,
   82                 -username      => $config->{'user'}
   83             );
   84         }elsif ($config->{'sec_level'} =~ /^authNoPriv$/i){
   85             ($target, $error) = Net::SNMP->session(
   86                 -hostname      => $config->{'host'},
   87                 -port          => $config->{'port'},
   88                 -version       => $config->{'version'},
   89                 -timeout       => $config->{'timeout'},
   90                 -translate     => 0,
   91                 -username      => $config->{'user'},
   92                 -authpassword  => $config->{'auth_pass'},
   93                 -authprotocol  => $config->{'auth_method'}
   94             );
   95         }elsif ($config->{'sec_level'} =~ /^authPriv$/i){
   96             ($target, $error) = Net::SNMP->session(
   97                 -hostname      => $config->{'host'},
   98                 -port          => $config->{'port'},
   99                 -version       => $config->{'version'},
  100                 -timeout       => $config->{'timeout'},
  101                 -translate     => 0,
  102                 -username      => $config->{'user'},
  103                 -authpassword  => $config->{'auth_pass'},
  104                 -authprotocol  => $config->{'auth_method'},
  105                 -privpassword  => $config->{'priv_pass'},
  106                 -privprotocol  => $config->{'priv_method'}
  107             );
  108         }
  109     }
  110 
  111 
  112     return ($target, $error);
  113 }
  114 
  115 sub snmp_walk {
  116     my ($target, $oid) = @_;
  117     my $result = {};
  118 
  119     my $walk = $target->get_table(
  120         -baseoid => $oid,
  121     );
  122 
  123     if (defined($walk)){
  124         $result = $walk;
  125     }
  126 
  127     return $result;
  128 }
  129 
  130 sub snmp_get {
  131     my ($target, $oid) = @_;
  132     my $result = '';
  133 
  134     my $get = $target->get_request(
  135         -varbindlist => [$oid],
  136     );
  137 
  138     if (defined($get)){
  139         $result = $get->{$oid};
  140     }
  141 
  142     return $result;
  143 }
  144 
  145 #
  146 # MAIN
  147 ##############
  148 
  149 @ARGV = map { decode(locale => $_, 1) } @ARGV if -t STDIN;
  150 binmode STDOUT, ":encoding(console_out)" if -t STDOUT;
  151 binmode STDERR, ":encoding(console_out)" if -t STDERR;
  152 
  153 my %Param = ();
  154 GetOptions(
  155     \%Param,
  156     # General
  157     'community=s',
  158     'version=s',
  159     'host=s',
  160     'port=s',
  161     'timeout=s',
  162     # Version 3
  163     'user=s',
  164     'authMethod=s',
  165     'authPass=s',
  166     'privMethod=s',
  167     'privPass=s',
  168     'secLevel=s',
  169     # Process
  170     'process=s',
  171     # Help option
  172     'Help',
  173 );
  174 
  175 if ($Param{Help}){
  176     print $HELP;
  177     exit 0;
  178 }
  179 
  180 my $config;
  181 
  182 # General parameters
  183 $config->{'community'}   = $Param{community}  || '';
  184 $config->{'version'}     = $Param{version}    || '';
  185 $config->{'host'}        = $Param{host}       || '';
  186 $config->{'port'}        = $Param{port}       || '161';
  187 $config->{'timeout'}     = $Param{timeout}    || '2';
  188 
  189 # Version 3 parameters
  190 $config->{'auth_method'} = $Param{authMethod} || '';
  191 $config->{'user'}        = $Param{user}       || '';
  192 $config->{'auth_pass'}   = $Param{authPass}   || '';
  193 $config->{'priv_method'} = $Param{privMethod} || '';
  194 $config->{'priv_pass'}   = $Param{privPass}   || '';
  195 $config->{'sec_level'}   = $Param{secLevel}   || '';
  196 
  197 $config->{'auth_method'} = uc($config->{'auth_method'});
  198 $config->{'priv_method'} = uc($config->{'priv_method'});
  199 
  200 # Operation
  201 my $process = $Param{process}  || '';
  202 
  203 # Verify parameters
  204 if (!$config->{'host'} || !$config->{'version'} || !$process){
  205     print $HELP;
  206     print "Host, version and process are required.\n";
  207     exit 1;
  208 }
  209 
  210 if ($config->{'version'} ne '1' && $config->{'version'} ne '2c' && $config->{'version'} ne '3'){
  211     print $HELP;
  212     print "Invalid SNMP version provided.\n";
  213     exit 1;
  214 }
  215 
  216 if ($config->{'version'} eq '1' || $config->{'version'} eq '2c'){
  217     if (!$config->{'community'}){
  218         print $HELP;
  219         print "SNMP community required for version 1 or 2c.\n";
  220         exit 1;
  221     }
  222 }
  223 
  224 if ($config->{'version'} eq '3'){
  225     if ($config->{'sec_level'} =~ /^noAuthNoPriv$/i){
  226         if (!$config->{'user'}){
  227             print $HELP;
  228             print "Username required for SNMP version 3 and security level 'noAuthNoPriv'.\n";
  229             exit 1;
  230         }
  231     }elsif ($config->{'sec_level'} =~ /^authNoPriv$/i){
  232         if (!$config->{'user'} && !$config->{'auth_pass'} && !$config->{'auth_method'}){
  233             print $HELP;
  234             print "Username, authentication password and authentication method required for SNMP version 3 and security level 'authNoPriv'.\n";
  235             exit 1;
  236         }
  237     }elsif ($config->{'sec_level'} =~ /^authPriv$/i){
  238         if (!$config->{'user'} && !$config->{'auth_pass'} && !$config->{'auth_method'} && !$config->{'priv_pass'} && !$config->{'priv_method'}){
  239             print $HELP;
  240             print "Username, authentication password, authentication method, privacy password and privacy method required for SNMP version 3 and security level 'authPriv'.\n";
  241             exit 1;
  242         }
  243     }else{
  244         print $HELP;
  245         print "Invalid SNMP security level provided for version 3.\n";
  246         exit 1;
  247     }
  248 
  249     if ($config->{'auth_method'} && $config->{'auth_method'} ne 'MD5' && $config->{'auth_method'} ne 'SHA'){
  250         print $HELP;
  251         print "Invalid SNMP authentication method provided for version 3.\n";
  252         exit 1;
  253     }
  254 
  255     if ($config->{'priv_method'} && $config->{'priv_method'} ne 'DES' && $config->{'priv_method'} ne 'AES'){
  256         print $HELP;
  257         print "Invalid SNMP privacy method provided for version 3.\n";
  258         exit 1;
  259     }
  260 }
  261 
  262 # Create SNMP target
  263 my ($target, $error) = new_snmp_target($config);
  264 
  265 if (!$target){
  266     print $error . "\n";
  267     exit 1;
  268 }
  269 
  270 # Get all running processes
  271 my $processes = snmp_walk($target, '.1.3.6.1.2.1.25.4.2.1.2');
  272 
  273 my $result = 0;
  274 
  275 # Search process name
  276 foreach my $k (keys %{$processes}){
  277     if ($processes->{$k} eq $process){
  278         $result = 1;
  279         last;
  280     }
  281 }
  282 
  283 print $result . "\n";