Advertisement
Guest User

Untitled

a guest
Apr 15th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. <?php
  2. $site = "http://insert/your/site";
  3.  
  4. function print_layout()
  5. {
  6. global $site;
  7.  
  8. $ch = curl_init("$site/api.php?method=get_rackspace");
  9.  
  10.  
  11. curl_setopt($ch, CURLOPT_HEADER, 0);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  13. $data = curl_exec($ch);
  14. curl_close($ch);
  15.  
  16. $d = json_decode($data, true );
  17.  
  18. $result = $d['response'];
  19. foreach ( $result as $location ) {
  20. echo $location['location_name']."\n";
  21. echo " - ".$location['row_name']."\n";
  22. foreach ( $location['racks'] as $row ) {
  23. echo " - ".$row['name']."\n";
  24. $rack_curl = curl_init("$site/api.php?method=get_rack&rack_id=".$row['id']);
  25. curl_setopt($rack_curl, CURLOPT_HEADER, 0);
  26. curl_setopt($rack_curl, CURLOPT_RETURNTRANSFER,true);
  27. $rackdata = curl_exec($rack_curl);
  28. curl_close($rack_curl);
  29.  
  30. $rack_json = json_decode($rackdata, true );
  31. $rack = $rack_json['response'];
  32. foreach( $rack['mountedObjects'] as $obj ) {
  33. $obj_curl = curl_init("$site/racktables/api.php?method=get_object&object_id=$obj");
  34. curl_setopt($obj_curl, CURLOPT_HEADER, 0);
  35. curl_setopt($obj_curl, CURLOPT_RETURNTRANSFER,true);
  36. $objdata = curl_exec($obj_curl);
  37. curl_close($obj_curl);
  38.  
  39. $obj_json = json_decode($objdata, true );
  40. $object = $obj_json['response'];
  41. echo " - ".$object['name']."\n";
  42. }
  43. //}
  44.  
  45. }
  46.  
  47. }
  48.  
  49. }
  50.  
  51. /* Print only objects with type 4 for servers, may need to come back when virtualization is figured out more */
  52. /* Example output:
  53. hostname, Model, OS, S/N, App, Tier, Role, Rack, Postition, IPs with interface, Port Connections, Admin IP, Serial Connection, Admin Connection
  54. test1, HP ProLiant DL140, Ubuntu 11.10, 1234567abc, MGT, Webserver, TEST, Rack 1, U28-29, net0/192.168.0.25, e1000g0->test2/e1000g0, 192.168.0.254, test-serial:7001, netmgmt->test2/netmgmt
  55. */
  56. function print_servers() {
  57.  
  58. global $site;
  59.  
  60. $ch = curl_init("$site/api.php?method=get_depot");
  61.  
  62.  
  63. curl_setopt($ch, CURLOPT_HEADER, 0);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  65. $data = curl_exec($ch);
  66. curl_close($ch);
  67.  
  68. $d = json_decode($data, true );
  69.  
  70. $data = $d['response'];
  71. echo "hostname, Model, OS, S/N, App, Tier, Role, Rack, Postition, IPs with interface, Port Connections, Admin IP, Serial Connection, Admin Connection\n";
  72. foreach( $data as $id => $obj )
  73. {
  74. /* Set/Reset variables */
  75. $rack_name = NULL;
  76. $high = ""; $low = "";
  77. $u_size = "";
  78. $chassis = '';
  79. $sn = '';
  80. $application = '';
  81. $role = '';
  82. $app = '';
  83. $os = '';
  84. $netmgmt = '';
  85. $netmgmt_conn = '';
  86. $ip_addr = '';
  87. $conn_info = '';
  88. $serial = '';
  89.  
  90. $obj_curl = curl_init("$site/api.php?method=get_object&object_id=$id&include_attrs=1&include_unset_attrs=1");
  91. curl_setopt($obj_curl, CURLOPT_HEADER, 0);
  92. curl_setopt($obj_curl, CURLOPT_RETURNTRANSFER,true);
  93. $objdata = curl_exec($obj_curl);
  94. curl_close($obj_curl);
  95.  
  96. $obj_json = json_decode($objdata, true );
  97. $object = $obj_json['response'];
  98. if ( $object['objtype_id'] != 4 ) {
  99. continue;
  100. }
  101.  
  102. /* Used to retrieve one host only */
  103. /* if ( $obj['name'] == 'object-to-filter on' ) {
  104. print_r( $obj );
  105. } else {
  106. continue;
  107. } */
  108.  
  109. /* Retrieve additional attributes */
  110. $attrs = $object['attrs'];
  111. foreach ( $attrs as $attr ) {
  112. switch ( $attr['id'] ) {
  113. case 1:
  114. $sn = $attr['a_value'];
  115. break;
  116. case 2:
  117. $chassis = $attr['a_value'];
  118. break;
  119. case 4:
  120. $os = $attr['a_value'];
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126.  
  127. /* Retrieve explicit tags */
  128. $etags = $object['etags'];
  129. foreach ( $etags as $tag ) {
  130. switch( $tag['parent_id'] ) {
  131. case 31:
  132. $application = $tag['tag'];
  133. break;
  134. case 33:
  135. $role = strtoupper($tag['tag']);
  136. break;
  137. case 32:
  138. $app = strtoupper(substr($tag['tag'],0, 3 ));
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144.  
  145. /* Retrieve IPv4 addresses */
  146. $device = array();
  147. $ipv4 = $object['ipv4'];
  148. foreach( $ipv4 as $ip ) {
  149. if ( $ip['osif'] == "netmgmt" ) {
  150. $netmgmt = $ip['addrinfo']['ip'];
  151. continue;
  152. }
  153. $device[] = $ip['osif'];
  154. $addr[] = $ip['addrinfo']['ip'];
  155. }
  156.  
  157. /* Build IP string */
  158. for ( $i = 0; $i <sizeof($device); $i++ ) {
  159. if ( isset($device[$i]) ) {
  160. $ip_addr = $ip_addr.$device[$i]."/".$addr[$i];
  161. }
  162. if ( $i + 1 < sizeof($device) ) { $ip_addr = $ip_addr."; "; }
  163.  
  164. }
  165. unset($device); unset($addr);
  166.  
  167. /* Retrieve port connections */
  168. $ports = $object['ports'];
  169. $remote_nname = array();
  170.  
  171. foreach ( $ports as $port ) {
  172. switch($port['oif_id']) {
  173. case 24:
  174. if ( $port['name'] == "netmgmt" && $port['remote_name'] != "" ) {
  175. $netmgmt_conn = "netmgmt->".$port['remote_object_name']."/".$port['remote_name'];
  176. continue;
  177. }
  178. if ( isset( $port['remote_name'] ) && $port['remote_name'] != "" ) {
  179. $remote_nname[] = $port['remote_name'];
  180. $remote_name[] = $port['name'];
  181. $remote_oname[] = $port['remote_object_name'];
  182. }
  183. break;
  184.  
  185. case 29:
  186. if ( $port['remote_object_name'] != "" ) {
  187. $port_num = trim($port['remote_name'], "tty") + 7000;
  188. $serial = $port['remote_object_name'].":$port_num";
  189. }
  190. default:
  191. /* Recover wwn info here */
  192. break;
  193. }
  194. }
  195. for ( $i = 0; $i < sizeof( $remote_nname ); $i++ ) {
  196. if ( isset($remote_nname[$i]) ) {
  197. $conn_info = $conn_info.$remote_name[$i]."->".$remote_oname[$i]."/".$remote_nname[$i];
  198. }
  199. if ( $i + 1 < sizeof($remote_nname) ) { $conn_info = $conn_info."; "; }
  200. }
  201. unset($remote_name); unset($renote_oname); unset($remote_nname);
  202.  
  203. /* Check object allocation */
  204. $rack_curl = curl_init("$site/api.php?method=get_object_allocation&object_id=$id");
  205. curl_setopt($rack_curl, CURLOPT_HEADER, 0);
  206. curl_setopt($rack_curl, CURLOPT_RETURNTRANSFER,true);
  207. $rackdata = curl_exec($rack_curl);
  208. curl_close($rack_curl);
  209.  
  210. $rack_json = json_decode($rackdata, true );
  211. $racks = $rack_json['response']['racks'];
  212. //print_r($racks);
  213. foreach ( $racks as $rack )
  214. {
  215. $rack_name = $rack['name'];
  216. $low = 100; $high = 0;
  217. /* Kludge together a basic server height */
  218. for ( $i = 1; $i <= $rack['height'] ; $i++ )
  219. {
  220. if ( $rack[$i][2]['state'] == 'T' && $rack[$i][2]['object_id'] == $id )
  221. {
  222. // echo "We're mounted!";
  223. if ( $i > $high ) { $high = $i; }
  224. if ( $i < $low ) { $low = $i; }
  225. }
  226.  
  227. }
  228. if ( $high == "" || $low == "" ) { $high = ""; $low = ""; } else { $u_size = "U$low-$high"; }
  229. }
  230. unset($racks);
  231. echo $obj['name'].", $chassis, $os, $sn, $app, $application, $role, $rack_name, $u_size, $ip_addr, $conn_info, $netmgmt, $serial, $netmgmt_conn";
  232.  
  233. echo "\n";
  234.  
  235. }
  236. // print_r($data);
  237. }
  238.  
  239. print_servers();
  240.  
  241.  
  242.  
  243. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement