<?php
	require_once("inc/session.php");
	$serviceType = isset($_GET['service_type']) ? $_GET['service_type'] : $_POST['service_type'];	
	if($serviceType == "") $serviceType = 0;

	header("Content-type: text/html; charset=euc-kr");  


?>
<html>
<head>
<title>Cloud Storage RAID Status</title>
<STYLE type=text/css>
TD { FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: 'ARIAL,±¼¸²Ã¼'; TEXT-DECORATION: none; }
TR { FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: 'ARIAL,±¼¸²Ã¼'; TEXT-DECORATION: none; }
TABLE { FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: 'ARIAL,±¼¸²Ã¼'; TEXT-DECORATION: none; line-height : 120%}
INPUT { FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: 'ARIAL,±¼¸²Ã¼'; TEXT-DECORATION: none; line-height : 120%}
}
</STYLE>
<meta http-equiv="Cache-Control" content="no-cache"/> 
<meta http-equiv="Expires" content="0"/> 
<meta http-equiv="Pragma" content="no-cache"/> 
<link rel="stylesheet" href="clockstyle.css" type="text/css">
<style type="text/css">
#tooltip {position:absolute;
top:5;
left:5;
border:solid 3 ffccff;
background:ffffcc;
color:660066;
font-size:9pt;
width:200;
height:20;
padding:2;
overflow:hidden;
z-index:10;
display:none;
}
</style>
<? require "layer.js";?>
<script language="javascript1.2"  type="text/javascript" src="clock.js" ></script>
<script language="javascript1.2">
var sh_win_obj =null;

function openBrWindow(theURL,winName,features) {
		 if(sh_win_obj!=null)sh_win_obj.close();
			sh_win_obj=window.open(theURL,winName,features);
		 }
</script>
</head>

<body onLoad="UpdateClocks()">
<form name="frm" action=<?=$_SERVER['PHP_SELF']?> method=POST>
	<input type="hidden" name="service_type" value="<?= $serviceType ?>">
<table border="0" cellspacing="0" width="100%">
  <tr bgcolor=#003366  class="ratNavText">
    <td ID="Clock0" width="25%" >   </td>
    <td ID="Clock1" width="25%" >   </td>
    <td ID="Clock2" width="25%" >   </td>
    <td ID="Clock3" width="25%" >   </td>
  </tr>
  <tr bgcolor=#003366 class="ratNavText">
    <td ID="Clock4" width="25%" >   </td>
    <td ID="Clock5" width="25%" >   </td>
    <td ID="Clock6" width="25%" >   </td>
    <td ID="Clock7" width="25%" >   </td>
  </tr>
</table>
<br>
<table border="0" cellspacing="1" width="530" align="center">
<tr align="center">
    <td> <h3>Cloud Storage RAID Status</h3></td>
</tr>
</table>
<?php
require_once ("/user/service/wwwroot/sh_raid_monitor/inc/contact.html");
?>
<marquee id="tooltip" name="tooltip"></marquee>
<?
	$server_addr[0][0] = "127.0.0.1";
	$server_addr[0][1] = "30001";
	$server_addr[0][2] = "KT-IDC";

	$str_request = "I'M YOUR FATHER. BASTARD !";

	$tot = count($server_addr);
	for($i=0; $i < $tot; $i++){

		if($i%3 == 0) echo "<table align=\"center\"><tr>";

		$fp = @fsockopen("udp://{$server_addr[$i][0]}", $server_addr[$i][1], $errno, $errstr);
		if (!$fp) {
			echo "ERROR: $errno - $errstr<br>\n";
			continue;
		} else {				
			fwrite($fp,"GET_SERVSTATUS", 15);
			while(1) {								
				$test2 = fread($fp, 2048);
				$test .= trim($test2);
				$j = strlen($test2);				
				if($j < 1024) break;
			}
			fclose($fp);
		}		
		?>
		<td valign=top>
		<?
		$pt = new parsePipe();	
		$test = $pt->nullTrim($test);		
		//echo "<!-- $test -->";
		$tmpArr = $pt->textArr($test);
		$serviceName = $pt->serviceName($test);
		$result = $pt->parseIt($tmpArr[$serviceType]);
		$tot_tr = $pt->countIt();
		
        echo "          <table border=1 cellpadding=0 cellspacing=0>\n";
        echo "              <tr align=center>\n";

        for($k=0; $k<sizeof($serviceName); $k++) 
		{

            echo "              <td width='90' height='35'>\n";
            echo "              <font size='4'>";
            echo "              <a href='".$_SERVER['PHP_SELF']."?service_type=".$k."'>";
            if($serviceType==$k) echo "<b>";
		
			switch($serviceName[$k])
			{
				default : echo $serviceName[$k]; break;
			}
            
			if($serviceType==$k) echo "</b>";
           
			echo "              </a>";
            echo "                  </font>";
            echo "              </td>\n";   
        }
        
		
		echo "              </tr>\n";
        echo "          </table>\n";
		?>		
			<table border=1 cellpadding=1 cellspacing=0>
			<tr height="60">
				<td colspan="10">
				<center><b>
				<font color="#000066" size="5">
					<?= $serviceName[$serviceType] ?><br><br>
				</font>
				<font color="#000066" size="4">
					MNG SERVER IP: <?=$server_addr[$i][0]?>,&nbsp;&nbsp;  PORT: <?=$server_addr[$i][1]?> 
					(<?=$server_addr[$i][2]?>)
				</font>
				</b>
				</center>
				</td>
			</tr>
			<tr align="center">
				<td width="170"><center><b><font color="#000066">Name</font></b></center></td>
				<td width="110"><center><b><font color="#000066">Server IP</font></b></center></td>
				<td width="50"><center><b><font color="#000066">Server Port</font></b></center></td>
				<td width="150"><center><b><font color="#000066">Last Status <!--Changed-->Checked At</font></b></center></td>
				<td width="70"><center><b><font color="#000066">Raid</font></b></center></td>
			</tr>
				<?	
				
				for($j=0; $j<$tot_tr; $j++){
						$alt_str = "[$j]".
								" [SERVER STATUS] : ".$pt->translateIt($result[$j][0], 2).
								" [SERVER NAME] : ".$pt->translateIt($result[$j][2],2).
								" [SERVER IP] : ".$pt->translateIt($result[$j][3],3). 
								" [SERVER PORT] : ".$pt->translateIt($result[$j][4],4);
				if(substr_count($pt->translateIt($result[$j][2],3), "RTS") == 0 && substr_count($pt->translateIt($result[$j][2],3), "FHS")==0) continue;

				//$mode = "I'M YOUR FATHER. BASTARD !";
				//$command = "bin/arcmond_client ".$pt->translateIt($result[$j][3],3)." 8787 1";
				$mode = "EVENT:INFO:ONE";
				//$command = 'bin/raidmond_client '.$pt->translateIt($result[$j][3],3).' 8787 "'.$mode.'"';
				$command = 'bin/arcmond_client '.$pt->translateIt($result[$j][3],3).' 8787 "'.$mode.'"';
//echo $command;				
				$raid_res = exec($command);
echo "<!-- raid_res : $raid_res -->";
				$raid_str = explode(":",$raid_res);

				  ?>
				<span onmouseover="showtip('<?=$alt_str?>');" onmouseout="hidetip();">
					<tr>
						<td>(<?=$pt->translateIt($result[$j][0],1)?>&nbsp;<?=$pt->translateIt($result[$j][1],0)?>)&nbsp;<?=$result[$j][2]?></td>
						<td align="center"><?=$pt->translateIt($result[$j][3],3)?></td>
						<td align="center"><?=$pt->translateIt($result[$j][4],4)?></td>
						<td align="center"><?=date("Y-m-d H:i:s", $raid_str[1])?></td>
						</td>
						<td>
							<?
							  $raid_res="";
$last_num = count($raid_str) - 1;
//echo $raid_str[0]."<br>";
//echo $command;
							  if($raid_str[0]=="0")
							  {
								echo $pt->translateIt(1,1);
								echo "<a href=\"javascript:openBrWindow('raid_view.html?ip=".$pt->translateIt($result[$j][3],3)."&mode=2','viewDetail','width=600, height=650, left=300,top=300, resizable=no, scrollbars=yes, status=yes')\" >";
								#echo "NO EVENT";   
								echo "$raid_str[$last_num]";
								echo "</a>";
								//echo $raid_str[2];
							  }
							  elseif($raid_str[0]=="1")
							  {
								  echo $pt->translateIt(2,1);
								  echo "<a href=\"javascript:openBrWindow('raid_view.html?ip=".$pt->translateIt($result[$j][3],3)."&mode=2','viewDetail','width=600, height=650, left=300,top=300, resizable=no, scrollbars=yes, status=yes')\" >";
								  echo "$raid_str[$last_num]";   
								  echo "</a>";

							  }
							  else 
							  {
								  echo $pt->translateIt(0,1);
								  //echo "<a target=_blank href='raid_view.html?ip=".$pt->translateIt($result[$j][3],3)."&mode=2'>";
								  echo "<a href=\"javascript:openBrWindow('raid_view.html?ip=".$pt->translateIt($result[$j][3],3)."&mode=2','viewDetail','width=600, height=650, left=300,top=300, resizable=no, scrollbars=yes, status=yes')\" >";
								echo "check err $raid_str[2]$raid_str[3]";
								echo "</a>";
							  }

							?>
						</td>
					</tr>
				</span>
				<?
				}
				?>
<!--
					<tr>
						<td align="center" colspan="6"><b>Storage Total</b></td>
						<td align="center"><?= ($totalDisk == "") ? "-" : $pt->translateIt($totalDisk, 9)?></td>
						<td align="center"><?= ($totalDisk == "") ? "-" : $pt->translateIt($freeDisk, 9)?></td>
						<td align="center"><?= ($totalDisk == "") ? "-" : $pt->translateIt($usedDisk, 9)?></td>
						<td align="center">.</td>
					</tr>
-->	
			</table></td>
			<?
			if($i%3 == 2) echo "</table></tr>"; //
		}
		?>
	<table>
	</table>
	<?

////////////////////
//Print REFRESHER
////////////////////

extract($_POST);
if($refresh_rate<1 || $refresh_rate>1000)
	$refresh_rate = 60;

?>
<br>
<table align="center">
	<tr>
	<td>
		
			Refresh Rate :
			<input type=text name=refresh_rate value=<?=$refresh_rate?>>
			<input type=submit value=SET>
			(MIN : 1, MAX : 1000)
			<input type="hidden" name="chk" value="0">
		</form>
	</td>
	</tr><tr>
	<td align=right>
		<form action=<?=$_SERVER['PHP_SELF']?> method=POST>
			<input type="hidden" name="service_type" value="<?= $serviceType?>">
			<input type=hidden name=refresh_rate value=30>
			<input type=submit value=RESET(30)>
		</form>
	</td>
	</tr>
</table>
<script language="javascript"  type="text/javascript" src="tooltip.js" ></script>
<script language=javascript>
function showtip(text) {
tooltip.innerText=text; 
tooltip.style.display="inline";
}
function hidetip() {
tooltip.style.display="none";
}
function movetip() {
tooltip.style.pixelTop=event.y+document.body.scrollTop; 
tooltip.style.pixelLeft=event.x+document.body.scrollLeft+10; 
}
document.onmousemove=movetip;
</script>
</body>
</html>
<META http-equiv="refresh" content="<?=$refresh_rate?>; url=<?=$$_SERVER['PHP_SELF']?>?refresh_rate=<?=$refresh_rate?>&service_type=<?= $serviceType ?>"> 
<?
########
#CLASSES
########
class parsePipe {
	var $tot_line;

	function parseIt($givenText){
		$cnt = 0;
		$minValue = 0;
		$maxValue = 0;
		$tmp_arr = array();
		$temp_arr = explode("\r\n", $givenText);
		$result_arr[$i][9] = "";
		$this->tot_line = count($temp_arr) == 1 ? 0 : count($temp_arr) - 1;
		for($i=0; $i < $this->tot_line; $i++){
			if($temp_arr[$i]!='') {				
				$result_arr[$i] = explode("|", $temp_arr[$i]);
				$result_arr[$i][10] = "";
				$tmp_arr[$i] = ($result_arr[$i][9] == '' || $result_arr[$i][9] == '-') ? 0 : $this->nullTrim($result_arr[$i][9]);
				$result_arr[$i][8] = $this->nullTrim($result_arr[$i][8]);
				$result_arr[$i][9] = $this->nullTrim($result_arr[$i][9]);
				$result_arr[$i][10] = $this->nullTrim($result_arr[$i][10]);

				if($result_arr[$i][9] != '-' && $result_arr[$i] != '' && $result_arr[$i] != 0) {
					if($cnt == 0) {
						$minValue = $result_arr[$i][9];
						$cnt++;
					}
					if($minValue > $result_arr[$i][9]) {
						$minValue = $result_arr[$i][9];
					}
				}
			}
		}

		$cnt = 0;
		arsort($tmp_arr);
		reset($tmp_arr);

		while (list ($key, $val) = each ($tmp_arr)) {
			//echo "$key = $val<br>";
			if($val != 0) {
				if($cnt == 0) {
					$maxValue = $val;
					$result_arr[$key][10] = "99FFFF";
					$cnt++;
				} else {
					//if($minValue == $val) {
					if($val < (2 * 1024 * 1024 * 1024 * 10)) {
						$result_arr[$key][10] = "red";
					}else if(($maxValue - $val) < (1024 * 1024 * 1024)) {
						//echo "1GB Highed<br>";
						$result_arr[$key][10] = "99FFFF";
					} else {
						//echo "1GB Lowed<br>";
						$result_arr[$key][10] = "yellow";//$maxValue;
					}
				}
			} else {
				$result_arr[$key][10] = "white";
			}
		}
		return $result_arr;
	}

	function countIt(){
		return $this->tot_line;
	}

	function translateIt($parsedText, $type){
		if($parsedText == '') return;

		$eServerType = array('WWW', 'RTS', 'RC', 'CCDB', 'RCTS', 'GTS', "RCDB");
		/*$eStatus = array('<img src=images/bu16.gif width=11 height=11>UNKNOWN',
						'<img src=images/O EVENTbu14.gif width=11 height=11>ALIVE',
						'<img src=images/bu15.gif width=11 height=11>DEAD');*/
		$eStatus = array('<img src=images/bu16.gif width=11 height=11>',
						'<img src=images/bu14.gif width=11 height=11>',
						'<img src=images/bu15.gif width=11 height=11>');
		$eStatusChar = array('UNKNOWN', 'ALIVE', 'DEAD');
		switch($type){
			case 0:
				return $eServerType[$parsedText];
			case 1:
				return $eStatus[$parsedText];
			case 2:
				return $eStatusChar[$parsedText];
			case 8:			
				if(trim($parsedText) == "-" || $parsedText == "") return "-";
				else return sprintf("%.2lfG", (trim($parsedText) / 1024) / 1024 / 1024);
			case 9:
				if(trim($parsedText) == "-" || $parsedText == "" ) return "-";
				else {
					if (trim($parsedText) / 1024 / 1024 / 1024 > 1024) {
						return sprintf("%.2fT", trim($parsedText) / 1024 / 1024 / 1024 / 1024);
					} else 
						return sprintf("%.2fG", trim($parsedText) / 1024 / 1024 / 1024);
				}
			default:
				return $parsedText;
		}
	}

	function nullTrim($tmp_str)
	{
		$return_str = "";
		$maxLength = strlen($tmp_str);

		for($i=0 ; $i < $maxLength ; $i++) {
			if(ord(substr($tmp_str, $i, 1)) != 0) {
				$return_str .= substr($tmp_str, $i, 1);
			}
		}
		return $return_str;
	}
	
	function textArr($text)
	{		
		$cnt = -1;
		$tmp_cnt = 0;
		$str_arr = array();
		$tmp_search_str = "";
		$temp_arr = explode("\r\n", $text);		
		
		for($i=0; $i<sizeof($temp_arr); $i++) {
			$split_str = explode("|", $temp_arr[$i]);
			$search_str = explode("_", $split_str[2]);
			if($tmp_search_str != $search_str[0]) {
				$tmp_search_str = $search_str[0];															
				//$str_arr[$cnt][30] = $search_str[0];
				//echo $str_arr[$cnt][0]."<br>".$tmp_search_str."->".$cnt."<-"."<br>";
				$cnt++;
			}
			$str_arr[$cnt] .= $temp_arr[$i]."\r\n";
			
		}			
		return $str_arr;
	}
	
	function serviceName($text)
	{		
		$cnt = -1;
		$tmp_cnt = 0;
		$str_arr = array();
		$tmp_search_str = "";
		$temp_arr = explode("\r\n", $text);		
		
		for($i=0; $i<sizeof($temp_arr); $i++) {
			$split_str = explode("|", $temp_arr[$i]);						
			$search_str = explode("_", $split_str[2]);
			if($tmp_search_str != $search_str[0]) {
				$tmp_search_str = $search_str[0];																			
				$cnt++;
			}
			$str_arr[$cnt] = $tmp_search_str;
		}	
		return $str_arr;
	}
}
?>
