List

Try: ca/tecreations/ViewFile.php -- Online


<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/tec_SiteRoot.php";
global $traffic;
$traffic = "traffic";

function createTableTraffic() {
    global $site_db, $traffic;
    $sql = "CREATE TABLE $traffic (_time_local VARCHAR(26), _bytes VARCHAR(128) NOT NULL)";
    $site_db->setDebug(true);
    $site_db->issue($sql);
} 

function describe($table_name) {
     global $site_db;
     $output = $site_db->query("DESCRIBE $table_name");
     return $output;
}

function dropTableIfExists($name) {
    global $site_db;
    $site_db->setDebug(true);
    $site_db->issue("DROP TABLE IF EXISTS $name");
}

function getData() {
    global $site_db, $traffic;
    $result = $site_db->issue("SELECT * FROM $traffic");
    return $result;
}

function getEth0Index($parts) {
    for($i = 0; $i < count($parts); $i++) {
        if ($parts[$i] == "eth0:") return $i;        
    }
    return false;
}

function getLastBytes() {
    global $site_db;
    $result = $site_db->select("SELECT _bytes FROM traffic");
    if (is_array($result['rows'])) {
        if (count($result['rows']) == 1) {
            return (int)$result['rows']['_bytes'];
        } else {
            return 0;
        }
    }
}

function getLastTime() {
    global $site_db, $time;
    $result = $site_db->select("SELECT _time_local FROM traffic");
    if (is_array($result)  && $result['count'] != 0) {
        return $result['rows']['_time_local'];
    } else {
        return $time->getNowMicro(); 
    }
} 

function getProcNetDevParts() {
    $data = shell_exec("cat /proc/net/dev");
    $parts = explode(" ",$data);
    $nonEmpty = array();
    $count = 0;

    for($i = 0; $i < count($parts);$i++) {
        if ($parts[$i] != "") {
	    $nonEmpty[$count] = $parts[$i];
            $count++; 
        }
    }
    return $nonEmpty;
}

function getTrafficBytes() {
    $parts = getProcNetDevParts();
    $totalBytesIndex = getEth0Index($parts) + 1;
    return $parts[$totalBytesIndex];       
}


function hasRecords() {
    global $site_db, $traffic;
    $count = $site_db->select("SELECT COUNT(*) FROM $traffic");
    return $count > 0;
}

function init() {
    global $traffic;
    dropTableIfExists($traffic);
    createTableTraffic();
    replace(getTrafficBytes());
}

function replace($bytes) {
    global $site_db, $time, $traffic;
    $site_db->issue("DELETE FROM $traffic");
    $site_db->issue("INSERT INTO $traffic (_time_local,_bytes) VALUES('" . $time->getNowMicro() . "','$bytes')");
}

function printIfExists() {
    global $site_db, $traffic;
    if ($site_db->hasTable($traffic)) {
        print("Exists: $traffic<br />");
    } else { 
        print("Does not exist: $traffic<br />");
    }
    print("<br />");
}

function updateTraffic($bytes) {
    global $site_db, $nowMicro;
    $site_db->setDebug(true);
    $site_db->issue("DELETE FROM traffic");
    $bytes = getTrafficBytes();
    doInsert($bytes);
}

global $site_db, $traffic;
// ugh, test if description is equal to what we want? failing equality...
//init();
$data = getData();
if (is_array($data) && count($data) != 0) {
   init();
} else {
    $parts = getProcNetDevParts();
    print_r("Proc Parts: ");
    print_r($parts);
    print("<br />");
    $result = getData();
    print("Stored: ");
    print_r($result);
    print("<br />");
    $eth0Index = getEth0Index($parts);
    $bytesLarge = getTrafficBytes();

    $t0 = getLastTime(); 
    $bytesSmall = getLastBytes();


    //$output = $site_db->describe($traffic);
    //print("Described as: ");
    //print_r($output);
    //print("<br />");

    $bytes = $bytesLarge - $bytesSmall;

    global $time;
    $timeDiff = ($time->getSecondsBetween_Timestamp(getLastTime(),$time->getNowMicro()));
    print("Bytes: " . $bytes . "<br />");
    print("Time(seconds): " . $timeDiff . "<br />");
    if ((int)$timeDiff > 0) {
        print("bps: " . ($bytes/$timeDiff) . "<br />");
    }
    replace($bytesLarge);
}