If you’re using Vultr as your cloud service provider and want to keep a close eye on your account balance, this guide is for you. I’ve gone through the process of setting up an automated alert system for my own account and I’m here to share the steps. This approach can help you avoid service interruptions by notifying you when your balance falls below a certain threshold. While this is tailored to Vultr, the principles can be applied to any hosting service with an accessible API. Let’s walk through the setup together.
Step 1: Install Dependencies
First, ensure that jq
is installed on your system. It’s a lightweight and flexible command-line JSON processor. Open your terminal and run:
sudo apt-get update
sudo apt-get install -y jq
Step 2: Store Your API Key Securely
Create a .env
file in your script’s directory to store your Vultr API key. This file should only be readable by the user running the script:
echo "VULTR_API_KEY=your_vultr_api_key_here" > /path/to/your/script/.env
chmod 600 /path/to/your/script/.env
Replace your_vultr_api_key_here
with your actual API key.
Step 3: Crafting the Alert Script
To create an automated alert system for your Vultr account balance, follow these sub-steps:
3.1 Create the Script File
Open your terminal and use a text editor to create a new file named vultr_alert.sh
:
touch /path/to/your/script/vultr_alert.sh
nano /path/to/your/script/vultr_alert.sh
3.2 Script Template
Copy the following script template into your editor. This script checks your Vultr account balance and sends email notifications based on predefined thresholds.
#!/bin/bash
# Load the API key from the environment file
source "$(dirname "$0")/.env"
# Define thresholds for balance and pending charges percentage
SOFT_LIMIT_BALANCE=100
HARD_LIMIT_BALANCE=80
CRITICAL_LIMIT_BALANCE=60
SOFT_LIMIT_PERCENTAGE=50
HARD_LIMIT_PERCENTAGE=65
CRITICAL_LIMIT_PERCENTAGE=85
# Email configuration
FROM_EMAIL="your_email@example.com"
TO_EMAIL="recipient_email@example.com"
# Function to send email
send_email() {
local subject=$1
local body=$2
echo "$body" | mail -s "$subject" -r "$FROM_EMAIL" "$TO_EMAIL"
}
# Function to check balance and send notifications
check_balance() {
local balance=$1
local pending_charges=$2
local balance_abs=${balance#-} # Remove the negative sign if present
local percentage=$(echo "scale=2; $pending_charges / $balance_abs * 100" | bc)
if (($(echo "$balance_abs < $CRITICAL_LIMIT_BALANCE || $percentage > $CRITICAL_LIMIT_PERCENTAGE" | bc -l))); then
send_email "Critical Vultr Balance Alert" "Your Vultr balance is critically low. Immediate action required."
elif (($(echo "$balance_abs < $HARD_LIMIT_BALANCE || $percentage > $HARD_LIMIT_PERCENTAGE" | bc -l))); then
send_email "Hard Limit Vultr Balance Alert" "Your Vultr balance is below the hard limit. Please add funds."
elif (($(echo "$balance_abs < $SOFT_LIMIT_BALANCE || $percentage > $SOFT_LIMIT_PERCENTAGE" | bc -l))); then
send_email "Soft Limit Vultr Balance Alert" "Your Vultr balance is approaching the soft limit. Consider adding funds."
fi
}
# Main script execution
response=$(curl -s -H "Authorization: Bearer $VULTR_API_KEY" "https://api.vultr.com/v2/account")
balance=$(echo "$response" | jq -r '.account.balance')
pending_charges=$(echo "$response" | jq -r '.account.pending_charges')
check_balance "$balance" "$pending_charges"
3.3 Customize the Script
Replace your_email@example.com
with your email address and recipient_email@example.com
with the email address where you want to receive the alerts. Adjust the SOFT_LIMIT_BALANCE
, HARD_LIMIT_BALANCE
, and CRITICAL_LIMIT_BALANCE
variables to set your desired thresholds for balance alerts.
3.4 Save and Close the File
After pasting the script into your editor, save the file and exit. If you’re using nano
, you can save by pressing Ctrl + O
, then Enter
, and exit with Ctrl + X
.
3.5 Make the Script Executable
Grant execute permissions to the script:
chmod +x /path/to/your/script/vultr_alert.sh
Now, your script is ready to be scheduled with a cron job to run at regular intervals, as described in Step 4. Remember to replace /path/to/your/script/
with the actual path to your script.
Step 4: Set Up a Cron Job
To automate the execution of your script, set up a cron job. Open your crontab with crontab -e
and add the following line to run the script every 6 hours:
0 */6 * * * /bin/bash /path/to/your/script/vultr_alert.sh
Step 5: Test the Script
Before relying on the script, test it to ensure it works as expected. You can manually run the script using:
bash /path/to/your/script/vultr_alert.sh
Check your email to see if you received the alerts.
Step 6: Monitor and Adjust
Keep an eye on the script’s performance. If you receive false alerts or no alerts when you should, revisit the script and adjust the thresholds or check the API key and email settings.
By following these steps, you’ll have a robust system in place to alert you of any potential issues with your Vultr account balance. Remember to keep your API key secure and to use this script responsibly.
Please ensure that you replace /path/to/your/script/
with the actual path where your script is located. Also, replace placeholder text like your_vultr_api_key_here
with your actual information.