getting temp under control
This commit is contained in:
1 parent
9077f7e0a7
commit
10958450ba
4 files changed
+427
-1
No files matched your search
@@ -0,0 +1,101 @@
|
||||
# Temperature Monitor Script
|
||||
|
||||
A nushell script for monitoring your laptop's CPU/GPU temperatures, storing them with timestamps, and visualizing them as a graph.
|
||||
|
||||
## Features
|
||||
|
||||
- **Real-time monitoring**: Collects temperature readings at configurable intervals
|
||||
- **Persistent storage**: Saves all readings to a CSV file with timestamps
|
||||
- **Multiple sources**: Tries multiple methods to read temperature data (lm_sensors, acpi, sysfs)
|
||||
- **ASCII graphs**: Displays temperature data as an ASCII bar graph
|
||||
- **Flexible configuration**: Customize duration, interval, and output location
|
||||
|
||||
## Requirements
|
||||
|
||||
The script automatically detects and uses available tools. It will try these in order:
|
||||
1. `sensors` (from `lm_sensors`) - **Recommended** - Most reliable
|
||||
2. `acpi` - Fallback option
|
||||
3. `/sys/class/thermal/thermal_zone0/temp` - Linux sysfs fallback
|
||||
|
||||
On NixOS, you can add these to your environment with:
|
||||
```nix
|
||||
environment.systemPackages = with pkgs; [
|
||||
lm_sensors
|
||||
acpi
|
||||
];
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic usage (60-second collection with 2-second intervals)
|
||||
```bash
|
||||
./bin/temp-monitor
|
||||
```
|
||||
|
||||
### Collect for 5 minutes with 5-second intervals
|
||||
```bash
|
||||
./bin/temp-monitor --duration 300 --interval 5
|
||||
```
|
||||
|
||||
### Collect and display graph
|
||||
```bash
|
||||
./bin/temp-monitor --duration 60 --graph
|
||||
```
|
||||
|
||||
### Custom output file location
|
||||
```bash
|
||||
./bin/temp-monitor --output ~/my-temps.csv --duration 120 --graph
|
||||
```
|
||||
|
||||
### View stored data
|
||||
```bash
|
||||
cat ~/.cache/temp-data.csv
|
||||
```
|
||||
|
||||
## Command-line Options
|
||||
|
||||
- `--duration <seconds>` - How long to collect data (default: 60)
|
||||
- `--interval <seconds>` - Seconds between readings (default: 2)
|
||||
- `--output <path>` - CSV file location (default: ~/.cache/temp-data.csv)
|
||||
- `--graph` - Display ASCII graph after collection completes
|
||||
|
||||
## Output Format
|
||||
|
||||
The script stores data in CSV format with columns:
|
||||
```
|
||||
timestamp,cpu_temp,gpu_temp,max_temp
|
||||
2024-07-11 14:30:45,65.5,N/A,65.5
|
||||
2024-07-11 14:30:47,66.2,N/A,66.2
|
||||
```
|
||||
|
||||
## Graph Example
|
||||
|
||||
```
|
||||
2024-07-11 14:30:45: ████████████ 65.5°C
|
||||
2024-07-11 14:30:47: ██████████████ 66.2°C
|
||||
2024-07-11 14:30:49: ████████████████ 68.1°C
|
||||
|
||||
Min: 65.5°C | Max: 68.1°C | Range: 2.6°C
|
||||
```
|
||||
|
||||
## Tips
|
||||
|
||||
- Run multiple instances with different `--output` files to track different time periods
|
||||
- Use cron to collect continuous background data: `*/5 * * * * ~/nixos-setup/bin/temp-monitor --duration 1 --interval 1`
|
||||
- High temperatures may indicate a need for thermal paste replacement or fan cleaning
|
||||
- The graph is great for spotting thermal throttling or fan ramp-ups during workloads
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**No temperature readings (showing "N/A")**:
|
||||
- Check if `sensors` is installed and working: `sensors`
|
||||
- Check thermal zone: `cat /sys/class/thermal/thermal_zone0/temp`
|
||||
- Run `sudo sensors-detect` to configure lm_sensors on first use
|
||||
|
||||
**Permission denied reading temperatures**:
|
||||
- Some systems require sudo for detailed sensor access
|
||||
- Try: `sudo ./bin/temp-monitor`
|
||||
|
||||
**CSV file grows too large**:
|
||||
- Archive old data: `mv ~/.cache/temp-data.csv ~/.cache/temp-data-$(date +%s).csv`
|
||||
- Or use `--output` with a date-stamped filename
|
||||
Executable
+202
@@ -0,0 +1,202 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
# Temperature monitoring script for laptop
|
||||
# Collects CPU/GPU temperatures, stores them with timestamps, and displays a graph
|
||||
|
||||
def main [
|
||||
--duration: int = 60 # Duration to collect data in seconds (default: 60)
|
||||
--interval: int = 2 # Interval between readings in seconds (default: 2)
|
||||
--output: string = "~/.cache/temp-data.csv" # Output file for temperature data
|
||||
--graph # Show graph at the end
|
||||
--show-graph # Just show graph from existing data (don't collect)
|
||||
] {
|
||||
let data_file = ($output | path expand)
|
||||
|
||||
if $show_graph {
|
||||
show-graph $data_file
|
||||
return
|
||||
}
|
||||
|
||||
let cache_dir = ($data_file | path dirname)
|
||||
|
||||
# Create cache directory if it doesn't exist
|
||||
if not ($cache_dir | path exists) {
|
||||
mkdir $cache_dir
|
||||
}
|
||||
|
||||
# Initialize or append to CSV file with headers if empty
|
||||
if not ($data_file | path exists) or (open $data_file | is-empty) {
|
||||
"timestamp,cpu_temp,gpu_temp,max_temp" | save -f $data_file
|
||||
}
|
||||
|
||||
print $"🌡️ Temperature Monitor - Collecting data for ($duration)s with ($interval)s intervals..."
|
||||
print $"📊 Data will be saved to: ($data_file)\n"
|
||||
|
||||
let iterations = (($duration / $interval) | math round)
|
||||
|
||||
for i in 1..$iterations {
|
||||
let timestamp = (date now | format date '%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# Get temperature reading
|
||||
let temp_info = (get-temperature)
|
||||
|
||||
let cpu_temp = $temp_info.cpu
|
||||
let gpu_temp = $temp_info.gpu
|
||||
|
||||
let max_temp = (
|
||||
if ($cpu_temp != "N/A") and ($gpu_temp != "N/A") {
|
||||
[($cpu_temp | into float) ($gpu_temp | into float)] | math max
|
||||
} else if ($cpu_temp != "N/A") {
|
||||
$cpu_temp | into float
|
||||
} else if ($gpu_temp != "N/A") {
|
||||
$gpu_temp | into float
|
||||
} else {
|
||||
"N/A"
|
||||
}
|
||||
)
|
||||
|
||||
let row = $"($timestamp),($cpu_temp),($gpu_temp),($max_temp)"
|
||||
|
||||
# Append to CSV file
|
||||
let content = (open $data_file --raw)
|
||||
($content | str trim) + "\n" + $row | save -f $data_file
|
||||
|
||||
print $"✓ ($timestamp) | CPU: ($cpu_temp)°C | GPU: ($gpu_temp)°C | Max: ($max_temp)°C"
|
||||
|
||||
if $i < $iterations {
|
||||
sleep ($interval | into duration -u sec)
|
||||
}
|
||||
}
|
||||
|
||||
print $"\n✅ Data collection complete!"
|
||||
|
||||
# Always show the graph at the end
|
||||
show-graph $data_file
|
||||
|
||||
print $"📁 Data saved to: ($data_file)"
|
||||
}
|
||||
|
||||
def get-temperature [] {
|
||||
# Try lm_sensors first (most reliable)
|
||||
if (which sensors | is-not-empty) {
|
||||
get-temp-sensors
|
||||
} else if (which acpi | is-not-empty) {
|
||||
get-temp-acpi
|
||||
} else if ("/sys/class/thermal/thermal_zone0/temp" | path exists) {
|
||||
get-temp-sysfs
|
||||
} else {
|
||||
{ cpu: "N/A", gpu: "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
def get-temp-sensors [] {
|
||||
try {
|
||||
# Try to get CPU temp from k10temp (AMD)
|
||||
let cpu_temp = (
|
||||
try {
|
||||
sensors k10temp-pci-00c3 | grep "Tctl" | grep -oE "[0-9]+\\.[0-9]+" | head -1
|
||||
} catch {
|
||||
# Fallback: look in all sensors output
|
||||
sensors | grep -i "Tctl\|Package" | head -1 | grep -oE "[0-9]+\\.[0-9]+" | head -1
|
||||
}
|
||||
)
|
||||
|
||||
# Try to get GPU temp from amdgpu
|
||||
let gpu_temp = (
|
||||
try {
|
||||
sensors amdgpu-pci-c100 | grep "edge" | grep -oE "[0-9]+\\.[0-9]+" | head -1
|
||||
} catch {
|
||||
"N/A"
|
||||
}
|
||||
)
|
||||
|
||||
{ cpu: $cpu_temp, gpu: $gpu_temp }
|
||||
} catch {
|
||||
{ cpu: "N/A", gpu: "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
def get-temp-acpi [] {
|
||||
try {
|
||||
let temp = (acpi -t | head -1 | grep -oE "[0-9]+\\.[0-9]+" | first)
|
||||
{ cpu: $temp, gpu: "N/A" }
|
||||
} catch {
|
||||
{ cpu: "N/A", gpu: "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
def get-temp-sysfs [] {
|
||||
try {
|
||||
let raw_temp = (open /sys/class/thermal/thermal_zone0/temp | str trim | into float)
|
||||
let temp = ($raw_temp / 1000)
|
||||
{ cpu: $temp, gpu: "N/A" }
|
||||
} catch {
|
||||
{ cpu: "N/A", gpu: "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
def show-graph [file: string] {
|
||||
print "\n📈 Temperature Graph:\n"
|
||||
|
||||
try {
|
||||
# Read the CSV file as a regular table
|
||||
let data = (open $file)
|
||||
|
||||
# Filter out rows where max_temp is not a number
|
||||
let temps = (
|
||||
$data
|
||||
| where { $in.max_temp != "N/A" }
|
||||
| select timestamp max_temp
|
||||
| rename time temp
|
||||
| each { |row|
|
||||
{
|
||||
time: $row.time
|
||||
temp: ($row.temp | into float)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if ($temps | is-empty) {
|
||||
print "No valid temperature data to display."
|
||||
return
|
||||
}
|
||||
|
||||
# Find min/max for scaling
|
||||
let min_temp = ($temps | get temp | math min)
|
||||
let max_temp = ($temps | get temp | math max)
|
||||
let temp_range = ($max_temp - $min_temp)
|
||||
let graph_height = 20
|
||||
|
||||
# Create simple ASCII graph
|
||||
let graph = (
|
||||
$temps
|
||||
| each { |item|
|
||||
let normalized = (
|
||||
if ($temp_range == 0) {
|
||||
0.5
|
||||
} else {
|
||||
(($item.temp - $min_temp) / $temp_range)
|
||||
}
|
||||
)
|
||||
let bar_height = (($normalized * $graph_height) | math round)
|
||||
let bar = (
|
||||
if $bar_height > 0 {
|
||||
0..<$bar_height | each { "█" } | str join ""
|
||||
} else {
|
||||
"."
|
||||
}
|
||||
)
|
||||
let time_only = ($item.time | str substring 11..19)
|
||||
$"($time_only): ($bar) ($item.temp)°C"
|
||||
}
|
||||
| str join "\n"
|
||||
)
|
||||
|
||||
print $graph
|
||||
print $"\nMin: ($min_temp | math round -p 1)°C | Max: ($max_temp | math round -p 1)°C | Range: ($temp_range | math round -p 1)°C"
|
||||
} catch { |err|
|
||||
print $"Error displaying graph: ($err)"
|
||||
}
|
||||
}
|
||||
|
||||
main
|
||||
Reference in new issue
Block a user