AutoHotkey Language for System Performance Monitoring
Introduction
AutoHotkey (AHK) is a scripting language for automating the Windows GUI and general scripting. It is widely used for creating hotkeys, automating repetitive tasks, and even for system performance monitoring. In this article, we will explore how to use AutoHotkey to monitor various aspects of your system's performance, such as CPU usage, memory consumption, disk I/O, and network activity. By the end of this article, you will have a solid understanding of how to write scripts that can help you keep an eye on your system's health and performance.
Prerequisites
Before we dive into the code, make sure you have the following prerequisites:
1. AutoHotkey installed on your system: You can download it from the official website (https://www.autohotkey.com/).
2. Basic knowledge of AutoHotkey syntax and functions: If you are new to AutoHotkey, it is recommended to go through the official documentation (https://www.autohotkey.com/docs/AHK.htm) to familiarize yourself with the language.
Monitoring CPU Usage
To monitor CPU usage in AutoHotkey, we can use the `Process` object, which provides information about the system's processes. The following script will display the current CPU usage every second:
ahk
Persistent
SingleInstance, Force
SetTimer, UpdateCPU, 1000
UpdateCPU:
Process, Exist, ahk_class System Tray
If ErrorLevel {
CPUUsage := ProcessGetCPU()
MsgBox, CPU Usage: %CPUUsage% %
}
return
In this script, we use the `SetTimer` function to run the `UpdateCPU` subroutine every 1000 milliseconds (1 second). The `ProcessGetCPU` function retrieves the current CPU usage percentage. If the system tray is running (which is a good indicator that the system is active), we display the CPU usage in a message box.
Monitoring Memory Consumption
Monitoring memory consumption is also possible using the `Process` object. The following script will display the current memory usage every second:
ahk
Persistent
SingleInstance, Force
SetTimer, UpdateMemory, 1000
UpdateMemory:
Process, Exist, ahk_class System Tray
If ErrorLevel {
MemoryUsage := ProcessGetMem()
MsgBox, Memory Usage: %MemoryUsage% MB
}
return
In this script, we use the `ProcessGetMem` function to retrieve the current memory usage in megabytes. Similar to the CPU usage script, we display the memory usage in a message box if the system tray is running.
Monitoring Disk I/O
Monitoring disk I/O can be a bit more complex, as AutoHotkey does not provide built-in functions for this purpose. However, we can use the Windows Management Instrumentation (WMI) interface to retrieve disk I/O statistics. The following script will display the current disk I/O every second:
ahk
Persistent
SingleInstance, Force
SetTimer, UpdateDiskIO, 1000
UpdateDiskIO:
Process, Exist, ahk_class System Tray
If ErrorLevel {
WMI := ComObjCreate("WbemScripting.SWbemLocator")
WMI := WMI.ConnectServer(".")
Disks := WMI.ExecQuery("Select from Win32_PerfFormattedData_PerfDisk_PerfDiskPhysicalDisk")
Disks := Disks.Next()
AvgDiskReadsPerSec := Disks.AvgDiskReadsPerSec
AvgDiskWritesPerSec := Disks.AvgDiskWritesPerSec
AvgDiskReadTime := Disks.AvgDiskReadTime
AvgDiskWriteTime := Disks.AvgDiskWriteTime
MsgBox, Disk I/O: Reads: %AvgDiskReadsPerSec% / Writes: %AvgDiskWritesPerSec% / Read Time: %AvgDiskReadTime% ms / Write Time: %AvgDiskWriteTime% ms
}
return
In this script, we use the `ComObjCreate` function to create a COM object that allows us to interact with WMI. We then query the `Win32_PerfFormattedData_PerfDisk_PerfDiskPhysicalDisk` class to retrieve disk I/O statistics. The script displays the average number of reads and writes per second, as well as the average read and write times in milliseconds.
Monitoring Network Activity
Monitoring network activity can be achieved using the `Netstat` command, which provides statistics about network connections. The following script will display the current network activity every second:
ahk
Persistent
SingleInstance, Force
SetTimer, UpdateNetwork, 1000
UpdateNetwork:
Process, Exist, ahk_class System Tray
If ErrorLevel {
Run, netstat -ano > %A_Temp%etstat.txt, , Hide
FileRead, OutputVar, %A_Temp%etstat.txt
MsgBox, Network Activity: %OutputVar%
FileDelete, %A_Temp%etstat.txt
}
return
In this script, we use the `Run` function to execute the `netstat -ano` command and redirect its output to a temporary file. We then read the contents of the file and display them in a message box. Finally, we delete the temporary file to clean up.
Conclusion
In this article, we have explored how to use AutoHotkey to monitor various aspects of your system's performance, including CPU usage, memory consumption, disk I/O, and network activity. By utilizing the `Process` object, WMI, and system commands, we have created scripts that can help you keep an eye on your system's health and performance. With these scripts, you can gain valuable insights into your system's behavior and take appropriate actions to optimize its performance.
Comments NOTHING