Skip to content

Instantly share code, notes, and snippets.

@frantic1048
Last active October 12, 2020 10:18
Show Gist options
  • Save frantic1048/41f56fd6328fa83ce6ad5acb3a4c0336 to your computer and use it in GitHub Desktop.
Save frantic1048/41f56fd6328fa83ce6ad5acb3a4c0336 to your computer and use it in GitHub Desktop.
KDE KSysGuard NVIDIA GPU temperature/memory/utilization sensor
#!/usr/bin/perl -w
# act as a KSysGuard sensor
# provides NVIDIA GPU info via `nvidia-settings`
# Usage (e.g. add gpu temperature sensor)
# 1. save this file, make sure it has a exec permission
# 2. in KSysGuard's menu, open `File` -> `Monitor Remote Machine`
# 3.1 in new dialog, type `Host` whatever you want
# 3.2 set `Connection Type` to `Custom command`
# 3.3 set `Command` like `path/to/this-sensor.pl`
# 4. click `OK`, now you can find new sensor named `gpu_temp`
# which is provides GPU temperature
# See Also
# https://techbase.kde.org/Development/Tutorials/Sensors
$|=1;
print "ksysguardd 1.2.0\n";
print "ksysguardd> ";
while(<>){
if(/monitors/){
print "gpu_temp\tinteger\n";
print "gpu_graphics\tinteger\n";
print "gpu_memory\tinteger\n";
print "gpu_video_engine\tinteger\n";
}
if(/gpu_temp/){
if(/\?/){
print "GPU Temp\t0\t0\n";
}else{
print `nvidia-settings -tq gpucoretemp | head -n1`;
}
}
if(/gpu_graphics/){
if(/\?/){
print "GPU\t0\t0\n";
}else{
print `nvidia-settings -tq [gpu:0]/GPUUtilization | awk -F"," '{print(substr(\$1,index(\$1,"=")+1))}'`;
}
}
if(/gpu_memory/){
if(/\?/){
print "GPU Memory\t0\t0\n";
}else{
print `nvidia-settings -tq [gpu:0]/GPUUtilization | awk -F"," '{print(substr(\$2,index(\$2,"=")+1))}'`;
}
}
if(/gpu_video_engine/){
if(/\?/){
print "Video Engine\t0\t0\n";
}else{
print `nvidia-settings -tq [gpu:0]/GPUUtilization | awk -F"," '{print(substr(\$3,index(\$3,"=")+1))}'`;
}
}
print "ksysguardd> ";
}
@frantic1048
Copy link
Author

@BryanByteZero , To make it work on AMD, you just need to find a way to get GPU information via command line, and extract the info you want (e.g. temperature), then replace the command in Line 31

Another way, if the sensor can be detected by lm_sensors, then you totally do not need this external script. KSysGuard has already got it. You can manually run sudo sensors-detect to find if there is any more usable sensor. For more about lm_sensors, you can take a look at https://wiki.archlinux.org/index.php/Lm_sensors#Setup

I'm not familiar with AMD command line tools. Good luck~

@sanwablo
Copy link

sanwablo commented Jan 4, 2017

Where do you add that code?

@hacker1024
Copy link

I've modified your script to add units and detect the amount of GPU memory.
https://gist.github.com/hacker1024/c01a773f50769bd8216fa01ea0a1ef33

@fonic
Copy link

fonic commented Dec 29, 2019

Fantastic, exactly what I was looking for. I'm using @hacker1024's modified version.

Two things should be added to the comments:

  • In KSysGuard, File -> Monitor Remote Machine is only available on custom/new tabs, but not on the default tabs, i.e. Process Table, System Load. Took me a while to figure that out.
  • nvidia-settings -tq all or nvidia-settings -q all may be used to list all queryable attributes in case one wants to add something

Many thanks!

@fonic
Copy link

fonic commented Dec 29, 2019

I ported your script to Bash, extended it and significantly lowered its CPU footprint:
https://gist.github.com/fonic/8f38e5e3ce5c8693ae3a23aa1af21fb9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment