AutoHotkey 语言 监控系统 CPU 温度并报警实战

AutoHotkey阿木 发布于 3 天前 4 次阅读


AutoHotkey Language: Monitoring System CPU Temperature and Alarm Practical Application

Introduction

AutoHotkey (AHK) is a powerful scripting language for automating the Windows GUI and general scripting. It is often used for creating keyboard shortcuts, automating repetitive tasks, and even building complex applications. In this article, we will explore how to create an AutoHotkey script that monitors the system's CPU temperature and triggers an alarm if the temperature exceeds a certain threshold. This practical application can be particularly useful for system administrators or users who want to ensure their hardware remains within safe operating temperatures.

Prerequisites

Before we dive into the code, ensure you have the following prerequisites:

1. AutoHotkey installed on your system: You can download it from the official website (https://www.autohotkey.com/).
2. Administrative privileges: Running scripts that monitor system resources typically require administrative rights.

Monitoring CPU Temperature

To monitor the CPU temperature, we will use the Windows Management Instrumentation (WMI) interface provided by AutoHotkey. WMI is a powerful feature of Windows that allows scripts to interact with various system components, including hardware sensors.

Step 1: Importing the WMI Library

First, we need to import the WMI library into our AutoHotkey script. This can be done using the `Include` directive:

ahk
Include, WMI.ahk

The `WMI.ahk` file is a standard AutoHotkey library that provides access to WMI data. You can download it from the AutoHotkey community or create your own version.

Step 2: Retrieving CPU Temperature

Once the WMI library is imported, we can retrieve the CPU temperature using the following code snippet:

ahk
; Define the WMI query to retrieve CPU temperature
query := "SELECT FROM MSAcpi_ThermalZoneTemperature"

; Execute the query and store the result in a variable
tempResult := WMI.Query(query)

; Check if the query was successful
if (ErrorLevel) {
MsgBox, Failed to retrieve CPU temperature.
return
}

; Extract the temperature value from the result
temp := tempResult.Item(1).CurrentTemperature / 10

; Display the temperature
MsgBox, Current CPU temperature: %temp%°C

This code snippet defines a WMI query to retrieve the temperature from the first thermal zone (which typically corresponds to the CPU). The `CurrentTemperature` property is then divided by 10 to convert it from tenths of degrees Kelvin to Celsius.

Triggering an Alarm

Now that we have the CPU temperature, we can set up an alarm to notify the user if the temperature exceeds a certain threshold.

Step 1: Defining the Alarm Threshold

First, we need to define the temperature threshold at which the alarm will trigger. For example, let's set the threshold to 75°C:

ahk
threshold := 75

Step 2: Checking the Temperature and Triggering the Alarm

We can now modify the previous code snippet to include the alarm functionality:

ahk
; ... (previous code to retrieve CPU temperature)

; Check if the temperature exceeds the threshold
if (temp > threshold) {
MsgBox, Warning: CPU temperature is too high! %temp%°C
; Additional actions can be taken here, such as sending an email or executing a shutdown command
}

; Wait for a specified interval before checking the temperature again
Sleep, 60000 ; Wait for 1 minute

This code snippet checks if the CPU temperature exceeds the defined threshold and displays a message box if it does. You can also add additional actions, such as sending an email or executing a shutdown command, to handle the situation more effectively.

Conclusion

In this article, we have explored how to create an AutoHotkey script that monitors the system's CPU temperature and triggers an alarm if the temperature exceeds a certain threshold. By using the WMI library and the `Sleep` command, we can periodically check the temperature and ensure that the system remains within safe operating conditions.

This practical application can be a valuable tool for system administrators or users who want to keep an eye on their hardware's health. With AutoHotkey's flexibility and the power of WMI, you can extend this script to monitor other system resources or create more complex automation solutions.