Skip to content

Commit 34f6acc

Browse files
Merge pull request #718 from jaredhendrickson13/next_patch
v2.5.2 Fixes
2 parents 4412f17 + 3ec5291 commit 34f6acc

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Fields/FloatField.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class FloatField extends RESTAPI\Core\Field {
144144

145145
# Otherwise, the internal value cannot be represented by this Field. Throw an error.
146146
throw new RESTAPI\Responses\ServerError(
147-
message: "Cannot parse FloatField '$this->name' from internal because its internal value is not
148-
a numeric value. Consider changing this field to a StringField.",
147+
message: "Cannot parse FloatField '$this->name' from internal because its internal value is not " .
148+
'a numeric value. Consider changing this field to a StringField.',
149149
response_id: 'FLOAT_FIELD_WITH_NON_FLOAT_INTERNAL_VALUE',
150150
);
151151
}

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.

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIModelsSystemStatusTestCase.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RESTAPI\Tests;
44

55
use RESTAPI\Core\Command;
6+
use RESTAPI\Core\Model;
67
use RESTAPI\Core\TestCase;
78
use RESTAPI\Models\SystemStatus;
89

@@ -44,4 +45,33 @@ class APIModelsSystemStatusTestCase extends TestCase {
4445
(new Command(command: '/bin/kenv -q smbios.bios.reldate 2>/dev/null', trim_whitespace: true))->output,
4546
);
4647
}
48+
49+
/**
50+
* Checks that the load average can can be read correctly despite the system's assign language. Normally,
51+
* the load average format can change based on the systems assigned language and cause issues parsing the
52+
* value into an array. This is a regression test for #716.
53+
*/
54+
public function test_language_does_not_change_cpu_load_avg(): void {
55+
# Change the language to German
56+
Model::set_config('system/language', 'de_DE');
57+
set_language();
58+
59+
# Ensure we can read the SystemStatus cpu_load_average without an error
60+
$this->assert_does_not_throw(
61+
callable: function () {
62+
$system_status = new SystemStatus();
63+
$this->assert_equals(count($system_status->cpu_load_avg->value), 3);
64+
},
65+
);
66+
67+
# Reset the language to English and check again
68+
Model::set_config('system/language', 'en_US');
69+
set_language();
70+
$this->assert_does_not_throw(
71+
callable: function () {
72+
$system_status = new SystemStatus();
73+
$this->assert_equals(count($system_status->cpu_load_avg->value), 3);
74+
},
75+
);
76+
}
4777
}

0 commit comments

Comments
 (0)