This project is a simple Go application that monitors the availability of a server using ping
and automatically starts the server on Hetzner Robot if it remains offline for a specified duration.
- Ping Monitoring: Uses
ping
to check the availability of the server. - Configuration via YAML: Configuration settings are stored in
config.yaml
. - Logging with Zerolog: Structured logging using the Zerolog library.
- Automatic Server Start: Starts the server on Hetzner Cloud if it remains offline for a specified period.
- Verification of Server Status: Checks if the server is online after attempting to start it.
- Go 1.16 or later
- Hetzner Cloud Account
- Hetzner Robot API Credentials
git clone https://github.com/skinner12/hetzner-server-monitor.git
cd hetzner-server-monitor
make deps
Create a config.yaml
file in the root directory with the following content:
ip_to_monitor: "192.168.1.1"
timeout_minutes: 5
hetzner_username: "your_username"
hetzner_password: "your_password"
server_id: 321
post_start_wait: 5 # Time to wait in minutes after starting the server
ping_count: 3 # Number of ICMP packets to send
Replace the placeholders with your actual values.
make build
make run
The application uses Zerolog for structured logging. Logs will be printed to the console with timestamps and log levels.
Info when the server is online:
{"level":"info","time":1633072800,"message":"IP 192.168.1.1 is online"}
Warn when the server is offline:
{"level":"warn","time":1633072860,"message":"IP 192.168.1.1 is offline"}
Info when the server comes back online:
{"level":"info","time":1633073160,"message":"IP 192.168.1.1 is back online after being offline for 5m0s"}
Info when the server start request is sent:
{"level":"info","time":1633073220,"message":"Server start request successfully sent!"}
Info when the server is started and online:
{"level":"info","time":1633073340,"message":"Server started and is online successfully!"}
Warn when the server is started but still offline:
{"level":"warn","time":1633073340,"message":"Server started but still offline. Please try again later."}
Error when there is an error starting the server:
{"level":"error","time":1633073400,"error":"failed to start server: Request error","message":"Error starting the server"}
Fatal when there is an error loading the configuration:
{"level":"fatal","time":1633073460,"error":"Configuration loading error","message":"Failed to load configuration"}
The Makefile provides several commands to manage the project:
make build
: Compiles the program and saves the executable in thebin/
directory.make clean
: Removes thebin/
directory.make deps
: Installs the dependencies.make load-config
: Checks if theconfig.yaml
file is present.make run
: Compiles and runs the program.make help
: Displays available commands.
make build
make run
make clean
make deps
make load-config
make help
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to open issues or pull requests.
monitor/
├── bin/
├── main.go
├── config.yaml
├── .gitignore
├── go.mod
├── go.sum
├── README.md
└── Makefile