You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于Telnet的Check MK自定义电源监控检查开发

Alright, let's build out that Expect script to fully meet your Check MK monitoring needs. I'll take your existing snippet and expand it with all the logic to extract the power value, apply thresholds, and output the format Check MK expects. Here's the complete solution:

Complete Expect Script for Telnet Power Monitoring
#!/usr/bin/expect -f

# Accept host and port as command-line arguments for flexibility
set host [lindex $argv 0]
set port [lindex $argv 1]

# Set timeout (adjust if your device takes longer to respond)
set timeout 5

# Spawn the Telnet connection
spawn telnet $host $port

# Wait for the device's prompt (update patterns if your device uses a different prompt like "#" or ">")
expect {
    "$ " { }
    ">" { }
    timeout {
        puts "2 power=0 CRIT: Failed to connect to $host:$port or no prompt detected"
        exit 1
    }
}

# Send the power query command
send "get pw\r"

# Capture and process the device's response
expect {
    # Regex to match the third line and extract the numeric power value
    -re {get pw (\d+)W} {
        set power_value $expect_out(1,string)
        
        # Evaluate thresholds and set status
        if {$power_value < 50} {
            set status_code 2
            set status_msg "CRIT: Power value $power_value is below critical threshold (50)"
        } elseif {$power_value < 100} {
            set status_code 1
            set status_msg "WARN: Power value $power_value is below warning threshold (100)"
        } else {
            set status_code 0
            set status_msg "OK: Power value is $power_value"
        }
        
        # Output in Check MK Local Check format: <status_code> <metric_name>=<value> <status_message>
        puts "$status_code power=$power_value $status_msg"
    }
    timeout {
        puts "2 power=0 CRIT: No valid response received from $host:$port"
        exit 1
    }
    eof {
        puts "2 power=0 CRIT: Connection closed unexpectedly by $host:$port"
        exit 1
    }
}

# Clean up the Telnet session
send "exit\r"
expect eof
Key Setup & Usage Details
  • Running the Script: Execute it directly with target device details: ./check_telnet_power <device_ip> <telnet_port>
  • Prompt Matching: The script looks for $ or > as the device prompt. If your device uses a different prompt (like # ), add or replace the pattern in the first expect block.
  • Check MK Integration:
    1. Save the script as check_telnet_power in your Check MK Agent's local checks directory (typically /usr/lib/check_mk_agent/local/ on Linux).
    2. Grant execute permissions: chmod +x /usr/lib/check_mk_agent/local/check_telnet_power
    3. For multiple devices, create wrapper scripts or use Check MK's rule-based config to pass unique IP/port pairs to the script.
  • Timeout Adjustment: If your device is slow to respond, increase the set timeout 5 value (in seconds).
Troubleshooting Tips
  • First test the Telnet connection manually: telnet <device_ip> <port> to confirm you can connect and see the expected prompt.
  • If the power value isn't extracting correctly, check the exact response format from your device. Tweak the regex {get pw (\d+)W} to match (e.g., if the line is Power Reading: 600W, update it to Power Reading: (\d+)W).

内容的提问来源于stack exchange,提问作者David Vrtin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:28:13