Example 6: Automated Alert System for Low Server Utilization
This example demonstrates how to create a script that sends notifications when your servers on the Clore marketplace experience low utilization over a certain period. This can help you take proactive steps, such as adjusting prices or promoting your listings, to increase server usage and avoid idle time.
Objective
To monitor server utilization and send alerts if a server remains unused for a specified period, allowing the owner to take corrective actions.
Prerequisites
Authorization token for Clore API.
Configured email or messaging service to receive alerts.
Implementation
import requests
import time
from datetime import datetime, timedelta
import smtplib
from email.mime.text import MIMEText
# Clore API credentials
AUTH_TOKEN = 'your_auth_token_here'
SERVERS_URL = 'https://api.clore.ai/v1/my_servers'
# Alert configuration
LOW_UTILIZATION_THRESHOLD = 24 # Hours of idle time to trigger alert
CHECK_INTERVAL = 3600 # Check every hour
# Email configuration for alerts
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 587
SMTP_USER = 'your_email@example.com'
SMTP_PASSWORD = 'your_email_password'
ALERT_RECIPIENT = 'alert_recipient@example.com'
def get_servers():
headers = {'auth': AUTH_TOKEN}
response = requests.get(SERVERS_URL, headers=headers)
if response.status_code == 200:
data = response.json()
if data.get("code") == 0:
return data['servers']
else:
print("Error fetching server data:", response.status_code, response.text)
return []
def check_utilization(servers):
current_time = datetime.now()
for server in servers:
if not server['online'] and server['rental_status'] == 0:
# Assume server['last_rented'] contains the last time it was rented
last_rented = datetime.strptime(server['last_rented'], '%Y-%m-%dT%H:%M:%S')
idle_time = current_time - last_rented
if idle_time > timedelta(hours=LOW_UTILIZATION_THRESHOLD):
send_alert(server, idle_time)
def send_alert(server, idle_time):
subject = f"Low Utilization Alert for Server: {server['name']}"
body = (f"Your server '{server['name']}' has been idle for {idle_time.total_seconds() / 3600:.1f} hours.\n"
f"Consider adjusting the price or promoting your listing on the marketplace.\n\n"
f"Server Details:\n"
f"ID: {server['id']}\n"
f"Last Rented: {server['last_rented']}\n"
f"Current Price: {server['pricing']['bitcoin']} BTC\n")
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = SMTP_USER
msg['To'] = ALERT_RECIPIENT
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USER, SMTP_PASSWORD)
server.sendmail(SMTP_USER, ALERT_RECIPIENT, msg.as_string())
print(f"Alert sent for server {server['name']} (ID: {server['id']})")
def main():
while True:
servers = get_servers()
if servers:
check_utilization(servers)
time.sleep(CHECK_INTERVAL) # Wait before the next utilization check
if __name__ == "__main__":
main()
Explanation
get_servers: Retrieves server data, including rental status.
check_utilization: Checks each server’s utilization. If a server has been idle for more than the threshold period, it triggers an alert.
send_alert: Sends an email notification to alert the owner about low server utilization, including server details to help them take corrective actions.
main loop: Continuously monitors server utilization, checking every hour.
Summary
This script helps server owners stay informed about low utilization, allowing them to take timely action to optimize server usage. By sending automatic alerts, the script minimizes idle time and maximizes potential revenue.