1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
| #! /usr/bin/perl -w
# Author: Martin Fuerstenau, Oce Printing Systems
# martin.fuerstenau_at_oce.com or Martin.fuerstenau_at_nagiossw.org
#
# Date: 23 Jul 2011
#
#
# Purpose and features of the program:
#
# - Get the network usage for Windows, Solaris and Linux servers.
#
# History and Changes:
#
# - 23 Jul 2011 Version 1
# - First released version
#
# - 29 Sep 2011 Version 1.1
# - Corrected minor bug (String xxx in output)
# - Enhanced for NetApp network interfaces
#
# - 9 Oct 2012 Version 1.2
# - Corrections for Windows 2012 server.
#
use strict;
use Getopt::Long;
use Net::SNMP;
my $ProgName = "check_int_traffic";
my $help;
my $hostname; # hostname
my $host; # Valid hostname
my $snmpport; # SNMP port
my $snmpport_def = "161"; # SNMP port default
my $os; # To store the operating system name
my $sysDescr; # Contains the system description. Needed to decide
# whether it is Solaris, a Linux or Windows
my $sysObject; # $sysObject and $sysObjectID are needed for Linux
my $sysObjectID; # only to determine whether it is an old ucd-snmp or net-snmp
my $ucdintcnt = 0; # Interface counter f. ucd-snmp. ucd-snmp doesn't report
# eth0, eth1 etc.. It reports only eth. But we need a unique
# name. So as a workaround this counter is added
my $out; # Contains the output
my $perfout; # Contains the performance output
my $firstloop = 0; # To determine whether it is the first loop run or not
my $IntIdx; # Interface Index
my $IntDescr; # Contains the interface name (description)
my $oid2descr; # Gets the result of the get_table for description
my $descr_oid = '1.3.6.1.2.1.2.2.1.2'; # Base OID for description
my $in_octet; # Contains the incoming octets
my $oid2in_octet; # Gets the result of the get_table for incoming octets
my $in_octet_oid = '1.3.6.1.2.1.2.2.1.10'; # Base OID for description
my $out_octet; # Contains the outgoing octets
my $oid2out_octet; # Gets the result of the get_table for outgoing octets
my $out_octet_oid = '1.3.6.1.2.1.2.2.1.16'; # Base OID for description
my $oper; # Contains the interface operational status
my $oid2oper; # Gets the result of the get_table for operational status
my $oper_oid = '1.3.6.1.2.1.2.2.1.8'; # Base OID for description
my %InterfaceStat = (
"1" => "up",
"2" => "down",
"3" => "testing",
"4" => "unknown",
"5" => "dormant",
"6" => "notPresent",
"7" => "lowerLayerDown"
); # Get the operational status of the interface Enumerations:
my ($session,$error); # Needed to establish the session
my $key; # Needed in the loops. Contains various OIDs
my $snmpversion; # SNMP version
my $snmpversion_def = 1; # SNMP version default
my $community; # community
my $oid2get; # To store the OIDs
my $IntDownAlert; # Alarm if interface is down. Default is no alarm
my $IsDown = 0; # Used in loop. Switched to 1 if a interface is down
$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';
# Start of the main routine
Getopt::Long::Configure('bundling');
GetOptions
("h" => \$help, "help" => \$help,
"v=s" => \$snmpversion, "snmpversion=s" => \$snmpversion,
"d" => \$IntDownAlert, "down" => \$IntDownAlert,
"H=s" => \$hostname, "hostname=s" => \$hostname,
"C=s" => \$community, "community=s" => \$community,
"p=s" => \$snmpport, "port=s" => \$snmpport);
if ($help)
{
print_help();
exit 0;
}
if (!$hostname)
{
print "Host name/address not specified\n\n";
print_usage();
exit 3;
}
if ($hostname =~ /([-.A-Za-z0-9]+)/)
{
$host = $1;
}
if (!$host)
{
print "Invalid host: $hostname\n\n";
print_usage();
exit 3;
}
if (!$community)
{
$community = "public";
}
if (!$IntDownAlert)
{
# 0 = No alarm
# 1 = Alarm if an interface is down
$IntDownAlert = 0;
}
if (!$snmpversion)
{
$snmpversion = $snmpversion_def;
}
if (!$snmpport)
{
$snmpport = $snmpport_def;
}
if (!($snmpversion eq "1" || $snmpversion eq "2"))
{
print "\nError! Only SNMP V1 or 2 supported!\n";
print "Wrong version submitted.\n";
print_usage();
exit 3;
}
# --------------- Begin main subroutine ----------------------------------------
# We initialize the snmp connection
($session, $error) = Net::SNMP->session( -hostname => $hostname,
-version => $snmpversion,
-community => $community,
-port => $snmpport,
-retries => 10,
-timeout => 10
);
# If there is something wrong...exit
if (!defined($session))
{
printf("ERROR: %s.\n", $error);
exit 3;
}
# Get rid of UTF8 translation in case of accentuated caracters
$session->translate(Net::SNMP->TRANSLATE_NONE);
# Get the operating system
$oid2get = ".1.3.6.1.2.1.1.1.0";
$sysDescr = $session->get_request( -varbindlist => ["$oid2get"] );
$os = $$sysDescr{$oid2get};
$os =~ s/^.*Software://;
$os =~ s/^\s+//;
$os =~ s/ .*//;
# Get all interface tables
$oid2descr = $session->get_table( -baseoid => $descr_oid );
$oid2in_octet = $session->get_table( -baseoid => $in_octet_oid );
$oid2out_octet = $session->get_table( -baseoid => $out_octet_oid );
$oid2oper = $session->get_table( -baseoid => $oper_oid );
if ( $os eq "NetApp" )
{
# Because ucd list only eth (or so) without a number (like eth0) we
# have to determine wether it is ucd (2021) or net-snmp (8072) so we can
# set up a counter to generate this information
$oid2get = ".1.3.6.1.2.1.1.2.0";
$sysObject = $session->get_request( -varbindlist => ["$oid2get"] );
$sysObjectID = $$sysObject{$oid2get};
$sysObjectID =~ s/^\.1\.3\.6\.1\.4\.1\.//;
$sysObjectID =~ s/\..*$//;
foreach $key ( keys %$oid2descr)
{
# Monitoring traffic on a loopback interface doesn't make sense
if ($$oid2descr{$key} =~ m/lo.*$/isog)
{
delete $$oid2descr{$key};
}
else
{
# This is a little bit tricky. If we have deleted the loopback interface
# during this run of the loop $key is not set. Therefore the if-statement
# will cause an error because $key is not initialized. So we first have to check
# it is :-))
# Kick out any sit interface
if ($$oid2descr{$key} =~ m/vh.*$/isog)
{
delete $$oid2descr{$key};
}
}
}
# 0 = No alarm
# 1 = Alarm if an interface is down
# $IntDownAlert; # Alarm if interface is down. Default is no alarm
# $IsDown = 0; # Used in loop. Switched to 1 if a interface is down
foreach $key ( keys %$oid2descr)
{
$IntIdx = $key;
$IntIdx =~ s/^.*\.//;
if ( $sysObjectID == 2021 )
{
$$oid2descr{$key} = $$oid2descr{$key}.$ucdintcnt;
$ucdintcnt++;
}
# Get the incoming octets
$oid2get = $in_octet_oid.".".$IntIdx;
$in_octet = $$oid2in_octet{$oid2get};
# Get the outgoing octets
$oid2get = $out_octet_oid.".".$IntIdx;
$out_octet = $$oid2out_octet{$oid2get};
# Get the operational status of the interface
# Enumerations:
# 1 - up
# 2 - down
# 3 - testing
# 4 - unknown
# 5 - dormant
# 6 - notPresent
# 7 - lowerLayerDown
$oid2get = $oper_oid.".".$IntIdx;
$oper = $$oid2oper{$oid2get};
$IntDescr = $$oid2descr{$key};
if ( $IntDownAlert == 1 )
{
if ( $oper != 1 )
{
$IsDown = 1;
}
}
if ( $firstloop == 0 )
{
$firstloop = 1;
$IntDescr =~ s/\0//isog;
$out = $IntDescr . ":" . $InterfaceStat{$oper};
$perfout = "'" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
else
{
$IntDescr =~ s/\0//isog;
$out .= " " . $IntDescr . ":" . $InterfaceStat{$oper};
$perfout .= " '" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
}
if ( $IsDown == 1 )
{
print "Critical! $out";
print " | $perfout";
exit 2;
}
else
{
print "OK. $out";
print " | $perfout";
exit 0;
}
}
if ( $os eq "Linux" )
{
# Because ucd list only eth (or so) without a number (like eth0) we
# have to determine wether it is ucd (2021) or net-snmp (8072) so we can
# set up a counter to generate this information
$oid2get = ".1.3.6.1.2.1.1.2.0";
$sysObject = $session->get_request( -varbindlist => ["$oid2get"] );
$sysObjectID = $$sysObject{$oid2get};
$sysObjectID =~ s/^\.1\.3\.6\.1\.4\.1\.//;
$sysObjectID =~ s/\..*$//;
foreach $key ( keys %$oid2descr)
{
# Monitoring traffic on a loopback interface doesn't make sense
if ($key =~ m/^.*\.1$/)
{
delete $$oid2descr{$key};
}
else
{
# This is a little bit tricky. If we have deleted the loopback interface
# during this run of the loop $key is not set. Therefore the if-statement
# will cause an error because $key is not initialized. So we first have to check
# it is :-))
# Kick out any sit interface
if ($$oid2descr{$key} =~ m/sit.*$/isog)
{
delete $$oid2descr{$key};
}
}
}
# 0 = No alarm
# 1 = Alarm if an interface is down
# $IntDownAlert; # Alarm if interface is down. Default is no alarm
# $IsDown = 0; # Used in loop. Switched to 1 if a interface is down
foreach $key ( keys %$oid2descr)
{
$IntIdx = $key;
$IntIdx =~ s/^.*\.//;
if ( $sysObjectID == 2021 )
{
$$oid2descr{$key} = $$oid2descr{$key}.$ucdintcnt;
$ucdintcnt++;
}
# Get the incoming octets
$oid2get = $in_octet_oid.".".$IntIdx;
$in_octet = $$oid2in_octet{$oid2get};
# Get the outgoing octets
$oid2get = $out_octet_oid.".".$IntIdx;
$out_octet = $$oid2out_octet{$oid2get};
# Get the operational status of the interface
# Enumerations:
# 1 - up
# 2 - down
# 3 - testing
# 4 - unknown
# 5 - dormant
# 6 - notPresent
# 7 - lowerLayerDown
$oid2get = $oper_oid.".".$IntIdx;
$oper = $$oid2oper{$oid2get};
$IntDescr = $$oid2descr{$key};
if ( $IntDownAlert == 1 )
{
if ( $oper != 1 )
{
$IsDown = 1;
}
}
if ( $firstloop == 0 )
{
$firstloop = 1;
$IntDescr =~ s/\0//isog;
$out = $IntDescr . ":" . $InterfaceStat{$oper};
$perfout = "'" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
else
{
$IntDescr =~ s/\0//isog;
$out .= " " . $IntDescr . ":" . $InterfaceStat{$oper};
$perfout .= " '" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
}
if ( $IsDown == 1 )
{
print "Critical! $out";
print " | $perfout";
exit 2;
}
else
{
print "OK. $out";
print " | $perfout";
exit 0;
}
}
if ( $os eq "SunOS" )
{
foreach $key ( keys %$oid2descr)
{
# Monitoring traffic on a loopback interface doesn't make sense
if ($key =~ m/^.*\.1$/)
{
delete $$oid2descr{$key};
}
}
foreach $key ( keys %$oid2descr)
{
$IntIdx = $key;
$IntIdx =~ s/^.*\.//;
# Get the incoming octets
$oid2get = $in_octet_oid.".".$IntIdx;
$in_octet = $$oid2in_octet{$oid2get};
# Get the outgoing octets
$oid2get = $out_octet_oid.".".$IntIdx;
$out_octet = $$oid2out_octet{$oid2get};
# Get the operational status of the interface
# Enumerations:
# 1 - up
# 2 - down
# 3 - testing
# 4 - unknown
# 5 - dormant
# 6 - notPresent
# 7 - lowerLayerDown
$oid2get = $oper_oid.".".$IntIdx;
$oper = $$oid2oper{$oid2get};
$IntDescr = $$oid2descr{$key};
if ( $IntDownAlert == 1 )
{
if ( $oper != 1 )
{
$IsDown = 1;
}
}
if ( $firstloop == 0 )
{
$firstloop = 1;
$IntDescr =~ s/\0//isog;
$out = $IntDescr . ":" . $InterfaceStat{$oper};
$perfout = "'" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
else
{
$IntDescr =~ s/\0//isog;
$out .= " " . $IntDescr . ":" . $InterfaceStat{$oper};
$perfout .= " '" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
}
if ( $IsDown == 1 )
{
print "Critical! $out";
print " | $perfout";
exit 2;
}
else
{
print "OK. $out";
print " | $perfout";
exit 0;
}
}
if ( $os eq "Windows" )
{
foreach $key ( keys %$oid2descr)
{
# Monitoring traffic on a loopback interface doesn't make sense
if ($key =~ m/^.*\.1$/)
{
delete $$oid2descr{$key};
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^WAN.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^RAS.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^.*LightWeight Filter.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^.*QoS Packet Scheduler.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^Microsoft ISATAP Adapter.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^Microsoft Network Adapter.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^Microsoft Debug Adapter.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^Microsoft Kernel Debug.*$/)
{
delete $$oid2descr{$key};
}
}
if ($$oid2descr{$key})
{
if ($$oid2descr{$key} =~ m/^.*Pseudo-Interface.*$/)
{
delete $$oid2descr{$key};
}
}
}
foreach $key ( keys %$oid2descr)
{
$IntIdx = $key;
$IntIdx =~ s/^.*\.//;
# Get the incoming octets
$oid2get = $in_octet_oid.".".$IntIdx;
$in_octet = $$oid2in_octet{$oid2get};
# Get the outgoing octets
$oid2get = $out_octet_oid.".".$IntIdx;
$out_octet = $$oid2out_octet{$oid2get};
# Get the operational status of the interface
# Enumerations:
# 1 - up
# 2 - down
# 3 - testing
# 4 - unknown
# 5 - dormant
# 6 - notPresent
# 7 - lowerLayerDown
$oid2get = $oper_oid.".".$IntIdx;
$oper = $$oid2oper{$oid2get};
$IntDescr = $$oid2descr{$key};
if ( $IntDownAlert == 1 )
{
if ( $oper != 1 )
{
$IsDown = 1;
}
}
if ( $firstloop == 0 )
{
$firstloop = 1;
$IntDescr =~ s/\0//isog;
$out = $IntDescr . ":" . $InterfaceStat{$oper};
$perfout = "'" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
else
{
$IntDescr =~ s/\0//isog;
$out .= " " . $IntDescr . ":" . $InterfaceStat{$oper};
$perfout .= " '" . $IntDescr . "_in_octet'=" . $in_octet . "c '" . $IntDescr . "_out_octet'=" . $out_octet . "c";
}
}
if ( $IsDown == 1 )
{
print "Critical! $out";
print " | $perfout";
exit 2;
}
else
{
print "OK. $out";
print " | $perfout";
exit 0;
}
}
# Not kicked out yet? So it seems to unknown
exit 3;
# --------------- Begin subroutines ----------------------------------------
sub print_usage
{
print "\nUsage: $ProgName -H <host> [-C community] [-d]\n\n";
print "or\n";
print "\nUsage: $ProgName -h for help.\n\n";
}
sub print_help
{
print "$ProgName,Version 1.0\n";
print "Copyright (c) 2011 Martin Fuerstenau - Oce Printing Systems\n";
print_usage();
print " -H, --hostname=HOST Name or IP address of host to check\n";
print " -C, --community=community SNMP community (default public)\n\n";
print " -v, --snmpversion=snmpversion Version of the SNMP protocol. At present version 1 or 2c\n\n";
print " -p, --portsnmpversion=snmpversion Version of the SNMP protocol. At present version 1 or 2c\n\n";
print " -d, --down Alarm if any of the interfaces is down\n\n";
print " -h, --help Short help message\n\n";
print "\n";
} |
Partager