rbicelli / pfsense-zabbix-template

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

Multi pfSense setup Speedtest #82

Closed GuillaumeHullin closed 3 years ago

GuillaumeHullin commented 3 years ago

I have a network with 2 pfSense running with the same hardware ISP routers... the problem I encounter is that the cron task for the speedtest is starting a test in the same time for both pfSense. Obviously that create a wrong result cause they stress the upload and download in the same time.

My solution is bit "random" so if somebody got a better one I'll take it :)

function pfz_speedtest_exec ($ifname, $ipaddr){

    // Sleep random delay in order to avoid problem when 2 pfSense on the same Internet line
    sleep (rand ( 1, 60));

    $filename = "/tmp/speedtest-$ifname";
    $filetemp = "$filename.tmp";
    $filerun = "/tmp/speedtest-run"; 

    if ( (time()-filemtime($filename) > SPEEDTEST_INTERVAL * 3600) || (file_exists($filename)==false) ) {
        // file is older than SPEEDTEST_INTERVAL
        if ( (time()-filemtime($filerun) > 180 ) ) @unlink($filerun);

        if (file_exists($filerun)==false) {
            touch($filerun);
            $st_command = "/usr/local/bin/speedtest --source $ipaddr --json > $filetemp";
            exec ($st_command);
            rename($filetemp,$filename);
            @unlink($filerun);
        }
    }   

    return true;

I added a random sleep between 1 and 60 seconds...

rbicelli commented 3 years ago

The solution looks good to me. One possible solution could be to put a

sleep $(($RANDOM%60)) && 

In the cron line, but I think your solution is the proper one. At this point I would raise the random interval up to 90 seconds, since some speedtest goes over 30 seconds (and this is why I needed to pursue the cron approach)

GuillaumeHullin commented 3 years ago

I see, originally I went for 120 but I got some timeout while testing. Seems to work with 90.

rbicelli commented 3 years ago

Added 90 secs random value to speedtest execution.