Esempio n. 1
0
 protected function decode_point($point_node)
 {
     $t = array();
     $t['lat'] = (double) $point_node->getAttribute('lat');
     $t['lon'] = (double) $point_node->getAttribute('lon');
     if (is_NaN($t['lat']) or is_NaN($t['lon'])) {
         return false;
     }
     $timestr = $this->lookup_node($point_node, 'time')->nodeValue;
     $D = new \DateTime($timestr);
     $t['time'] = (int) $D->format('U');
     $t['ele'] = (double) $this->lookup_node($point_node, 'ele')->nodeValue;
     $t['speed'] = (double) $this->lookup_node($point_node, 'speed')->nodeValue;
     return $t;
 }
function interface_query($if)
{
    global $config;
    global $g;
    //set data files for appropriate interface
    $wandatalastfile = '/tmp/ifbwstats-' . $if . '.last';
    $wandataallfile = '/tmp/ifbwstats-' . $if . '.data';
    $wandatabackupfile = '/cf/conf/ifbwstats-' . $if . '.data';
    //assume max is 4GB because of the 32bit counter wrap
    $maxbytesin = 4294967296.0;
    $maxbytesout = 4294967296.0;
    //create (or clear if already used)  variables
    $wandatacurrent = array();
    $wandatalast = array();
    $wandataall = array();
    //----------start modified code insert from ifstats.php----------
    $ifinfo = array();
    $ifinfo['hwif'] = $config['interfaces'][$if]['if'];
    if (!$ifinfo['hwif']) {
        $ifinfo['hwif'] = $if;
    }
    $ifinfo['if'] = $ifinfo['hwif'];
    /* run netstat to determine link info */
    $linkinfo = "";
    unset($linkinfo);
    exec("/usr/bin/netstat -I " . $ifinfo['hwif'] . " -nWb -f link", $linkinfo);
    $linkinfo = preg_split("/\\s+/", $linkinfo[1]);
    if (preg_match("/\\*\$/", $linkinfo[0])) {
        $ifinfo['status'] = "down";
    } else {
        $ifinfo['status'] = "up";
    }
    if (preg_match("/^enc|^tun/i", $ifinfo['if'])) {
        $ifinfo['inbytes'] = $linkinfo[5];
        $ifinfo['outbytes'] = $linkinfo[8];
    } else {
        $ifinfo['inbytes'] = $linkinfo[6];
        $ifinfo['outbytes'] = $linkinfo[9];
    }
    //----------end modified code insert from ifstats.php----------
    //check for errors
    if (file_exists($wandatalastfile) && $ifinfo['inbytes'] == 0 && $ifinfo['outbytes'] == 0) {
        $ifinfo['status'] = "down";
    }
    if (is_NaN($ifinfo['inbytes']) || is_NaN($outinfo['inbytes'])) {
        $ifinfo['status'] = "down";
    }
    if ($ifinfo['status'] == "up") {
        $wandatacurrent[0] = $ifinfo['inbytes'];
        $wandatacurrent[1] = $ifinfo['outbytes'];
        if (file_exists($wandatalastfile)) {
            //read last read file
            $wandatalast = explode("|", file_get_contents($wandatalastfile));
        } else {
            $wandatalast = $wandatacurrent;
        }
        if (!is_numeric($wandatalast[0]) || is_NaN($wandatalast[1])) {
            $wandatalast = $wandatacurrent;
        }
        $fp = fopen($wandatalastfile, "w") or die("Error Reading File");
        fwrite($fp, $wandatacurrent[0] . '|' . $wandatacurrent[1]);
        fclose($fp);
        //account for 4gig counter reset
        if ($wandatacurrent[0] < $wandatalast[0]) {
            $inbytes = 4294967296.0 - $wandatalast[0] + $wandatacurrent[0];
        } else {
            $inbytes = $wandatacurrent[0] - $wandatalast[0];
        }
        if ($wandatacurrent[1] < $wandatalast[1]) {
            $outbytes = 4294967296.0 - $wandatalast[1] + $wandatacurrent[1];
        } else {
            $outbytes = $wandatacurrent[1] - $wandatalast[1];
        }
        //check to make sure inbytes and outbytes are possible, if not, 0 both and erase last data as it may be corrupt
        if ($inbytes < 0 || $inbytes > $maxbytesin || $outbytes < 0 || $outbytes > $maxbytesout) {
            $inbytes = 0;
            $outbytes = 0;
            if (file_exists($wandatalastfile)) {
                unlink($wandatalastfile);
            }
        }
        $foundfile = 'null';
        if (file_exists($wandataallfile)) {
            $foundfile = $wandataallfile;
        } else {
            if (file_exists($wandatabackupfile)) {
                $foundfile = $wandatabackupfile;
            }
        }
        //if no file is found, create new data, else read file and add to existing data
        if ($foundfile == 'null') {
            $wanwritedata = date("Y-m-d") . '|' . $inbytes . '|' . $outbytes;
        } else {
            //read data file
            $wandataall = explode("\n", file_get_contents($foundfile));
            $n = count($wandataall);
            //if last line of data date matchs current date, add to totals, else add new line
            $dataset = explode("|", $wandataall[$n - 1]);
            if ($dataset[0] == date("Y-m-d")) {
                $dataset[1] = $dataset[1] + $inbytes;
                $dataset[2] = $dataset[2] + $outbytes;
                $wandataall[$n - 1] = $dataset[0] . '|' . $dataset[1] . '|' . $dataset[2];
            } else {
                $wandataall[$n] = date("Y-m-d") . '|' . $inbytes . '|' . $outbytes;
            }
            //number of data entries (days)
            $n = count($wandataall);
            //if more than three years worth of data, trim data to 4 years (1460 days)
            $start = 0;
            if ($n > 1460) {
                $start = $n - 1460;
            }
            //generate file data to write
            for ($i = $start; $i < $n - 1; $i++) {
                $wanwritedata = $wanwritedata . $wandataall[$i] . "\n";
            }
            $wanwritedata = $wanwritedata . $wandataall[$n - 1];
        }
        //write data file
        $fp = fopen($wandataallfile, "w") or die("Error Reading File");
        fwrite($fp, $wanwritedata);
        fclose($fp);
    } else {
        if (file_exists($wandatalastfile)) {
            unlink($wandatalastfile);
        }
    }
}