A lightweight REST API written in Go for performing ICMP ping tests. The API allows users to send GET requests to a /ICMP
endpoint to check the reachability of a specified hostname.
This API is useful for monitoring and diagnosing network connectivity by performing ICMP ping tests to verify the reachability and response times of hosts. It can be integrated into monitoring tools, automated scripts, or dashboards to provide insights into network health and latency.
ie.
- Bridgehead into an internal network to get ping responses without exposing services directly(uptime monitoring)
- Use with https://upptime.js.org/ to monitor hosts that do not have a TCP/HTTP endpoint already
- Perform ICMP ping tests to any hostname.
- Authenticate requests using an environment-defined API key.
- Return detailed responses, including average ping time (in milliseconds) or appropriate HTTP error codes for failures.
- Go 1.21 or higher
- Docker (optional, for containerized deployment)
- Docker Compose (optional, for simplified multi-container management)
- A valid hostname to ping
AUTH_KEY
: A secret key used to authenticate API requests.
Description: Perform an ICMP ping to a specified hostname.
hostname
(required): The hostname or IP address to ping.key
(required): The authentication key to verify the request.
- 200 OK:
{ "hostname": "example.com", "avgPingTime": 12.34 }
- 400 Bad Request: Missing or invalid parameters.
{"error": "Missing hostname or authentication key"}
- 401 Unauthorized: Invalid authentication key.
{"error": "Invalid authentication key"}
- 408 Request Timeout: Ping timed out.
{"error": "Ping timed out"}
- 500 Internal Server Error: Ping execution failed.
{"error": "Failed to execute ping", "details": "Some error message"}
-
Clone the repository:
git clone https://github.com/your-repo/icmp-api.git cd icmp-api
-
Set the
AUTH_KEY
environment variable:export AUTH_KEY=my_secret_key
-
Run the application:
go run main.go
-
Test the endpoint:
curl "http://localhost:8080/ICMP?hostname=example.com&key=my_secret_key"
-
Build the Docker image:
docker build -t icmp-api .
-
Run the container:
docker run -d -p 8080:8080 --env AUTH_KEY=my_secret_key icmp-api
-
Test the endpoint:
curl "http://localhost:8080/ICMP?hostname=example.com&key=my_secret_key"
-
Create a
docker-compose.yml
file with the following content:version: '3.8' services: icmp-api: build: context: . dockerfile: Dockerfile ports: - "8080:8080" environment: - AUTH_KEY=my_secret_key restart: always
-
Start the service:
docker-compose up --build
-
Test the endpoint:
curl "http://localhost:8080/ICMP?hostname=example.com&key=my_secret_key"
To scale the service horizontally:
docker-compose up --scale icmp-api=3