How to ping plumber (in same network)?

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:

  1. Ping plumber on given port

  2. 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

I believe the approach is off. ping can only determine if the host is un/available. ping does not allow for ports or url routes.


Trying to determine the cause

  • Computer down
  • plumber not running on given port
  • Code within plumber Fails

Computer is down

Your dirty solution of ping2 is not bad. ping should return a non-zero exit value if it fails, so it could be updated to...

ping2 <- function(host) {
  res <- system(command = paste0("ping -n 1 ", host))
  res != 0L
}
ping2("google.com") 
# TRUE
ping2("google-doesntexist.com")
# FALSE

plumber not running on given port

If this is ever the case, the httpuv ecosystem will be in trouble. I would not worry about this one.

I like the idea of setting a /echo route. This should address your plumber is alive check.

Code within plumber fails

Plumber should return an error value in the request.

If nothing is being returned, I would double check that your browser / requestor is not timing out and being unexpectedly cut off. This could even be a proxy sitting in the middle of your request. Example: curl and plumber will not timeout if they are connected. However, a load balancer in the middle could timeout after 60 seconds.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.