欢迎访问机房运维工作室

机房运维托管

模拟获取机房状态的函数,获取机房状态模拟器

频道:机房运维托管 日期: 浏览:294
``python ,def get_server_status(server_id):, # 模拟从数据库中查询服务器状态, server_status = {, "server1": {"status": "active", "load": 20, "ram_usage": 40},, "server2": {"status": "inactive", "load": 0, "ram_usage": 0}, }, , return server_status.get(server_id, {"status": "unknown"}),`,这段代码定义了一个get_server_status函数,用于模拟获取指定服务器的状态,它首先创建一个字典来存储服务器的状态信息,然后使用dict.get()`方法返回对应的服务器状态,如果指定的服务器ID不存在,则返回一个默认的状态为“unknown”的结果。
import random
import time
from datetime import datetime
def get_room_status():
    # Randomly generate some status data
    temperature = round(random.uniform(20, 30), 2)
    humidity = round(random.uniform(40, 70), 2)
    power_supply = "Normal" if random.choice([True, False]) else "Abnormal"
    fan_status = "Normal" if random.choice([True, False]) else "Faulty"
    return {
        'temperature': temperature,
        'humidity': humidity,
        'power_supply': power_supply,
        'fan_status': fan_status
    }
# Simulate saving the status data to a database using a list
status_records = []
# Monitoring interval in seconds
monitoring_interval = 10
while True:
    status = get_room_status()
    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(f"{timestamp}: Temperature: {status['temperature']}°C, Humidity: {status['humidity']}%, Power Supply: {status['power_supply']}, Fan Status: {status['fan_status']}")
    # Assume we are storing the status record into the database
    status_records.append((timestamp, status))
    # Wait for the next monitoring cycle
    time.sleep(monitoring_interval)

This code simulates a basic room status monitoring system that checks the temperature, humidity, power supply, and fan status every 10 seconds. It prints out the status information and assumes it is being stored in a database. In real-world applications, you would need to integrate with actual hardware sensors and databases, handle errors, and implement security measures to protect sensitive data.