Try: ca/tecreations/ViewFile.php
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/tec_SiteRoot.php";
global $traffic;
$traffic = "traffic";
$parts = getProcNetDevParts();
$eth0Index = getEth0Index($parts);
$b1 = getTrafficBytes();
function createTableTraffic() {
global $site_db, $traffic;
$sql = "CREATE TABLE $traffic (_time_local VARCHAR(26), _bytes int(16))";
$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 doInsert($bytes) {
global $site_db, $nowMicro, $traffic;
$site_db->issue("INSERT INTO $traffic (_time_local,_bytes) VALUES('$nowMicro','$bytes')");
}
function dropTableIfExists($name) {
global $site_db;
$site_db->setDebug(true);
$site_db->issue("DROP TABLE IF EXISTS $name");
}
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");
return $result['rows']['_bytes'];
}
function getLastTime() {
global $site_db;
$result = $site_db->select("SELECT _time_local FROM traffic");
return $result['rows']['_time_local'];
}
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();
insert(getTrafficBytes());
}
function insert($bytes) {
global $site_db, $nowMicro;
$bytes = getTrafficBytes();
doInsert($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);
}
$t0 = getLastTime();
$b0 = getLastBytes();
global $site_db, $traffic;
$output = $site_db->describe($traffic);
print_r($output);
print("<br />");
$site_db->setDebug(false);
if (!$site_db->hasTable($traffic)) {
init();
}
$bytes = (getTrafficBytes() - $b0);
$timeDiff = (getSecondsBetween_Timestamp(getLastTime(),$nowMicro));
print("Bytes: " . $bytes . "<br />");
print("Time(seconds): " . $timeDiff . "<br />");
print("bps: " . ($bytes/$timeDiff) . "<br />");
if (hasRecords()) {
updateTraffic(getTrafficBytes());
} else {
insert(getTrafficBytes());
}