#!/usr/bin/perl -w
###############################################################################
#
# Webmin Sysstats Module
# Copyright (C) 2002 by Eric Gerbier
# Bug reports to: gerbier@users.sourceforge.net
# $Id$
#
# 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.
#
###############################################################################
use strict;
use warnings;
no warnings 'redefine';
use RRDs;
use English qw(-no_match_vars);
use FindBin;
# to be used alone or from sysstat.pl without warning
# because require verify the path
if ( !exists $ENV{WEBMINSTAT_TEMP} ) {
use lib "$FindBin::Bin/../..";
## no critic(RequireBarewordIncludes)
require 'sysstats-aquisition-lib.pl';
## use critic
}
my $module_name;
my %config;
read_file_cached( 'config', \%config );
$module_name = $config{'name'};
debug( "run $module_name version " . $config{'version'} );
my %codes = action_load( \%config );
# the code is written to accept 2 ways to get snmp info :
# - perl code (using Net::SNMP module)
# - external (shell) command : snmpget
# we can force the use with a line in config file
# or let the code find it's way (default method)
my $pm_flag;
if ( exists $config{'pm_flag'} ) {
# get method from config file
$pm_flag = $config{'pm_flag'};
debug("from config, pm_flag = $pm_flag\n");
}
else {
# search for perl module
unset_warnings();
eval { require Net::SNMP };
set_warnings();
if ($EVAL_ERROR) {
$pm_flag = 0;
debug("use smpget command : $EVAL_ERROR\n");
}
else {
$pm_flag = 1;
debug("use perl module Net::SNMP\n");
import Net::SNMP;
}
}
if ($pm_flag) {
# import perl module if necessary
require Net::SNMP;
import Net::SNMP;
}
else {
# check for snmp command
my $snmpget = search_command('snmpget');
if ( !$snmpget ) {
warning('snmget command not found');
goto FIN;
}
}
my $pre = $config{'pre'};
my $snmp_num = 1;
while ( exists $config{ $pre . $snmp_num } ) {
my @tab = split /,/, $config{ $pre . $snmp_num };
my $real_snmp_num = $tab[0]; # database number
my $snmp_name = $tab[1]; # display name
my $snmp_field = $tab[2]; # field to get (oid)
my $snmp_commu = $tab[3]; # communauty : public or so
my $snmp_host = $tab[4]; # hostname or ip adresse
# tab[5] is the colour
my $runstop = $tab[6]; # control parameter aquisistion
#debug( "snmp = $snmp");
my $result = 'U';
if ( is_param_runstop($runstop) ) {
if ($pm_flag) {
# perl code
my ( $session, $error ) = Net::SNMP->session(
-hostname => $snmp_host || 'localhost',
-community => $snmp_commu || 'public',
-translate => 0
);
if ( !defined $session ) {
warning("cannot open snmp session: $error\n");
next;
}
my $res = $session->get_request( -varbindlist => [$snmp_field] );
if ( !defined $res ) {
warning("cannot get snmp field $snmp_field : "
. $session->error
. "\n" );
$session->close;
next;
}
$result = $res->{$snmp_field};
$session->close;
}
else {
# shell code
# -Ov : Print values only (not OID = value).
my $cmd = "snmpget -v1 -Ov -c $snmp_commu $snmp_host $snmp_field"
. stderr_debug();
## no critic (ProhibitBacktickOperators)
my $snmp_result = `$cmd`;
if ( $snmp_result =~ m/(.*):\s(.*)/ ) {
# ex : Counter32: 0
my $type = $1;
$result = $2;
}
elsif ( $snmp_result =~ m/=\s(.*)/ ) {
# ex : 1500
$result = $1;
}
}
debug(
"$module_name ($snmp_name $real_snmp_num $snmp_host $snmp_commu $snmp_field): total=$result"
);
action_param( $snmp_name, $result, \%codes );
}
else {
debug("aquisition for $real_snmp_num stopped");
}
my $rrdbase = $real_snmp_num . '.rrd';
RRDs::update( $rrdbase, "N:$result" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
$snmp_num++;
}
# a label to quit
# #I can not use return when executing this script standalone,
# # so I had to use goto
# FIN: