From 53a95359757524c974e4db6f11216af58b155cb3 Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Wed, 19 May 2010 17:03:16 +0000 Subject: [PATCH] 2010-05-19 Sergio Martin * util/plugin/snmp_process.pl: Added the SNMP Server Plugin to Server. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2756 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 5 + pandora_server/util/plugin/snmp_process.pl | 184 +++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100755 pandora_server/util/plugin/snmp_process.pl diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 45f6acd77a..b670b07642 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,8 @@ +2010-05-19 Sergio Martin + + * util/plugin/snmp_process.pl: Added the SNMP Server + Plugin to Server. + 2010-05-19 Ramon Novoa * lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm, diff --git a/pandora_server/util/plugin/snmp_process.pl b/pandora_server/util/plugin/snmp_process.pl new file mode 100755 index 0000000000..db3d846df3 --- /dev/null +++ b/pandora_server/util/plugin/snmp_process.pl @@ -0,0 +1,184 @@ +#!/usr/bin/perl +################################################################################## +# SNMP Plugin for Pandora FMS 2.0 +# (c) Sergio Martin 2010, sergio.martin@artica.es +# +# 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; version 2 +# 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. +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +################################################################################## + +my $cfg_remote_host = ""; +my $cfg_password = ""; +my $cfg_process = ""; +my $cfg_type = ""; +my $cfg_quiet = ""; +my $OID; + +use strict; +use Getopt::Std; + +# ------------------------------------------------------------------------------------------ +# This function show a brief doc. +# ------------------------------------------------------------------------------------------ +sub help { + print "SNMP Plugin for Pandora FMS 2.0, (c) Sancho Lerena 2008 \n"; + print "Syntax: \n\n"; + print "\t -i \n\t -c \n\t -p \n\t -t \n\t -q\n"; + print "\n"; +} + +# ------------------------------------------------------------------------------------------ +# Print an error and exit the program. +# ------------------------------------------------------------------------------------------ +sub error { + if ($cfg_quiet == 0) { + print (STDERR "[err] $_[0]\n"); + } + exit 1; +} + + +# ------------------------------------------------------------------------------------------ +# Read configuration from commandline parameters +# ------------------------------------------------------------------------------------------ +sub config { + my %opts; + my $tmp; + + # Get options + if (getopts ('i:c:p:t:hq', \%opts) == 0 || defined ($opts{'h'})) { + help (); + exit 1; + } + + # Address + if (defined ($opts{'i'})) { + $cfg_remote_host = $opts{'i'}; + if ($cfg_remote_host !~ /^[a-zA-Z\.]+$/ && ($cfg_remote_host !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ + || $1 < 0 || $1 > 255 || $2 < 0 || $2 > 255 + || $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255)) { + error ("Address $cfg_remote_host is not valid."); + } + } + + # Password + if (defined ($opts{'c'})) { + $cfg_password = $opts{'c'}; + } + + # Process name + if (defined ($opts{'p'})) { + $cfg_process = $opts{'p'}; + } + + # Type of query + if (defined ($opts{'t'})) { + $cfg_type = $opts{'t'}; + if (($cfg_type ne "status") && ($cfg_type ne "cpu") && ($cfg_type ne "mem")){ + error ("Type $cfg_type is not valid."); + } + } + + # Quiet mode + if (defined ($opts{'q'})) { + $cfg_quiet = 1; + } + + if ($cfg_remote_host eq ""){ + error ("You need to define remote host to use this plugin"); + } + + my $snmpoid_execution = "snmpwalk -Os -c $cfg_password -v 1 $cfg_remote_host hrSWRunName | grep $cfg_process | awk '{print \$1}' | awk -F. '{print \$2}' | tail -1"; + + $OID = `$snmpoid_execution`; + + chomp($OID); +} + +# ------------------------------------------------------------------------------------------ +# This function get process status +# ------------------------------------------------------------------------------------------ + +sub get_status { + my $output; + eval { + my $snmpstatus_execution = "snmpwalk -Os -c $cfg_password -v 1 $cfg_remote_host hrSWRunStatus.$OID 2>/dev/null | awk '{print \$4}' | awk -F'(' '{print \$2}' | awk -F')' '{print \$1}'"; + + $output = `$snmpstatus_execution`; + + chomp($output); + + if($output eq '2') { + $output = "1\n"; + } + else { + $output = "0\n"; + } + }; + return $output; +} + +# ------------------------------------------------------------------------------------------ +# This function get CPU consumption +# ------------------------------------------------------------------------------------------ + +sub get_cpu { + my $output; + eval { + my $snmpcpu_execution = "snmpwalk -Os -c $cfg_password -v 1 $cfg_remote_host hrSWRunPerfCPU.$OID 2>/dev/null | awk '{print \$4}'"; + + $output = `$snmpcpu_execution`; + + if($output eq "") { + $output = "0\n"; + } + }; + return $output; +} + +# ------------------------------------------------------------------------------------------ +# This function get Memory consumption +# ------------------------------------------------------------------------------------------ + +sub get_memory { + my $output; + eval { + my $snmpmemory_execution = "snmpwalk -Os -c $cfg_password -v 1 $cfg_remote_host hrSWRunPerfMem.$OID 2>/dev/null | awk '{print \$4}'"; + + my $snmpunits_execution = "snmpwalk -Os -c $cfg_password -v 1 $cfg_remote_host hrSWRunPerfMem.$OID 2>/dev/null | awk '{print \$5}'"; + + my $mem = `$snmpmemory_execution`; + my $units = `$snmpunits_execution`; + + chomp($mem); + $output = $mem.' '.$units; + + if($output eq " ") { + $output = "0 KBytes\n"; + } + }; + return $output; +} +# ------------------------------------------------------------------------------------------ +# Main program +# ------------------------------------------------------------------------------------------ + + config(); + if ($cfg_type eq "status") { + print get_status(); + } + if ($cfg_type eq "mem") { + print get_memory(); + } + if ($cfg_type eq "cpu") { + print get_cpu(); + } + exit;