PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.2.0/www/server/grids/instances_list.php

http://scalr.googlecode.com/
PHP | 255 lines | 191 code | 55 blank | 9 comment | 50 complexity | 0bfbb5e5ea182063d55e4d14a7462369 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. $response = array();
  3. // AJAX_REQUEST;
  4. $context = 6;
  5. try
  6. {
  7. $enable_json = true;
  8. include("../../src/prepend.inc.php");
  9. if ($req_farmid)
  10. {
  11. if ($_SESSION["uid"] != 0)
  12. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?", array($req_farmid, $_SESSION["uid"]));
  13. else
  14. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=?", array($req_farmid));
  15. if (!$farminfo)
  16. throw new Exception(_("Farm not found in database"));
  17. $clientid = $farminfo['clientid'];
  18. $region = $farminfo['region'];
  19. }
  20. else
  21. {
  22. if ($_SESSION["uid"] == 0)
  23. throw new Exception(_("Requested page cannot be viewed from admin account"));
  24. else
  25. $clientid = $_SESSION['uid'];
  26. $region = $_SESSION['aws_region'];
  27. }
  28. // Load Client Object
  29. $Client = Client::Load($clientid);
  30. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  31. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  32. Core::Load("NET/SNMP");
  33. $SNMP = new SNMP();
  34. // Rows
  35. $aws_response = $AmazonEC2Client->DescribeInstances();
  36. $rowz = $aws_response->reservationSet->item;
  37. if ($rowz instanceof stdClass)
  38. $rowz = array($rowz);
  39. $rowz1 = array();
  40. // Custom properties
  41. foreach ($rowz as $pk=>$pv)
  42. {
  43. if ($req_query && (!stristr($rowz[$pk]->instancesSet->item->instanceId, $req_query) &&
  44. !stristr($rowz[$pk]->instancesSet->item->privateIpAddress, $req_query) &&
  45. !stristr($rowz[$pk]->instancesSet->item->ipAddress, $req_query)))
  46. continue;
  47. // Groupslist
  48. $gl = array();
  49. if (is_array($rowz[$pk]->groupSet->item))
  50. {
  51. foreach ($rowz[$pk]->groupSet->item as $g)
  52. {
  53. $gl[] = $g->groupId;
  54. }
  55. $rowz[$pk]->groupsList = implode($gl, ", ");
  56. }
  57. else
  58. $rowz[$pk]->groupsList = $rowz[$pk]->groupSet->item->groupId;
  59. $iinfo = $db->GetRow("SELECT * FROM farm_instances WHERE instance_id='{$rowz[$pk]->instancesSet->item->instanceId}'");
  60. if ($iinfo)
  61. {
  62. if (isset($req_farm_roleid) && $iinfo['farm_roleid'] != $req_farm_roleid)
  63. continue;
  64. $alias = $db->GetOne("SELECT alias FROM roles WHERE ami_id='{$iinfo['ami_id']}'");
  65. if ($alias == ROLE_ALIAS::MYSQL && $iinfo['state'] == INSTANCE_STATE::RUNNING)
  66. $prefix = $iinfo['isdbmaster'] ? _(" (master)") : _(" (slave)");
  67. else
  68. $prefix = "";
  69. $rowz[$pk]->Role = $iinfo["role_name"].$prefix;
  70. $rowz[$pk]->Alias = $alias;
  71. $fname = $db->GetOne("SELECT name FROM farms WHERE id=?", array($iinfo['farmid']));
  72. $rowz[$pk]->Farm = $fname;
  73. $eip = $db->GetOne("SELECT id FROM elastic_ips WHERE ipaddress=? AND farmid=?",
  74. array($rowz[$pk]->instancesSet->item->ipAddress, $iinfo['farmid'])
  75. );
  76. $rowz[$pk]->IsElastic = ($iinfo['custom_elastic_ip'] || $eip) ? 1 : 0;
  77. $rowz[$pk]->IsActive = $iinfo["isactive"];
  78. $rowz[$pk]->IsRebootLaunched = $iinfo["isrebootlaunched"];
  79. $rowz[$pk]->FarmID = $iinfo["farmid"];
  80. try
  81. {
  82. $DBFarmRole = DBFarmRole::LoadByID($iinfo['farm_roleid']);
  83. $row->instancesSet->item->imageId = $DBFarmRole->AMIID;
  84. $rowz[$pk]->canUseCustomEIPs = ($DBFarmRole->GetSetting(DBFarmRole::SETTING_AWS_USE_ELASIC_IPS)) ? false : true;
  85. }
  86. catch(Exception $e)
  87. {
  88. }
  89. $rowz[$pk]->customEIP = $iinfo['custom_elastic_ip'];
  90. $rowz[$pk]->internalIP = $iinfo['internal_ip'];
  91. $rowz[$pk]->InstanceIndex = $iinfo['index'];
  92. ///
  93. ///
  94. ///
  95. if ($rowz[$pk]->instancesSet->item->ipAddress)
  96. {
  97. $community = $db->GetOne("SELECT hash FROM farms WHERE id=(SELECT farmid FROM farm_instances WHERE instance_id='{$rowz[$pk]->instancesSet->item->instanceId}')");
  98. $SNMP->Connect($rowz[$pk]->instancesSet->item->ipAddress, null, $community, null, null, true);
  99. $res = $SNMP->Get(".1.3.6.1.4.1.2021.10.1.3.3");
  100. if (!$res)
  101. $rowz[$pk]->LA = _("Unknown");
  102. else
  103. $rowz[$pk]->LA = number_format((float)$res, 2);
  104. }
  105. else
  106. $rowz[$pk]->LA = _("Unknown");
  107. ///
  108. $rowz[$pk]->instancesSet->item->imageId = $iinfo['ami_id'];
  109. }
  110. else
  111. {
  112. if ($req_hide_non_scalr == 'true')
  113. continue;
  114. }
  115. if ($rowz[$pk]->instancesSet->item->launchTime)
  116. {
  117. $ltime = strtotime($rowz[$pk]->instancesSet->item->launchTime);
  118. $uptime = time()-$ltime;
  119. $uptime_days = floor($uptime/86400);
  120. $uptime_h = floor($uptime % 86400 / 3600);
  121. if ($uptime_h < 10)
  122. $uptime_h = "0{$uptime_h}";
  123. $uptime_m = floor($uptime % 86400 % 3600 / 60);
  124. if ($uptime_m < 10)
  125. $uptime_m = "0{$uptime_m}";
  126. $pr = $uptime_days > 1 ? "s" : "";
  127. $rowz[$pk]->Uptime = "{$uptime_days} day{$pr}, {$uptime_h}:{$uptime_m}";
  128. }
  129. $doadd = true;
  130. if (isset($get_state))
  131. {
  132. if ($iinfo["state"] != $get_state)
  133. $doadd = false;
  134. }
  135. if (isset($get_iid))
  136. {
  137. if ($get_iid && $rowz[$pk]->instancesSet->item->instanceId != $get_iid)
  138. $doadd = false;
  139. }
  140. if ($req_hide_terminated == 'true' && $rowz[$pk]->instancesSet->item->instanceState->name == 'terminated')
  141. continue;
  142. if ($rowz[$pk]->instancesSet->item->instanceState->name == 'running' ||
  143. $rowz[$pk]->instancesSet->item->instanceState->name == 'pending')
  144. {
  145. if ($iinfo["state"])
  146. $rowz[$pk]->State = $iinfo["state"];
  147. }
  148. else
  149. $rowz[$pk]->State = ucfirst($rowz[$pk]->instancesSet->item->instanceState->name);
  150. if ($get_farmid)
  151. {
  152. if (!$db->GetOne("SELECT id FROM farm_instances WHERE farmid=? AND instance_id=?", array($get_farmid, $rowz[$pk]->instancesSet->item->instanceId)))
  153. $doadd = false;
  154. }
  155. if ($doadd)
  156. {
  157. if ($iinfo['isdbmaster'] != 1)
  158. $rowz1[] = $rowz[$pk];
  159. else
  160. array_unshift($rowz1, $rowz[$pk]);
  161. }
  162. }
  163. $response["total"] = count($rowz1);
  164. $start = $req_start ? (int) $req_start : 0;
  165. $limit = $req_limit ? (int) $req_limit : 20;
  166. $rowz = (count($rowz1) > $limit) ? array_slice($rowz1, $start, $limit) : $rowz1;
  167. $response["data"] = array();
  168. foreach ($rowz as $row)
  169. {
  170. $response["data"][] = array(
  171. "role" => ($row->Role) ? $row->Role : "Non-Scalr instance",
  172. "farm" => ($row->Farm) ? $row->Farm : "Non-Scalr instance",
  173. "alias" => ($row->Alias) ? $row->Alias : "",
  174. "uptime" => $row->Uptime,
  175. "public_ip" => ($row->instancesSet->item->ipAddress) ? $row->instancesSet->item->ipAddress : "Unknown",
  176. "private_ip" => ($row->instancesSet->item->privateIpAddress) ? $row->instancesSet->item->privateIpAddress : "Unknown",
  177. "is_elastic" => ($row->IsElastic) ? $row->IsElastic : "",
  178. "is_active" => ($row->IsActive) ? $row->IsActive : "",
  179. "is_rebooting" => ($row->IsRebootLaunched) ? $row->IsRebootLaunched : "",
  180. "can_use_ceip" => ($row->canUseCustomEIPs) ? $row->canUseCustomEIPs : "",
  181. "custom_eip" => ($row->customEIP) ? $row->customEIP : "",
  182. "instance_index"=> ($row->InstanceIndex) ? $row->InstanceIndex : 0,
  183. "LA" => $row->LA,
  184. "state" => ($row->State) ? $row->State : ucfirst($row->instancesSet->item->instanceState->name),
  185. "instance_id" => $row->instancesSet->item->instanceId,
  186. "ami_id" => $row->instancesSet->item->imageId,
  187. "type" => $row->instancesSet->item->instanceType,
  188. "avail_zone" => $row->instancesSet->item->placement->availabilityZone,
  189. "id" => $row->instancesSet->item->instanceId,
  190. "farmid" => $row->FarmID,
  191. "region" => $region
  192. );
  193. }
  194. }
  195. catch(Exception $e)
  196. {
  197. $response = array("error" => $e->getMessage(), "data" => array());
  198. }
  199. print json_encode($response);
  200. ?>