Improved Speedtest.

This commit is contained in:
Riccardo Bicelli 2021-07-06 23:03:29 +02:00
parent 9adb054faa
commit 9bd55db4be

View File

@ -9,7 +9,7 @@ This program is licensed under Apache 2.0 License
//Some Useful defines //Some Useful defines
define('SPEEDTEST_INTERVAL',8); //Speedtest Interval (in hours) define('SPEEDTEST_INTERVAL', 8); //Speedtest Interval (in hours)
require_once('globals.inc'); require_once('globals.inc');
require_once('functions.inc'); require_once('functions.inc');
@ -93,18 +93,18 @@ function pfz_test(){
// Interface Discovery // Interface Discovery
// Improved performance // Improved performance
function pfz_interface_discovery($is_wan=false) { function pfz_interface_discovery($is_wan=false,$is_cron=false) {
$ifdescrs = get_configured_interface_with_descr(true); $ifdescrs = get_configured_interface_with_descr(true);
$ifaces = get_interface_arr(); $ifaces = get_interface_arr();
$ifcs=array(); $ifcs=array();
$if_ret=array();
$json_string = '{"data":['; $json_string = '{"data":[';
foreach ($ifdescrs as $ifname => $ifdescr){ foreach ($ifdescrs as $ifname => $ifdescr){
$ifinfo = get_interface_info($ifname); $ifinfo = get_interface_info($ifname);
$ifinfo["description"] = $ifdescr; $ifinfo["description"] = $ifdescr;
$ifcs[$ifname] = $ifinfo; $ifcs[$ifname] = $ifinfo;
} }
foreach ($ifaces as $hwif) { foreach ($ifaces as $hwif) {
@ -123,6 +123,7 @@ function pfz_interface_discovery($is_wan=false) {
} }
if ( ($is_wan==false) || (($is_wan==true) && ($has_gw==true) && ($is_vpn==false)) ) { if ( ($is_wan==false) || (($is_wan==true) && ($has_gw==true) && ($is_vpn==false)) ) {
$if_ret[]=$hwif;
$json_string .= '{"{#IFNAME}":"' . $hwif . '"'; $json_string .= '{"{#IFNAME}":"' . $hwif . '"';
$json_string .= ',"{#IFDESCR}":"' . $ifdescr . '"'; $json_string .= ',"{#IFDESCR}":"' . $ifdescr . '"';
$json_string .= '},'; $json_string .= '},';
@ -132,48 +133,79 @@ function pfz_interface_discovery($is_wan=false) {
$json_string = rtrim($json_string,","); $json_string = rtrim($json_string,",");
$json_string .= "]}"; $json_string .= "]}";
if ($is_cron) return $if_ret;
echo $json_string; echo $json_string;
} }
//Interface Speedtest //Interface Speedtest
function pfz_interface_speedtest_value($ifname, $value){ function pfz_interface_speedtest_value($ifname, $value){
$ifdescrs = get_configured_interface_with_descr(true); $tvalue = explode(".", $value);
$ifaces = get_interface_arr();
$pf_interface_name='';
$subvalue=false;
$tvalue = explode(".", $value);
if (count($tvalue)>1) { if (count($tvalue)>1) {
$value = $tvalue[0]; $value = $tvalue[0];
$subvalue = $tvalue[1]; $subvalue = $tvalue[1];
} }
foreach ($ifdescrs as $ifn => $ifd){
$ifinfo = get_interface_info($ifn);
if($ifinfo['hwif']==$ifname) {
$pf_interface_name = $ifn;
break;
}
}
//If the interface has a gateway is considered WAN, so let's do the speedtest //If the interface has a gateway is considered WAN, so let's do the speedtest
if (array_key_exists("gateway", $ifinfo)) { $filename = "/tmp/speedtest-$ifname";
$ipaddr = $ifinfo['ipaddr'];
$speedtest_data = pfz_speedtest_exec($pf_interface_name,$ipaddr); if (file_exists($filename)) {
if (array_key_exists($value,$speedtest_data)) { $speedtest_data = json_decode(file_get_contents($filename), true);
if (array_key_exists($value, $speedtest_data)) {
if ($subvalue == false) if ($subvalue == false)
echo $speedtest_data[$value]; echo $speedtest_data[$value];
else else
echo $speedtest_data[$value][$subvalue]; echo $speedtest_data[$value][$subvalue];
} }
} }
} }
function pfz_speedtest_cron(){
require_once("services.inc");
$ifdescrs = get_configured_interface_with_descr(true);
$ifaces = get_interface_arr();
$pf_interface_name='';
$subvalue=false;
$ifcs = pfz_interface_discovery(true, true);
foreach ($ifcs as $ifname) {
foreach ($ifdescrs as $ifn => $ifd){
$ifinfo = get_interface_info($ifn);
if($ifinfo['hwif']==$ifname) {
$pf_interface_name = $ifn;
break;
}
}
//If the interface has a gateway is considered WAN, so let's do the speedtest
if (array_key_exists("gateway", $ifinfo)) {
$ipaddr = $ifinfo['ipaddr'];
pfz_speedtest_exec($ifname, $ipaddr);
}
}
}
//installs a cron job for speedtests
function pfz_speedtest_cron_install($enable=true){
//Install Cron Job
$command = "/usr/local/bin/php " . __FILE__ . " speedtest_cron";
install_cron_job($command, $enable, $minute = "*/15", "*", "*", "*", "*", "root", false);
}
function pfz_speedtest_exec ($ifname, $ipaddr, $is_cron=false){ function pfz_speedtest_exec ($ifname, $ipaddr, $is_cron=false){
$filename = "/tmp/speedtest-$ifname"; $filename = "/tmp/speedtest-$ifname";
$filerun = "/tmp/speedtest-run"; $filerun = "/tmp/speedtest-run";
$filecron = "/tmp/speedtest.cron"; $filecron = "/tmp/speedtest.cron";
if (file_exists($filename)) { if (file_exists($filename)) {
$json_output = json_decode(file_get_contents($filename), true); $json_output = json_decode(file_get_contents($filename), true);
@ -199,6 +231,7 @@ function pfz_speedtest_exec ($ifname, $ipaddr, $is_cron=false){
return false; return false;
} }
// OpenVPN Server Discovery // OpenVPN Server Discovery
function pfz_openvpn_get_all_servers(){ function pfz_openvpn_get_all_servers(){
$servers = openvpn_get_active_servers(); $servers = openvpn_get_active_servers();
@ -1145,6 +1178,7 @@ switch (strtolower($argv[1])){
pfz_gw_rawstatus(); pfz_gw_rawstatus();
break; break;
case "if_speedtest_value": case "if_speedtest_value":
pfz_speedtest_cron_install();
pfz_interface_speedtest_value($argv[2],$argv[3]); pfz_interface_speedtest_value($argv[2],$argv[3]);
break; break;
case "openvpn_servervalue": case "openvpn_servervalue":
@ -1182,6 +1216,13 @@ switch (strtolower($argv[1])){
break; break;
case "file_exists": case "file_exists":
pfz_file_exists($argv[2]); pfz_file_exists($argv[2]);
break;
case "cron_speedtest":
pfz_speedtest_cron_install();
pfz_speedtest_cron();
break;
case "cron_cleanup":
pfz_speedtest_cron_install(false);
break; break;
default: default:
pfz_test(); pfz_test();