rbicelli / pfsense-zabbix-template

Zabbix Template for pfSense
Apache License 2.0
244 stars 107 forks source link

WAN without Gateway Speedtest #81

Closed GuillaumeHullin closed 2 years ago

GuillaumeHullin commented 3 years ago

So I got pfSense which run with the WAN interface WITHOUT gateway and where the IP address is a public IP.

In order to get the speedtest to work I changed this:

// Interface Discovery
// Improved performance
function pfz_interface_discovery($is_wan=false,$is_cron=false) {
    $ifdescrs = get_configured_interface_with_descr(true);
    $ifaces = get_interface_arr();
    $ifcs=array();
    $if_ret=array();

    $json_string = '{"data":[';

    foreach ($ifdescrs as $ifname => $ifdescr){
          $ifinfo = get_interface_info($ifname);
          $ifinfo["description"] = $ifdescr;
          $ifcs[$ifname] = $ifinfo;       
    }    

    foreach ($ifaces as $hwif) {

        $ifdescr = $hwif;
        $has_gw = false;
        $has_public_ip=false;
        $is_vpn = false;

        foreach($ifcs as $ifc=>$ifinfo){
                if ($ifinfo["hwif"] == $hwif){
                        $ifdescr = $ifinfo["description"];
                        if (array_key_exists("gateway",$ifinfo)) $has_gw=true;
                        if (filter_var($ifinfo["ipaddr"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) $has_public_ip=true; // https://stackoverflow.com/a/13818647/15093007                   
                        if (strpos($ifinfo["if"],"ovpn")!==false) $is_vpn=true;
                        break;
                }
        }

        if ( ($is_wan==false) ||  (($is_wan==true) && (($has_gw==true) || ($has_public_ip==true)) && ($is_vpn==false)) ) { 
            $if_ret[]=$hwif;
            $json_string .= '{"{#IFNAME}":"' . $hwif . '"';
            $json_string .= ',"{#IFDESCR}":"' . $ifdescr . '"';
            $json_string .= '},';
        }

    }
    $json_string = rtrim($json_string,",");
    $json_string .= "]}";

    if ($is_cron) return $if_ret;

    echo $json_string;
}

I added the variable $has_public_ip which, if true and no gateway... then the interface is WAN.

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;
            }
        }

        pfz_speedtest_exec($ifname, $ifinfo['ipaddr']);     

    }
}

I removed the redundant check around pfz_speedtest_exec()

With that changed, it works fine.

rbicelli commented 3 years ago

Hello @GuillaumeHullin, would you like to make a PR for this?

GuillaumeHullin commented 2 years ago

Hi @rbicelli, I'm not yet fully familiar with Github but I'll try to do that.

rbicelli commented 2 years ago

Sorry, dont't worry about the PR, didn't checked #71. Already manually merged. Will be in next commit.