Skip to content

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.

Notifications You must be signed in to change notification settings

skinner12/hetzner-server-monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hetzner Server Monitor

Description

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.

Features

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

Requirements

  • Go 1.16 or later
  • Hetzner Cloud Account
  • Hetzner Robot API Credentials

Installation

1. Clone the Repository

git clone https://github.com/skinner12/hetzner-server-monitor.git
cd hetzner-server-monitor

2. Install Dependencies

make deps

3. Create Configuration File

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.

Usage

Compile the Application

make build

Run the Application

make run

Logs

The application uses Zerolog for structured logging. Logs will be printed to the console with timestamps and log levels.

Example Log Output

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"}

Makefile Commands

The Makefile provides several commands to manage the project:

  • make build: Compiles the program and saves the executable in the bin/ directory.
  • make clean: Removes the bin/ directory.
  • make deps: Installs the dependencies.
  • make load-config: Checks if the config.yaml file is present.
  • make run: Compiles and runs the program.
  • make help: Displays available commands.

Example Usage

Compile the Application

make build

Run the Application

make run

Clean the Build Directory

make clean

Install Dependencies

make deps

Check Configuration

make load-config

Display Available Commands

make help

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to open issues or pull requests.

Example Project Structure

monitor/
├── bin/
├── main.go
├── config.yaml
├── .gitignore
├── go.mod
├── go.sum
├── README.md
└── Makefile

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published