#!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use SNMP; die "Usage: $0 <inputFile>" if ($#ARGV < 0); my $community = 'community'; ### pingLoss function ### sub pingLoss { my ($param) = @_; my @result = `ping $param`; foreach my $str (@result) { if ($str =~ /(\d+)%/ && $1 < 100) { return $1; } } return 100; } ### stressTest function for one host ### sub stressTest { my $ifIp = shift; my $ifIndex; my $ifName = ""; my $ifAlias = ""; my $ifSpeed = ""; my $ifInOctets; my $ifInOctetsBegin; my $ifInOctetsEnd; my $sendBytes; my $sysName = ""; my $testPeriod; my $pingLossCount = ""; my $inOutPercent = ""; my $testStatus; my $nowDateTime = strftime "%Y.%m.%d %H:%M:%S", localtime; if (&pingLoss("$ifIp -c 3 -i 0.2 -W 2") > 90) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tping error\n"; return 0; } my $sess = new SNMP::Session(DestHost => "$ifIp:161", Community => $community, Version => "2c", NonIncreasing => 1, UseLongNames => 1,); $sysName = $sess->get('.1.3.6.1.2.1.1.5.0'); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp sysName error\n"; return 0; } if ($sysName =~ m/^([\w_-]+)\./) { $sysName = $1; } $ifIndex = $sess->get('.1.3.6.1.2.1.4.20.1.2.' . $ifIp); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp ifIndex error\n"; return 0; } $ifName = $sess->get('.1.3.6.1.2.1.31.1.1.1.1.' . $ifIndex); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp ifName error\n"; return 0; } $ifAlias = $sess->get('.1.3.6.1.2.1.31.1.1.1.18.' . $ifIndex); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp ifAlias error\n"; return 0; } $ifSpeed = $sess->get('.1.3.6.1.2.1.2.2.1.5.' . $ifIndex); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t\t$inOutPercent\t$pingLossCount\tsnmp ifSpeed error\n"; return 0; } if ($ifSpeed > 30000000) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tHigh speed channel\n"; return 0; } my $sendBytesSec = $ifSpeed * 0.85 / 8; system("killall udpblast > /dev/null 2> /dev/null"); system("udpblast -c 1000000 --rate $sendBytesSec,100s $ifIp > /dev/null 2> /dev/null &"); sleep(5); my $testBegin = strftime "%s", localtime; $ifInOctetsBegin = $sess->get('.1.3.6.1.2.1.2.2.1.10.' . $ifIndex); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp ifInOctetsBegin error\n"; return 0; } $pingLossCount = &pingLoss("$ifIp -c 60 -W 1.5"); system("killall udpblast > /dev/null 2> /dev/null"); $ifInOctetsEnd = $sess->get('.1.3.6.1.2.1.2.2.1.10.' . $ifIndex); if ($sess->{ErrorNum}) { print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\tsnmp ifInOctetsEnd error\n"; return 0; } my $testEnd = strftime "%s", localtime; $testPeriod = $testEnd - $testBegin; $ifInOctets = $ifInOctetsEnd - $ifInOctetsBegin; $sendBytes = $sendBytesSec * $testPeriod * 1.04; $inOutPercent = sprintf("%d", $ifInOctets * 100 / $sendBytes); $testStatus = $inOutPercent > 89 && $pingLossCount < 6 ? "Good" : "Bad"; print "$nowDateTime\t$ifIp\t$sysName\t$ifName\t$ifAlias\t$ifSpeed\t$inOutPercent\t$pingLossCount\t$testStatus\n"; } ### Main ### open (inputFile, $ARGV[0]) or die "Failed to open $ARGV[0]: $!\n"; print "date time \tipAddress \tsysName \tifName \tifAlias \tifSpeed \tinOutPercent\tpingLossCount\ttestStatus\n"; foreach my $readString (<inputFile>) { my $ipAddress = ""; if ($readString =~ m/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ && $1 < 256 && $2 < 256 && $3 < 256 && $4 < 256) { $ipAddress = "$1.$2.$3.$4"; } else {next}; &stressTest($ipAddress); }
Source: https://habr.com/ru/post/352592/
All Articles