<?php

	/* STATUS SAVER
	 CODING TAB SPACE = 4 ( set ts=4 )
	*/
	$db_handle = pg_connect("dbname=##CCDB_NAME## user=##CCDB_USER## password=##CCDB_PASSWD## host=##CCDB_VIP## port=##CCDB_PORT##"); // Written by redeye

	$server_addr[0][0] = "##GMS_IP##";           // Written by redeye
	$server_addr[0][1] = "##GMS_STATUS_PORT##";  // Written by redeye
	$server_addr[0][2] = "##CC_NODE_NAME##";     // Written by redeye

	$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);
		$test2 = "";                                                                             // Written by redeye
		$test = "";                                                                              // Written by redeye
		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);
		}							
		//echo "<!-- TEST : $test -->";

		$pt = new parsePipe(); 			
		$test = $pt->nullTrim($test);		
		$tmpArr = $pt->textArr($test);	
		$serviceName = $pt->serviceName($test);	
		//$result = $pt->parseIt($tmpArr[$serviceType]);
		//$tot_tr = $pt->countIt();		

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

			//if($rcid == "time" || $rcid == "rcid_err") continue;
			if($rcid == "TIME" || $rcid == "rcid_err") continue;

			$result = $pt->parseIt($tmpArr[$k]);
			$tot_tr = $pt->countIt();		
			
			$totalDisk = 0;
			$freeDisk = 0;
			$usedDisk = 0;
			$totalTrafficINB = 0;
			$totalPacketsINB = 0;
			$totalTrafficOUTB = 0;
			$totalPacketsOUTB = 0;
			$totalConnEstab = 0;
			$totalConnAll = 0;
			$totalRTSCount = 0;
			$totalRCTSCount = 0;
			$totalLBCount = 0;
			$totalRCDBCount = 0;
			$activeRTSCount = 0;
			$activeRCTSCount = 0;
			$activeLBCount = 0;
			$activeRCDBCount = 0;

			for($j=0; $j<$tot_tr; $j++)
			{
				//print_r($result[$j]);
				$totalDisk += $result[$j][8];
				$freeDisk += $result[$j][9];
				$usedDisk += ($result[$j][8] - $result[$j][9]);

				/* Packets INB */
				if ($result[$j][16] != '-') {
					$totalPacketsINB += $result[$j][16];
				}
				/* Traffic INB */
				if ($result[$j][17] != '-') {
					$totalTrafficINB += $result[$j][17];
				}
				/* Packets OUTB */
				if ($result[$j][18] != '-') {
					$totalPacketsOUTB += $result[$j][18];
				}
				/* Traffic OUTB */
				if ($result[$j][19] != '-') {
					$totalTrafficOUTB += $result[$j][19];
				}
				/* Connection Estblished */
				if ($result[$j][14] != '-') {
					$totalConnEstab += $result[$j][14];
				}
				/* Connection All */
				if ($result[$j][15] != '-') {
					$totalConnAll += $result[$j][15];
				}
				/* Servers Total/Active */
				switch($result[$j][1])
				{
					//case "1":
					case "2":
						$totalRTSCount += 1;
						if( $result[$j][0] == "1") $activeRTSCount +=1;
						break;

					//case "4":
					case "3":
						$totalRCTSCount += 1;
						if( $result[$j][0] == "1") $activeRCTSCount +=1;
						break;

					//case "6":
					case "4":
						$strTemp = explode("_", $result[$j][2]);
						if( substr($strTemp[count($strTemp)-1], 0, 4) == "PGLB" ||  substr($strTemp[count($strTemp)-1], 0, 4) == "DBLB")
						{
							$totalLBCount += 1;
							if( $result[$j][0] == "1") $activeLBCount +=1;
						}
						else if( substr($strTemp[count($strTemp)-1], 0, 2) == "DB" || substr($strTemp[count($strTemp)-1], 0, 4) == "RCDB")
						{
							$totalRCDBCount += 1;
							if( $result[$j][0] == "1") $activeRCDBCount +=1;
						}
						else
						{
							echo"ERR";
						}
						break;

					default:
				}
			}

		?>
		<?php

//$totalDisk	= floor($totalDisk / 1024);
//$freeDisk 	= floor($freeDisk / 1024);
//$usedDisk 	= floor($usedDisk / 1024);
//$totalTrafficINB = floor($totalTrafficINB / 1024);
//$totalPacketsINB = floor($totalPacketsINB / 1024);
				 
        $query = "INSERT INTO cs_stat.cs_stat_rc(
                              id, stg_total, stg_used, net_in, net_out, fhs_total,
                              fhs_active, rcts_total, rcts_active, dblb_total, dblb_active,
                              rcdb_total, rcdb_active)
                      VALUES ('$rcid', '$totalDisk', '$usedDisk', '$totalTrafficINB', '$totalTrafficOUTB', '$totalRTSCount',
                              '$activeRTSCount', '$totalRCTSCount', '$activeRCTSCount', '$totalLBCount', '$activeLBCount',
                              '$totalRCDBCount', '$activeRCDBCount')";        // Written by redeye					 
			
//echo "$strSql";
		// Eliminate abnormal large number.
		if($totalDisk < 10000000000000000 && $totalTrafficINB < 30000000000 && $totalTrafficOUTB < 30000000000)
			$result_pgsql = pg_exec($db_handle, $query);          // Written by redeye
		}
		pg_close($db_handle);                                         // Written by redeye


	}

########
#CLASSES
########
class parsePipe {
	var $tot_line;

	function parseIt($givenText){
		$cnt = 0;
		$minValue = 0;
		$maxValue = 0;
		$minLevel = 1000;
		$tmp_arr = array();
		$temp_arr = explode("\r\n", $givenText);
		$i = "";                                          // Written by redeye
		$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][20] = "";
				$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];
					}
				}
				*/
				if ($result_arr[$i][11] != '-' && $result_arr[$i] != '' && $result_arr[$i] != 0) {
					if ($minLevel > $result_arr[$i][11]) {
						$minLevel = $result_arr[$i][11];
					}
				}
			}
		}

		$tot_tr = "";                           // Written by redeye
		for ($i = 0; $i < $tot_tr; $i++) {
			if ($result_arr[$i][11] != '-' && $result_arr[$i] != '' && $result_arr[$i] != 0 && $result_arr[$i][11] <= $minLevel) {
				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];
					}
				}
			}
		}


		//echo "minLevel $minLevel minValue $minValue<br>";
		$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) {
					if ($minLevel < $result_arr[$key][11] ||
							$result_arr[$key][10] > 0) {
						$result_arr[$key][20] = "yellow";//$maxValue;
					} else {
						//echo "11 ".$result_arr[$key][11]." 10 ".$result_arr[$key][10]."<br>";
						$maxValue = $val;
						$result_arr[$key][20] = "99FFFF";
						$cnt++;
					}
				} else {
					//if($minValue == $val) {
					if($val < (2 * 1024 * 1024 * 1024 * 10)) {
						$result_arr[$key][20] = "red";
					} else if(($maxValue - $val) < (1024 * 1024 * 1024) &&
							$minLevel == $result_arr[$key][11]) {
						//echo "1GB Highed<br>";
						//echo "minLevel $minLevel maxValue $maxValue<br>";
						$result_arr[$key][20] = "99FFFF";
					} else {
						//echo "1GB Lowed<br>";
						$result_arr[$key][20] = "yellow";//$maxValue;
					}
				}
			} else {
				$result_arr[$key][20] = "white";
			}
		}
		return $result_arr;
	}

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


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

		for($i=0 ; $i < $maxLength ; $i++) {
			if(ord(substr($tmp_str, $i, 1)) != 0 || $tmp_str != " ") {
				$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[] = array();                          // Written by redeye
			$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]);
			//echo $split_str[0]."<br>";
			if($tmp_search_str != $search_str[0]) {
				$tmp_search_str = $search_str[0];																			
				$cnt++;
			}
			$str_arr[$cnt][0] = $tmp_search_str;
			if($split_str[0] == 2) {
				$str_arr[$cnt][1] = "dead";
			}
			//	$str_arr[4][1] = "dead";
			//	$str_arr[1][1] = "dead";
		}	
		return $str_arr;
	}

	function tailer($name)
	{
		$temp_arr = explode("_", $name);		
		$temp_arr2 = substr($name, strlen($temp_arr[0]) + 1, strlen($name));
		return $temp_arr2;
	}
	
	function noYear($date)
	{
		$date = substr($date, 5, 14);

		return $date;
	}
}

?>
