<?php
/* iLO */
function getIloInfo($server_ip, $username, $password) {
	//$params['iloIPAddress'] = $server_ip;
	
	$connection = ssh_Connect($server_ip, $username, $password);
	if ($connection == 2) {
		$params['result'] = 'SSH Authentication Failed';
		return $params;
	}
	//$params['result'] = 'Success';
	//$params['date']   = time();

	
	
// Base Informationen:
	$table = array(
		'name'		=> 'fwName',
		'date'		=> 'fwDate',
		'version'	=> 'fwVersion',
		'license'	=> 'fwLicense',
		'Hostname'	=> 'fwHostName',
		'HostName'	=> 'fwHostName',
		'DomainName'	=> 'fwDomainName',
	);
	
	$params = array();
	
	$params['base_info'] = getIloData($connection, 'show /map1', $table);
	$params['fw_info'] = getIloData($connection, 'show /map1/firmware1', $table);
	$params['dns_info'] = getIloData($connection, 'show /map1/dnsendpt1', $table);



// Temparatur Informationen:
	$table = array(
		'CurrentReading'		=> "CurrentReading",
		'oemhp_CautionValue'	=> "CautionValue",
		'oemhp_CriticalValue'	=> "CriticalValue",
		'ElementName'			=> "ElementName",
	);
	
	$params['temperature_info'] = array();
	if (preg_match('/iLO\ 3/', $params['base_info']['fwName'])) {
		for ($i = 1; $i <= 30; $i++) {
			$params['temperature_info']['sensor'.$i] = getIloData($connection, "show /system1/sensor$i", $table);
			$params['temperature_info']['sensor'.$i]['Sensor_ID'] = 'sensor'.$i;
		}
	}
	if (preg_match('/iLO\ 4/', $params['base_info']['fwName'])) {
		for ($i = 1; $i <= 30; $i++) {
			$params['temperature_info']['sensor'.$i] = getIloData($connection, "show /system1/sensor$i", $table);
			$params['temperature_info']['sensor'.$i]['Sensor_ID'] = 'sensor'.$i;
		}
	}
	
	//print_r("====TEST====");
	//var_dump($params);
	//print_r("====TEST====");



// Server Infoemarionen:
	$system_table = array(
		'name'			=> 'productName',
		'product_id'		=> 'productId',
		'number'		=> 'serialNumber',
		'enabledstate'		=> 'enabledstate'
	);
	
	// -> Fehler passiert hier!!!!
	//$params['system_info'] = getIloData($connection, 'show /system1', $system_table);  //War wahrscheindlich zu viel output!

	$params['system_info'] = getIloData($connection, 'show /system1 name', $system_table);
	//$params['system_info'] = getIloData($connection, 'show /system1 number', $system_table);
	$params['system_enabled_info'] = getIloData($connection, 'show /system1 enabledstate', $system_table);
	//var_dump(getIloData($connection, 'show /system1', $system_table));
	//print_r($params['system_info']);
	//echo "test";

	
// Server Power Infoemarionen:
	$power_table = array(
		'oemhp_PresentPower'	=> 'presentPower',
		'oemhp_AveragePower'	=> 'averagePower',
		'oemhp_AvgPower'	=> 'averagePower',
		'oemhp_MinPower'	=> 'minimumPower',
		'oemhp_MaxPower'	=> 'maximumPower',
		'oemhp_powerreg'	=> 'powerProfile'
	);
	if (preg_match('/iLO\ 2 Standard/', $params['base_info']['fwName'])) {
		/* iLO2: Leistungsinformationen */
		ssh_sendCommand($connection, 'cd /system1');
		$params['system_power'] = getIloData($connection, 'show oemhp_PresentPower', $power_table);
		ssh_sendCommand($connection, 'cd /');
	}
	if (preg_match('/iLO\ 3/', $params['base_info']['fwName']) ||
	    preg_match('/iLO\ 4/', $params['base_info']['fwName'])    ) {
		/* iLO3/iLO4: Leistungsinformationen */
		$params['system_power'] = getIloData($connection, 'show /system1/oemhp_power1', $power_table);
		//print_r($params['system_power']);
	}
	//var_dump($params);



//BIOS Version:
	$bios_table = array(
		'date'		=> 'biosDate',
		'version'	=> 'biosVersion'
	);
	
	$params['bios_info'] = getIloData($connection, 'show /system1/firmware1', $bios_table);

	

	// Zum Schluss, gibt "return" das komplette Array wieder zurück an die Index.php
	//var_dump($params);
	//return $params;
	
	$fp = fopen('ilo_results.json', 'w');
	fwrite($fp, json_encode($params));
	fclose($fp);
	
}

function getIloData($connection, $command, $table) {
	try {
		$data = ssh_sendCommand($connection, $command);
		$parsedData = iLO_parseData($data, $table);
	} catch (Exception $e) {
		print_r($e);
	}
	return $parsedData;
}

function iLO_ParseData($data, $table) {
	$data = explode("\n", $data);
	$flag = 0;
	$out = '';
	foreach($data as $line) {
		$line = preg_replace('/(\x1B\x5BD)/', '', $line);	// HP's bug?
		$line = trim($line);
		if ($line == '') {continue;}
		if ($line == 'Properties') {
			$flag = 1;
			continue;
		} else if ($line == "Power Monitoring commands aren't supported") {
			$line = 'oemhp_PresentPower=Not Supported';
		} else if ($line == 'Verbs' || $line == 'An iLO 2 License key is required.') {
			$flag = 0;
			break;
		}
		if ($flag) {
			$line = explode("=", $line);
			$line[0] = trim($line[0]);
			$line[1] = trim($line[1]);
			$key = (isset($table[$line[0]])) ? $table[$line[0]] : '';
			if ($key) {
				$out[$key] = $line[1];
			}
		}
	}
	return $out;
}

?>