Monitoring real-time data like CPU usage, RAM usage, CPU temperature, GPU temperature, and fan speed involves querying various system sensors and may require administrative privileges. Additionally, such operations are highly platform-specific.
For Windows, you can use the Windows Management Instrumentation (WMI) API or Performance Counters, while on Linux, you often read from system files or use specific libraries like lm-sensors. For other systems, the APIs and libraries may differ.
Below is an example of how to monitor CPU and RAM usage in C++ on Windows using the Windows Performance Counters:
#define _WIN32_DCOM
#include <iostream>
#include <Windows.h>
#include <Pdh.h>
#pragma comment(lib, "pdh.lib")
int main() {
PDH_HQUERY cpuQuery;
PDH_HCOUNTER cpuTotal;
PDH_HQUERY memoryQuery;
PDH_HCOUNTER memoryCounter;
PdhOpenQuery(NULL, NULL, &cpuQuery);
PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
PdhOpenQuery(NULL, NULL, &memoryQuery);
PdhAddCounter(memoryQuery, L"\\Memory\\% Committed Bytes In Use", NULL, &memoryCounter);
PdhCollectQueryData(memoryQuery);
while (true) {
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
std::cout << "CPU Usage: " << counterVal.doubleValue << "%" << std::endl;
PdhCollectQueryData(memoryQuery);
PdhGetFormattedCounterValue(memoryCounter, PDH_FMT_DOUBLE, NULL, &counterVal);
std::cout << "RAM Usage: " << counterVal.doubleValue << "%" << std::endl;
Sleep(1000);
}
PdhCloseQuery(memoryQuery);
PdhCloseQuery(cpuQuery);
return 0;
}
For temperature and fan speed, you often need direct access to the hardware sensors, which can be achieved with specialized libraries or by directly talking to the hardware through system-specific interfaces.
For example, on Linux, you might use the lm-sensors library to retrieve temperature and fan speed information:
// This example requires lm-sensors to be installed and initialized
// sudo apt-get install lm-sensors
// sudo sensors-detect
#include <sensors/sensors.h>
int main() {
sensors_init(nullptr);
// Iterate over all available features for all chips
const sensors_chip_name *chip_name;
int chip_nr = 0;
while ((chip_name = sensors_get_detected_chips(nullptr, &chip_nr)) != nullptr) {
const sensors_feature *feature;
int feature_nr = 0;
while ((feature = sensors_get_features(chip_name, &feature_nr)) != nullptr) {
double value;
if (sensors_get_value(chip_name, feature->number, &value) == 0) {
std::cout << feature->name << ": " << value;
if (feature->type == SENSORS_FEATURE_TEMP) {
std::cout << "°C" << std::endl;
} else if (feature->type == SENSORS_FEATURE_FAN) {
std::cout << " RPM" << std::endl;
} else {
std::cout << std::endl;
}
}
}
}
sensors_cleanup();
return 0;
}
Remember to install lm-sensors and run sensors-detect on Linux before using this code section.
Monitoring GPU temperature and other stats may require using the proprietary tools provided by GPU manufacturers, such as NVAPI for Nvidia GPUs and ADL for AMD GPUs. These APIs generally have their own set of functions to access such hardware-specific data.
Due to the level of detail and platform specificity required to implement a full monitoring application as you've described, I recommend looking into existing libraries or programs on your target platform that already provide these capabilities, and potentially interfacing with them or invoking them from your C++ code.
Always check the documentation for these APIs and libraries to ensure they're being used correctly and that they're compatible with the system and hardware you're targeting.