Advertisement
| 05.15.2008 at 07:07PM PDT, ID: 23407260 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: |
<?php
require "./config_device.php";
$path = "./device_data/";
$failed_file_path = "./failed_device_data/";
$extension = ".csv";
/* Drop all tables in the database */
function drop_all_tables($database_name){
global $DB_SERVER;
global $DB_LOGIN;
global $DB_PASSWD;
$link = mysql_connect($DB_SERVER, $DB_LOGIN, $DB_PASSWD) or die("Could not connect");
mysql_select_db($database_name, $link) or die("Could not select database");
$query = "SHOW TABLES FROM $database_name";
$result = mysql_query($query, $link) or die("Query failed");
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$query = "DROP TABLE $row[0]";
mysql_query($query, $link) or die("Query failed: ".mysql_error());
}
mysql_free_result($result);
mysql_close($link);
}
/* Delete all device data files directory */
function delete_all_device_data_files(){
global $path;
rm_recursive($path);
mkdir($path, 0777);
chmod($path, 0777);
}
/* recursively remove everything in a directory, also including remove the directory itself */
function rm_recursive($filepath){
if (is_dir($filepath) && !is_link($filepath))
{
if ($dh = opendir($filepath))
{
while (($sf = readdir($dh)) !== false)
{
if ($sf == '.' || $sf == '..')
{
continue;
}
if (!rm_recursive($filepath.'/'.$sf))
{
throw new Exception($filepath.'/'.$sf.' could not be deleted.');
}
}
closedir($dh);
}
return rmdir($filepath);
}
return unlink($filepath);
}
/* Get a list of device from the database */
function get_list(){
$DB_SERVER = "10.247.2.1";
global $DB_LOGIN;
global $DB_PASSWD;
$link = mysql_connect($DB_SERVER, $DB_LOGIN, $DB_PASSWD) or die("Could not connect");
mysql_select_db("mcs", $link) or die("Could not select database");
$query = "SELECT DISTINCT(EquipID) FROM installation WHERE (EquipID like \"_________BSR\" or EquipID like \"_________WA_\" or EquipID like \"_________WB_\" or EquipID like \"_________WC_\" or EquipID like \"_________RA_\")
and status = \"Installed\" ORDER BY EquipID";
mysql_query("LOCK TABLES installation READ");
$str = mysql_query($query, $link) or die("Query failed");
mysql_query("UNLOCK TABLES");
mysql_close($link);
$i=0;
while($re = mysql_fetch_array($str,MYSQL_ASSOC))
{
$code[$i] = $re['EquipID'];
$i++;
}
return $code;
}
/* Get the data of a device from 61.10.0.168 */
function get_data($equipment_id){
change_cell(2, "fetching");
$command = "wget -O - http://username:password@61.10.0.168/cgi-bin/chkdocoper_chid.cgi?$equipment_id";
$result = shell_exec($command);
$first_pos = 0;
$last_pos = 0;
$table_pos = 0;
while(true){
$first_pos = strpos($result, "<table", $last_pos);
$last_pos = strpos($result, "</table>", $first_pos);
if($first_pos == 0 || $last_pos == 0){
break;
}
$table[$table_pos] = substr($result, $first_pos, $last_pos - $first_pos + 8);
$table_pos++;
}
change_cell(2, "done");
return $table;
}
/* Process data including remove tags and spaces */
function process_data($data){
change_cell(3, "processing");
$k = 0;
$modem_timeout = false;
for($i=0; $i<count($data); $i++){
$data[$i] = strip_tags($data[$i], '<td>');
$data[$i] = trim(preg_replace('/\s+/sim', ' ', $data[$i]));
preg_match_all('/<([^\s\/]+)([^>]+)>(.+?)<\/\1>/i', $data[$i], $matches);
for($j=0; $j<count($matches[3]); $j++){
if(strpos($matches[3]{$j}, "ModemTimeout!")!==false){
$modem_timeout = true;
}else{
$temp[$i][$j] = $matches[3]{$j};
}
}
if($modem_timeout == false){
$result[$k] = $temp[$i];
$k++;
}
$modem_timeout = false;
}
change_cell(3, "done");
return $result;
}
/* Write the data into a file */
function write_data($name, $data, $extension){
global $path;
change_cell(4, "writing");
$filename = $name.$extension;
$fp = fopen($path.$filename,"wb") or die("Could not create".$path.$filename);
for($i=0; $i<count($data); $i++){
for($j=0; $j<count($data[$i]); $j++){
fwrite($fp, '"'.$data[$i][$j].'"');
if($j+1 < count($data[$i])){
fwrite($fp, ',');
}
}
fwrite($fp, "\n");
}
fclose($fp);
change_cell(4, "done");
}
/* Delete failed device file */
function delete_failed_data(){
global $failed_file_path;
$filename = "failed_device.txt";
if(file_exists($failed_file_path.$filename)){
unlink($failed_file_path.$filename);
}
}
/* Write failed device name */
function write_failed_data($device_name){
global $failed_file_path;
$filename = "failed_device.txt";
//$filename = "failed_device ".date('Y-m-d Hi').".txt";
$fp = fopen($failed_file_path.$filename,"ab") or die("Could not create".$failed_file_path.$filename);
fwrite($fp, "$device_name\n");
fclose($fp);
}
/* Store the data in a database */
function store_data($name, $data){
global $DB_SERVER;
global $DB_LOGIN;
global $DB_PASSWD;
change_cell(5, "storing");
if(count($data) > 0){
$number_of_attribute = count($data[0]);
}
$link = mysql_connect($DB_SERVER, $DB_LOGIN, $DB_PASSWD) or die("Could not connect");
mysql_select_db("device_info", $link) or die("Could not select database");
/* Drop table */
$drop_table = "DROP TABLE IF EXISTS $name";
$str = mysql_query($drop_table, $link) or die("Query failed".mysql_error());
/* Create table */
$create_table = "CREATE TABLE $name( ";
if(count($data) > 0){
for($i=0; $i<count($data[0]); $i++){
$attribute = str_replace(" ", "_", $data[0][$i]);
/* This switch is used to replace the attribute name of the table */
switch($attribute){
case "Index":
$attribute = "Id";
break;
}
$create_table .= $attribute." VARCHAR(1000)";
if($i+1 < count($data[0])){
$create_table .= ",";
}
}
}
$create_table .= " )";
$str = mysql_query($create_table, $link) or die("Query failed: ".$create_table."MySQL Error: ".mysql_error());
/* Insert data */
if(count($data) > 0){
for($i=1; $i<count($data); $i++){
$insert_query = "INSERT INTO $name VALUES(";
for($j=0; $j<count($data[$i]); $j++){
$insert_query .= "\"".$data[$i][$j]."\"";
if($j+1 < count($data[$i])){
$insert_query .= ",";
}
}
/* add NULL if data not enough */
for($k=$j; $k<$number_of_attribute; $k++){
$insert_query .= ",NULL";
}
$insert_query .= ")";
$str = mysql_query($insert_query, $link) or die("Query failed".mysql_error());
}
}
mysql_close($link);
change_cell(5, "done");
}
/* Summarize the data in the database */
function summarize_data($name){
global $DB_SERVER;
global $DB_LOGIN;
global $DB_PASSWD;
change_cell(6, "summarizing");
$link = mysql_connect($DB_SERVER, $DB_LOGIN, $DB_PASSWD) or die("Could not connect");
mysql_select_db("device_info", $link) or die("Could not select database");
$query = "INSERT INTO cable_modem.cable_modem SELECT NOW(), \"$name\", Uid, COUNT(Uid), AVG(RX), AVG(TX), AVG(SNR), STD(RX), STD(TX), STD(SNR) FROM device_info.$name GROUP BY Uid";
mysql_query($query, $link) or die("Query failed".mysql_error());
mysql_close($link);
change_cell(6, "done");
}
/* Immediate force the output on the screen */
function flush_now(){
ob_flush();
flush(); // needed ob_flush
usleep(5000); // delay minimum of .05 seconds to allow ie to flush to screen
}
function create_table(){
?>
<table id="tb1" width="800" border="1">
<tr>
<td>No.</td>
<td>Node</td>
<td>Fetch Data</td>
<td>Process Data</td>
<td>Write Data</td>
<td>Store Data</td>
<td>Summarize Data</td>
</tr>
</table>
<?php
flush_now();
}
function insert_row(){
?>
<script type="text/javascript">
var record = document.getElementById('tb1').insertRow();
var number = record.insertCell(0);
var node = record.insertCell(1);
var fetch_data = record.insertCell(2);
var process_data = record.insertCell(3);
var write_data = record.insertCell(4);
var store_data = record.insertCell(5);
var summarize_data = record.insertCell(6);
number.innerHTML = " ";
node.innerHTML = " ";
fetch_data.innerHTML = "pending";
process_data.innerHTML = "pending";
write_data.innerHTML = "pending";
store_data.innerHTML = "pending";
summarize_data.innerHTML = "pending";
</script>
<?php
flush_now();
}
function change_cell($cell, $content){
?>
<script type="text/javascript">
var x=document.getElementById('tb1').rows;
x[x.length-1].cells[<?= $cell ?>].innerHTML="<?= $content ?>";
</script>
<?php
flush_now();
}
function change_image($cell, $type){
?>
<script type="text/javascript">
var x=document.getElementById('tb1').rows;
switch(<?= $type ?>){
case 0:
x[x.length-1].cells[<?= $cell ?>].innerHTML="<img src=\"../stuff/coffee.gif\" width=\"20\" height=\"20\">";
break;
case 1:
x[x.length-1].cells[<?= $cell ?>].innerHTML="<img src=\"../stuff/check_gray.gif\" width=\"20\" height=\"20\">";
break;
case 2:
x[x.length-1].cells[<?= $cell ?>].innerHTML="<img src=\"../stuff/check_gray.gif\" width=\"20\" height=\"20\">";
break;
case 3:
x[x.length-1].cells[<?= $cell ?>].innerHTML="<img src=\"../stuff/warning.gif\" width=\"20\" height=\"20\">";
break;
}
</script>
<?php
flush_now();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel=stylesheet type="text/css" href="./css/style.css">
<title></title>
</head>
<body>
<?php
set_time_limit(0);
$start_time = time();
echo "<img src=\"../stuff/clock.gif\" width=\"20\" height=\"20\"> Start Time: ".date('l, jS F, Y g:i:s A')."<br>";
echo "Please wait while the data is being prepared<br><br>";
flush_now();
/* Main Prograrm */
echo "Getting list of devices... ";
flush_now();
$code = get_list();
//$code[0] = "CHW2B0433WAC";
//$code[1] = "CHW2B0436WC2";
echo "done!<br>";
flush_now();
$number_of_code = count($code);
$each_percentage = 100 / $number_of_code;
echo "Number of Device: $number_of_code<br><br>";
drop_all_tables("device_info");
delete_all_device_data_files();
delete_failed_data();
/* Create an HTML table on the page */
create_table();
for($i=0; $i<$number_of_code; $i++){
insert_row();
change_cell(0, $i+1);
$device_name = $code[$i];
change_cell(1, $device_name);
$device_data = get_data($device_name);
/* If device data available */
if(count($device_data) > 0 ){
$device_data = process_data($device_data); // Make data readable and presentable
write_data($device_name, $device_data, $extension); // Write data in an Excel format file
store_data($device_name, $device_data); // Store data in the database
summarize_data($device_name); // Summarize data
flush_now();
}else{
change_cell(2, "failed");
change_cell(3, "failed");
change_cell(4, "failed");
change_cell(5, "failed");
change_cell(6, "failed");
flush_now();
write_failed_data($device_name);
}
unset($device_name);
unset($device_data);
}
echo "<br>The entire process is finished!<br>";
echo "<img src=\"../stuff/clock.gif\" width=\"20\" height=\"20\"> End Time: ".date('l, jS F, Y g:i:s A')."<br><br>";
?>
</body>
</html>
|
Advertisement