Hi deanMKD1
I have been playing with the code, I have change the file names init.php to config.php
the config.php is in sub folder called "include," the init.php is no cluttered, this my fist attempt to get this working
There is some problems in the code that I have change , hope you can fill in the gaps.
I'm new to this PHP to MySQL, will keep on working on it hope to get it working 100%.
Here is the config.php code:
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) die('Could not connect: ' . mysql_error());
mysql_select_db('tikstat');
$db_host="localhost";
$db_user="root";
$db_pass="password";
$db_database="tikstat";
global $db;
$db = new mysqli($db_host, $db_user, $db_pass, $db_database);
if ($db->connect_error) die('Could not connect');
?>
Here is the collector.php code:
<?php
/*
/collector.php?sn=<SERIAL NUMBER>&tx=<INTERFACE OUR BYTES>&rx=<INTERFACE IN BYTES>
*/
include("include/config.php");
// Check input data
if (isset($_GET[sn])
and isset($_GET[tx]) and is_numeric($_GET[tx])
and isset($_GET[rx]) and is_numeric($_GET[rx])) {
$device_serial = substr($_GET[sn], 0, 12);
} else {
echo 'No code fail';
exit;
}
// Create new devices if not exist
$link = "insert into devices(id, last_tx, last_rx, FROM devices WHERE sn='".$device_serial."'";
$link = mysql_query($db);
if (!is_numeric($device[0][id])) {
$link = "INSERT INTO devices(sn, last_check, last_tx, last_rx) VALUES ('".$device_serial."', '".time()."', '".$_GET[tx]."', '".$_GET[rx]."')";
$device[0][id] = $db->lastInsertRowid();
$txBytes = $_GET[tx];
$rxBytes = $_GET[rx];
$link = mysql_query($db);
echo 'INSERT INTO device';
} else {
// update last receiving data
$db = "UPDATE devices SET last_check='".time()."', last_tx='".$_GET[tx]."', last_rx='".$_GET[rx]."' WHERE id='".$device[0][id]."'";
// check last received value
if ($device[0][last_tx] > $_GET[tx]) {
$txBytes = $_GET[tx];
} else {
$txBytes = $_GET[tx]-$device[0][last_tx];
}
if ($device[0][last_rx] > $_GET[rx]) {
$rxBytes = $_GET[rx];
} else {
$rxBytes = $_GET[rx]-$device[0][last_rx];
}
$link = mysql_query($db);
}
$db = "SELECT * FROM traffic WHERE device_id='".$device[0][id]."' AND datetime = ".mktime(date('H'),0,0)." LIMIT 1";
$link = mysql_query($db);
if (is_numeric($link[0][id])) {
$db = "UPDATE traffic SET tx='".($link[0][tx]+$txBytes)."', rx='".($link[0][rx]+$rxBytes)."' WHERE id='".$link[0][id]."'";
$link = mysql_query($db);
} else {
$db = "INSERT INTO traffic(device_id, datetime, tx, rx) VALUES (".$device[0][id].", ".mktime(date('H'),0,0).", ".$txBytes.", ".$rxBytes.")";
$link = mysql_query($db);
}
?>