<?php
function GetConfInfo()
{
	$handle=fopen("/usr/local/omc/bin/conf/raid.conf","r");
	while(($line=fgets($handle)))
	{
		if(strstr($line,"#"))
			continue;
		
		if(strstr($line,"SystemType="))
		{
			$sp=explode("SystemType=",$line);
			$confInfo[SystemType]=trim($sp[1]);
		}
		else if(strstr($line,"EMS,"))
		{
			if(strcmp($confInfo[SystemType],"MCSS") == 0)
			{
				if(strstr($line,"Intel"))
					$confInfo[ServerType][0]=0;
				else
					$confInfo[ServerType][0]=1;
			}
		}
		else if(strstr($line,"MSS,"))
		{
			if(strstr($line,"Intel"))
				$confInfo[ServerType][320]=0;
			else
				$confInfo[ServerType][320]=1;
		}
		else if(strstr($line,"VMS,"))
		{
			if(strstr($line,"Intel"))
				$confInfo[ServerType][390]=0;
			else
				$confInfo[ServerType][390]=1;
		}
		else if(strstr($line,"RCM,"))
		{
			if(strstr($line,"Intel"))
				$confInfo[ServerType][395]=0;
			else
				$confInfo[ServerType][395]=1;
		}
		else if(strstr($line,"MCMS,"))
		{
			if(strcmp($confInfo[SystemType],"MCSS") != 0)
			{
				if(strstr($line,"Intel"))
					$confInfo[ServerType][0]=0;
				else
					$confInfo[ServerType][0]=1;
			}
		}
	}
	fclose($handle);
	
	return $confInfo;
}

function getOmcSysNo()
{
	$omcSysNo=0;
	$handle=fopen("/usr/local/omc/bin/conf/omcd.conf","r");
	while(($line=fgets($handle)))
	{
		if(strstr($line,"#"))
			continue;
		if(strstr($line,"omcSysNo="))
		{
			$sp=explode("omcSysNo=",$line);
			$omcSysNo=trim($sp[1]);
			break;
		}
	}
	fclose($handle);
	return $omcSysNo;
}

function GetDiskInfo($sysTypeNo,$sysNo)
{
	$sysTypeNo=$_REQUEST['sysTypeNo'];
	$sysNo=$_REQUEST['sysNo'];

	global $OMC_server;
	$raidConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "RAID_DB");
	$sql="select * from RAID_DB.raid_status where sysTypeNo=$sysTypeNo and sysNo=$sysNo ";
	$res=mysqli_query($raidConn,$sql);
	$num=mysqli_num_rows($res);
	$diskInfo[DiskCount][Value]=0;
	$diskInfo[DiskCount][Color]="#000000";
	
	$confInfo=GetConfInfo();
	
	switch($sysTypeNo)
	{
	case 0:
		if(strcmp($confInfo[SystemType],"MCSS") == 0)
			$diskInfo[ServerName][Value]="EMS-".$sysNo;
		else
			$diskInfo[ServerName][Value]="MCMS-".$sysNo;
		break;
	case 320:
		$diskInfo[ServerName][Value]="MSS-".$sysNo;
		break;
	case 390:
		$diskInfo[ServerName][Value]="VMS-".$sysNo;
		break;
	case 395:
		$diskInfo[ServerName][Value]="RCM-".$sysNo;
		break;
	}
	$diskInfo[ServerName][Color]="#000000";
	
	$diskInfo[UpdateTime][Value]="0000-00-00 00:00:00";
	$diskInfo[UpdateTime][Color]="#ff0000";
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		if($row[updateTime] >= $diskInfo[UpdateTime][Value])
			$diskInfo[UpdateTime][Value]=$row[updateTime];
			
		if(strcmp($row[fieldName],"CardName") == 0)
		{
			$diskInfo[CardName][Value]=$row[fieldValue];
			$diskInfo[CardName][Color]="#000000";
		}
		else if(strcmp($row[fieldName],"DiskNum") == 0)
		{
			$diskInfo[DiskCount][Value]=$row[fieldValue]-0;
			$diskInfo[DiskCount][Color]="#000000";
		}
		else if(strcmp($row[fieldName],"DiskState") == 0)
		{
			$value=$row[fieldValue]-0;
			
			//Intel
			if($confInfo[ServerType][$sysTypeNo] == 0)
			{
				switch($value)
				{
				case 24:
					$diskInfo[$disk_no][DiskState][Value]="Online";
					$diskInfo[$disk_no][DiskState][Color]="#0000ff";
					break;
				case 16:
					$diskInfo[$disk_no][DiskState][Value]="Offline";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				case 17:
					$diskInfo[$disk_no][DiskState][Value]="Failed";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				case 20:
					$diskInfo[$disk_no][DiskState][Value]="Rebuild";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				default:
					$diskInfo[$disk_no][DiskState][Value]="Unknown";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				}
			}
			//Dell
			else
			{
				switch($value)
				{
				case 3:
					$diskInfo[$disk_no][DiskState][Value]="Online";
					$diskInfo[$disk_no][DiskState][Color]="#0000ff";
					break;
				case 4:
					$diskInfo[$disk_no][DiskState][Value]="Offline";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				case 2:
					$diskInfo[$disk_no][DiskState][Value]="Failed";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				case 24:
					$diskInfo[$disk_no][DiskState][Value]="Rebuild";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				default:
					$diskInfo[$disk_no][DiskState][Value]="Unknown";
					$diskInfo[$disk_no][DiskState][Color]="#ff0000";
					break;
				}
			}
		}
		else
		{
			$disk_no=$row[instance]-0;
			$diskInfo[$disk_no][$row[fieldName]][Value]=$row[fieldValue];
			$diskInfo[$disk_no][$row[fieldName]][Color]="#000000";
		}
	}
	
	$refresh_time=$diskInfo[UpdateTime][Value];
	$due_date=date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s")-60,date("m"),date("d"),date("Y")));
	$ove_date=date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s")+60,date("m"),date("d"),date("Y")));
	if($refresh_time < $due_date || $refresh_time > $ove_date)
		$diskInfo[UpdateTime][Color]="#ff0000";
	else
		$diskInfo[UpdateTime][Color]="#0000ff";
	
	return $diskInfo;
}
?>
