perl 的snmp应用,以下代码是获取服务器的运行时间。 和服务器的网卡流量

#! /usr/local/bin/perl
use strict;
use warnings;
use Net::SNMP;

my $OID_sysUpTime = '1.3.6.1.2.1.1.3.0';
my $OID_sysoutput = '1.3.6.1.2.1.2.2.1.10.2';
my $OID_sysinput = '1.3.6.1.2.1.2.2.1.16.2';

my ($session, $error) = Net::SNMP->session(
      -hostname  => shift || '192.168.1.195',
      -community => shift || 'public',
      -version     => 'snmpv2c',
);

if (!defined $session) {
      printf "ERROR: %s.\n", $error;
      exit 1;
}

my $result = $session->get_request(-varbindlist => [ $OID_sysUpTime,
                        $OID_sysoutput,$OID_sysinput],);
if (!defined $result) {
        printf "ERROR: %s.\n", $session->error();
        $session->close();
        exit 1;
}

printf "The sysUpTime for host '%s' is %s.\n",
    $session->hostname(), $result->{$OID_sysUpTime};
print "$result->{$OID_sysoutput}\t$result->{$OID_sysinput}\n";
$session->close();
exit 0;