Automated Monitoring and Notification of Rental Status
In this example, we’ll create a monitoring tool that regularly checks the rental status of servers on Clore’s marketplace and sends an email notification whenever the rental status changes. This can be especially useful for server owners who want to be informed immediately when their servers are rented out or become available.
Objective
To set up a script that monitors server rental statuses and sends notifications when a server's rental state changes (e.g., from rented to available or vice versa).
Prerequisites
Authorization token for Clore API.
Email configuration to send notifications (e.g., using an SMTP server like Gmail or a service like SendGrid).
Implementation
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time
# Clore API credentials
AUTH_TOKEN = 'your_auth_token_here'
SERVERS_URL = 'https://api.clore.ai/v1/my_servers'
# Email configuration
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
SENDER_EMAIL = 'your_email@gmail.com'
SENDER_PASSWORD = 'your_email_password'
RECIPIENT_EMAIL = 'recipient_email@gmail.com'
# Tracking dictionary for server rental status
server_status = {}
def send_email_notification(subject, message):
msg = MIMEMultipart()
msg['From'] = SENDER_EMAIL
msg['To'] = RECIPIENT_EMAIL
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
print("Email notification sent.")
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_rental_status():
servers = get_servers()
for server in servers:
server_id = server['id']
name = server['name']
current_rental_status = server['rental_status'] # 0 = not rented, 1 = rented on spot, 2 = rented on-demand
# Check if server rental status has changed
if server_id in server_status:
if server_status[server_id] != current_rental_status:
# Status has changed, send notification
status_message = (
"now available" if current_rental_status == 0
else "now rented on spot market" if current_rental_status == 1
else "now rented on-demand"
)
subject = f"Rental Status Update for Server {name}"
message = f"Server '{name}' with ID {server_id} is {status_message}."
send_email_notification(subject, message)
# Update the stored rental status
server_status[server_id] = current_rental_status
def main():
while True:
check_rental_status()
time.sleep(600) # Check every 10 minutes
if __name__ == "__main__":
main()
Explanation
send_email_notification: Sends an email with the rental status change details.
get_servers: Fetches server information and retrieves their rental statuses.
check_rental_status: Compares current rental statuses with the previously recorded ones and sends a notification if there’s a change.
main loop: Continuously monitors server statuses, checking for updates every 10 minutes.
Summary
With this setup, server owners are promptly notified when their servers are rented or become available again, ensuring they stay informed about their server usage and potential revenue. This solution improves management efficiency by automating status tracking and notifications.