Skip to content

Commit 75de555

Browse files
fix: handle inconsistent delimiter for SystemStatus cpu_load_average #716
This commit fixes a bug where pfSense's get_load_average() function returns a load average string that changes based on the system's language setting. Previously, the numbers in the load average string might be separated by just a space, or by a comma and a space. This inconsistency caused an error when the API tried to convert that string into a usable array of values. Now, we're ensuring the load average string is always delimited by a single space, no matter what language pfSense is using. This prevents the logic error and ensures the API can correctly convert the load average to its intended array representation.
1 parent 4412f17 commit 75de555

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/SystemStatus.inc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class SystemStatus extends Model {
116116
allow_null: true,
117117
read_only: true,
118118
many: true,
119-
delimiter: ', ',
119+
delimiter: ' ',
120120
help_text: 'The CPU load averages. The first value represents the load average for the last minute, the ' .
121121
'second value represents the load average for the last 5 minutes, and the third value represents the ' .
122122
'load average for the last 15 minutes.',
@@ -181,7 +181,7 @@ class SystemStatus extends Model {
181181
'mds_mitigation' => get_single_sysctl('hw.mds_disable_state'),
182182
'temp_c' => $this->get_system_temp(),
183183
'temp_f' => $this->get_system_temp(as_fahrenheit: true),
184-
'cpu_load_avg' => get_load_average(),
184+
'cpu_load_avg' => $this->get_cpu_load_average(),
185185
'cpu_count' => get_single_sysctl('hw.ncpu'),
186186
'cpu_usage' => $this->get_cpu_usage(),
187187
'mbuf_usage' => $this->get_mbuf_usage(),
@@ -223,6 +223,24 @@ class SystemStatus extends Model {
223223
return $as_fahrenheit ? $temp * 1.8 + 32 : $temp;
224224
}
225225

226+
/**
227+
* Obtains the CPU load average. This method is responsible for obtaining the raw load average string from pfSense
228+
* and ensuring it is formatted correctly before the cpu_load_average field splits it into an array.
229+
* @return string|null The CPU load average as a string or null if it could not be obtained.
230+
*/
231+
private function get_cpu_load_average(): ?string {
232+
# Obtain the load average using pfSense's get_load_average() function
233+
$load_avg = get_load_average();
234+
235+
# The system language may affect the load average's delimiter (missing a comma in some languages)
236+
# ensure values are only ever separated by a single space (no commas). #716
237+
if (str_contains(", ", $load_avg)) {
238+
$load_avg = str_replace(', ', ' ', $load_avg);
239+
}
240+
241+
return $load_avg ?? null;
242+
}
243+
226244
/**
227245
* Determines approximate CPU usage using the last minute load average.
228246
* @return float|null The CPU usage as a whole percentage.

0 commit comments

Comments
 (0)