A GitHub Actions-based monitoring tool that tracks Hetzner server bandwidth usage, sends Slack alerts when thresholds are exceeded, and can automatically shut down servers to prevent excessive bandwidth charges.
This tool monitors your Hetzner Cloud servers' bandwidth usage and:
- Sends Slack notifications when servers exceed a configurable bandwidth threshold (percentage, based on free
included_traffic
, 50% by default) - Automatically shuts down servers that exceed a critical bandwidth threshold (percentage, based on free
included_traffic
)- Only shuts down servers that exceed critical threshold (90% by default)
- Provides detailed usage reports via CLI and Slack
It's better to run this as a Github Action than run it on Hetzner, so you're not at risk of accidentally shutting down this script!
It's possible that Hetzner billing can lag, so you this might not fully prevent excess usage.
Not tested with Robot (Dedicated) servers.
With default settings, this will SHUT DOWN (but not destroy) a server at 90% usage. Make sure you understand the implications of this before running it.
The script uses the following environment variables:
Important: Percentages are 0-100, not 0-1!
Variable | Description | Default | Required |
---|---|---|---|
HETZNER_API_TOKEN |
Your Hetzner Cloud API token | - | Yes |
SLACK_WEBHOOK_URL |
Slack webhook URL for notifications | - | No |
THRESHOLD_PERCENT_NOTIF |
Percentage threshold for alerts | 50 | No |
THRESHOLD_PERCENT_KILL |
Percentage threshold for killing servers | 90 | No |
SEND_USAGE_NOTIF_ALWAYS |
Send usage notifications even if no thresholds are exceeded | false | No |
OBFUSCATE_SERVER_NAMES_FROM_CONSOLE_LOG |
Hide server names in console output for privacy | false | No |
.env template for local testing:
export HETZNER_API_TOKEN=your_hetzner_api_token_here
export SLACK_WEBHOOK_URL=your_slack_webhook_url_here
export THRESHOLD_PERCENT_NOTIF=50
export THRESHOLD_PERCENT_KILL=90
export SEND_USAGE_NOTIF_ALWAYS=false
export OBFUSCATE_SERVER_NAMES_FROM_CONSOLE_LOG=false
Github will stop running the action if it's not used for 60 days. I think they'll send you an email to reactivate. This is another solution I came across that might help (and I might eventually integrate directly) https://github.com/marketplace/actions/keepalive-workflow.
- Fork this repository by clicking the "Fork" button at the top right of this page
- Go to your forked repository (e.g.,
https://github.com/YOUR-USERNAME/hetzner-alert-and-kill
) - Navigate to the "Actions" tab
- Click the "I understand my workflows, go ahead and enable them" button to enable GitHub Actions
- Log in to your Hetzner Cloud Console
- Navigate to Security → API Tokens
- Create a new API token with read and write permissions (write required for shutdown)
- Copy the token (you'll need it for GitHub secrets)
- Create a new Slack app at api.slack.com/apps
- Enable Incoming Webhooks
- Create a new webhook URL for your workspace
- Copy the webhook URL (you'll need it for GitHub secrets)
- Go to your repository's Settings → Secrets and variables → Actions → New repository secret
- Add the following repository secrets:
HETZNER_API_TOKEN
: Your Hetzner API tokenSLACK_WEBHOOK_URL
: Your Slack webhook URL (optional)
- You can customize the monitoring thresholds by adding the following environment variables (Repository settings -> Actions -> Variables -> New repository variable):
THRESHOLD_PERCENT_NOTIF
: Percentage of bandwidth usage that triggers a notification (default: 50)THRESHOLD_PERCENT_KILL
: Percentage of bandwidth usage that triggers server shutdown (default: 90)SEND_USAGE_NOTIF_ALWAYS
: Set to 'true' to always send usage reports to Slack (default: false)OBFUSCATE_SERVER_NAMES_FROM_CONSOLE_LOG
: Set to 'true' to hide server names in console output for privacy (default: false)
If you don't want it to kill, set THRESHOLD_PERCENT_KILL
to a high value. Numbers can be set > 100 if you're OK with some overages.
By default, this runs every 20 minutes. You can customize this with the cron job in monitor.yml
.
The GitHub Action runs automatically according to the schedule defined in the workflow file (every 20 minutes).
You can also trigger the workflow manually:
- Go to the Actions tab in your GitHub repository
- Select the "Hetzner Traffic Stats" workflow
- Click "Run workflow"
Note: with the settings below, if you're over 90% usage, it will shut down your server!
To run the script locally:
- Clone the repository
- Install dependencies:
npm install
- Set environment variables:
export HETZNER_API_TOKEN="your_token_here" export SLACK_WEBHOOK_URL="https://hooks.slack.com/services/..." export THRESHOLD_PERCENT_NOTIF="50" export THRESHOLD_PERCENT_KILL="90" export SEND_USAGE_NOTIF_ALWAYS: 'true' export OBFUSCATE_SERVER_NAMES_FROM_CONSOLE_LOG="false"
- Run the script:
node scripts/monitor.js
This script is provided as-is and without warranty. Use it at your own risk!