CPU Usage
Retrieves the current CPU usage (percent) of the system.
Import this State Bar Item to WebSSH
JavaScript Content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(function() {
// Check if the SSH connection is active, if not, return empty label and default icon
if (!$ssh.isConnected()) {
return {
label: '',
icon: 'tachometer'
};
}
/**
* Parse the first “cpu” line of /proc/stat and compute
* - total time (sum of all fields)
* - idle time (idle + iowait)
*
* @param {string} statOutput Full content of /proc/stat
* @returns {{ total: number, idle: number }}
*/
function extractCpuTimes(statOutput) {
// Find the line that starts with “cpu ’ (aggregate over all cores)
const line = statOutput.split('\n').find(l => l.startsWith('cpu '));
// Split on whitespace, discard the “cpu” label, take the next 7 fields:
// user, nice, system, idle, iowait, irq, softirq
const parts = line.trim().split(/\s+/).slice(1, 8).map(Number);
// Sum all fields to get the “total” jiffies since boot
const total = parts.reduce((sum, val) => sum + val, 0);
// Idle time is idle + iowait (fields 3 and 4 in our zero-based array)
const idle = parts[3] + parts[4];
return { total, idle };
}
// Read the current /proc/stat over SSH, typical output:
// cpu 2255 34 2290 22625563 6290 127 456 0 0 0
// cpu0 1132 19 1441 11311718 3675 44 240 0 0 0
// cpu1 1123 15 849 11313845 2615 83 216 0 0 0
// intr 114930548 113199788 3 0 5 ...
// ctxt 1990473
// btime 1612191234
// processes 2915
// procs_running 1
// procs_blocked 0
const procStatContent = $ssh.exec('cat /proc/stat');
// Check if the command was successful
if (!procStatContent) {
console.warn('Failed to read /proc/stat');
// If the command fails, return empty label and default icon
return {
label: '',
icon: 'tachometer'
};
}
// Parse the fresh snapshot
const newStat = extractCpuTimes(procStatContent);
// Retrieve the previous snapshot from our vars store (or default to newStat on first run)
const oldStat = $vars.get('CPU_STAT', newStat);
// Save the new snapshot for next time
$vars.set('CPU_STAT', newStat);
// Compute differences in total time and idle time between snapshots
const totalDiff = newStat.total - oldStat.total;
const idleDiff = newStat.idle - oldStat.idle;
// Calculate CPU usage percentage over the interval
let usagePercent;
if (totalDiff > 0) {
// (busy time) / (total time) = (totalDiff – idleDiff) / totalDiff
usagePercent = Math.round(100 * (totalDiff - idleDiff) / totalDiff);
} else {
// If no time has elapsed (unlikely), consider usage 0%
usagePercent = 0;
}
// Choose an icon based on usage tiers
let icon;
if (usagePercent < 33) {
icon = 'gauge.with.dots.needle.0percent';
} else if (usagePercent < 50) {
icon = 'gauge.with.dots.needle.33percent';
} else if (usagePercent < 67) {
icon = 'gauge.with.dots.needle.50percent';
} else if (usagePercent < 80) {
icon = 'gauge.with.dots.needle.67percent';
} else {
icon = 'gauge.with.dots.needle.100percent';
}
// Return an object that e.g. your dashboard can use
return { label: usagePercent + '%', icon };
})();
Icon
tachometer
Compatibility
WebSSH >=29.3
iOS >=17.2
iPadOS >=17.2
macOS >=14.0
Supported Targets
Linux
macOS
FreeBSD
Windows
Needed Capabilities
The table below lists the capabilities required for this script to operate. Capabilities marked as “Safe” are generally considered secure. Capabilities with a “Warning” level indicate that, while they are typically acceptable, their safety cannot be fully guaranteed in all scenarios. For your peace of mind, we recommend reviewing the script before running it.
Capability Safety Level ssh_exec 🟠 Warning vars_get 🟢 Safe vars_set 🟢 Safe
Additional Information
This script retrieves the current CPU usage (percent) of the system by reading the /proc/stat file. It calculates the CPU usage based on the difference between the current and previous CPU times. The script uses a simple algorithm to determine the CPU usage percentage and returns it as a label with an appropriate icon.
Hope to be a cross-platform script in the future. Currently only Linux is supported.
This content is licensed under MIT by the author.Trending Tags