2013-07-15 14:07:10 +02:00
|
|
|
#!/usr/bin/perl
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
# SNMP remote plugin
|
|
|
|
# Depending on the configuration returns the result of these modules:
|
|
|
|
# - % Memory Use
|
|
|
|
# - % CPU Use
|
|
|
|
# - % Disk Use
|
|
|
|
# - Show if a process is running or not
|
|
|
|
#
|
2023-06-23 23:37:27 +02:00
|
|
|
# Pandora FMS
|
2013-07-15 14:07:10 +02:00
|
|
|
# Copyright (C) 2013 mario.pulido@artica.es
|
|
|
|
#
|
|
|
|
# License: GPLv2+
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# GPL License: http://www.gnu.org/licenses/gpl.txt
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
# Revised by Kevin Rojas 2020 kevin.rojas@pandorafms.com
|
|
|
|
# - Added check for Module options
|
|
|
|
# - Optimized SNMP v3 code
|
|
|
|
# - Improved Help
|
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Getopt::Std;
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
|
|
|
|
my $VERSION = 'v1r2';
|
2013-07-15 14:07:10 +02:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
# HELP
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
if ($#ARGV == -1 ) {
|
2020-03-20 10:03:42 +01:00
|
|
|
print "SNMP remote plugin ($VERSION). Retrieves information via SNMP for:\n";
|
|
|
|
print "\t- Memory Usage (%)\n";
|
|
|
|
print "\t- CPU Usage (%)\n";
|
|
|
|
print "\t- Disk Usage (%)\n";
|
|
|
|
print "\t- Whether a process is running (1) or not (0)\n";
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
print "Arguments:\n\n";
|
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
print "-H, --host=STRING\n";
|
|
|
|
print "\tHost IP\n";
|
|
|
|
|
|
|
|
print "-c, --community=STRING\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
print "\tSNMP Community\n";
|
2014-07-23 14:11:36 +02:00
|
|
|
|
|
|
|
print "-m, --module=STRING\n";
|
|
|
|
print "\tDefine module (memuse|diskuse|process|cpuload) \n";
|
|
|
|
|
|
|
|
print "-d, --disk=STRING\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
print "\t[Only for diskuse module] Define disk name (C:, D: in Windows) or mount point (Linux). Default: /\n";
|
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
print "-p, --process=STRING\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
print "\t[Only for process module] Process or service name\n\n";
|
2014-07-23 14:11:36 +02:00
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
print "SNMP v3 options:\n";
|
|
|
|
print "\n";
|
|
|
|
|
2017-10-05 12:12:59 +02:00
|
|
|
print "-v, --version=STRING\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
print "\tSNMP version (Default 2c)\n";
|
2014-07-23 14:11:36 +02:00
|
|
|
|
|
|
|
print "-u, --user=STRING\n";
|
|
|
|
print "\tAuth user\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
print "-l, --level=STRING\n";
|
|
|
|
print "\tSecurity level\n";
|
|
|
|
|
|
|
|
print "-a STRING\n";
|
|
|
|
print "\tAuth method\n";
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
print "-A, --auth=STRING\n";
|
|
|
|
print "\tAuth pass\n";
|
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
print "-x STRING\n";
|
|
|
|
print "\tPrivacy method\n";
|
|
|
|
|
|
|
|
print "-X STRING\n";
|
|
|
|
print "\tPrivacy pass\n";
|
|
|
|
|
|
|
|
print "\n";
|
2020-03-20 10:03:42 +01:00
|
|
|
print "Usage: \n";
|
|
|
|
print "SNMP v1|2|2c:\n" ;
|
|
|
|
print "\tperl $0 -H host -c community -m (memuse|diskuse|process|cpuload) [-p process -d disk] \n" ;
|
|
|
|
print "SNMP v3:\n" ;
|
|
|
|
print "\tperl $0 -H host -v 3 -u user -l seclevel -a authMethod -A authPass -x privMethod -X privPass -m (memuse|diskuse|process|cpuload) [-p process -d disk] \n" ;
|
|
|
|
print "\n" ;
|
2014-07-23 14:11:36 +02:00
|
|
|
exit;
|
2013-07-15 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 16:24:27 +01:00
|
|
|
|
|
|
|
my (
|
|
|
|
$host, # $opts{"H"}
|
|
|
|
$community, # $opts{"c"}
|
|
|
|
$module, # $opts{"m"}
|
|
|
|
$disk, # $opts{"d"}
|
|
|
|
$process, # $opts{"p"}
|
|
|
|
$version, # $opts{"v"}
|
|
|
|
$user, # $opts{"u"}
|
|
|
|
$pass, # $opts{"A"}
|
|
|
|
$security_level, # $opts{"l"}
|
|
|
|
$auth_method, # $opts{"a"}
|
|
|
|
$privacy_method, # $opts{"x"}
|
|
|
|
$privacy_pass # $opts{"X"}
|
|
|
|
) = &options;
|
2013-07-15 14:07:10 +02:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
# OPTIONS
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2013-07-15 14:07:10 +02:00
|
|
|
sub options {
|
2014-07-23 14:11:36 +02:00
|
|
|
|
|
|
|
# Get and check args
|
|
|
|
my %opts;
|
|
|
|
getopt( 'HcmdpvuAlaxX', \%opts );
|
|
|
|
|
|
|
|
#~ ' -u ' . $snmp3_auth_user .
|
|
|
|
#~ ' -A ' . $snmp3_auth_pass .
|
|
|
|
#~ ' -l ' . $snmp3_security_level .
|
|
|
|
#~ ' -a ' . $snmp3_auth_method .
|
|
|
|
#~ ' -x ' . $snmp3_privacy_method .
|
|
|
|
#~ ' -X' $snmp3_privacy_pass;
|
|
|
|
|
2015-03-03 16:24:27 +01:00
|
|
|
# host
|
|
|
|
$opts{"H"} = 0 unless ( exists( $opts{"H"} ) );
|
|
|
|
# community
|
|
|
|
$opts{"c"} = 0 unless ( exists( $opts{"c"} ) );
|
|
|
|
# module
|
|
|
|
$opts{"m"} = 0 unless ( exists( $opts{"m"} ) );
|
|
|
|
# disk
|
|
|
|
$opts{"d"} = "/" unless ( exists( $opts{"d"} ) );
|
|
|
|
# process
|
|
|
|
$opts{"p"} = 0 unless ( exists( $opts{"p"} ) );
|
|
|
|
# version
|
2017-10-05 12:12:59 +02:00
|
|
|
$opts{"v"} = "2c" unless ( exists( $opts{"v"} ) );
|
2015-03-03 16:24:27 +01:00
|
|
|
# user
|
|
|
|
$opts{"u"} = "" unless ( exists( $opts{"u"} ) );
|
|
|
|
# auth_pass
|
|
|
|
$opts{"A"} = "" unless ( exists( $opts{"A"} ) );
|
|
|
|
# security level
|
|
|
|
$opts{"l"} = "noAuthNoPriv" unless ( exists( $opts{"l"} ) );
|
|
|
|
# auth method
|
|
|
|
$opts{"a"} = "" unless ( exists( $opts{"a"} ) );
|
|
|
|
# privacy method
|
|
|
|
$opts{"x"} = "" unless ( exists( $opts{"x"} ) );
|
|
|
|
# privacy pass
|
|
|
|
$opts{"X"} = "" unless ( exists( $opts{"X"} ) );
|
2014-07-23 14:11:36 +02:00
|
|
|
|
2015-03-03 16:24:27 +01:00
|
|
|
return (
|
|
|
|
$opts{"H"},
|
2014-07-23 14:11:36 +02:00
|
|
|
$opts{"c"},
|
|
|
|
$opts{"m"},
|
|
|
|
$opts{"d"},
|
2015-03-03 16:24:27 +01:00
|
|
|
$opts{"p"},
|
2014-07-23 14:11:36 +02:00
|
|
|
$opts{"v"},
|
|
|
|
$opts{"u"},
|
|
|
|
$opts{"A"},
|
|
|
|
$opts{"l"},
|
|
|
|
$opts{"a"},
|
|
|
|
$opts{"x"},
|
|
|
|
$opts{"X"});
|
2013-07-15 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
unless ( $module ~~ ["memuse","cpuload","process","diskuse"] ){
|
|
|
|
print "Error: Invalid or missing argument (-m).\n";
|
|
|
|
print "Available options: memuse | diskuse | process | cpuload \n\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ($host){
|
|
|
|
print "Error: missing host address.\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
# SNMP Version parameters
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
my $command_line_parameters;
|
|
|
|
if ($version == "3") {
|
2020-08-31 10:37:52 +02:00
|
|
|
if (lc($security_level) eq lc('authNoPriv')) {
|
|
|
|
$command_line_parameters = "-v $version -u $user -a $auth_method -A '$pass' -l $security_level $host";
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
2020-08-31 10:37:52 +02:00
|
|
|
elsif (lc($security_level) eq lc("AuthPriv")) {
|
|
|
|
$command_line_parameters = "-v $version -u $user -a $auth_method -A '$pass' -l $security_level -x $privacy_method -X '$privacy_pass' $host";
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-03-20 10:03:42 +01:00
|
|
|
$command_line_parameters = "-v $version -u $user -l $security_level $host";
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 10:03:42 +01:00
|
|
|
else {
|
|
|
|
$command_line_parameters = "-v $version -c $community $host";
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
# Memory use % module
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
if ($module eq "memuse") {
|
|
|
|
my $memuse = 0;
|
2014-07-23 14:11:36 +02:00
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
my $memid = `snmpwalk -On $command_line_parameters .1.3.6.1.2.1.25.2.3.1.3 | grep Physical | head -1 | gawk '{print \$1}' | gawk -F "." '{print \$13}' | tr -d "\r"`;
|
|
|
|
my @memtot = split(/\s/, `snmpget $command_line_parameters .1.3.6.1.2.1.25.2.3.1.5.$memid `) ;
|
|
|
|
my @memfree = split(/\s/, `snmpget $command_line_parameters .1.3.6.1.2.1.25.2.3.1.6.$memid `) ;
|
|
|
|
|
|
|
|
if ($memid){
|
|
|
|
$memuse = ($memfree[-1]) * 100 / $memtot[-1];
|
2014-07-23 14:11:36 +02:00
|
|
|
printf("%.2f", $memuse);
|
2020-03-20 10:03:42 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
print STDOUT "-1";
|
|
|
|
print STDERR "Error: Memory OID not found";
|
|
|
|
}
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
2013-07-15 14:07:10 +02:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
# Disk use % module
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
if ($module eq "diskuse") {
|
|
|
|
my $diskuse = 0;
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
unless ($disk){
|
|
|
|
print "Error: Invalid or missing argument (-d).\n";
|
|
|
|
exit;
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
2017-10-05 12:12:59 +02:00
|
|
|
|
2018-01-22 15:50:01 +01:00
|
|
|
if ($disk =~ /\\ /) {
|
2017-10-05 12:12:59 +02:00
|
|
|
$disk =~ s/\\/\\\\/g;
|
|
|
|
}
|
2020-03-20 10:03:42 +01:00
|
|
|
my $diskid = `snmpwalk -r 2 -On $command_line_parameters .1.3.6.1.2.1.25.2.3.1.3 | grep -F '$disk' | head -1 | gawk '{print \$1}' | gawk -F "." '{print \$13}' | tr -d "\r"`;
|
|
|
|
my @disktot= split /\s/, `snmpget -r 2 $command_line_parameters .1.3.6.1.2.1.25.2.3.1.5.$diskid` ;
|
|
|
|
|
|
|
|
if ($diskid == ""){
|
|
|
|
print STDOUT "-1";
|
|
|
|
print STDERR "Error: Disk or partition not found\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if ($disktot[-1] == 0) {
|
2014-07-23 14:11:36 +02:00
|
|
|
$diskuse = 0;
|
|
|
|
}
|
|
|
|
else {
|
2021-03-16 10:14:28 +01:00
|
|
|
# hrStorageAllocationUnits
|
|
|
|
my @diskUsed = split (/\s/, `snmpget -r 2 $command_line_parameters .1.3.6.1.2.1.25.2.3.1.6.$diskid`) ;
|
2020-03-20 10:03:42 +01:00
|
|
|
|
2021-03-16 10:14:28 +01:00
|
|
|
$diskuse = ($diskUsed[-1] * 100) / $disktot[-1];
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%.2f", $diskuse);
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
# Process Status module
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
if ($module eq "process") {
|
|
|
|
my $status = 0;
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
$status = `snmpwalk $command_line_parameters .1.3.6.1.2.1.25.4.2.1.2 2>/dev/null`;
|
2018-01-22 15:50:01 +01:00
|
|
|
|
|
|
|
if ($? == 0) {
|
|
|
|
print (($status =~ m/$process/mi)?1:0);
|
|
|
|
}
|
2014-07-23 14:11:36 +02:00
|
|
|
}
|
2013-07-15 14:07:10 +02:00
|
|
|
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
2020-03-20 10:03:42 +01:00
|
|
|
# CPU Load % module
|
2014-07-23 14:11:36 +02:00
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
if ($module eq "cpuload") {
|
|
|
|
my $cputotal = 0;
|
|
|
|
|
2020-03-20 10:03:42 +01:00
|
|
|
my $cpuload = `snmpwalk $command_line_parameters .1.3.6.1.2.1.25.3.3.1.2 | gawk '{print \$4}' `;
|
2014-07-23 14:11:36 +02:00
|
|
|
my @cpuload = split(/\n/, $cpuload);
|
|
|
|
my $sum;
|
|
|
|
my $counter = 0;
|
|
|
|
foreach my $val(@cpuload) {
|
|
|
|
$sum = $sum + $val;
|
|
|
|
$counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cputotal = $sum/$counter;
|
|
|
|
|
|
|
|
print $cputotal;
|
|
|
|
}
|
2013-07-15 14:07:10 +02:00
|
|
|
|