dev.clore.ai
  • Getting Started
  • Introduction to Clore API
    • API Key Management
    • Transaction Processing with Clore Coin
    • RESTful Protocols in Clore
  • Marketplace Automation and Resource Optimization
    • Automated Spot Price Adjustment for Cost Optimization
    • Automated Server Retrieval and Analysis
    • Spot Price Optimization
    • Predictive Market Analytics
  • Server Management and Configuration
    • Bulk Server Onboarding and Dynamic Pricing Configuration
  • Dynamic Pricing and Profit Maximization
    • Dynamic Pricing Adjustments Based on Server Profitability
    • Automated Price Adjustment Based on Market Demand
    • Automated Spot Rental Based on Price Thresholds
  • Rental Strategies and Arbitrage
  • Monitoring and Notifications
    • Automated Monitoring and Notification of Rental Status
    • Automated Alert System for Low Server Utilization
    • Automated Retrieval and Analysis of Available Servers on the Marketplace
  • Security and Compliance
  • UI Automation and Visualization
  • API Performance Optimization
  • Community Extensions and Integrations
  • Advanced Data Processing and Analysis
  • Scalability and Infrastructure Management
  • Machine Learning and AI Integrations
    • Integrating ML Models into Server Operations
  • Developer Tools and SDKs
    • Setting Up the Clore Developer Toolkit
    • Using Clore SDK for Rapid Prototyping
  • Billing, Accounting, and Financial Reporting
  • Workflow Automation and Scripting
  • Multi-Cloud and Hybrid Cloud Integrations
  • Security Monitoring and Incident Management
  • Blockchain Interactions and Smart Contracts
  • Resource Optimization and Cost-Saving Techniques
Powered by GitBook
On this page
  1. Monitoring and Notifications

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

  1. send_email_notification: Sends an email with the rental status change details.

  2. get_servers: Fetches server information and retrieves their rental statuses.

  3. check_rental_status: Compares current rental statuses with the previously recorded ones and sends a notification if there’s a change.

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

PreviousMonitoring and NotificationsNextAutomated Alert System for Low Server Utilization

Last updated 6 months ago