求助:基于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 firstexpectblock. - Check MK Integration:
- Save the script as
check_telnet_powerin your Check MK Agent's local checks directory (typically/usr/lib/check_mk_agent/local/on Linux). - Grant execute permissions:
chmod +x /usr/lib/check_mk_agent/local/check_telnet_power - For multiple devices, create wrapper scripts or use Check MK's rule-based config to pass unique IP/port pairs to the script.
- Save the script as
- Timeout Adjustment: If your device is slow to respond, increase the
set timeout 5value (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 isPower Reading: 600W, update it toPower Reading: (\d+)W).
内容的提问来源于stack exchange,提问作者David Vrtin
相关产品推荐
相关产品推荐

