Скрипт измерения скорости между микротиками, с отправкой алертов на почту.
Создаем файл filials.txt
test isp1 = 1.1.1.1
test isp2 = 2.2.2.2
test isp3 = 3.3.3.3
test isp4 = 4.4.4.4
test isp5 = 5.5.5.5
Переменные:
testSpeed - максимальная скорость теста в мегабитах.
testAlert - минимальная скорость до алерта в мегабитах
duration - время тестирования каждого канала, в секундах
testuser - пользователь удаленного микротика с правами test (bandwishtest)
testpass - пароль пользователя удаленного микротика с правами test (bandwishtest)
sender - email отправителя
mailreciever - email получателя
mailServer - адрес mail сервера
:local testSpeed 30;
:local testAlert 15;
:local duration 60;
:local testuser testusername;
:local testpass testP@ssword;
:local mailreciever "rАдрес электронной почты защищен от спам-ботов. Для просмотра адреса в браузере должен быть включен Javascript. ";
:local sender "sАдрес электронной почты защищен от спам-ботов. Для просмотра адреса в браузере должен быть включен Javascript. ";
:local mailServer mailServer.address;
# 1) Get file content
:local fileContent [ /file get [ find name="filials.txt" ] contents ];
# 2) Split content by Lines to $splitLines array
:local splitLines [ :toarray "" ];
:local tempStr "";
:for i from=0 to=( [ :len $fileContent ] - 1 ) do={
:local char [ :pick $fileContent $i ( $i + 1 ) ];
:if ( $char != "\n" && $char != "\r" ) do={
:set tempStr ( $tempStr . $char );
};
:if ( $char = "\n" ) do={
:if ( [ :len $tempStr ] > 2 ) do={
:set splitLines ( $splitLines , $tempStr );
};
:set $tempStr "";
};
};
# service function for trim spaces at start and end of string
:local trimString do={
:local inputString $1;
:local outputString "";
:local endOfSpaces false;
:for i from=0 to=( [ :len $inputString ] - 1 ) do={
:local char [ :pick $inputString $i ( $i + 1 ) ];
:if ( $endOfSpaces = true || ( $char != " " && $char != "\f" && $char != "\n" && $char != "\r" && $char != "\t" && $char != "\v" ) ) do={
:if ( $endOfSpaces = false ) do={ :set endOfSpaces true };
:set outputString ( $outputString . $char );
};
};
:set inputString $outputString;
:set outputString "";
:set endOfSpaces false;
:for i from=( [ :len $inputString ] - 1 ) to=0 step=-1 do={
:local char [ :pick $inputString $i ( $i + 1 ) ];
:if ( $endOfSpaces = true || ( $char != " " && $char != "\f" && $char != "\n" && $char != "\r" && $char != "\t" && $char != "\v" ) ) do={
:if ( $endOfSpaces = false ) do={ :set endOfSpaces true };
:set outputString ( $char . $outputString );
};
};
:return $outputString;
};
# 3) Split every line to array: { "description"="descvalue"; "ip"=IPvalue } and combine final array filials
:local filials [ :toarray "" ];
:local counter 0;
:foreach line in=$splitLines do={
:local tempDescription "";
:local tempIp "";
:local flagIpInString false;
:for i from=0 to=( [ :len $line ] - 1 ) do={
:local char [ :pick $line $i ( $i + 1 ) ];
:if ( $char != "=" ) do={
:if ( $flagIpInString = false ) do={
:set tempDescription ( $tempDescription . $char );
} else={
:set tempIp ( $tempIp . $char );
};
} else={
:set flagIpInString true;
};
};
:set ( $filials->$counter ) { "description"=[ $trimString $tempDescription ]; "ip"=[ :toip [ $trimString $tempIp ] ] };
:set counter ( $counter + 1 );
};
:local currentDate ([/system clock get date]." ".[/system clock get time]);
:local rxSpeed 0;
:local txSpeed 0;
:local message "";
:local subject "";
:set subject (">>> ".$testAlert." Mbps Bandwidth report on ".$currentDate);
:log warning ($subject);
:foreach filial in=$filials do={
:do {
:local testcount 0;
:for i from=0 to=1 do={
:if ($testcount = 0) do={
/tool bandwidth-test address=[:put ( $filial->"ip" );] duration=[:put ("$duration"."s")] protocol=tcp user=$testuser password=$testpass direction=both local-tx-speed=[:put ("$testSpeed"."M")] remote-tx-speed=[:put ("$testSpeed"."M")] do={ :set rxSpeed [$"rx-total-average"]; :set txSpeed [$"tx-total-average"]; }
};
:set rxSpeed ($rxSpeed/1048576);
:set txSpeed ($txSpeed/1048576);
:set message ($message("Speed to ".[:put ( $filial->"description" );].", IP: ".[:put ( $filial->"ip" );]." Rx: ".$rxSpeed." mbps".", Tx: ".$txSpeed." mbps"));
:if ($rxSpeed > 0 && $txSpeed > 0) do={
:if ($rxSpeed < $testAlert || $txSpeed < $testAlert) do={ :log error ($message); /tool e-mail send to=$mailreciever subject=("Speed Alert - ".[:put ( $filial->"description" );]) body="Speedtest alerts on $currentDate:\r\nSpeed lower then $testAlert mbps\r\n$message" server=$mailServer from=$sender } else={ :log warning ($message);};
:set testcount 1;
} else={
:if ($i = 0 && $testcount = 0) do={ :log error ("Error, test again. ".$message); };
:if ($i > 0 && $testcount = 0) do={ :log error ($message); /tool e-mail send to=$mailreciever subject=("Speed Alert - ".[:put ( $filial->"description" );]) body="Speedtest alerts on $currentDate:\r\nSpeed lower then $testAlert mbps\r\n$message" server=$mailServer from=$sender };
};
};
} on-error={ :log warning (">>> Script error. Not found any IP"); };
};
:log warning (">>> Test completed");