I have plumber running on a Computer within the same Network. Sometimes the request Fails and i would like to differentiate the cause:
-
Computer down
-
plumber not running on given port
-
Code within plumber Fails
Therefore, (in case the plumber request Fails) i thought i could:
-
Ping plumber on given port
-
If that Fails i ping the Computer (without that specific port)
What i tried.
Ensure plumber is running.
url <- paste0("http://192.168.1.11:7192/echo")
> httr::GET(url = url, body = "msg=asd") %>% content
$msg
$msg[[1]]
[1] "The message is: ''"
So that works, now try the "ping". Following https://stackoverflow.com/questions/7012796/ping-a-website-in-r:
ping <- function(x, stderr = FALSE, stdout = FALSE, ...){
pingvec <- system2("ping -n 1", x,
stderr = FALSE,
stdout = FALSE,...)
if (pingvec == 0) TRUE else FALSE
}
> ping(url)
[1] FALSE
Warning message:
In system2("ping -n 1", x, stderr = FALSE, stdout = FALSE, ...) :
'"ping -n 1"' not found
> ping("http://192.168.1.11")
[1] FALSE
Warning message:
In system2("ping -n 1", x, stderr = FALSE, stdout = FALSE, ...) :
'"ping -n 1"' not found
> RCurl::url.exists(url)
[1] FALSE
That Fails.
Trying "manually":
system("ping -n 1 192.168.1.11")
Ping wird ausgefhrt fr 192.168.1.11 mit 32 Bytes Daten:
Antwort von 192.168.1.11: Bytes=32 Zeit=4ms TTL=64
Ping-Statistik fr 192.168.1.11:
Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0
(0% Verlust),
Ca. Zeitangaben in Millisek.:
Minimum = 4ms, Maximum = 4ms, Mittelwert = 4ms
[1] 0
> system("ping -n 1 192.168.1.112")
Ping wird ausgefhrt fr 192.168.1.112 mit 32 Bytes Daten:
Antwort von 192.168.1.145: Zielhost nicht erreichbar.
Ping-Statistik fr 192.168.1.112:
Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0
(0% Verlust),
[1] 0
Text indicates that .11 is up and .112 does not exist, which is correct.
Dirty solution:
ping2 <- function(host){
res <- system(command = paste0("ping -n 1 ", host), intern = TRUE)
!sum(grepl(pattern = "nicht erreichbar", x = res))
}
host <- "192.168.1.11"
ping2(host)
However, that does not work for checking the port: (Here port 999999 is not open).
host <- "192.168.1.11:999999"
ping2(host)
TRUE
Question:
How to debug my plumber api - specifically how should i ping the host and the port?
Related:
I asked first on Stackoverflow: https://stackoverflow.com/questions/60871275/how-to-ping-plumber-in-same-network-with-r