Rename Speedtest class

This commit is contained in:
Ely Deckers 2022-02-20 14:55:49 +01:00
parent 20b631cf9b
commit f2bdbd222c

View File

@ -328,6 +328,16 @@ class Util
return $result; return $result;
} }
public static function space_to_underscore($value)
{
return str_replace(" ", "__", $value);
}
public static function underscore_to_space($value)
{
return str_replace("__", " ", $value);
}
} }
class Interfaces class Interfaces
@ -439,7 +449,7 @@ class Discoveries
$id = is_null($maybe_id) ? "" : $service[$maybe_id]; $id = is_null($maybe_id) ? "" : $service[$maybe_id];
return [ return [
"{#SERVICE}" => str_replace(" ", "__", $service["name"]) . $id, "{#SERVICE}" => sprintf("%s%s", Util::space_to_underscore($service["name"]), $id),
"{#DESCRIPTION}" => $service["description"], "{#DESCRIPTION}" => $service["description"],
]; ];
}, $named_services)); }, $named_services));
@ -483,7 +493,7 @@ class Discoveries
$leases = PfEnv::system_get_dhcpleases(); $leases = PfEnv::system_get_dhcpleases();
self::print_json(array_map(fn($data) => [ self::print_json(array_map(fn($data) => [
"{#FAILOVER_GROUP}" => str_replace(" ", "__", $data["name"]), "{#FAILOVER_GROUP}" => Util::space_to_underscore($data["name"]),
], $leases["failover"])); ], $leases["failover"]));
} }
@ -540,7 +550,7 @@ class Discoveries
} }
} }
class Speedtest class SpeedTest
{ {
public static function interface_value($if_name, $value) public static function interface_value($if_name, $value)
{ {
@ -551,18 +561,22 @@ class Speedtest
return; return;
} }
$speedtest_data = json_decode(file_get_contents($filename), true); $speed_test_data = json_decode(file_get_contents($filename), true);
if (array_key_exists($value, $speedtest_data)) { if (array_key_exists($value, $speed_test_data)) {
return; return;
} }
echo empty($tv1) ? $speedtest_data[$value] : $speedtest_data[$tv0][$tv1]; echo empty($tv1) ? $speed_test_data[$value] : $speed_test_data[$tv0][$tv1];
} }
public static function cron_install($enable = true) public static function cron_install($enable = true)
{ {
$command = "/usr/local/bin/php " . __FILE__ . " speedtest_cron"; PfEnv::install_cron_job(
PfEnv::install_cron_job($command, $enable, "*/15", "*", "*", "*", "*", "root", true); implode(" ", ["/usr/local/bin/php", __FILE__, "speedtest_cron"]),
$enable,
"*/15", "*", "*", "*", "*",
"root",
true);
} }
public static function exec($if_name, $ip_address) public static function exec($if_name, $ip_address)
@ -619,21 +633,22 @@ class Commands
{ {
$gws = PfEnv::return_gateways_status(true); $gws = PfEnv::return_gateways_status(true);
$is_known_gw = array_key_exists($gw, $gws); $maybe_gw = array_key_exists($gw, $gws) ? $gws[$gw] : null;
if (!$is_known_gw) { if (!$maybe_gw) {
return; return Util::result("");
} }
$value = $gws[$gw][$value_key]; $value = $maybe_gw[$value_key];
if ($value_key != "status") { if ($value_key != "status") {
echo $value; return Util::result($value);
return;
} }
// Issue #70: Gateway Forced Down $substatus = $maybe_gw["substatus"];
$v = ($gws[$gw]["substatus"] != "none") ? $gws[$gw]["substatus"] : $value; $has_relevant_substatus = $substatus != "none"; // Issue #70: Gateway Forced Down
echo self::get_value_mapping("gateway.status", $v); return Util::result(self::get_value_mapping(
"gateway.status",
$has_relevant_substatus ? $substatus : $value));
} }
public static function gw_status() public static function gw_status()
@ -646,8 +661,8 @@ class Commands
public static function if_speedtest_value($if_name, $value) public static function if_speedtest_value($if_name, $value)
{ {
Speedtest::cron_install(); SpeedTest::cron_install();
Speedtest::interface_value($if_name, $value); SpeedTest::interface_value($if_name, $value);
} }
public static function openvpn_servervalue(int $server_id, $value_key) public static function openvpn_servervalue(int $server_id, $value_key)
@ -701,7 +716,7 @@ class Commands
public static function service_value(string $name, string $value) public static function service_value(string $name, string $value)
{ {
$sanitized_name = str_replace("__", " ", $name); $sanitized_name = Util::underscore_to_space($name);
// List of service which are stopped on CARP Slave. // List of service which are stopped on CARP Slave.
// For now this is the best way I found for filtering out the triggers // For now this is the best way I found for filtering out the triggers
@ -876,13 +891,13 @@ class Commands
public static function speedtest_cron() public static function speedtest_cron()
{ {
foreach (Interfaces::retrieve_wan_interfaces() as $if_info) { foreach (Interfaces::retrieve_wan_interfaces() as $if_info) {
Speedtest::exec($if_info["hwif"], $if_info["ipaddr"]); SpeedTest::exec($if_info["hwif"], $if_info["ipaddr"]);
} }
} }
public static function cron_cleanup() public static function cron_cleanup()
{ {
Speedtest::cron_install(false); SpeedTest::cron_install(false);
} }
// S.M.A.R.T Status // S.M.A.R.T Status