Skip to content

Commit

Permalink
Better server ping check in OpenMPTCProuter local API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ysurac committed Aug 2, 2024
1 parent 52e3e73 commit 3390598
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions openmptcprouter-api/files/usr/libexec/rpcd/openmptcprouter
Original file line number Diff line number Diff line change
Expand Up @@ -1504,44 +1504,53 @@ function interfaces_status()
local current = uci:get("openmptcprouter",s[".name"],"current") or "0"
if current == "1" then
for key, value in pairs(serverips) do
local serverip = mArray.openmptcprouter["service_addr"]
if serverip == "127.0.0.1" then
serverip = mArray.openmptcprouter["wan_addr"]
end
if gateway ~= "" then
serverip = sys.exec('resolveip -4 ' .. value .. ' | head -n 1')
elseif gateway6 ~= "" then
serverip = sys.exec('resolveip -6 ' .. value .. ' | head -n 1')
end
if serverip == "" then
serverip = value
end
if serverip ~= "" then
local server_ping_test = sys.exec("ping -B -w 1 -c 1 -I " .. ifname .. " " .. serverip)
local server_ping_result = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep '100% packet loss'"))
local server_ping_nobind = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep 'Address not available'"))
if server_ping_result ~= "" then
server_ping = "DOWN"
--if connectivity == "OK" then
-- connectivity = "WARNING"
--end
elseif server_ping_nobind ~= "" then
server_ping_test = sys.exec("ping -w 1 -c 1 -I " .. ifname .. " " .. serverip)
server_ping_result = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep '100% packet loss'"))
if server_ping_result ~= "" then
server_ping = value
if server_ping ~= "UP" then
local serverip = mArray.openmptcprouter["service_addr"]
if serverip == "127.0.0.1" then
serverip = mArray.openmptcprouter["wan_addr"]
end
if gateway ~= "" then
serverip = sys.exec('resolveip -4 ' .. value .. ' | head -n 1')
elseif gateway6 ~= "" then
serverip = sys.exec('resolveip -6 ' .. value .. ' | head -n 1')
end
if serverip == "" then
serverip = value
end
if serverip ~= "" then
local server_ping_test = sys.exec("ping -B -w 1 -c 1 -I " .. ifname .. " " .. serverip .. " 2>&1")
local server_ping_result = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep '100% packet loss'"))
local server_ping_nobind = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep 'Address not available'"))
local server_ping_error = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep -i 'error'"))
if server_ping_error ~= "" then
server_ping = "DOWN"
--if connectivity == "OK" then
-- connectivity = "WARNING"
--end
if connectivity == "OK" then
connectivity = "WARNING"
end
elseif server_ping_result ~= "" then
server_ping = "DOWN"
if connectivity == "OK" then
connectivity = "WARNING"
end
elseif server_ping_nobind ~= "" then
server_ping_test = sys.exec("ping -w 1 -c 1 -I " .. ifname .. " " .. serverip)
server_ping_result = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep '100% packet loss'"))
if server_ping_result ~= "" then
server_ping = "DOWN"
if connectivity == "OK" then
connectivity = "WARNING"
end
else
mArray.openmptcprouter["vps_status"] = "UP"
server_ping = "UP"
latency = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | cut -d '/' -s -f5 | cut -d '.' -f1"))
end
else
mArray.openmptcprouter["vps_status"] = "UP"
server_ping = "UP"
latency = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | cut -d '/' -s -f5 | cut -d '.' -f1"))
end
else
mArray.openmptcprouter["vps_status"] = "UP"
server_ping = "UP"
latency = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | cut -d '/' -s -f5 | cut -d '.' -f1"))
end
end
end
Expand Down

0 comments on commit 3390598

Please sign in to comment.